From 19e8457df871732f528be7dbefc51fcea4c1925c Mon Sep 17 00:00:00 2001 From: David Barbet Date: Thu, 18 Jul 2019 10:48:09 -0700 Subject: [PATCH] Update liveshare callers to specify thread context. --- .../LanguageServer/Protocol/Handler/IRequestHandler.cs | 9 +++++++++ .../LiveShare/Impl/PreviewCodeActionsHandler.cs | 1 + .../LiveShare/Impl/Shims/AbstractLiveShareHandlerShim.cs | 9 +++++++++ .../Impl/Shims/FindImplementationsHandlerShim.cs | 2 +- .../LiveShare/Impl/Shims/FormatDocumentHandlerShim.cs | 2 +- .../Impl/Shims/FormatDocumentOnTypeHandlerShim.cs | 2 +- .../Impl/Shims/FormatDocumentRangeHandlerShim.cs | 2 +- .../LiveShare/Impl/Shims/RunCodeActionsHandlerShim.cs | 2 +- 8 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/Features/LanguageServer/Protocol/Handler/IRequestHandler.cs b/src/Features/LanguageServer/Protocol/Handler/IRequestHandler.cs index c0354301a58..84846ac1434 100644 --- a/src/Features/LanguageServer/Protocol/Handler/IRequestHandler.cs +++ b/src/Features/LanguageServer/Protocol/Handler/IRequestHandler.cs @@ -15,6 +15,15 @@ internal interface IRequestHandler internal interface IRequestHandler : IRequestHandler { + /// + /// Handles an LSP request. + /// + /// the solution to apply the request to. + /// the lsp request. + /// the client capabilities for the request. + /// a cancellation token. + /// a value to set if the threading context in the handler should be kept from the caller. + /// the lps response. Task HandleRequestAsync(Solution solution, RequestType request, ClientCapabilities clientCapabilities, CancellationToken cancellationToken, bool keepThreadContext = false); } } diff --git a/src/VisualStudio/LiveShare/Impl/PreviewCodeActionsHandler.cs b/src/VisualStudio/LiveShare/Impl/PreviewCodeActionsHandler.cs index 57d3b94d782..4724f1971fb 100644 --- a/src/VisualStudio/LiveShare/Impl/PreviewCodeActionsHandler.cs +++ b/src/VisualStudio/LiveShare/Impl/PreviewCodeActionsHandler.cs @@ -34,6 +34,7 @@ public async Task HandleAsync(RunCodeActionParams request, Reque var codeActions = await GetCodeActionsAsync(solution, request.CodeActionParams.TextDocument.Uri, request.CodeActionParams.Range, + keepThreadContext: false, cancellationToken).ConfigureAwait(false); var actionToRun = codeActions?.FirstOrDefault(a => a.Title == request.Title); diff --git a/src/VisualStudio/LiveShare/Impl/Shims/AbstractLiveShareHandlerShim.cs b/src/VisualStudio/LiveShare/Impl/Shims/AbstractLiveShareHandlerShim.cs index 886a0977c54..d282800121d 100644 --- a/src/VisualStudio/LiveShare/Impl/Shims/AbstractLiveShareHandlerShim.cs +++ b/src/VisualStudio/LiveShare/Impl/Shims/AbstractLiveShareHandlerShim.cs @@ -28,6 +28,15 @@ public virtual Task HandleAsync(RequestType param, RequestContext< return ((IRequestHandler)LazyRequestHandler.Value).HandleRequestAsync(requestContext.Context, param, requestContext.ClientCapabilities, cancellationToken); } + /// + /// Certain implementations require that the processing be done on the UI thread. + /// So allow the handler to specifiy that the thread context should be preserved. + /// + protected Task HandleAsyncPreserveThreadContext(RequestType param, RequestContext requestContext, CancellationToken cancellationToken) + { + return ((IRequestHandler)LazyRequestHandler.Value).HandleRequestAsync(requestContext.Context, param, requestContext.ClientCapabilities, cancellationToken, keepThreadContext: true); + } + protected Lazy GetRequestHandler(IEnumerable> requestHandlers, string methodName) { return requestHandlers.First(handler => handler.Metadata.MethodName == methodName); diff --git a/src/VisualStudio/LiveShare/Impl/Shims/FindImplementationsHandlerShim.cs b/src/VisualStudio/LiveShare/Impl/Shims/FindImplementationsHandlerShim.cs index 5b2cc957347..3e7f330e37d 100644 --- a/src/VisualStudio/LiveShare/Impl/Shims/FindImplementationsHandlerShim.cs +++ b/src/VisualStudio/LiveShare/Impl/Shims/FindImplementationsHandlerShim.cs @@ -29,7 +29,7 @@ public override async Task HandleAsync(TextDocumentPositionParams param, { // TypeScript requires this call to be on the UI thread. await _threadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken); - return await base.HandleAsync(param, requestContext, cancellationToken).ConfigureAwait(false); + return await base.HandleAsyncPreserveThreadContext(param, requestContext, cancellationToken).ConfigureAwait(false); } } } diff --git a/src/VisualStudio/LiveShare/Impl/Shims/FormatDocumentHandlerShim.cs b/src/VisualStudio/LiveShare/Impl/Shims/FormatDocumentHandlerShim.cs index e7e00d4f77f..13bf23472d6 100644 --- a/src/VisualStudio/LiveShare/Impl/Shims/FormatDocumentHandlerShim.cs +++ b/src/VisualStudio/LiveShare/Impl/Shims/FormatDocumentHandlerShim.cs @@ -29,7 +29,7 @@ public override async Task HandleAsync(DocumentFormattingParams para { // To get the formatting options, TypeScript expects to be called on the UI thread. await _threadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken); - return await base.HandleAsync(param, requestContext, cancellationToken).ConfigureAwait(false); + return await base.HandleAsyncPreserveThreadContext(param, requestContext, cancellationToken).ConfigureAwait(false); } } } diff --git a/src/VisualStudio/LiveShare/Impl/Shims/FormatDocumentOnTypeHandlerShim.cs b/src/VisualStudio/LiveShare/Impl/Shims/FormatDocumentOnTypeHandlerShim.cs index 94ff18bdc3c..428f8f7b631 100644 --- a/src/VisualStudio/LiveShare/Impl/Shims/FormatDocumentOnTypeHandlerShim.cs +++ b/src/VisualStudio/LiveShare/Impl/Shims/FormatDocumentOnTypeHandlerShim.cs @@ -29,7 +29,7 @@ public override async Task HandleAsync(DocumentOnTypeFormattingParam { // To get the formatting options, TypeScript expects to be called on the UI thread. await _threadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken); - return await base.HandleAsync(param, requestContext, cancellationToken).ConfigureAwait(false); + return await base.HandleAsyncPreserveThreadContext(param, requestContext, cancellationToken).ConfigureAwait(false); } } } diff --git a/src/VisualStudio/LiveShare/Impl/Shims/FormatDocumentRangeHandlerShim.cs b/src/VisualStudio/LiveShare/Impl/Shims/FormatDocumentRangeHandlerShim.cs index e0af4ab702e..fdfc314cc05 100644 --- a/src/VisualStudio/LiveShare/Impl/Shims/FormatDocumentRangeHandlerShim.cs +++ b/src/VisualStudio/LiveShare/Impl/Shims/FormatDocumentRangeHandlerShim.cs @@ -29,7 +29,7 @@ public override async Task HandleAsync(DocumentRangeFormattingParams { // To get the formatting options, TypeScript expects to be called on the UI thread. await _threadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken); - return await base.HandleAsync(param, requestContext, cancellationToken).ConfigureAwait(false); + return await base.HandleAsyncPreserveThreadContext(param, requestContext, cancellationToken).ConfigureAwait(false); } } } diff --git a/src/VisualStudio/LiveShare/Impl/Shims/RunCodeActionsHandlerShim.cs b/src/VisualStudio/LiveShare/Impl/Shims/RunCodeActionsHandlerShim.cs index 0e2aaa8c68f..5910b938ce3 100644 --- a/src/VisualStudio/LiveShare/Impl/Shims/RunCodeActionsHandlerShim.cs +++ b/src/VisualStudio/LiveShare/Impl/Shims/RunCodeActionsHandlerShim.cs @@ -51,7 +51,7 @@ public async override Task HandleAsync(LSP.ExecuteCommandParams request, { // Code actions must be applied from the UI thread in the VS context. await _threadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken); - return await base.HandleAsync(request, requestContext, cancellationToken).ConfigureAwait(true); + return await base.HandleAsyncPreserveThreadContext(request, requestContext, cancellationToken).ConfigureAwait(false); } catch (ArgumentException ex) { -- GitLab