From 7bc0fb436c695c2fce321752a5a3ea7abe2eae36 Mon Sep 17 00:00:00 2001 From: yoyo930021 Date: Wed, 11 Dec 2019 18:05:30 +0800 Subject: [PATCH] fix(completion): add TextEdit for fix #79 --- src/providers/completion.ts | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/providers/completion.ts b/src/providers/completion.ts index 71d564f2..77d2c234 100644 --- a/src/providers/completion.ts +++ b/src/providers/completion.ts @@ -1,6 +1,14 @@ 'use strict'; -import { CompletionList, CompletionItemKind, TextDocument, Files, CompletionItem } from 'vscode-languageserver'; +import { + CompletionList, + CompletionItemKind, + TextDocument, + Files, + CompletionItem, + Range, + TextEdit +} from 'vscode-languageserver'; import { IMixin, ISymbols } from '../types/symbols'; import { ISettings } from '../types/settings'; @@ -221,6 +229,23 @@ function createFunctionCompletionItems( return completions; } +function convertRange(document: TextDocument, offset: number, text: string) { + const start = + offset - + document + .getText() + .slice(0, offset) + .split('') + .reverse() + .findIndex(el => el === ' ' || el === ':'); + const startPosition = document.positionAt(start); + const endPosition = document.positionAt(start + text.length); + return Range.create(startPosition, endPosition); +} + +/** + * Do Completion :) + */ export async function doCompletion( document: TextDocument, offset: number, @@ -265,5 +290,10 @@ export async function doCompletion( completions.items = completions.items.concat(functions); } + completions.items = completions.items.map(el => ({ + ...el, + textEdit: TextEdit.replace(convertRange(document, offset, el.insertText ?? el.label), el.insertText ?? el.label) + })); + return completions; }