Add range to CompletionItems

This commit is contained in:
Beth Brennan
2022-12-06 16:15:35 -05:00
parent 7f3ed8c66a
commit bdcb592f90
2 changed files with 55 additions and 1 deletions
@@ -1,3 +1,4 @@
import {TextEdit} from "vscode-languageserver-types";
import {complete} from "./complete";
import {WorkflowContext} from "./context/workflow-context";
import {getPositionFromCursor} from "./test-utils/cursor-position";
@@ -329,4 +330,25 @@ o|
"You can now create workflows that are manually triggered with the new workflow_dispatch event. You will then see a 'Run workflow' button on the Actions tab, enabling you to easily trigger a run."
);
});
it("sets range when completing token", async () => {
const input = `on: push
jobs:
pre-build:
runs-on: ubuntu-latest
build:
runs-on: ubuntu-latest
needs: pre-bu|`;
const result = await complete(...getPositionFromCursor(input));
expect(result).not.toBeUndefined();
expect(result.length).toEqual(1);
let textEdit = result[0].textEdit as TextEdit;
expect(textEdit.newText).toEqual("pre-build");
expect(textEdit.range).toEqual({
start: {line: 6, character: 11},
end: {line: 6, character: 17}
});
});
});
+33 -1
View File
@@ -9,7 +9,7 @@ import {MappingToken} from "@github/actions-workflow-parser/templates/tokens/map
import {TokenType} from "@github/actions-workflow-parser/templates/tokens/types";
import {File} from "@github/actions-workflow-parser/workflows/file";
import {Position, TextDocument} from "vscode-languageserver-textdocument";
import {CompletionItem} from "vscode-languageserver-types";
import {CompletionItem, TextEdit, Range} from "vscode-languageserver-types";
import {ContextProviderConfig} from "./context-providers/config";
import {getContext} from "./context-providers/default";
import {getWorkflowContext, WorkflowContext} from "./context/workflow-context";
@@ -91,9 +91,27 @@ export async function complete(
}
const values = await getValues(token, keyToken, parent, valueProviderConfig, workflowContext);
let replaceRange: Range | undefined;
if (token?.range) {
replaceRange = {
start: {
line: token.range.start[0] - 1,
character: token.range.start[1] - 1
},
end: {
line: token.range.end[0] - 1,
character: token.range.end[1] - 1
}
};
}
return values.map(value => {
const item = CompletionItem.create(value.label);
item.detail = value.description;
if (replaceRange) {
item.textEdit = TextEdit.replace(replaceRange, value.label);
}
return item;
});
}
@@ -183,3 +201,17 @@ function filterAndSortCompletionOptions(options: Value[], existingValues?: Set<s
options.sort((a, b) => a.label.localeCompare(b.label));
return options;
}
// function getTokenRange(document: TextDocument, position: Position) {
// const lineRange: Range = {
// start: {line: position.line, character: 0},
// end: {line: position.line, character: Number.MAX_SAFE_INTEGER}
// };
// let line = document.getText(lineRange);
// const wordArray = line.split(/[\s,]+/);
// const word = wordArray[wordArray.length - 1];
// const start = new Position(position.line, Math.max(0, position.character - word.length));
// const end = new Position(position.line, position.character);
// return {start, end};
// }