From f0a84eb55796b732458e2b2e1b9a65fefe23019d Mon Sep 17 00:00:00 2001 From: David Barbet Date: Wed, 17 Jul 2019 17:39:08 -0700 Subject: [PATCH] Use thread context parameter for formatting handlers. --- .../Handler/Formatting/FormatDocumentHandler.cs | 5 +++-- .../Handler/Formatting/FormatDocumentHandlerBase.cs | 6 +++--- .../Formatting/FormatDocumentOnTypeHandler.cs | 13 +++++++------ .../Formatting/FormatDocumentRangeHandler.cs | 5 +++-- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/Features/LanguageServer/Protocol/Handler/Formatting/FormatDocumentHandler.cs b/src/Features/LanguageServer/Protocol/Handler/Formatting/FormatDocumentHandler.cs index 0b3007c7a0f..57629714a4b 100644 --- a/src/Features/LanguageServer/Protocol/Handler/Formatting/FormatDocumentHandler.cs +++ b/src/Features/LanguageServer/Protocol/Handler/Formatting/FormatDocumentHandler.cs @@ -11,9 +11,10 @@ namespace Microsoft.CodeAnalysis.LanguageServer.Handler [ExportLspMethod(LSP.Methods.TextDocumentFormattingName)] internal class FormatDocumentHandler : FormatDocumentHandlerBase, IRequestHandler { - public async Task HandleRequestAsync(Solution solution, LSP.DocumentFormattingParams request, LSP.ClientCapabilities clientCapabilities, CancellationToken cancellationToken) + public async Task HandleRequestAsync(Solution solution, LSP.DocumentFormattingParams request, LSP.ClientCapabilities clientCapabilities, + CancellationToken cancellationToken, bool keepThreadContext = false) { - return await GetTextEdits(solution, request.TextDocument.Uri, cancellationToken).ConfigureAwait(false); + return await GetTextEdits(solution, request.TextDocument.Uri, keepThreadContext, cancellationToken).ConfigureAwait(keepThreadContext); } } } diff --git a/src/Features/LanguageServer/Protocol/Handler/Formatting/FormatDocumentHandlerBase.cs b/src/Features/LanguageServer/Protocol/Handler/Formatting/FormatDocumentHandlerBase.cs index fda38125b46..89ec543f121 100644 --- a/src/Features/LanguageServer/Protocol/Handler/Formatting/FormatDocumentHandlerBase.cs +++ b/src/Features/LanguageServer/Protocol/Handler/Formatting/FormatDocumentHandlerBase.cs @@ -15,7 +15,7 @@ namespace Microsoft.CodeAnalysis.LanguageServer.Handler { internal class FormatDocumentHandlerBase { - protected async Task GetTextEdits(Solution solution, Uri documentUri, CancellationToken cancellationToken, LSP.Range range = null) + protected async Task GetTextEdits(Solution solution, Uri documentUri, bool keepThreadContext, CancellationToken cancellationToken, LSP.Range range = null) { var edits = new ArrayBuilder(); var document = solution.GetDocumentFromURI(documentUri); @@ -23,14 +23,14 @@ protected async Task GetTextEdits(Solution solution, Uri documen if (document != null) { var formattingService = document.Project.LanguageServices.GetService(); - var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false); + var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(keepThreadContext); TextSpan? textSpan = null; if (range != null) { textSpan = ProtocolConversions.RangeToTextSpan(range, text); } - var textChanges = await formattingService.GetFormattingChangesAsync(document, textSpan, cancellationToken).ConfigureAwait(false); + var textChanges = await formattingService.GetFormattingChangesAsync(document, textSpan, cancellationToken).ConfigureAwait(keepThreadContext); edits.AddRange(textChanges.Select(change => ProtocolConversions.TextChangeToTextEdit(change, text))); } diff --git a/src/Features/LanguageServer/Protocol/Handler/Formatting/FormatDocumentOnTypeHandler.cs b/src/Features/LanguageServer/Protocol/Handler/Formatting/FormatDocumentOnTypeHandler.cs index dd774b9428c..74ba030f10d 100644 --- a/src/Features/LanguageServer/Protocol/Handler/Formatting/FormatDocumentOnTypeHandler.cs +++ b/src/Features/LanguageServer/Protocol/Handler/Formatting/FormatDocumentOnTypeHandler.cs @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Collections.Generic; using System.Composition; @@ -17,14 +17,15 @@ namespace Microsoft.CodeAnalysis.LanguageServer.Handler [ExportLspMethod(Methods.TextDocumentOnTypeFormattingName)] internal class FormatDocumentOnTypeHandler : IRequestHandler { - public async Task HandleRequestAsync(Solution solution, DocumentOnTypeFormattingParams request, ClientCapabilities clientCapabilities, CancellationToken cancellationToken) + public async Task HandleRequestAsync(Solution solution, DocumentOnTypeFormattingParams request, ClientCapabilities clientCapabilities, + CancellationToken cancellationToken, bool keepThreadContext = false) { var edits = new ArrayBuilder(); var document = solution.GetDocumentFromURI(request.TextDocument.Uri); if (document != null) { var formattingService = document.Project.LanguageServices.GetService(); - var position = await document.GetPositionFromLinePositionAsync(ProtocolConversions.PositionToLinePosition(request.Position), cancellationToken).ConfigureAwait(false); + var position = await document.GetPositionFromLinePositionAsync(ProtocolConversions.PositionToLinePosition(request.Position), cancellationToken).ConfigureAwait(keepThreadContext); if (string.IsNullOrEmpty(request.Character)) { @@ -34,14 +35,14 @@ public async Task HandleRequestAsync(Solution solution, DocumentOnTy IList textChanges; if (SyntaxFacts.IsNewLine(request.Character[0])) { - textChanges = await formattingService.GetFormattingChangesOnReturnAsync(document, position, cancellationToken).ConfigureAwait(false); + textChanges = await formattingService.GetFormattingChangesOnReturnAsync(document, position, cancellationToken).ConfigureAwait(keepThreadContext); } else { - textChanges = await formattingService.GetFormattingChangesAsync(document, request.Character[0], position, cancellationToken).ConfigureAwait(false); + textChanges = await formattingService.GetFormattingChangesAsync(document, request.Character[0], position, cancellationToken).ConfigureAwait(keepThreadContext); } - var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false); + var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(keepThreadContext); if (textChanges != null) { edits.AddRange(textChanges.Select(change => ProtocolConversions.TextChangeToTextEdit(change, text))); diff --git a/src/Features/LanguageServer/Protocol/Handler/Formatting/FormatDocumentRangeHandler.cs b/src/Features/LanguageServer/Protocol/Handler/Formatting/FormatDocumentRangeHandler.cs index c5ad891d494..ed5b80a4154 100644 --- a/src/Features/LanguageServer/Protocol/Handler/Formatting/FormatDocumentRangeHandler.cs +++ b/src/Features/LanguageServer/Protocol/Handler/Formatting/FormatDocumentRangeHandler.cs @@ -11,9 +11,10 @@ namespace Microsoft.CodeAnalysis.LanguageServer.Handler [ExportLspMethod(Methods.TextDocumentRangeFormattingName)] internal class FormatDocumentRangeHandler : FormatDocumentHandlerBase, IRequestHandler { - public async Task HandleRequestAsync(Solution solution, DocumentRangeFormattingParams request, ClientCapabilities clientCapabilities, CancellationToken cancellationToken) + public async Task HandleRequestAsync(Solution solution, DocumentRangeFormattingParams request, ClientCapabilities clientCapabilities, + CancellationToken cancellationToken, bool keepThreadContext = false) { - return await GetTextEdits(solution, request.TextDocument.Uri, cancellationToken, range: request.Range).ConfigureAwait(false); + return await GetTextEdits(solution, request.TextDocument.Uri, keepThreadContext, cancellationToken, range: request.Range).ConfigureAwait(keepThreadContext); } } } -- GitLab