Skip to content

Commit

Permalink
Merge pull request #1677 from filipw/feature/codefixproviders-discover
Browse files Browse the repository at this point in the history
Do not discover abstract types as candidates for code fix providers
  • Loading branch information
bjorkstromm authored Jan 8, 2020
2 parents 1e93b07 + d5aedd8 commit 6754728
Showing 1 changed file with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,29 @@ private ImmutableArray<CodeFixProvider> LoadFrom(Project project)
var codeFixes = project.AnalyzerReferences
.OfType<AnalyzerFileReference>()
.SelectMany(analyzerFileReference => analyzerFileReference.GetAssembly().DefinedTypes)
.Where(x => x.IsSubclassOf(typeof(CodeFixProvider)))
.Where(x => !x.IsAbstract && x.IsSubclassOf(typeof(CodeFixProvider)))
.Select(x =>
{
try
{
var attribute = x.GetCustomAttribute<ExportCodeFixProviderAttribute>();

if (attribute?.Languages != null && attribute.Languages.Contains(project.Language))
if (attribute == null)
{
return x.AsType().CreateInstance<CodeFixProvider>();
_logger.LogTrace($"Skipping code fix provider '{x.AsType()}' because it is missing the ExportCodeFixProviderAttribute.");
return null;
}

_logger.LogInformation($"Skipping code fix provider '{x.AsType()}' because its language doesn't match '{project.Language}'.");
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}'.");
return null;
}

return null;
return x.AsType().CreateInstance<CodeFixProvider>();
}
catch (Exception ex)
{
_logger.LogError($"Creating instance of code fix provider '{x.AsType()}' failed, error: {ex}");

return null;
}
})
Expand Down

0 comments on commit 6754728

Please sign in to comment.