Skip to content

Commit

Permalink
Merge pull request #2620 from OmniSharp/dev/jorobich/fix-codefixer-lo…
Browse files Browse the repository at this point in the history
…ading

Fix code fix construction
  • Loading branch information
JoeRobich authored Jul 26, 2024
2 parents 2dad815 + 57f5898 commit 50095f2
Showing 1 changed file with 26 additions and 6 deletions.
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;
}
}
}
}

0 comments on commit 50095f2

Please sign in to comment.