From 23e3310f3c5ebcb2d1b5fe189df49d7982f06d4a Mon Sep 17 00:00:00 2001 From: Manish Vasani Date: Thu, 30 Sep 2021 01:10:25 -0700 Subject: [PATCH] Remove GetAdjustedSpanForCompilerAnalyzerAsync workaround in the IDE --- .../Diagnostics/DocumentAnalysisExecutor.cs | 46 ++----------------- .../Diagnostics/DocumentAnalysisScope.cs | 3 -- 2 files changed, 5 insertions(+), 44 deletions(-) diff --git a/src/Features/Core/Portable/Diagnostics/DocumentAnalysisExecutor.cs b/src/Features/Core/Portable/Diagnostics/DocumentAnalysisExecutor.cs index 8af079a925384..8a74f54b66e13 100644 --- a/src/Features/Core/Portable/Diagnostics/DocumentAnalysisExecutor.cs +++ b/src/Features/Core/Portable/Diagnostics/DocumentAnalysisExecutor.cs @@ -165,14 +165,14 @@ private async Task> GetCompilerAnalyzerDiagnosticsAsync(DiagnosticAnalyzer analyzer, TextSpan? span, CancellationToken cancellationToken) + private async Task> GetCompilerAnalyzerDiagnosticsAsync(DiagnosticAnalyzer analyzer, CancellationToken cancellationToken) { RoslynDebug.Assert(analyzer.IsCompilerAnalyzer()); RoslynDebug.Assert(_compilationWithAnalyzers != null); RoslynDebug.Assert(_compilationBasedAnalyzersInAnalysisScope.Contains(analyzer)); RoslynDebug.Assert(AnalysisScope.TextDocument is Document); - var analysisScope = AnalysisScope.WithAnalyzers(ImmutableArray.Create(analyzer)).WithSpan(span); + var analysisScope = AnalysisScope.WithAnalyzers(ImmutableArray.Create(analyzer)); var analysisResult = await GetAnalysisResultAsync(analysisScope, cancellationToken).ConfigureAwait(false); if (!analysisResult.TryGetValue(analyzer, out var result)) { @@ -200,7 +200,7 @@ private async Task> GetSyntaxDiagnosticsAsync(Dia return ImmutableArray.Empty; } - return await GetCompilerAnalyzerDiagnosticsAsync(analyzer, AnalysisScope.Span, cancellationToken).ConfigureAwait(false); + return await GetCompilerAnalyzerDiagnosticsAsync(analyzer, cancellationToken).ConfigureAwait(false); } if (_lazySyntaxDiagnostics == null) @@ -225,7 +225,6 @@ private async Task> GetSemanticDiagnosticsAsync(D RoslynDebug.Assert(_compilationWithAnalyzers != null); - var span = AnalysisScope.Span; var document = (Document)AnalysisScope.TextDocument; if (isCompilerAnalyzer) { @@ -233,8 +232,7 @@ private async Task> GetSemanticDiagnosticsAsync(D await VerifySpanBasedCompilerDiagnosticsAsync().ConfigureAwait(false); #endif - var adjustedSpan = await GetAdjustedSpanForCompilerAnalyzerAsync().ConfigureAwait(false); - return await GetCompilerAnalyzerDiagnosticsAsync(analyzer, adjustedSpan, cancellationToken).ConfigureAwait(false); + return await GetCompilerAnalyzerDiagnosticsAsync(analyzer, cancellationToken).ConfigureAwait(false); } if (_lazySemanticDiagnostics == null) @@ -248,44 +246,10 @@ private async Task> GetSemanticDiagnosticsAsync(D diagnosticAnalysisResult.GetDocumentDiagnostics(AnalysisScope.TextDocument.Id, AnalysisScope.Kind) : ImmutableArray.Empty; - async Task GetAdjustedSpanForCompilerAnalyzerAsync() - { - // This method is to workaround a bug (https://github.com/dotnet/roslyn/issues/1557) - // once that bug is fixed, we should be able to use given span as it is. - - Debug.Assert(isCompilerAnalyzer); - - if (!span.HasValue) - { - return null; - } - - var service = document.GetRequiredLanguageService(); - var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false); - var startNode = service.GetContainingMemberDeclaration(root, span.Value.Start); - var endNode = service.GetContainingMemberDeclaration(root, span.Value.End); - - if (startNode == endNode) - { - // use full member span - if (service.IsMethodLevelMember(startNode)) - { - return startNode.FullSpan; - } - - // use span as it is - return span; - } - - var startSpan = service.IsMethodLevelMember(startNode) ? startNode.FullSpan : span.Value; - var endSpan = service.IsMethodLevelMember(endNode) ? endNode.FullSpan : span.Value; - - return TextSpan.FromBounds(Math.Min(startSpan.Start, endSpan.Start), Math.Max(startSpan.End, endSpan.End)); - } - #if DEBUG async Task VerifySpanBasedCompilerDiagnosticsAsync() { + var span = AnalysisScope.Span; if (!span.HasValue) { return; diff --git a/src/Workspaces/Core/Portable/Diagnostics/DocumentAnalysisScope.cs b/src/Workspaces/Core/Portable/Diagnostics/DocumentAnalysisScope.cs index b9c5bc5e7833f..9c19f1c3f360b 100644 --- a/src/Workspaces/Core/Portable/Diagnostics/DocumentAnalysisScope.cs +++ b/src/Workspaces/Core/Portable/Diagnostics/DocumentAnalysisScope.cs @@ -54,9 +54,6 @@ private AdditionalText ComputeAdditionalFile() return TextDocument.Project.AnalyzerOptions.AdditionalFiles.First(a => PathUtilities.Comparer.Equals(a.Path, filePath)); } - public DocumentAnalysisScope WithSpan(TextSpan? span) - => new(TextDocument, span, Analyzers, Kind); - public DocumentAnalysisScope WithAnalyzers(ImmutableArray analyzers) => new(TextDocument, Span, analyzers, Kind); }