L3MON4D3 / LuaSnip

Snippet Engine for Neovim written in Lua.
Apache License 2.0
3.37k stars 241 forks source link

Cursor position outside buffer, in function 'nvim_win_set_cursor' #1182

Open mathjiajia opened 4 months ago

mathjiajia commented 4 months ago

I got the error msg as below when using a snippet with callbacks

   Error  20:36:58 msg_show.lua_error E5108: Error executing lua [string ":lua"]:1: Cursor position outside buffer
stack traceback:
    [C]: in function 'nvim_win_set_cursor'
    [string ":lua"]:1: in main chunk

By testing, it is caused by commit 0a4e55701720a111569cadc211b3642b96d7991d.

snippet is given as

s(
    { trig = "cf" },
    fmta([[\cite[<>]{<>}<>]], { i(1), i(2), i(0) }),
    {
        callbacks = {
            [2] = {
                [events.enter] = function()
                    require("telescope").extensions.bibtex.bibtex()
                end,
            },
        },
    }
),
L3MON4D3 commented 4 months ago

Hi :)

I tried to reproduce this, but couldn't :/ Could you try reducing your setup until the error disappears? One probably source (given the commit) is the new config-option exit_roots, could you try toggling that (or setting that to false if you don't have it currently set)

mathjiajia commented 4 months ago

type cf then <C-k> to expand, fill anything in the 1st insert node, <C-l> jump to the 2nd node, select one entry, then <C-l> to leave.

Here is a minimal config:

-- DO NOT change the paths and don't remove the colorscheme
local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "cache" }) do
    vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
    vim.fn.system({
        "git",
        "clone",
        "--filter=blob:none",
        "--single-branch",
        "https://github.com/folke/lazy.nvim.git",
        lazypath,
    })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
    "folke/tokyonight.nvim",

    -- add any other plugins here
    {
        "nvim-telescope/telescope.nvim",
        dependencies = { "nvim-telescope/telescope-bibtex.nvim" },
        config = function()
            local telescope = require("telescope")
            telescope.setup({ extensions = { bibtex = { format = "plain", context = true } } })
            telescope.load_extension("bibtex")
        end,
    },

    {
        "L3MON4D3/LuaSnip",
        config = function()
            local ls = require("luasnip")

            ls.setup({
                exit_roots = false,
                update_events = "TextChanged,TextChangedI",
                delete_check_events = "TextChanged",
            })

            local s = ls.snippet
            local i = ls.insert_node
            local fmta = require("luasnip.extras.fmt").fmta
            local events = require("luasnip.util.events")

            ls.add_snippets("tex", {
                s({ trig = "cf" }, fmta([[\cite[<>]{<>}<>]], { i(1), i(2), i(0) }), {
                    callbacks = {
                        [2] = {
                            [events.enter] = function()
                                require("telescope").extensions.bibtex.bibtex()
                            end,
                        },
                    },
                }),
            }, { key = "tex" })

            vim.keymap.set("i", "<C-k>", function()
                if ls.expandable() then
                    ls.expand()
                end
            end, { desc = "LuaSnip Expand" })

            vim.keymap.set({ "i", "s" }, "<C-l>", function()
                if ls.locally_jumpable(1) then
                    ls.jump(1)
                end
            end, { desc = "LuaSnip Forward Jump" })
        end,
    },

    { "nvim-lua/plenary.nvim" },
}

require("lazy").setup(plugins, {
    root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")
-- add anything else here

And you may test with the following files, saving as minimal.tex and minimal.bib:

\documentclass{article}
\usepackage{biblatex}
\addbibresource{minimal.bib}

\begin{document}

\end{document}
@article{birkar2010existencea,
  title         = {Existence of Minimal Models for Varieties of Log General Type},
  author        = {Birkar, Caucher and Cascini, Paolo and Hacon, Christopher D. and McKernan, James},
  date          = {2010-04},
  journaltitle  = {Journal of the American Mathematical Society},
  shortjournal  = {J. Amer. Math. Soc.},
  volume        = {23},
  number        = {2},
  pages         = {405--468}
}
mathjiajia commented 4 months ago

@L3MON4D3 could you reproduce this error?

L3MON4D3 commented 4 months ago

Hey, I couldn't look into this until now, sorry to keep you waiting. I think the main issue is that we completely assume that the buffer/window does not change once a call (like jump) begins, and since telescope opens a new window+buffer, this does not hold.

I think we should fix this, and it would actually be desirable, since a fix to this would include some other things I've wanted to tackle for some time, but I don't think I'll have time to do it soon :( (how: create table that contains parameters to some action (bufnr,winnr, no_move, dry_run) at action-toplevel, pass through to all called functions)

Until then, you can simply vim.schedule(require("telescope").extensions.bibtex.bibtex) to open telescope once luasnip is done with its stuff :)

PS: Thank you for the minimal config, that was a pleasure to work with :D

mathjiajia commented 4 months ago

thanks for the prompt response and the workaround.

hiberabyss commented 3 months ago

will occasionally get this error when used with nvim-cmp. once error occured, nvim-cmp stop work.