hsnips icon indicating copy to clipboard operation
hsnips copied to clipboard

Negative and Positive Lookaheads

Open jvonk opened this issue 1 year ago • 0 comments

Negative and positive lookahead expressions fail Ex: This snippet is supposed to match spacing and then a "}" and replace with that "}" and then the spacing while moving the cursor to the right. In latex.hsnips:

snippet `(\s+)(?=\})` "move extra space" iA
}``rv=m[1]``
endsnippet

This doesn't work because expressions are restricted to being right before cursor. Currently, the only way to do this is to have something like the following in the global section:

vscode.window.onDidChangeTextEditorSelection((e) => {
    if (e.textEditor !== undefined) {
        editor = e.textEditor;
        document = editor.document;
        if (document.languageId === "latex") {
            const pos = e.selections[0].active;
            const line = document.lineAt(pos.line).text;
            math_mode = !line.includes("text");
            let space_colon = [...line.matchAll(/(\s+)\}/g)];
            if (space_colon.length > 0) {
                editor.edit(editBuilder => {
                    space_colon.forEach(match => {
                        editBuilder.replace(new vscode.Range(pos.line, match.index, pos.line, match.index + match[0].length), "}"+match[1]);
                        if (pos.character >= match.index && pos.character < match.index + match[0].length && match[1].length > 0) {
                            vscode.commands.executeCommand("cursorMove", {'to':'right','value':match[1].length-1});
                        }
                    });
                });
            }
        }
    }
});

Is it possible to provide basic support for such regexes?

jvonk avatar Mar 30 '23 23:03 jvonk