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

Bug: in_string() errors in ftplugin/lua.lua #88

Open
chrisgrieser opened this issue Jun 10, 2024 · 4 comments
Open

Bug: in_string() errors in ftplugin/lua.lua #88

chrisgrieser opened this issue Jun 10, 2024 · 4 comments

Comments

@chrisgrieser
Copy link

Thanks for the nice plugin!


So, I often get errors when using a in_string() condition. In my case, I wanted to create a rule that pairs <> in lua when inside a string (useful for writing keymaps such as "<CR>").

Following the example in the docs, I added this to my config:

{ "<", ">", ft = { "lua" }, cond = function(fn) return not fn.in_string() end }

Which works, but errors quite often. For instance, when typing " here:

-- * = cursor
vim.api.nvim_create_user_command(*, command, opts)

I get this error:

...y/ultimate-autopair.nvim/lua/ultimate-autopair/utils.lua:161: Index out of bounds
stack traceback:
	...y/ultimate-autopair.nvim/lua/ultimate-autopair/debug.lua:77: in function <...y/ultimate-autopair.nvim/lua/ultimate-autopair/debug.lua:74>
	[C]: in function '_getlines'
	...y/ultimate-autopair.nvim/lua/ultimate-autopair/utils.lua:161: in function 'gettsnode'
	...autopair.nvim/lua/ultimate-autopair/extension/tsnode.lua:36: in function '_in_tsnode'
	...autopair.nvim/lua/ultimate-autopair/extension/tsnode.lua:101: in function 'filter'
	...autopair.nvim/lua/ultimate-autopair/extension/tsnode.lua:124: in function 'sfilter'
	...ua/ultimate-autopair/profile/default/utils/open_pair.lua:85: in function 'count_end_pair'
	...ua/ultimate-autopair/profile/default/utils/open_pair.lua:152: in function 'open_end_pair_after'
	...air.nvim/lua/ultimate-autopair/profile/default/pairs.lua:12: in function 'can_check'
	...air.nvim/lua/ultimate-autopair/profile/default/pairs.lua:26: in function <...air.nvim/lua/ultimate-autopair/profile/default/pairs.lua:25>
	[C]: in function 'xpcall'
	...y/ultimate-autopair.nvim/lua/ultimate-autopair/debug.lua:97: in function 'run'
	...zy/ultimate-autopair.nvim/lua/ultimate-autopair/core.lua:96: in function <...zy/ultimate-autopair.nvim/lua/ultimate-autopair/core.lua:87>

I also check the in_string function in the code itself, and taking a guess from the function's signature, I also tried this;

{
	"<",
	">",
	ft = { "lua" },
	cond = function(fn)
		local row, col = unpack(vim.api.nvim_win_get_cursor(0))
		return fn.in_string(fn.opt, row, col)
	end,
},

which does not appear to well either.

@altermo
Copy link
Owner

altermo commented Jun 10, 2024

I can't reproduce a glitch, so I'm shooting in the dark.
I created a commit on the development branch which might fix it so install the development branch and report back to me if it fixed it. And if not, I would like a minimal reproducible config.

@chrisgrieser
Copy link
Author

Okay, I did some testing, and it appears the issue only occurs in the lua ftplugin (nvim/after/ftplugin/lua.lua). While normally, ftplugins only get loaded when entering the buffer, apparently, the autopairs somehow trigger ftplugin to be reloaded in the "incomplete" state (as it is currently edited)?

So this appears to be really an edge case and, nonetheless, it took me a bit to figure out that it is only this specific file.

@altermo
Copy link
Owner

altermo commented Jun 10, 2024

I use vim.filetype.get_option() to get filetype-specific options for injected (and non-injected) languages. This function loads ftplugin files.

@chrisgrieser
Copy link
Author

I see, the error makes sense then.

So for now, it looks like this works as workaround

{
	"<",
	">",
	ft = { "lua" },
	cond = function(fn)
		return fn.in_string() and not vim.endswith(vim.api.nvim_buf_get_name(0), "/ftplugin/lua.lua")
	end,
},

@chrisgrieser chrisgrieser changed the title Bug: in_string() often errors Bug: in_string() errors in ftplugin/lua.lua Jun 12, 2024
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

2 participants