Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix code fix construction #2620

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,30 +61,30 @@ private ImmutableArray<CodeFixProvider> LoadFrom(Project project)
{
var codeFixesFromProjectReferences = project.AnalyzerReferences
.OfType<AnalyzerFileReference>()
.SelectMany(analyzerFileReference => analyzerFileReference.GetAssembly().DefinedTypes)
.SelectMany(analyzerFileReference => GetConcreteTypes(analyzerFileReference.GetAssembly()))
.Where(x => !x.IsAbstract && x.IsSubclassOf(typeof(CodeFixProvider)))
.Select(x =>
{
try
{
var attribute = x.GetCustomAttribute<ExportCodeFixProviderAttribute>();
var attribute = x.GetCustomAttribute<ExportCodeFixProviderAttribute>(inherit: false);
if (attribute == null)
{
_logger.LogTrace($"Skipping code fix provider '{x.AsType()}' because it is missing the ExportCodeFixProviderAttribute.");
_logger.LogTrace($"Skipping code fix provider '{x}' because it is missing the ExportCodeFixProviderAttribute.");
return null;
}

if (attribute.Languages == null || !attribute.Languages.Contains(project.Language))
{
_logger.LogInformation($"Skipping code fix provider '{x.AsType()}' because its language '{attribute.Languages?.FirstOrDefault()}' doesn't match '{project.Language}'.");
_logger.LogInformation($"Skipping code fix provider '{x}' because its language '{attribute.Languages?.FirstOrDefault()}' doesn't match '{project.Language}'.");
return null;
}

return x.AsType().CreateInstance<CodeFixProvider>();
return (CodeFixProvider)Activator.CreateInstance(x);
}
catch (Exception ex)
{
_logger.LogError($"Creating instance of code fix provider '{x.AsType()}' failed, error: {ex}");
_logger.LogError($"Creating instance of code fix provider '{x}' failed, error: {ex}");
return null;
}
})
Expand All @@ -98,5 +98,25 @@ private ImmutableArray<CodeFixProvider> LoadFrom(Project project)

return allCodeFixes;
}

private IEnumerable<Type> GetConcreteTypes(Assembly assembly)
{
try
{
var concreteTypes = assembly
.GetTypes()
.Where(type => !type.GetTypeInfo().IsInterface
&& !type.GetTypeInfo().IsAbstract
&& !type.GetTypeInfo().ContainsGenericParameters);

// Realize the collection to ensure exceptions are caught
return concreteTypes.ToList();
}
catch (Exception ex)
{
_logger.LogError($"Getting concrete types from assembly '{assembly}' failed, error: {ex}");
return Type.EmptyTypes;
}
}
}
}
Loading