@@ -75,6 +75,7 @@ private sealed class LatestDiagnosticsForSpanGetter
7575 private readonly Func < string , bool > ? _shouldIncludeDiagnostic ;
7676 private readonly bool _includeCompilerDiagnostics ;
7777 private readonly Func < string , IDisposable ? > ? _addOperationScope ;
78+ private readonly bool _cacheFullDocumentDiagnostics ;
7879
7980 private delegate Task < IEnumerable < DiagnosticData > > DiagnosticsGetterAsync ( DiagnosticAnalyzer analyzer , DocumentAnalysisExecutor executor , CancellationToken cancellationToken ) ;
8081
@@ -96,11 +97,17 @@ public static async Task<LatestDiagnosticsForSpanGetter> CreateAsync(
9697
9798 var ideOptions = owner . AnalyzerService . GlobalOptions . GetIdeAnalyzerOptions ( document . Project ) ;
9899
100+ // We want to cache computed full document diagnostics in LatestDiagnosticsForSpanGetter
101+ // only in LSP pull diagnostics mode. In LSP push diagnostics mode,
102+ // the background analysis from solution crawler handles caching these diagnostics and
103+ // updating the error list simultaneously.
104+ var cacheFullDocumentDiagnostics = owner . AnalyzerService . GlobalOptions . IsPullDiagnostics ( InternalDiagnosticsOptions . NormalDiagnosticMode ) ;
105+
99106 var compilationWithAnalyzers = await GetOrCreateCompilationWithAnalyzersAsync ( document . Project , ideOptions , stateSets , includeSuppressedDiagnostics , cancellationToken ) . ConfigureAwait ( false ) ;
100107
101108 return new LatestDiagnosticsForSpanGetter (
102109 owner , compilationWithAnalyzers , document , text , stateSets , shouldIncludeDiagnostic , includeCompilerDiagnostics ,
103- range , blockForData , addOperationScope , includeSuppressedDiagnostics , priority ) ;
110+ range , blockForData , addOperationScope , includeSuppressedDiagnostics , priority , cacheFullDocumentDiagnostics ) ;
104111 }
105112
106113 private static async Task < CompilationWithAnalyzers ? > GetOrCreateCompilationWithAnalyzersAsync (
@@ -141,7 +148,8 @@ private LatestDiagnosticsForSpanGetter(
141148 bool blockForData ,
142149 Func < string , IDisposable ? > ? addOperationScope ,
143150 bool includeSuppressedDiagnostics ,
144- CodeActionRequestPriority priority )
151+ CodeActionRequestPriority priority ,
152+ bool cacheFullDocumentDiagnostics )
145153 {
146154 _owner = owner ;
147155 _compilationWithAnalyzers = compilationWithAnalyzers ;
@@ -155,6 +163,7 @@ private LatestDiagnosticsForSpanGetter(
155163 _addOperationScope = addOperationScope ;
156164 _includeSuppressedDiagnostics = includeSuppressedDiagnostics ;
157165 _priority = priority ;
166+ _cacheFullDocumentDiagnostics = cacheFullDocumentDiagnostics ;
158167 }
159168
160169 public async Task < bool > TryGetAsync ( ArrayBuilder < DiagnosticData > list , CancellationToken cancellationToken )
@@ -320,8 +329,8 @@ private async Task ComputeDocumentDiagnosticsAsync(
320329 var diagnostics = diagnosticsMap [ stateSet . Analyzer ] ;
321330 builder . AddRange ( diagnostics . Where ( ShouldInclude ) ) ;
322331
323- // Cache the computed diagnostics if they were computed for the entire document.
324- if ( ! span . HasValue )
332+ // Save the computed diagnostics if caching is enabled and diagnostics were computed for the entire document.
333+ if ( _cacheFullDocumentDiagnostics && ! span . HasValue )
325334 {
326335 var state = stateSet . GetOrCreateActiveFileState ( _document . Id ) ;
327336 var data = new DocumentAnalysisData ( version , diagnostics ) ;
0 commit comments