import { default as Yasqe, Token, Position } from "./"; /** * When typing a query, this query is sometimes syntactically invalid, causing * the current tokens to be incorrect This causes problem for autocompletion. * http://bla might result in two tokens: http:// and bla. We'll want to combine * these */ export function getCompleteToken(yasqe: Yasqe, token?: Token, cur?: Position): Token { if (!cur) { cur = yasqe.getDoc().getCursor() as Position; } if (!token) { token = yasqe.getTokenAt(cur) as Token; } return expandTokenToEnd(yasqe, expandTokenToStart(yasqe, token, cur), cur); } function expandTokenToStart(yasqe: Yasqe, token: Token, cur: Position): Token { var prevToken: Token = yasqe.getTokenAt({ line: cur.line, ch: token.start, }); if ((token.type === "punc" || token.type === "error") && !token.state.possibleFullIri && !token.state.inPrefixDecl) { token.state.possibleCurrent = token.state.possibleNext; return token; } if (prevToken.type === "punc" && !prevToken.state.possibleFullIri && !prevToken.state.inPrefixDecl) { //assuming this is a path expression. Should not expand the token anymore //Also checking whether current token isnt an error, to avoid stopping on iri path delimiters return token; } // not start of line, and not whitespace if (prevToken.type != null && prevToken.type != "ws" && token.type != null && token.type != "ws") { token.start = prevToken.start; token.string = prevToken.string + token.string; return expandTokenToStart(yasqe, token, { line: cur.line, ch: prevToken.start, }); // recursively, might have multiple tokens which it should include } else if (token.type != null && token.type == "ws") { //always keep 1 char of whitespace between tokens. Otherwise, autocompletions might end up next to the previous node, without whitespace between them token.start = token.start + 1; token.string = token.string.substring(1); return token; } else { return token; } } function expandTokenToEnd(yasqe: Yasqe, token: Token, cur: Position): Token { if (token.string.indexOf(" ") >= 0) { /** * This is most likely a query ending with `