Auto completion for multi-line expressions
This commit is contained in:
@@ -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];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user