Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mathjiajia opened this issue May 24, 2024 · 7 comments
Open

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

mathjiajia opened this issue May 24, 2024 · 7 comments

Comments

@mathjiajia
Copy link
Contributor

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
Copy link
Owner

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
Copy link
Contributor Author

mathjiajia commented May 25, 2024

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
Copy link
Contributor Author

@L3MON4D3 could you reproduce this error?

@L3MON4D3
Copy link
Owner

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
Copy link
Contributor Author

thanks for the prompt response and the workaround.

@hiberabyss
Copy link

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

@Dzordzu
Copy link

Dzordzu commented Jan 16, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants