Skip to content

Commit 5b274fd

Browse files
committed
fix(completion): add TextEdit for fix #79
1 parent 4d4e254 commit 5b274fd

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

src/providers/completion.ts

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
'use strict';
22

3-
import { CompletionList, CompletionItemKind, TextDocument, Files, CompletionItem } from 'vscode-languageserver';
3+
import {
4+
CompletionList,
5+
CompletionItemKind,
6+
TextDocument,
7+
Files,
8+
CompletionItem,
9+
Range,
10+
TextEdit
11+
} from 'vscode-languageserver';
412

513
import { IMixin, ISymbols } from '../types/symbols';
614
import { ISettings } from '../types/settings';
@@ -221,6 +229,20 @@ function createFunctionCompletionItems(
221229
return completions;
222230
}
223231

232+
function convertRange(document: TextDocument, offset: number, text: string) {
233+
const start =
234+
offset -
235+
document
236+
.getText()
237+
.slice(0, offset)
238+
.split('')
239+
.reverse()
240+
.findIndex(el => el === ' ' || el === ':');
241+
const startPosition = document.positionAt(start);
242+
const endPosition = document.positionAt(start + text.length);
243+
return Range.create(startPosition, endPosition);
244+
}
245+
224246
/**
225247
* Do Completion :)
226248
*/
@@ -268,5 +290,10 @@ export function doCompletion(
268290
completions.items = completions.items.concat(functions);
269291
}
270292

293+
completions.items = completions.items.map(el => ({
294+
...el,
295+
textEdit: TextEdit.replace(convertRange(document, offset, el.insertText ?? el.label), el.insertText ?? el.label)
296+
}));
297+
271298
return completions;
272299
}

0 commit comments

Comments
 (0)