@@ -17,24 +17,24 @@ private partial class DiagnosticIncrementalAnalyzer
1717 {
1818 private partial class StateManager
1919 {
20- private HostAnalyzerStateSets GetOrCreateHostStateSets ( Project project , ProjectAnalyzerStateSets projectStateSets )
20+ private HostAnalyzerInfo GetOrCreateHostAnalyzerInfo ( Project project , ProjectAnalyzerInfo projectAnalyzerInfo )
2121 {
22- var key = new HostAnalyzerStateSetKey ( project . Language , project . State . HasSdkCodeStyleAnalyzers , project . Solution . SolutionState . Analyzers . HostAnalyzerReferences ) ;
22+ var key = new HostAnalyzerInfoKey ( project . Language , project . State . HasSdkCodeStyleAnalyzers , project . Solution . SolutionState . Analyzers . HostAnalyzerReferences ) ;
2323 // Some Host Analyzers may need to be treated as Project Analyzers so that they do not have access to the
2424 // Host fallback options. These ids will be used when building up the Host and Project analyzer collections.
2525 var referenceIdsToRedirect = GetReferenceIdsToRedirectAsProjectAnalyzers ( project ) ;
26- var hostStateSets = ImmutableInterlocked . GetOrAdd ( ref _hostAnalyzerStateMap , key , CreateLanguageSpecificAnalyzerMap , ( project . Solution . SolutionState . Analyzers , referenceIdsToRedirect ) ) ;
27- return hostStateSets . WithExcludedAnalyzers ( projectStateSets . SkippedAnalyzersInfo . SkippedAnalyzers ) ;
26+ var hostAnalyzerInfo = ImmutableInterlocked . GetOrAdd ( ref _hostAnalyzerStateMap , key , CreateLanguageSpecificAnalyzerMap , ( project . Solution . SolutionState . Analyzers , referenceIdsToRedirect ) ) ;
27+ return hostAnalyzerInfo . WithExcludedAnalyzers ( projectAnalyzerInfo . SkippedAnalyzersInfo . SkippedAnalyzers ) ;
2828
29- static HostAnalyzerStateSets CreateLanguageSpecificAnalyzerMap ( HostAnalyzerStateSetKey arg , ( HostDiagnosticAnalyzers HostAnalyzers , ImmutableHashSet < object > ReferenceIdsToRedirect ) state )
29+ static HostAnalyzerInfo CreateLanguageSpecificAnalyzerMap ( HostAnalyzerInfoKey arg , ( HostDiagnosticAnalyzers HostAnalyzers , ImmutableHashSet < object > ReferenceIdsToRedirect ) state )
3030 {
3131 var language = arg . Language ;
3232 var analyzersPerReference = state . HostAnalyzers . GetOrCreateHostDiagnosticAnalyzersPerReference ( language ) ;
3333
3434 var ( hostAnalyzerCollection , projectAnalyzerCollection ) = GetAnalyzerCollections ( analyzersPerReference , state . ReferenceIdsToRedirect ) ;
35- var analyzerMap = CreateStateSetMap ( projectAnalyzerCollection , hostAnalyzerCollection , includeWorkspacePlaceholderAnalyzers : true ) ;
35+ var ( hostAnalyzers , allAnalyzers ) = PartitionAnalyzers ( projectAnalyzerCollection , hostAnalyzerCollection , includeWorkspacePlaceholderAnalyzers : true ) ;
3636
37- return new HostAnalyzerStateSets ( analyzerMap ) ;
37+ return new HostAnalyzerInfo ( hostAnalyzers , allAnalyzers ) ;
3838 }
3939
4040 static ( IEnumerable < ImmutableArray < DiagnosticAnalyzer > > HostAnalyzerCollection , IEnumerable < ImmutableArray < DiagnosticAnalyzer > > ProjectAnalyzerCollection ) GetAnalyzerCollections (
@@ -90,68 +90,65 @@ static ImmutableHashSet<object> GetFeaturesAnalyzerReferenceIds(HostDiagnosticAn
9090 return builder . ToImmutable ( ) ;
9191 }
9292 }
93+ }
94+ }
9395
94- private sealed class HostAnalyzerStateSets
95- {
96- private const int FileContentLoadAnalyzerPriority = - 4 ;
97- private const int GeneratorDiagnosticsPlaceholderAnalyzerPriority = - 3 ;
98- private const int BuiltInCompilerPriority = - 2 ;
99- private const int RegularDiagnosticAnalyzerPriority = - 1 ;
100-
101- // ordered by priority
102- public readonly ImmutableArray < StateSet > OrderedStateSets ;
103-
104- public readonly ImmutableDictionary < DiagnosticAnalyzer , StateSet > StateSetMap ;
105-
106- private HostAnalyzerStateSets ( ImmutableDictionary < DiagnosticAnalyzer , StateSet > stateSetMap , ImmutableArray < StateSet > orderedStateSets )
107- {
108- StateSetMap = stateSetMap ;
109- OrderedStateSets = orderedStateSets ;
110- }
96+ private sealed class HostAnalyzerInfo
97+ {
98+ private const int FileContentLoadAnalyzerPriority = - 4 ;
99+ private const int GeneratorDiagnosticsPlaceholderAnalyzerPriority = - 3 ;
100+ private const int BuiltInCompilerPriority = - 2 ;
101+ private const int RegularDiagnosticAnalyzerPriority = - 1 ;
102+
103+ private readonly ImmutableHashSet < DiagnosticAnalyzer > _hostAnalyzers ;
104+ private readonly ImmutableHashSet < DiagnosticAnalyzer > _allAnalyzers ;
105+ public readonly ImmutableArray < DiagnosticAnalyzer > OrderedAllAnalyzers ;
106+
107+ public HostAnalyzerInfo (
108+ ImmutableHashSet < DiagnosticAnalyzer > hostAnalyzers ,
109+ ImmutableHashSet < DiagnosticAnalyzer > allAnalyzers )
110+ {
111+ _hostAnalyzers = hostAnalyzers ;
112+ _allAnalyzers = allAnalyzers ;
111113
112- public HostAnalyzerStateSets ( ImmutableDictionary < DiagnosticAnalyzer , StateSet > analyzerMap )
113- {
114- StateSetMap = analyzerMap ;
114+ // order analyzers.
115+ // order will be in this order
116+ // BuiltIn Compiler Analyzer (C#/VB) < Regular DiagnosticAnalyzers < Document/ProjectDiagnosticAnalyzers
117+ OrderedAllAnalyzers = [ .. _allAnalyzers . OrderBy ( PriorityComparison ) ] ;
118+ }
115119
116- // order statesets
117- // order will be in this order
118- // BuiltIn Compiler Analyzer (C#/VB) < Regular DiagnosticAnalyzers < Document/ProjectDiagnosticAnalyzers
119- OrderedStateSets = [ .. StateSetMap . Values . OrderBy ( PriorityComparison ) ] ;
120- }
120+ public bool IsHostAnalyzer ( DiagnosticAnalyzer analyzer )
121+ => _hostAnalyzers . Contains ( analyzer ) ;
121122
122- public HostAnalyzerStateSets WithExcludedAnalyzers ( ImmutableHashSet < DiagnosticAnalyzer > excludedAnalyzers )
123- {
124- if ( excludedAnalyzers . IsEmpty )
125- {
126- return this ;
127- }
123+ public HostAnalyzerInfo WithExcludedAnalyzers ( ImmutableHashSet < DiagnosticAnalyzer > excludedAnalyzers )
124+ {
125+ if ( excludedAnalyzers . IsEmpty )
126+ {
127+ return this ;
128+ }
128129
129- var stateSetMap = StateSetMap . Where ( kvp => ! excludedAnalyzers . Contains ( kvp . Key ) ) . ToImmutableDictionary ( ) ;
130- var orderedStateSets = OrderedStateSets . WhereAsArray ( stateSet => ! excludedAnalyzers . Contains ( stateSet . Analyzer ) ) ;
131- return new HostAnalyzerStateSets ( stateSetMap , orderedStateSets ) ;
132- }
130+ return new ( _hostAnalyzers , _allAnalyzers . Except ( excludedAnalyzers ) ) ;
131+ }
133132
134- private int PriorityComparison ( StateSet state1 , StateSet state2 )
135- => GetPriority ( state1 ) - GetPriority ( state2 ) ;
133+ private int PriorityComparison ( DiagnosticAnalyzer state1 , DiagnosticAnalyzer state2 )
134+ => GetPriority ( state1 ) - GetPriority ( state2 ) ;
136135
137- private static int GetPriority ( StateSet state )
138- {
139- // compiler gets highest priority
140- if ( state . Analyzer . IsCompilerAnalyzer ( ) )
141- {
142- return BuiltInCompilerPriority ;
143- }
144-
145- return state . Analyzer switch
146- {
147- FileContentLoadAnalyzer _ => FileContentLoadAnalyzerPriority ,
148- GeneratorDiagnosticsPlaceholderAnalyzer _ => GeneratorDiagnosticsPlaceholderAnalyzerPriority ,
149- DocumentDiagnosticAnalyzer analyzer => Math . Max ( 0 , analyzer . Priority ) ,
150- ProjectDiagnosticAnalyzer analyzer => Math . Max ( 0 , analyzer . Priority ) ,
151- _ => RegularDiagnosticAnalyzerPriority ,
152- } ;
153- }
136+ private static int GetPriority ( DiagnosticAnalyzer state )
137+ {
138+ // compiler gets highest priority
139+ if ( state . IsCompilerAnalyzer ( ) )
140+ {
141+ return BuiltInCompilerPriority ;
154142 }
143+
144+ return state switch
145+ {
146+ FileContentLoadAnalyzer _ => FileContentLoadAnalyzerPriority ,
147+ GeneratorDiagnosticsPlaceholderAnalyzer _ => GeneratorDiagnosticsPlaceholderAnalyzerPriority ,
148+ DocumentDiagnosticAnalyzer analyzer => Math . Max ( 0 , analyzer . Priority ) ,
149+ ProjectDiagnosticAnalyzer analyzer => Math . Max ( 0 , analyzer . Priority ) ,
150+ _ => RegularDiagnosticAnalyzerPriority ,
151+ } ;
155152 }
156153 }
157154}
0 commit comments