@@ -22,6 +22,8 @@ internal sealed partial class VisualStudioDiagnosticAnalyzerProvider : IHostDiag
2222{
2323 private const string AnalyzerContentTypeName = "Microsoft.VisualStudio.Analyzer" ;
2424
25+ private const string RazorContentTypeName = "Microsoft.VisualStudio.RazorAssembly" ;
26+
2527 /// <summary>
2628 /// Loader for VSIX-based analyzers.
2729 /// </summary>
@@ -31,6 +33,7 @@ internal sealed partial class VisualStudioDiagnosticAnalyzerProvider : IHostDiag
3133 private readonly Type _typeIExtensionContent ;
3234
3335 private readonly Lazy < ImmutableArray < ( AnalyzerFileReference reference , string extensionId ) > > _lazyAnalyzerReferences ;
36+ private readonly Lazy < ImmutableArray < ( string path , string extensionId ) > > _lazyRazorReferences ;
3437
3538 // internal for testing
3639 internal VisualStudioDiagnosticAnalyzerProvider ( object extensionManager , Type typeIExtensionContent )
@@ -40,24 +43,28 @@ internal VisualStudioDiagnosticAnalyzerProvider(object extensionManager, Type ty
4043
4144 _extensionManager = extensionManager ;
4245 _typeIExtensionContent = typeIExtensionContent ;
43- _lazyAnalyzerReferences = new Lazy < ImmutableArray < ( AnalyzerFileReference , string ) > > ( GetAnalyzerReferencesImpl ) ;
46+ _lazyAnalyzerReferences = new Lazy < ImmutableArray < ( AnalyzerFileReference , string ) > > ( ( ) => GetExtensionContent ( AnalyzerContentTypeName ) . SelectAsArray ( c => ( new AnalyzerFileReference ( c . path , AnalyzerAssemblyLoader ) , c . extensionId ) ) ) ;
47+ _lazyRazorReferences = new Lazy < ImmutableArray < ( string , string ) > > ( ( ) => GetExtensionContent ( RazorContentTypeName ) ) ;
4448 }
4549
4650 public ImmutableArray < ( AnalyzerFileReference reference , string extensionId ) > GetAnalyzerReferencesInExtensions ( )
4751 => _lazyAnalyzerReferences . Value ;
4852
49- private ImmutableArray < ( AnalyzerFileReference reference , string extensionId ) > GetAnalyzerReferencesImpl ( )
53+ public ImmutableArray < ( string path , string extensionId ) > GetRazorReferencesInExtensions ( )
54+ => _lazyRazorReferences . Value ;
55+
56+ private ImmutableArray < ( string path , string extensionId ) > GetExtensionContent ( string contentTypeName )
5057 {
5158 try
5259 {
5360 // dynamic is weird. it can't see internal type with public interface even if callee is
5461 // implementation of the public interface in internal type. so we can't use dynamic here
55- var _ = PooledDictionary < AnalyzerFileReference , string > . GetInstance ( out var analyzePaths ) ;
62+ var _ = PooledDictionary < string , string > . GetInstance ( out var analyzePaths ) ;
5663
57- // var enabledExtensions = extensionManager.GetEnabledExtensions(AnalyzerContentTypeName );
64+ // var enabledExtensions = extensionManager.GetEnabledExtensions(contentTypeName );
5865 var extensionManagerType = _extensionManager . GetType ( ) ;
5966 var extensionManager_GetEnabledExtensionsMethod = extensionManagerType . GetRuntimeMethod ( "GetEnabledExtensions" , [ typeof ( string ) ] ) ;
60- var enabledExtensions = ( IEnumerable < object > ) extensionManager_GetEnabledExtensionsMethod . Invoke ( _extensionManager , [ AnalyzerContentTypeName ] ) ;
67+ var enabledExtensions = ( IEnumerable < object > ) extensionManager_GetEnabledExtensionsMethod . Invoke ( _extensionManager , [ contentTypeName ] ) ;
6168
6269 foreach ( var extension in enabledExtensions )
6370 {
@@ -73,7 +80,7 @@ internal VisualStudioDiagnosticAnalyzerProvider(object extensionManager, Type ty
7380
7481 foreach ( var content in extension_Content )
7582 {
76- if ( ! ShouldInclude ( content ) )
83+ if ( ! ShouldInclude ( content , contentTypeName ) )
7784 {
7885 continue ;
7986 }
@@ -85,7 +92,7 @@ internal VisualStudioDiagnosticAnalyzerProvider(object extensionManager, Type ty
8592 continue ;
8693 }
8794
88- analyzePaths . Add ( new AnalyzerFileReference ( assemblyPath , AnalyzerAssemblyLoader ) , identifier ) ;
95+ analyzePaths . Add ( assemblyPath , identifier ) ;
8996 }
9097 }
9198
@@ -94,7 +101,7 @@ internal VisualStudioDiagnosticAnalyzerProvider(object extensionManager, Type ty
94101 GC . KeepAlive ( enabledExtensions ) ;
95102
96103 // Order for deterministic result.
97- return analyzePaths . OrderBy ( ( x , y ) => string . CompareOrdinal ( x . Key . FullPath , y . Key . FullPath ) ) . SelectAsArray ( entry => ( entry . Key , entry . Value ) ) ;
104+ return analyzePaths . OrderBy ( ( x , y ) => string . CompareOrdinal ( x . Key , y . Key ) ) . SelectAsArray ( entry => ( entry . Key , entry . Value ) ) ;
98105 }
99106 catch ( TargetInvocationException ex ) when ( ex . InnerException is InvalidOperationException )
100107 {
@@ -108,13 +115,13 @@ internal VisualStudioDiagnosticAnalyzerProvider(object extensionManager, Type ty
108115 }
109116 }
110117
111- private static bool ShouldInclude ( object content )
118+ private static bool ShouldInclude ( object content , string contentTypeName )
112119 {
113120 // var content_ContentTypeName = content.ContentTypeName;
114121 var contentType = content . GetType ( ) ;
115122 var contentType_ContentTypeNameProperty = contentType . GetRuntimeProperty ( "ContentTypeName" ) ;
116123 var content_ContentTypeName = contentType_ContentTypeNameProperty . GetValue ( content ) as string ;
117124
118- return string . Equals ( content_ContentTypeName , AnalyzerContentTypeName , StringComparison . InvariantCultureIgnoreCase ) ;
125+ return string . Equals ( content_ContentTypeName , contentTypeName , StringComparison . InvariantCultureIgnoreCase ) ;
119126 }
120127}
0 commit comments