Initial code import

This commit is contained in:
Christopher Schleiden
2022-11-08 17:00:59 -08:00
parent 7352cda0cf
commit 2e1652515e
38 changed files with 1354 additions and 1 deletions
@@ -0,0 +1,26 @@
import { getPositionFromCursor } from "./cursor-position";
describe("getPositionFromCursor", () => {
it("returns the position of the cursor and the document without that cursor", () => {
const input = "on: push\njobs:|";
const [newDoc, position] = getPositionFromCursor(input);
expect(position).toEqual({ line: 1, character: 5 });
expect(newDoc.getText()).toEqual("on: push\njobs:");
});
it("throws an error if no cursor is found", () => {
const input = "on: push\njobs:";
expect(() => getPositionFromCursor(input)).toThrowError(
"No cursor found in document"
);
});
it("handles a cursor at the beginning of the document", () => {
const input = "|on: push\njobs:";
const [newDoc, position] = getPositionFromCursor(input);
expect(position).toEqual({ line: 0, character: 0 });
expect(newDoc.getText()).toEqual("on: push\njobs:");
});
});
@@ -0,0 +1,22 @@
import { TextDocument, Position } from "vscode-languageserver-textdocument";
// Calculates the position of the cursor and the document without that cursor
// Cursor is represented by a `|` character
export function getPositionFromCursor(input: string): [TextDocument, Position] {
const doc = TextDocument.create("test://test/test.yaml", "yaml", 0, input);
const cursorIndex = doc.getText().indexOf("|");
if (cursorIndex === -1) {
throw new Error("No cursor found in document");
}
const position = doc.positionAt(cursorIndex);
const newDoc = TextDocument.create(
doc.uri,
doc.languageId,
doc.version,
doc.getText().replace("|", "")
);
return [newDoc, position];
}