Skip to content

Commit

Permalink
fix(completion): add TextEdit for fix #79
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyo930021 committed Jan 5, 2020
1 parent 051a50c commit 7bc0fb4
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/providers/completion.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}

0 comments on commit 7bc0fb4

Please sign in to comment.