Skip to content

Commit 5e6a0e1

Browse files
Move lsp handlers to async/await (attempt 2). (#81832)
2 parents 49852e5 + e42e5f2 commit 5e6a0e1

24 files changed

+64
-71
lines changed

src/LanguageServer/Protocol/Handler/CodeActions/CodeActionResolveHelper.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ await AddTextDocumentEditsAsync(
211211

212212
return new LSP.WorkspaceEdit { DocumentChanges = textDocumentEdits.ToArray() };
213213

214-
Task AddTextDocumentDeletionsAsync<TTextDocument>(
214+
async Task AddTextDocumentDeletionsAsync<TTextDocument>(
215215
IEnumerable<DocumentId> removedDocuments,
216216
Func<DocumentId, TTextDocument?> getOldDocument)
217217
where TTextDocument : TextDocument
@@ -223,8 +223,6 @@ Task AddTextDocumentDeletionsAsync<TTextDocument>(
223223

224224
textDocumentEdits.Add(new DeleteFile { DocumentUri = oldTextDoc.GetURI() });
225225
}
226-
227-
return Task.CompletedTask;
228226
}
229227

230228
async Task AddTextDocumentAdditionsAsync<TTextDocument>(

src/LanguageServer/Protocol/Handler/Completion/CompletionResolveHandler.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,25 @@ public CompletionResolveHandler(IGlobalOptionService globalOptions)
4444
public LSP.TextDocumentIdentifier? GetTextDocumentIdentifier(LSP.CompletionItem request)
4545
=> GetTextDocumentCacheEntry(request);
4646

47-
public Task<LSP.CompletionItem> HandleRequestAsync(LSP.CompletionItem completionItem, RequestContext context, CancellationToken cancellationToken)
47+
public async Task<LSP.CompletionItem> HandleRequestAsync(LSP.CompletionItem completionItem, RequestContext context, CancellationToken cancellationToken)
4848
{
4949
var completionListCache = context.GetRequiredLspService<CompletionListCache>();
5050

5151
if (!completionListCache.TryGetCompletionListCacheEntry(completionItem, out var cacheEntry))
5252
{
5353
// Don't have a cache associated with this completion item, cannot resolve.
5454
context.TraceWarning("No cache entry found for the provided completion item at resolve time.");
55-
return Task.FromResult(completionItem);
55+
return completionItem;
5656
}
5757

5858
var document = context.GetRequiredDocument();
5959
var capabilityHelper = new CompletionCapabilityHelper(context.GetRequiredClientCapabilities());
6060

61-
return ResolveCompletionItemAsync(
62-
completionItem, cacheEntry.CompletionList, document, _globalOptions, capabilityHelper, cancellationToken);
61+
return await ResolveCompletionItemAsync(
62+
completionItem, cacheEntry.CompletionList, document, _globalOptions, capabilityHelper, cancellationToken).ConfigureAwait(false);
6363
}
6464

65-
public static Task<LSP.CompletionItem> ResolveCompletionItemAsync(
65+
public static async Task<LSP.CompletionItem> ResolveCompletionItemAsync(
6666
LSP.CompletionItem completionItem,
6767
Document document,
6868
IGlobalOptionService globalOptions,
@@ -73,11 +73,11 @@ public CompletionResolveHandler(IGlobalOptionService globalOptions)
7373
if (!completionListCache.TryGetCompletionListCacheEntry(completionItem, out var cacheEntry))
7474
{
7575
// Don't have a cache associated with this completion item, cannot resolve.
76-
return Task.FromResult(completionItem);
76+
return completionItem;
7777
}
7878

79-
return ResolveCompletionItemAsync(
80-
completionItem, cacheEntry.CompletionList, document, globalOptions, capabilityHelper, cancellationToken);
79+
return await ResolveCompletionItemAsync(
80+
completionItem, cacheEntry.CompletionList, document, globalOptions, capabilityHelper, cancellationToken).ConfigureAwait(false);
8181
}
8282

8383
private static async Task<LSP.CompletionItem> ResolveCompletionItemAsync(

src/LanguageServer/Protocol/Handler/Definitions/AbstractGoToDefinitionHandler.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,16 @@ public AbstractGoToDefinitionHandler(IMetadataAsSourceFileService metadataAsSour
3535

3636
public abstract Task<LSP.Location[]?> HandleRequestAsync(TextDocumentPositionParams request, RequestContext context, CancellationToken cancellationToken);
3737

38-
protected Task<LSP.Location[]?> GetDefinitionAsync(LSP.TextDocumentPositionParams request, bool forSymbolType, RequestContext context, CancellationToken cancellationToken)
38+
protected async Task<LSP.Location[]?> GetDefinitionAsync(LSP.TextDocumentPositionParams request, bool forSymbolType, RequestContext context, CancellationToken cancellationToken)
3939
{
4040
var workspace = context.Workspace;
4141
var document = context.Document;
4242
if (workspace is null || document is null)
43-
return SpecializedTasks.Null<LSP.Location[]>();
43+
return null;
4444

4545
var linePosition = ProtocolConversions.PositionToLinePosition(request.Position);
4646

47-
return GetDefinitionsAsync(_globalOptions, _metadataAsSourceFileService, workspace, document, forSymbolType, linePosition, cancellationToken);
47+
return await GetDefinitionsAsync(_globalOptions, _metadataAsSourceFileService, workspace, document, forSymbolType, linePosition, cancellationToken).ConfigureAwait(false);
4848
}
4949

5050
internal static async Task<LSP.Location[]?> GetDefinitionsAsync(IGlobalOptionService globalOptions, IMetadataAsSourceFileService? metadataAsSourceFileService, Workspace workspace, Document document, bool forSymbolType, LinePosition linePosition, CancellationToken cancellationToken)

src/LanguageServer/Protocol/Handler/Diagnostics/AbstractDocumentPullDiagnosticHandler.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ internal abstract class AbstractDocumentPullDiagnosticHandler<TDiagnosticsParams
2626

2727
public abstract TextDocumentIdentifier? GetTextDocumentIdentifier(TDiagnosticsParams diagnosticsParams);
2828

29-
protected override ValueTask<ImmutableArray<IDiagnosticSource>> GetOrderedDiagnosticSourcesAsync(TDiagnosticsParams diagnosticsParams, string? requestDiagnosticCategory, RequestContext context, CancellationToken cancellationToken)
29+
protected override async ValueTask<ImmutableArray<IDiagnosticSource>> GetOrderedDiagnosticSourcesAsync(TDiagnosticsParams diagnosticsParams, string? requestDiagnosticCategory, RequestContext context, CancellationToken cancellationToken)
3030
{
3131
// Note: context.Document may be null in the case where the client is asking about a document that we have
3232
// since removed from the workspace. In this case, we don't really have anything to process.
@@ -38,15 +38,15 @@ protected override ValueTask<ImmutableArray<IDiagnosticSource>> GetOrderedDiagno
3838
if (identifier is null || context.TextDocument is null)
3939
{
4040
context.TraceDebug("Ignoring diagnostics request because no text document was provided");
41-
return new([]);
41+
return [];
4242
}
4343

4444
if (!context.IsTracking(identifier.DocumentUri))
4545
{
4646
context.TraceWarning($"Ignoring diagnostics request for untracked document: {identifier.DocumentUri}");
47-
return new([]);
47+
return [];
4848
}
4949

50-
return DiagnosticSourceManager.CreateDocumentDiagnosticSourcesAsync(context, requestDiagnosticCategory, cancellationToken);
50+
return await DiagnosticSourceManager.CreateDocumentDiagnosticSourcesAsync(context, requestDiagnosticCategory, cancellationToken).ConfigureAwait(false);
5151
}
5252
}

src/LanguageServer/Protocol/Handler/Diagnostics/AbstractPullDiagnosticHandler.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,8 @@ protected abstract ValueTask<ImmutableArray<IDiagnosticSource>> GetOrderedDiagno
9292
/// Used by public workspace pull diagnostics to allow it to keep the connection open until
9393
/// changes occur to avoid the client spamming the server with requests.
9494
/// </summary>
95-
protected virtual Task WaitForChangesAsync(string? category, RequestContext context, CancellationToken cancellationToken)
95+
protected virtual async Task WaitForChangesAsync(string? category, RequestContext context, CancellationToken cancellationToken)
9696
{
97-
return Task.CompletedTask;
9897
}
9998

10099
public async Task<TReturn?> HandleRequestAsync(

src/LanguageServer/Protocol/Handler/Diagnostics/AbstractWorkspacePullDiagnosticsHandler.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,17 @@ public void Dispose()
5959
_workspaceRegistrationService.LspSolutionChanged -= OnLspSolutionChanged;
6060
}
6161

62-
protected override ValueTask<ImmutableArray<IDiagnosticSource>> GetOrderedDiagnosticSourcesAsync(TDiagnosticsParams diagnosticsParams, string? requestDiagnosticCategory, RequestContext context, CancellationToken cancellationToken)
62+
protected override async ValueTask<ImmutableArray<IDiagnosticSource>> GetOrderedDiagnosticSourcesAsync(TDiagnosticsParams diagnosticsParams, string? requestDiagnosticCategory, RequestContext context, CancellationToken cancellationToken)
6363
{
6464
if (context.ServerKind == WellKnownLspServerKinds.RazorLspServer)
6565
{
6666
// If we're being called from razor, we do not support WorkspaceDiagnostics at all. For razor, workspace
6767
// diagnostics will be handled by razor itself, which will operate by calling into Roslyn and asking for
6868
// document-diagnostics instead.
69-
return new([]);
69+
return [];
7070
}
7171

72-
return DiagnosticSourceManager.CreateWorkspaceDiagnosticSourcesAsync(context, requestDiagnosticCategory, cancellationToken);
72+
return await DiagnosticSourceManager.CreateWorkspaceDiagnosticSourcesAsync(context, requestDiagnosticCategory, cancellationToken).ConfigureAwait(false);
7373
}
7474

7575
private void OnLspSolutionChanged(object? sender, WorkspaceChangeEventArgs e)

src/LanguageServer/Protocol/Handler/Diagnostics/DiagnosticSourceProviders/DocumentSyntaxAndSemanticDiagnosticSourceProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ internal abstract class AbstractDocumentSyntaxAndSemanticDiagnosticSourceProvide
2222

2323
public bool IsEnabled(ClientCapabilities clientCapabilities) => true;
2424

25-
public ValueTask<ImmutableArray<IDiagnosticSource>> CreateDiagnosticSourcesAsync(RequestContext context, CancellationToken cancellationToken)
25+
public async ValueTask<ImmutableArray<IDiagnosticSource>> CreateDiagnosticSourcesAsync(RequestContext context, CancellationToken cancellationToken)
2626
{
27-
return new([new DocumentDiagnosticSource(kind, context.GetRequiredDocument())]);
27+
return [new DocumentDiagnosticSource(kind, context.GetRequiredDocument())];
2828
}
2929

3030
[Export(typeof(IDiagnosticSourceProvider)), Shared]

src/LanguageServer/Protocol/Handler/Diagnostics/DiagnosticSources/AbstractProjectDiagnosticSource.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public override async Task<ImmutableArray<DiagnosticData>> GetDiagnosticsAsync(
5959
private sealed class CodeAnalysisDiagnosticSource(Project project, ICodeAnalysisDiagnosticAnalyzerService codeAnalysisService)
6060
: AbstractProjectDiagnosticSource(project)
6161
{
62-
public override Task<ImmutableArray<DiagnosticData>> GetDiagnosticsAsync(
62+
public override async Task<ImmutableArray<DiagnosticData>> GetDiagnosticsAsync(
6363
RequestContext context,
6464
CancellationToken cancellationToken)
6565
{
@@ -69,7 +69,7 @@ public override Task<ImmutableArray<DiagnosticData>> GetDiagnosticsAsync(
6969
// user. As such, it is definitely not "live" data, and it should be overridden by any subsequent fresh data
7070
// that has been produced.
7171
diagnostics = ProtocolConversions.AddBuildTagIfNotPresent(diagnostics);
72-
return Task.FromResult(diagnostics);
72+
return diagnostics;
7373
}
7474
}
7575
}

src/LanguageServer/Protocol/Handler/Diagnostics/DiagnosticSources/AbstractWorkspaceDocumentDiagnosticSource.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ AsyncLazy<ILookup<DocumentId, DiagnosticData>> GetLazyDiagnostics()
9494
private sealed class CodeAnalysisDiagnosticSource(TextDocument document, ICodeAnalysisDiagnosticAnalyzerService codeAnalysisService)
9595
: AbstractWorkspaceDocumentDiagnosticSource(document)
9696
{
97-
public override Task<ImmutableArray<DiagnosticData>> GetDiagnosticsAsync(
97+
public override async Task<ImmutableArray<DiagnosticData>> GetDiagnosticsAsync(
9898
RequestContext context,
9999
CancellationToken cancellationToken)
100100
{
@@ -104,7 +104,7 @@ public override Task<ImmutableArray<DiagnosticData>> GetDiagnosticsAsync(
104104
// user. As such, it is definitely not "live" data, and it should be overridden by any subsequent fresh data
105105
// that has been produced.
106106
diagnostics = ProtocolConversions.AddBuildTagIfNotPresent(diagnostics);
107-
return Task.FromResult(diagnostics);
107+
return diagnostics;
108108
}
109109
}
110110
}

src/LanguageServer/Protocol/Handler/Diagnostics/Public/PublicDocumentNonLocalDiagnosticSourceProvider.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ internal sealed class PublicDocumentNonLocalDiagnosticSourceProvider(
2828

2929
public bool IsEnabled(ClientCapabilities clientCapabilities) => true;
3030

31-
public ValueTask<ImmutableArray<IDiagnosticSource>> CreateDiagnosticSourcesAsync(RequestContext context, CancellationToken cancellationToken)
31+
public async ValueTask<ImmutableArray<IDiagnosticSource>> CreateDiagnosticSourcesAsync(RequestContext context, CancellationToken cancellationToken)
3232
{
3333
// Non-local document diagnostics are reported only when full solution analysis is enabled for analyzer execution.
3434
if (globalOptions.GetBackgroundAnalysisScope(context.GetRequiredDocument().Project.Language) == BackgroundAnalysisScope.FullSolution)
3535
{
3636
// NOTE: Compiler does not report any non-local diagnostics, so we only ask to run non-compiler-analyzers.
37-
return new([new NonLocalDocumentDiagnosticSource(context.GetRequiredDocument(), AnalyzerFilter.NonCompilerAnalyzer)]);
37+
return [new NonLocalDocumentDiagnosticSource(context.GetRequiredDocument(), AnalyzerFilter.NonCompilerAnalyzer)];
3838
}
3939

40-
return new([]);
40+
return [];
4141
}
4242
}

0 commit comments

Comments
 (0)