提交 f0a84eb5 编写于 作者: D David Barbet

Use thread context parameter for formatting handlers.

上级 4bb99c1c
...@@ -11,9 +11,10 @@ namespace Microsoft.CodeAnalysis.LanguageServer.Handler ...@@ -11,9 +11,10 @@ namespace Microsoft.CodeAnalysis.LanguageServer.Handler
[ExportLspMethod(LSP.Methods.TextDocumentFormattingName)] [ExportLspMethod(LSP.Methods.TextDocumentFormattingName)]
internal class FormatDocumentHandler : FormatDocumentHandlerBase, IRequestHandler<LSP.DocumentFormattingParams, LSP.TextEdit[]> internal class FormatDocumentHandler : FormatDocumentHandlerBase, IRequestHandler<LSP.DocumentFormattingParams, LSP.TextEdit[]>
{ {
public async Task<LSP.TextEdit[]> HandleRequestAsync(Solution solution, LSP.DocumentFormattingParams request, LSP.ClientCapabilities clientCapabilities, CancellationToken cancellationToken) public async Task<LSP.TextEdit[]> 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);
} }
} }
} }
...@@ -15,7 +15,7 @@ namespace Microsoft.CodeAnalysis.LanguageServer.Handler ...@@ -15,7 +15,7 @@ namespace Microsoft.CodeAnalysis.LanguageServer.Handler
{ {
internal class FormatDocumentHandlerBase internal class FormatDocumentHandlerBase
{ {
protected async Task<LSP.TextEdit[]> GetTextEdits(Solution solution, Uri documentUri, CancellationToken cancellationToken, LSP.Range range = null) protected async Task<LSP.TextEdit[]> GetTextEdits(Solution solution, Uri documentUri, bool keepThreadContext, CancellationToken cancellationToken, LSP.Range range = null)
{ {
var edits = new ArrayBuilder<LSP.TextEdit>(); var edits = new ArrayBuilder<LSP.TextEdit>();
var document = solution.GetDocumentFromURI(documentUri); var document = solution.GetDocumentFromURI(documentUri);
...@@ -23,14 +23,14 @@ protected async Task<LSP.TextEdit[]> GetTextEdits(Solution solution, Uri documen ...@@ -23,14 +23,14 @@ protected async Task<LSP.TextEdit[]> GetTextEdits(Solution solution, Uri documen
if (document != null) if (document != null)
{ {
var formattingService = document.Project.LanguageServices.GetService<IEditorFormattingService>(); var formattingService = document.Project.LanguageServices.GetService<IEditorFormattingService>();
var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false); var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(keepThreadContext);
TextSpan? textSpan = null; TextSpan? textSpan = null;
if (range != null) if (range != null)
{ {
textSpan = ProtocolConversions.RangeToTextSpan(range, text); 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))); edits.AddRange(textChanges.Select(change => ProtocolConversions.TextChangeToTextEdit(change, text)));
} }
......
// 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.Collections.Generic;
using System.Composition; using System.Composition;
...@@ -17,14 +17,15 @@ namespace Microsoft.CodeAnalysis.LanguageServer.Handler ...@@ -17,14 +17,15 @@ namespace Microsoft.CodeAnalysis.LanguageServer.Handler
[ExportLspMethod(Methods.TextDocumentOnTypeFormattingName)] [ExportLspMethod(Methods.TextDocumentOnTypeFormattingName)]
internal class FormatDocumentOnTypeHandler : IRequestHandler<DocumentOnTypeFormattingParams, TextEdit[]> internal class FormatDocumentOnTypeHandler : IRequestHandler<DocumentOnTypeFormattingParams, TextEdit[]>
{ {
public async Task<TextEdit[]> HandleRequestAsync(Solution solution, DocumentOnTypeFormattingParams request, ClientCapabilities clientCapabilities, CancellationToken cancellationToken) public async Task<TextEdit[]> HandleRequestAsync(Solution solution, DocumentOnTypeFormattingParams request, ClientCapabilities clientCapabilities,
CancellationToken cancellationToken, bool keepThreadContext = false)
{ {
var edits = new ArrayBuilder<TextEdit>(); var edits = new ArrayBuilder<TextEdit>();
var document = solution.GetDocumentFromURI(request.TextDocument.Uri); var document = solution.GetDocumentFromURI(request.TextDocument.Uri);
if (document != null) if (document != null)
{ {
var formattingService = document.Project.LanguageServices.GetService<IEditorFormattingService>(); var formattingService = document.Project.LanguageServices.GetService<IEditorFormattingService>();
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)) if (string.IsNullOrEmpty(request.Character))
{ {
...@@ -34,14 +35,14 @@ public async Task<TextEdit[]> HandleRequestAsync(Solution solution, DocumentOnTy ...@@ -34,14 +35,14 @@ public async Task<TextEdit[]> HandleRequestAsync(Solution solution, DocumentOnTy
IList<TextChange> textChanges; IList<TextChange> textChanges;
if (SyntaxFacts.IsNewLine(request.Character[0])) 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 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) if (textChanges != null)
{ {
edits.AddRange(textChanges.Select(change => ProtocolConversions.TextChangeToTextEdit(change, text))); edits.AddRange(textChanges.Select(change => ProtocolConversions.TextChangeToTextEdit(change, text)));
......
...@@ -11,9 +11,10 @@ namespace Microsoft.CodeAnalysis.LanguageServer.Handler ...@@ -11,9 +11,10 @@ namespace Microsoft.CodeAnalysis.LanguageServer.Handler
[ExportLspMethod(Methods.TextDocumentRangeFormattingName)] [ExportLspMethod(Methods.TextDocumentRangeFormattingName)]
internal class FormatDocumentRangeHandler : FormatDocumentHandlerBase, IRequestHandler<DocumentRangeFormattingParams, TextEdit[]> internal class FormatDocumentRangeHandler : FormatDocumentHandlerBase, IRequestHandler<DocumentRangeFormattingParams, TextEdit[]>
{ {
public async Task<TextEdit[]> HandleRequestAsync(Solution solution, DocumentRangeFormattingParams request, ClientCapabilities clientCapabilities, CancellationToken cancellationToken) public async Task<TextEdit[]> 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);
} }
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册