Auto completion for multi-line expressions

This commit is contained in:
Christopher Schleiden
2022-12-09 08:38:50 -08:00
parent 1af3f0b672
commit efe71dd684
3 changed files with 86 additions and 8 deletions
@@ -1,18 +1,30 @@
import {Position, TextDocument} from "vscode-languageserver-textdocument";
import {createDocument} from "./document";
// 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] {
/**
* Calculates the position of the cursor and the document without that cursor
* Cursor is represented by a `|` character
* @param input Input string
* @param skip Instances of `|` to skip
*/
export function getPositionFromCursor(input: string, skip = 0): [TextDocument, Position] {
const doc = createDocument("test.yaml", input);
const cursorIndex = doc.getText().indexOf("|");
let cursorIndex = doc.getText().indexOf("|");
for (let i = 0; i < skip && cursorIndex !== -1; i++) {
cursorIndex = doc.getText().indexOf("|", cursorIndex + 1);
}
if (cursorIndex === -1) {
throw new Error("No cursor found in document");
}
// Replace only the last occurence of | in string
let newText = doc.getText();
newText = newText.substring(0, cursorIndex) + newText.substring(cursorIndex + 1);
const position = doc.positionAt(cursorIndex);
const newDoc = TextDocument.create(doc.uri, doc.languageId, doc.version, doc.getText().replace("|", ""));
const newDoc = TextDocument.create(doc.uri, doc.languageId, doc.version, newText);
return [newDoc, position];
}