JoosepAlviste / nvim-ts-context-commentstring

Neovim treesitter plugin for setting the commentstring based on the cursor location in a file.
MIT License
1.18k stars 35 forks source link

Need help in debugging strange situation #103

Closed msva closed 1 month ago

msva commented 8 months ago

Hi there! Unfortunatelly, I'm unable to prepare minimal-reproducible config for the issue, I'm experience, so I need help in investigation.

If I set enable_autocmd in setup{} to true, I'm getting strange error when I open some file types (like "dockerfile" or "sh")

The error is:

Error detected while processing CursorHold Autocommands for "<buffer=1>":
Error executing lua callback: ...-commentstring/lua/ts_context_commentstring/internal.lua:126: attempt to index local 'match' (a function value)
stack traceback:
  ...-commentstring/lua/ts_context_commentstring/internal.lua:126: in function 'calculate_commentstring'
  ...-commentstring/lua/ts_context_commentstring/internal.lua:86: in function 'update_commentstring'
  ...-commentstring/lua/ts_context_commentstring/internal.lua:33: in function <...-commentstring/lua/ts_context_commentstring/internal.lua:32>

It seems like setting enable_autocmd is not enough by itself, and maybe something in my config is related as well, because it does not reproduce with minimal config from this repo.

But still, it reproduces every time with my config (if this option is enabled). And I'm not sure, what plugins/settings shuld I look for to add to the repro-config to make error reproduce there.

Can you give some advices? Or, maybe, another ideas of the reasons why match can become a function instead of a table there?

1dr0z commented 4 months ago

I ran into this same issue and did some debugging. The issue is here: https://github.com/JoosepAlviste/nvim-ts-context-commentstring/blob/1277b4a1f451b0f18c0790e1a7f12e1e5fdebfee/lua/ts_context_commentstring/internal.lua#L118-L123

The issue happens when language_config is a string and node_type is the name of a string method e.g., char. Then doing language_config[node_type] returns the string function like so:

("")["char"] -- returns the string.char() function

The reason this only breaks sometimes is because if the node_type doesn't match the name of a string method then lua returns nil as expected:

("")["notastringmethod"] -- returns nil
JoosepAlviste commented 1 month ago

Hey! Thanks for the debugging @1dr0z, that was exactly what was happening. It's especially noticeable if you use the autocmd because then we're constantly re-calculating the commentstring.

I've pushed a commit fixing this: https://github.com/JoosepAlviste/nvim-ts-context-commentstring/commit/9c74db656c3d0b1c4392fc89a016b1910539e7c0 and will close this issue.

Let me know if this wasn't fixed or comes up again!