mireq / luasnip-snippets

Vim-snippets (snipMate & UltiSnip Snippets) converted to native luasnip snippets
59 stars 2 forks source link

KeyError: 'LS_CAPTURE_1' #5

Closed WizardStark closed 5 months ago

WizardStark commented 5 months ago

Love the plugin, has saved me hours of making/converting snippets.

I am getting the following error. I made sure to install all dependencies and am using the provided LuaSnip config with nvim-cmp.

Error while evaluating functionNode@1 for snippet 'lorem(([1-4])?[0-9])?':
function provider#python3#Call, line 18: Vim(return):Error invoking 'python_eval' on channel 3 (python3-script-host):
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/user/.local/share/nvim/lazy/luasnip-snippets/pythonx/luasnip_snippets_python_helper.py", line 330, in execute_code
    start = (int(env['TM_LINE_NUMBER']), int(env['LS_CAPTURE_1']))
KeyError: 'TM_LINE_NUMBER'

:h luasnip-docstring for more info
E5108: Error executing lua function provider#python3#Call, line 18: Vim(return):Error invoking 'python_eval' on channel 3 (python3-script-host):
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/user/.local/share/nvim/lazy/luasnip-snippets/pythonx/luasnip_snippets_python_helper.py", line 330, in execute_code
    start = (int(env['TM_LINE_NUMBER']), int(env['LS_CAPTURE_1']))
KeyError: 'LS_CAPTURE_1'
stack traceback:
    [C]: in function 'py3eval'
    ...snip-snippets/lua/luasnip_snippets/common/snip_utils.lua:296: in function 'fn'
    ...are/nvim/lazy/LuaSnip/lua/luasnip/nodes/functionNode.lua:52: in function 'update'
    ...al/share/nvim/lazy/LuaSnip/lua/luasnip/nodes/snippet.lua:1026: in function 'update'
    ...al/share/nvim/lazy/LuaSnip/lua/luasnip/nodes/snippet.lua:769: in function 'trigger_expand'
    ...user/.local/share/nvim/lazy/LuaSnip/lua/luasnip/init.lua:272: in function 'snip_expand'
    ...cal/share/nvim/lazy/cmp_luasnip/lua/cmp_luasnip/init.lua:169: in function 'execute'
    .../user/.local/share/nvim/lazy/nvim-cmp/lua/cmp/source.lua:396: in function 'execute'
    ...e/user/.local/share/nvim/lazy/nvim-cmp/lua/cmp/entry.lua:482: in function 'execute'
    /home/user/.local/share/nvim/lazy/nvim-cmp/lua/cmp/core.lua:503: in function </home/user/.local/share/nvim/lazy/nvim-cmp/lua/cmp/core.lua:502>
    ...ocal/share/nvim/lazy/nvim-cmp/lua/cmp/utils/feedkeys.lua:47: in function <...ocal/share/nvim/lazy/nvim-cmp/lua/cmp/utils/feedkeys.lua:45>
mireq commented 5 months ago

@WizardStark thanks for reporting issue. I have no automated tests yet, so every feedback is valuable.

I have (probably) published fix. Please try it now.

WizardStark commented 5 months ago

Wow, thanks for the super fast response!

It seems that made some progress, but not fixed yet (a new error is its own reward sometimes I guess):


Error while evaluating functionNode@1 for snippet 'lorem(([1-4])?[0-9])?':
function provider#python3#Call, line 18: Vim(return):Error invoking 'python_eval' on channel 16 (python3-script-host):
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/user/.local/share/nvim/lazy/luasnip-snippets/pythonx/luasnip_snippets_python_helper.py", line 330, in execute_code
    start = match_context['start']
TypeError: list indices must be integers or slices, not str

:h luasnip-docstring for more info
E5108: Error executing lua function provider#python3#Call, line 18: Vim(return):Error invoking 'python_eval' on channel 16 (python3-script-host):
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/user/.local/share/nvim/lazy/luasnip-snippets/pythonx/luasnip_snippets_python_helper.py", line 330, in execute_code
    start = match_context['start']
TypeError: list indices must be integers or slices, not str
stack traceback:
    [C]: in function 'py3eval'
    ...snip-snippets/lua/luasnip_snippets/common/snip_utils.lua:296: in function 'fn'
    ...are/nvim/lazy/LuaSnip/lua/luasnip/nodes/functionNode.lua:52: in function 'update'
    ...al/share/nvim/lazy/LuaSnip/lua/luasnip/nodes/snippet.lua:1026: in function 'update'
    ...al/share/nvim/lazy/LuaSnip/lua/luasnip/nodes/snippet.lua:769: in function 'trigger_expand'
    ...user/.local/share/nvim/lazy/LuaSnip/lua/luasnip/init.lua:272: in function 'snip_expand'
    ...cal/share/nvim/lazy/cmp_luasnip/lua/cmp_luasnip/init.lua:169: in function 'execute'
    .../user/.local/share/nvim/lazy/nvim-cmp/lua/cmp/source.lua:396: in function 'execute'
    ...e/user/.local/share/nvim/lazy/nvim-cmp/lua/cmp/entry.lua:482: in function 'execute'
    /home/user/.local/share/nvim/lazy/nvim-cmp/lua/cmp/core.lua:503: in function </home/user/.local/share/nvim/lazy/nvim-cmp/lua/cmp/core.lua:502>
    ...ocal/share/nvim/lazy/nvim-cmp/lua/cmp/utils/feedkeys.lua:47: in function <...ocal/share/nvim/lazy/nvim-cmp/lua/cmp/utils/feedkeys.lua:45>
mireq commented 5 months ago

@WizardStark this is strange, match_context should be never empty, it's set in common/snip_utils.lua.

Anyway, i made helper functions more robust to missing context. Try it now.

WizardStark commented 5 months ago

The next error:

E5108: Error executing lua function provider#python3#Call, line 18: Vim(return):Error invoking 'python_eval' on channel 28 (python3-script-host):
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/user/.local/share/nvim/lazy/luasnip-snippets/pythonx/luasnip_snippets_python_helper.py", line 359, in execute_code
    exec(compiled_code, node_locals)
  File "<exec-interpolation-code>", line 1, in <module>
NameError: name 'match' is not defined
stack traceback:
    [C]: in function 'py3eval'
    ...snip-snippets/lua/luasnip_snippets/common/snip_utils.lua:296: in function 'fn'
    ...are/nvim/lazy/LuaSnip/lua/luasnip/nodes/functionNode.lua:52: in function 'update'
    ...al/share/nvim/lazy/LuaSnip/lua/luasnip/nodes/snippet.lua:1026: in function 'update'
    ...al/share/nvim/lazy/LuaSnip/lua/luasnip/nodes/snippet.lua:769: in function 'trigger_expand'
    ...user/.local/share/nvim/lazy/LuaSnip/lua/luasnip/init.lua:272: in function 'snip_expand'
    ...cal/share/nvim/lazy/cmp_luasnip/lua/cmp_luasnip/init.lua:169: in function 'execute'
    .../user/.local/share/nvim/lazy/nvim-cmp/lua/cmp/source.lua:396: in function 'execute'
    ...e/user/.local/share/nvim/lazy/nvim-cmp/lua/cmp/entry.lua:482: in function 'execute'
    /home/user/.local/share/nvim/lazy/nvim-cmp/lua/cmp/core.lua:503: in function </home/user/.local/share/nvim/lazy/nvim-cmp/lua/cmp/core.lua:502>
    ...ocal/share/nvim/lazy/nvim-cmp/lua/cmp/utils/feedkeys.lua:47: in function <...ocal/share/nvim/lazy/nvim-cmp/lua/cmp/utils/feedkeys.lua:45>
mireq commented 5 months ago

@WizardStark can you paste minimal configuration, which shows this error?

WizardStark commented 5 months ago

Sure thing, I probably should have done so earlier.

local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
    vim.fn.system({
        "git",
        "clone",
        "--filter=blob:none",
        "https://github.com/folke/lazy.nvim.git",
        "--branch=stable", -- latest stable release
        lazypath,
    })
end
vim.opt.rtp:prepend(lazypath)

require("lazy").setup({
    {
        "mireq/luasnip-snippets",
        dependencies = { "L3MON4D3/LuaSnip" },
        init = function()
            -- Mandatory setup function
            require("luasnip_snippets.common.snip_utils").setup()
        end,
    },
    {
        "L3MON4D3/LuaSnip",
        version = "2.*",
        build = "make install_jsregexp",
        dependencies = {
            "nvim-treesitter/nvim-treesitter",
        },
        init = function()
            local ls = require("luasnip")
            ls.setup({
                -- Required to automatically include base snippets, like "c" snippets for "cpp"
                load_ft_func = require("luasnip_snippets.common.snip_utils").load_ft_func,
                ft_func = require("luasnip_snippets.common.snip_utils").ft_func,
                -- To enable auto expansin
                enable_autosnippets = true,
                -- Uncomment to enable visual snippets triggered using <c-x>
                -- store_selection_keys = '<c-x>',
            })
            -- LuaSnip key bindings
            vim.keymap.set({ "i", "s" }, "<Tab>", function()
                if ls.expand_or_jumpable() then
                    ls.expand_or_jump()
                else
                    vim.api.nvim_input("<C-V><Tab>")
                end
            end, { silent = true })
            vim.keymap.set({ "i", "s" }, "<S-Tab>", function()
                ls.jump(-1)
            end, { silent = true })
            vim.keymap.set({ "i", "s" }, "<C-E>", function()
                if ls.choice_active() then
                    ls.change_choice(1)
                end
            end, { silent = true })
        end,
    },
    {
        "hrsh7th/nvim-cmp",
        version = false,
        event = "VeryLazy",
        dependencies = {
            "L3MON4D3/LuaSnip",
            "saadparwaiz1/cmp_luasnip",
        },
        opts = function()
            local luasnip = require("luasnip")
            local cmp = require("cmp")

            local function tab(fallback)
                if cmp.visible() then
                    cmp.select_next_item()
                elseif luasnip.expand_or_jumpable() then
                    luasnip.expand_or_jump()
                else
                    fallback()
                end
            end

            local function shift_tab(fallback)
                if cmp.visible() then
                    cmp.select_prev_item()
                elseif luasnip.jumpable(-1) then
                    luasnip.jump(-1)
                else
                    fallback()
                end
            end

            cmp.setup({
                snippet = {
                    expand = function(args)
                        luasnip.lsp_expand(args.body)
                    end,
                },
                mapping = {
                    ["<Tab>"] = cmp.mapping(tab, { "i", "s" }),
                    ["<S-Tab>"] = cmp.mapping(shift_tab, { "i", "s" }),
                    ["<C-u>"] = cmp.mapping.scroll_docs(-4),
                    ["<C-d>"] = cmp.mapping.scroll_docs(4),
                    ["<C-Space>"] = cmp.mapping.complete(),
                    ["<C-e>"] = cmp.mapping.close(),
                    ["<CR>"] = cmp.mapping.confirm({
                        select = false,
                    }),
                    ["<Up>"] = cmp.mapping(up, { "i", "s" }),
                    ["<Down>"] = cmp.mapping(down, { "i", "s" }),
                },
                sources = cmp.config.sources({
                    { name = "luasnip" },
                }, {
                    { name = "buffer" },
                }),
            })
        end,
    },
})
mireq commented 5 months ago

@WizardStark strange, i tried exactly this and it's working without problems.

Can you check neovim, LuaSnip, and lua versions?

My versions are:

$ nvim --version
NVIM v0.9.5
Build type: Release
LuaJIT 2.1.0-beta3

LuaSnip v2.2.0
WizardStark commented 5 months ago
nvim --version
NVIM v0.9.5
Build type: RelWithDebInfo
LuaJIT 2.1.1692716794

LuaSnip v2.2.0

From checkhealth (just to show python provider config as well):

provider: health#provider#check

Clipboard (optional) ~
- OK Clipboard tool found: neovide

Python 3 provider (optional) ~
- Using: g:python3_host_prog = "/usr/bin/python3"
- Executable: /usr/bin/python3
- Python version: 3.10.12
- pynvim version: 0.5.0
- OK Latest pynvim is installed.
WizardStark commented 5 months ago

I might add that this does not really affect me at all, 99% of snippets from this plugin work perfectly, the only one from recent memory that does not work is lorem(([1-4])?[0-9])? which obviously is not the most useful snippet

mireq commented 5 months ago

@WizardStark i will try to find problem. I tried neovide and i can see now that error. Standard neovim works without issues. I must investigate where is problem, but it's much deeper, somewhere in LuaSnip maybe.

WizardStark commented 5 months ago

Very interesting, regular nvim gives the same error for me - on both WSL2 in windows and on MacOS.

mireq commented 5 months ago

@WizardStark It's nothing with neovide, my fault. I tried older version of luasnip-snippets.

Can you add here before if env print statement - print(env) and then run :messages in vim commandline?

WizardStark commented 5 months ago

@mireq here is the output:

{}
{'SELECT_DEDENT': [], 'TM_SELECTED_TEXT': [], 'TM_LINE_INDEX': '135', 'TM_LINE_NUMBER': '136', 'LS_TRIGGER': 'lorem(([1-4])?[0-9])?', 'TM_CURRENT_WORD': '9])?', 'SELECT_RAW': [], 'LS_SELECT_RAW': [], 'LS_SELECT_DEDENT': [], 'TM_CURRENT_LINE': 'lorem(([1-4])?[0-9])?'}
E5108: Error executing lua function provider#python3#Call, line 18: Vim(return):Error invoking 'python_eval' on channel 4 (python3-script-host):
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/user/.local/share/nvim/lazy/luasnip-snippets/pythonx/luasnip_snippets_python_helper.py", line 359, in execute_code
    exec(compiled_code, node_locals)
  File "<exec-interpolation-code>", line 1, in <module>
NameError: name 'match' is not defined
stack traceback:
    [C]: in function 'py3eval'
    ...snip-snippets/lua/luasnip_snippets/common/snip_utils.lua:296: in function 'fn'
    ...are/nvim/lazy/LuaSnip/lua/luasnip/nodes/functionNode.lua:52: in function 'update'
    ...al/share/nvim/lazy/LuaSnip/lua/luasnip/nodes/snippet.lua:1026: in function 'update'
    ...al/share/nvim/lazy/LuaSnip/lua/luasnip/nodes/snippet.lua:769: in function 'trigger_expand'
    ...user/.local/share/nvim/lazy/LuaSnip/lua/luasnip/init.lua:272: in function 'snip_expand'
    ...cal/share/nvim/lazy/cmp_luasnip/lua/cmp_luasnip/init.lua:169: in function 'execute'
    .../user/.local/share/nvim/lazy/nvim-cmp/lua/cmp/source.lua:396: in function 'execute'
    ...e/user/.local/share/nvim/lazy/nvim-cmp/lua/cmp/entry.lua:482: in function 'execute'
    /home/user/.local/share/nvim/lazy/nvim-cmp/lua/cmp/core.lua:503: in function </home/user/.local/share/nvim/lazy/nvim-cmp/lua/cmp/core.lua:502>
    ...ocal/share/nvim/lazy/nvim-cmp/lua/cmp/utils/feedkeys.lua:47: in function <...ocal/share/nvim/lazy/nvim-cmp/lua/cmp/utils/feedkeys.lua:45>
mireq commented 5 months ago

@WizardStark thanks. I have now all relevant informations. Problem is somewhere in my hack used to pass regex match to python function. It will be much harder to fix it, so i will probably not have time to fix it this week.

mireq commented 5 months ago

@WizardStark this error should be fixed.

It was caused by direct expansion of snippet with regex trigger from cmp. This led to missing context. Regex expressions have still not so good support in cmp, but it don't trigger error at least.

If you want trigger, it's probably better to directly write something like lorem 10.

WizardStark commented 5 months ago

Fixed indeed, thanks!