Skip to content

Commit

Permalink
Remove GetAdjustedSpanForCompilerAnalyzerAsync workaround in the IDE
Browse files Browse the repository at this point in the history
  • Loading branch information
mavasani committed Sep 30, 2021
1 parent ab4f918 commit 23e3310
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 44 deletions.
46 changes: 5 additions & 41 deletions src/Features/Core/Portable/Diagnostics/DocumentAnalysisExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,14 @@ private async Task<ImmutableDictionary<DiagnosticAnalyzer, DiagnosticAnalysisRes
}
}

private async Task<ImmutableArray<DiagnosticData>> GetCompilerAnalyzerDiagnosticsAsync(DiagnosticAnalyzer analyzer, TextSpan? span, CancellationToken cancellationToken)
private async Task<ImmutableArray<DiagnosticData>> 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))
{
Expand Down Expand Up @@ -200,7 +200,7 @@ private async Task<ImmutableArray<DiagnosticData>> GetSyntaxDiagnosticsAsync(Dia
return ImmutableArray<DiagnosticData>.Empty;
}

return await GetCompilerAnalyzerDiagnosticsAsync(analyzer, AnalysisScope.Span, cancellationToken).ConfigureAwait(false);
return await GetCompilerAnalyzerDiagnosticsAsync(analyzer, cancellationToken).ConfigureAwait(false);
}

if (_lazySyntaxDiagnostics == null)
Expand All @@ -225,16 +225,14 @@ private async Task<ImmutableArray<DiagnosticData>> GetSemanticDiagnosticsAsync(D

RoslynDebug.Assert(_compilationWithAnalyzers != null);

var span = AnalysisScope.Span;
var document = (Document)AnalysisScope.TextDocument;
if (isCompilerAnalyzer)
{
#if DEBUG
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)
Expand All @@ -248,44 +246,10 @@ private async Task<ImmutableArray<DiagnosticData>> GetSemanticDiagnosticsAsync(D
diagnosticAnalysisResult.GetDocumentDiagnostics(AnalysisScope.TextDocument.Id, AnalysisScope.Kind) :
ImmutableArray<DiagnosticData>.Empty;

async Task<TextSpan?> 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<ISyntaxFactsService>();
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<DiagnosticAnalyzer> analyzers)
=> new(TextDocument, Span, analyzers, Kind);
}
Expand Down

0 comments on commit 23e3310

Please sign in to comment.