LuaSnip icon indicating copy to clipboard operation
LuaSnip copied to clipboard

Cursor position outside buffer, in function 'nvim_win_set_cursor'

Open mathjiajia opened this issue 1 year ago • 6 comments

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,
			},
		},
	}
),

mathjiajia avatar May 24 '24 12:05 mathjiajia

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)

L3MON4D3 avatar May 24 '24 15:05 L3MON4D3

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 avatar May 25 '24 07:05 mathjiajia

@L3MON4D3 could you reproduce this error?

mathjiajia avatar May 27 '24 02:05 mathjiajia

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

L3MON4D3 avatar May 27 '24 20:05 L3MON4D3

thanks for the prompt response and the workaround.

mathjiajia avatar May 28 '24 02:05 mathjiajia

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

hiberabyss avatar Jul 04 '24 03:07 hiberabyss

I was able to track the issue in my case. It is caused by a simple end of the file and improper definition of the snippet. It is definitely what it says: it's outside of the current buffer :P In order to fix my issue LuaSnip would need to detect if the position is outside of the current buffer. If so, it should go to the last symbol instead of accessing the nonexistent char in the buf.

How to reproduce

  1. Create a snippet that jumps to the newline after completion (for example for1 defined within jinja_lsp)
  2. Go to the end of the file (G)
  3. Try to use a snippet

Dzordzu avatar Jan 16 '25 18:01 Dzordzu