altermo / ultimate-autopair.nvim

A treesitter supported autopairing plugin with extensions, and much more
MIT License
501 stars 4 forks source link

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

Open chrisgrieser opened 5 months ago

chrisgrieser commented 5 months ago

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 commented 5 months ago

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 commented 5 months ago

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 commented 5 months ago

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

chrisgrieser commented 5 months ago

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