draivin / hsnips

HyperSnips: a powerful snippet engine for VS Code, inspired by vim's UltiSnips
MIT License
154 stars 25 forks source link

Negative and Positive Lookaheads #162

Open jvonk opened 1 year ago

jvonk commented 1 year ago

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?