draivin / hsnips

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

activeTextEditor as global variable refers to the hsnips file #146

Closed anonymousALLDAY closed 1 year ago

anonymousALLDAY commented 1 year ago
global
    const vscode = require("vscode");
    var editor=vscode.window.activeTextEditor
endglobal

if you do this, the variable editor refers to .hsnips file, not current file.

you can reproduce this by

  1. create a snippet that

    pos = editor.selection.active
    rv = [pos.line, pos.character]
  2. Before trying the snippet in a different file, put the cursor in a different position from when you're in .hsnips file.

  3. test the snippet, expand it.

then you can easily check that the return value of position is of the cursor of .hsnips file, not the file you are currently editing.

This might not be a bug, but it would be more useful for activeTextEditor to target current document even though it was declared in global block, as calling activeTextEditor in each snippet block is tedious and inefficient.

draivin commented 1 year ago

The global scope is evaluated only once, when parsing the snippets, and so this is not a bug. If you intend to get the current active window, you should probably have that code inside a function defined on the global scope, and evaluate the function by using context during the snippet expansion.