/// <summary> /// Get diagnostics for the given document. /// /// This is a simple API to get all diagnostics for the given document. /// /// The intended audience for this API is for ones that pefer simplicity over performance such as document that belong to misc project. /// this doesn't cache nor use cache for anything. it will re-caculate new diagnostics every time for the given document. /// it will not persist any data on disk nor use OOP to calcuate the data. /// /// This should never be used when performance is a big concern. for such context, use much complex API from IDiagnosticAnalyzerService /// that provide all kinds of knobs/cache/persistency/OOP to get better perf over simplicity. /// </summary> private async Task <ImmutableArray <DiagnosticData> > GetDiagnosticsAsync( TextDocument document, AnalysisKind kind, CancellationToken cancellationToken) { var loadDiagnostic = await document.State.GetLoadDiagnosticAsync(cancellationToken).ConfigureAwait(false); if (loadDiagnostic != null) { return(ImmutableArray.Create(DiagnosticData.Create(loadDiagnostic, document))); } var project = document.Project; var analyzers = GetAnalyzers(project.Solution.State.Analyzers, project); if (analyzers.IsEmpty) { return(ImmutableArray <DiagnosticData> .Empty); } var compilationWithAnalyzers = await AnalyzerHelper.CreateCompilationWithAnalyzersAsync( project, analyzers, includeSuppressedDiagnostics : false, cancellationToken).ConfigureAwait(false); var analysisScope = new DocumentAnalysisScope(document, span: null, analyzers, kind); var executor = new DocumentAnalysisExecutor(analysisScope, compilationWithAnalyzers, _service._analyzerInfoCache); var builder = ArrayBuilder <DiagnosticData> .GetInstance(); foreach (var analyzer in analyzers) { builder.AddRange(await executor.ComputeDiagnosticsAsync(analyzer, cancellationToken).ConfigureAwait(false)); } return(builder.ToImmutableAndFree()); }
/// <summary> /// Get diagnostics for the given document. /// /// This is a simple API to get all diagnostics for the given document. /// /// The intended audience for this API is for ones that pefer simplicity over performance such as document that belong to misc project. /// this doesn't cache nor use cache for anything. it will re-caculate new diagnostics every time for the given document. /// it will not persist any data on disk nor use OOP to calcuate the data. /// /// This should never be used when performance is a big concern. for such context, use much complex API from IDiagnosticAnalyzerService /// that provide all kinds of knobs/cache/persistency/OOP to get better perf over simplicity. /// </summary> private async Task <ImmutableArray <DiagnosticData> > GetDiagnosticsAsync( Document document, AnalysisKind kind, CancellationToken cancellationToken) { var loadDiagnostic = await document.State.GetLoadDiagnosticAsync(cancellationToken).ConfigureAwait(false); if (loadDiagnostic != null) { return(ImmutableArray.Create(DiagnosticData.Create(loadDiagnostic, document))); } var analyzerInfoCache = _service._analyzerInfoCache; var analyzers = GetAnalyzers(analyzerInfoCache, document.Project); var compilationWithAnalyzers = await AnalyzerHelper.CreateCompilationWithAnalyzersAsync( document.Project, analyzers, includeSuppressedDiagnostics : false, cancellationToken).ConfigureAwait(false); var builder = ArrayBuilder <DiagnosticData> .GetInstance(); foreach (var analyzer in analyzers) { builder.AddRange(await AnalyzerHelper.ComputeDiagnosticsAsync(analyzer, document, kind, analyzerInfoCache, compilationWithAnalyzers, span: null, cancellationToken).ConfigureAwait(false)); } return(builder.ToImmutableAndFree()); }