From 3ceb10ba1cdccdba54f9804368bb0cc6e66c6713 Mon Sep 17 00:00:00 2001 From: Fredric Silberberg Date: Thu, 17 Sep 2020 22:48:26 -0700 Subject: [PATCH] Support TextEdit in completion responses This is the vscode side of https://github.com/OmniSharp/omnisharp-roslyn/pull/1941. This is written in such a fashion as to be mergeable now, as it will fall back to InsertText if TextEdit is not present in the completion response. After some appropriate period of time, we can remove the InsertText component. --- src/features/completionProvider.ts | 19 +++++++++++++------ src/omnisharp/protocol.ts | 1 + 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/features/completionProvider.ts b/src/features/completionProvider.ts index 33069812b..b0359813b 100644 --- a/src/features/completionProvider.ts +++ b/src/features/completionProvider.ts @@ -60,20 +60,26 @@ export default class OmnisharpCompletionProvider extends AbstractProvider implem } private _convertToVscodeCompletionItem(omnisharpCompletion: protocol.OmnisharpCompletionItem): CompletionItem { - let docs: MarkdownString | undefined = omnisharpCompletion.Documentation ? new MarkdownString(omnisharpCompletion.Documentation, false) : undefined; + const docs: MarkdownString | undefined = omnisharpCompletion.Documentation ? new MarkdownString(omnisharpCompletion.Documentation, false) : undefined; - const mapTextEdit = function (edit: protocol.LinePositionSpanTextChange): TextEdit { + const mapRange = function (edit: protocol.LinePositionSpanTextChange): Range { const newStart = new Position(edit.StartLine - 1, edit.StartColumn - 1); const newEnd = new Position(edit.EndLine - 1, edit.EndColumn - 1); - const newRange = new Range(newStart, newEnd); - return new TextEdit(newRange, edit.NewText); + return new Range(newStart, newEnd); + }; + + const mapTextEdit = function (edit: protocol.LinePositionSpanTextChange): TextEdit { + return new TextEdit(mapRange(edit), edit.NewText); }; const additionalTextEdits = omnisharpCompletion.AdditionalTextEdits?.map(mapTextEdit); + const newText = omnisharpCompletion.TextEdit?.NewText ?? omnisharpCompletion.InsertText; const insertText = omnisharpCompletion.InsertTextFormat === InsertTextFormat.Snippet - ? new SnippetString(omnisharpCompletion.InsertText) - : omnisharpCompletion.InsertText; + ? new SnippetString(newText) + : newText; + + const insertRange = omnisharpCompletion.TextEdit ? mapRange(omnisharpCompletion.TextEdit) : undefined; return { label: omnisharpCompletion.Label, @@ -84,6 +90,7 @@ export default class OmnisharpCompletionProvider extends AbstractProvider implem preselect: omnisharpCompletion.Preselect, filterText: omnisharpCompletion.FilterText, insertText: insertText, + range: insertRange, tags: omnisharpCompletion.Tags, sortText: omnisharpCompletion.SortText, additionalTextEdits: additionalTextEdits, diff --git a/src/omnisharp/protocol.ts b/src/omnisharp/protocol.ts index 7de7f9925..6f1b5896e 100644 --- a/src/omnisharp/protocol.ts +++ b/src/omnisharp/protocol.ts @@ -523,6 +523,7 @@ export interface OmnisharpCompletionItem { FilterText?: string; InsertText?: string; InsertTextFormat?: InsertTextFormat; + TextEdit?: LinePositionSpanTextChange; CommitCharacters?: string[]; AdditionalTextEdits?: LinePositionSpanTextChange[]; Data: any;