Set completion range for invalid token

This commit is contained in:
Jacob Wallraff
2023-01-12 15:17:46 -08:00
parent dd386b06bb
commit 51908ec789
2 changed files with 38 additions and 0 deletions
@@ -374,6 +374,37 @@ jobs:
});
});
it("sets a range for invalid tokens", async () => {
const input = "on: push\njobs:\n build:\n runs-o|";
const result = await complete(...getPositionFromCursor(input));
expect(result).not.toBeUndefined();
expect(result.map(e => e.label)).toContain("runs-on");
let textEdit = result.filter(e => e.label === "runs-on")[0].textEdit as TextEdit;
expect(textEdit.newText).toEqual("runs-on");
expect(textEdit.range).toEqual({
start: {line: 3, character: 4},
end: {line: 3, character: 10}
});
});
it("sets a 0-length range while no initial token", async () => {
const input = "on: push\njobs:\n build:\n |";
const result = await complete(...getPositionFromCursor(input));
expect(result).not.toBeUndefined();
expect(result.map(e => e.label)).toContain("runs-on");
let textEdit = result.filter(e => e.label === "runs-on")[0].textEdit as TextEdit;
expect(textEdit.newText).toEqual("runs-on");
expect(textEdit.range).toEqual({
start: {line: 3, character: 4},
end: {line: 3, character: 4}
});
});
describe("completes with indentation", () => {
it("default indentation", async () => {
const input = `on: push
+7
View File
@@ -103,6 +103,13 @@ export async function complete(
if (token?.range) {
replaceRange = mapRange(token.range);
}
else if (!token) {
// Not a valid token, create a range from the current position
const line = newDoc.getText({start: {line: position.line, character: 0}, end: position});
// Get the length of the current word
const val = line.match(/[\w_-]*$/)?.[0].length || 0;
replaceRange = Range.create({ line: position.line, character: position.character - val } as Position, position);
}
return values.map(value => {
const newText = value.insertText || value.label;