-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathDocumentPullDiagnosticHandler.cs
121 lines (103 loc) · 6.35 KB
/
DocumentPullDiagnosticHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Immutable;
using System.Composition;
using System.Reflection.Metadata;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.EditAndContinue;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using Roslyn.Utilities;
using Range = Microsoft.VisualStudio.LanguageServer.Protocol.Range;
namespace Microsoft.CodeAnalysis.LanguageServer.Handler.Diagnostics
{
[Method(VSInternalMethods.DocumentPullDiagnosticName)]
internal class DocumentPullDiagnosticHandler : AbstractPullDiagnosticHandler<VSInternalDocumentDiagnosticsParams, VSInternalDiagnosticReport, VSInternalDiagnosticReport[]>
{
public DocumentPullDiagnosticHandler(
IDiagnosticAnalyzerService analyzerService,
EditAndContinueDiagnosticUpdateSource editAndContinueDiagnosticUpdateSource,
IGlobalOptionService globalOptions)
: base(analyzerService, editAndContinueDiagnosticUpdateSource, globalOptions)
{
}
public override TextDocumentIdentifier? GetTextDocumentIdentifier(VSInternalDocumentDiagnosticsParams diagnosticsParams)
=> diagnosticsParams.TextDocument;
protected override VSInternalDiagnosticReport CreateReport(TextDocumentIdentifier identifier, VisualStudio.LanguageServer.Protocol.Diagnostic[]? diagnostics, string? resultId)
=> new VSInternalDiagnosticReport
{
Diagnostics = diagnostics,
ResultId = resultId,
Identifier = DocumentDiagnosticIdentifier,
// Mark these diagnostics as superseding any diagnostics for the same document from the
// WorkspacePullDiagnosticHandler. We are always getting completely accurate and up to date diagnostic
// values for a particular file, so our results should always be preferred over the workspace-pull
// values which are cached and may be out of date.
Supersedes = WorkspaceDiagnosticIdentifier,
};
protected override VSInternalDiagnosticReport CreateRemovedReport(TextDocumentIdentifier identifier)
=> CreateReport(identifier, diagnostics: null, resultId: null);
protected override VSInternalDiagnosticReport CreateUnchangedReport(TextDocumentIdentifier identifier, string resultId)
=> CreateReport(identifier, diagnostics: null, resultId);
protected override ImmutableArray<PreviousPullResult>? GetPreviousResults(VSInternalDocumentDiagnosticsParams diagnosticsParams)
{
if (diagnosticsParams.PreviousResultId != null && diagnosticsParams.TextDocument != null)
{
return ImmutableArray.Create(new PreviousPullResult(diagnosticsParams.PreviousResultId, diagnosticsParams.TextDocument));
}
// The client didn't provide us with a previous result to look for, so we can't lookup anything.
return null;
}
protected override DiagnosticTag[] ConvertTags(DiagnosticData diagnosticData)
=> ConvertTags(diagnosticData, potentialDuplicate: false);
protected override ValueTask<ImmutableArray<IDiagnosticSource>> GetOrderedDiagnosticSourcesAsync(RequestContext context, CancellationToken cancellationToken)
{
return ValueTaskFactory.FromResult(GetRequestedDocument(context));
}
protected override VSInternalDiagnosticReport[]? CreateReturn(BufferedProgress<VSInternalDiagnosticReport> progress)
{
return progress.GetValues();
}
internal static ImmutableArray<IDiagnosticSource> GetRequestedDocument(RequestContext context)
{
// For the single document case, that is the only doc we want to process.
//
// Note: context.Document may be null in the case where the client is asking about a document that we have
// since removed from the workspace. In this case, we don't really have anything to process.
// GetPreviousResults will be used to properly realize this and notify the client that the doc is gone.
//
// Only consider open documents here (and only closed ones in the WorkspacePullDiagnosticHandler). Each
// handler treats those as separate worlds that they are responsible for.
if (context.Document == null)
{
context.TraceInformation("Ignoring diagnostics request because no document was provided");
return ImmutableArray<IDiagnosticSource>.Empty;
}
if (!context.IsTracking(context.Document.GetURI()))
{
context.TraceWarning($"Ignoring diagnostics request for untracked document: {context.Document.GetURI()}");
return ImmutableArray<IDiagnosticSource>.Empty;
}
return ImmutableArray.Create<IDiagnosticSource>(new DocumentDiagnosticSource(context.Document));
}
private record struct DocumentDiagnosticSource(Document Document) : IDiagnosticSource
{
public ProjectOrDocumentId GetId() => new(Document.Id);
public Project GetProject() => Document.Project;
public Uri GetUri() => Document.GetURI();
public async Task<ImmutableArray<DiagnosticData>> GetDiagnosticsAsync(IDiagnosticAnalyzerService diagnosticAnalyzerService, RequestContext context, DiagnosticMode diagnosticMode, CancellationToken cancellationToken)
{
// We call GetDiagnosticsForSpanAsync here instead of GetDiagnosticsForIdsAsync as it has faster perf characteristics.
// GetDiagnosticsForIdsAsync runs analyzers against the entire compilation whereas GetDiagnosticsForSpanAsync will only run analyzers against the request document.
var allSpanDiagnostics = await diagnosticAnalyzerService.GetDiagnosticsForSpanAsync(Document, range: null, cancellationToken: cancellationToken).ConfigureAwait(false);
return allSpanDiagnostics;
}
}
}
}