Merge pull request #87 from github/thyeggman/token-completion-range

Set completion range for invalid token key
This commit is contained in:
Jacob Wallraff
2023-01-24 14:05:54 -08:00
committed by GitHub
2 changed files with 36 additions and 0 deletions
@@ -372,6 +372,36 @@ jobs:
});
});
it("sets a range for token key", 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 key", 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
+6
View File
@@ -112,6 +112,12 @@ export async function complete(
let replaceRange: Range | undefined;
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}, position);
}
return values.map(value => {