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; }