Skip to content

Commit 7bc0fb4

Browse files
committed
fix(completion): add TextEdit for fix #79
1 parent 051a50c commit 7bc0fb4

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

src/providers/completion.ts

+31-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,23 @@ 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+
246+
/**
247+
* Do Completion :)
248+
*/
224249
export async function doCompletion(
225250
document: TextDocument,
226251
offset: number,
@@ -265,5 +290,10 @@ export async function doCompletion(
265290
completions.items = completions.items.concat(functions);
266291
}
267292

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+
268298
return completions;
269299
}

0 commit comments

Comments
 (0)