Merge pull request #81 from github/cschleiden/clean-transform

Clean up transform function
This commit is contained in:
Christopher Schleiden
2023-01-10 16:36:46 -08:00
committed by GitHub
3 changed files with 49 additions and 23 deletions
@@ -70,6 +70,12 @@ describe("auto-complete", () => {
}); });
}); });
describe("in multi-line expressions", () => {
it("includes built-in functions", () => {
expect(testComplete("1 == (\nto").map(x => x.label)).toContainEqual("toJson");
});
});
describe("for contexts", () => { describe("for contexts", () => {
it("provides suggestions for env", () => { it("provides suggestions for env", () => {
const expected = completionItems("BAR_TEST", "FOO"); const expected = completionItems("BAR_TEST", "FOO");
@@ -37,7 +37,7 @@ jobs:
jobs: jobs:
build: build:
runs-on: runs-on:
- dummy`); - key`);
expect(newPos.character).toEqual(9); expect(newPos.character).toEqual(9);
}); });
@@ -53,7 +53,23 @@ jobs:
jobs: jobs:
build: build:
runs-on: runs-on:
dummy:`); key:`);
expect(newPos.character).toEqual(7); expect(newPos.character).toEqual(7);
}); });
it("does not transform expression lines", () => {
const [doc, pos] = getPositionFromCursor(`on: push
jobs:
build:
runs-on:
\${{ github| }}`);
const [newDoc, newPos] = transform(doc, pos);
expect(newDoc.getText()).toEqual(`on: push
jobs:
build:
runs-on:
\${{ github }}`);
expect(newPos.character).toEqual(16);
});
}); });
+25 -21
View File
@@ -1,7 +1,7 @@
import {Position, TextDocument} from "vscode-languageserver-textdocument"; import {Position, TextDocument} from "vscode-languageserver-textdocument";
import {Range} from "vscode-languageserver-types"; import {Range} from "vscode-languageserver-types";
const DUMMY_KEY = "dummy"; const PLACEHOLDER_KEY = "key";
// Transform a document to work around YAML parsing issues // Transform a document to work around YAML parsing issues
// Based on `_transform` in https://github.com/cschleiden/github-actions-parser/blob/main/src/lib/parser/complete.ts#L311 // Based on `_transform` in https://github.com/cschleiden/github-actions-parser/blob/main/src/lib/parser/complete.ts#L311
@@ -27,29 +27,33 @@ export function transform(doc: TextDocument, pos: Position): [TextDocument, Posi
// Special case for Actions, if this line contains an expression marker, do _not_ transform. This is // Special case for Actions, if this line contains an expression marker, do _not_ transform. This is
// an ugly fix for auto-completion in multi-line YAML strings. At this point in the process, we cannot // an ugly fix for auto-completion in multi-line YAML strings. At this point in the process, we cannot
// determine if a line is in such a multi-line string. // determine if a line is in such a multi-line string.
if (line.indexOf("${{") === -1) { if (line.indexOf("${{") !== -1) {
const colon = line.indexOf(":"); return [doc, pos];
if (colon === -1) { }
const trimmedLine = line.trim();
if (trimmedLine === "" || trimmedLine === "-") {
// Node in sequence or empty line
let spacer = "";
if (trimmedLine === "-" && !line.endsWith(" ")) {
spacer = " ";
offset++;
}
line = const containsColon = line.indexOf(":") !== -1;
line.substring(0, linePos) + spacer + DUMMY_KEY + (trimmedLine === "-" ? "" : ":") + line.substring(linePos); if (!containsColon) {
const trimmedLine = line.trim();
// Adjust pos by one to prevent a sequence node being marked as active if (trimmedLine === "" || trimmedLine === "-") {
// Pos in sequence or empty line
let spacer = "";
if (trimmedLine === "-" && !line.endsWith(" ")) {
spacer = " ";
offset++; offset++;
} else if (!trimmedLine.startsWith("-")) {
// Add `:` to end of line
line = line + ":";
} }
} else {
offset = offset - 1; line =
line.substring(0, linePos) +
spacer +
PLACEHOLDER_KEY +
(trimmedLine === "-" ? "" : ":") +
line.substring(linePos);
// Adjust pos by one to prevent a sequence node being marked as active
offset++;
} else if (!trimmedLine.startsWith("-")) {
// Add `:` to end of line
line = line + ":";
} }
} }