p00f/nvim-ts-rainbow

File type change not being handled

HiPhish opened this issue · 17 comments

Describe the bug

If the file type of a buffer changes the parser of the initial language will still be used instead of instantiating a new parser.

Steps to reproduce

  • Create a new buffer with a file type, e.g. nvim derp.html
  • Change the file type, e.g. set ft=lisp

Expected behavior

The parentheses highlighting rules should match the new file type

We can use an auto command to watch for file type changes. The question is what to do with the old parser. Do we keep it around? Do we replace it? I would advocate for the latter, file types change very rarely, so there is no point in keeping an old parser around.

Screenshot_20220417_115245

p00f commented

Hmm parser.get_parser uses lang, which might not correspond to filetype
https://github.com/HiPhish/nvim-ts-rainbow/blob/1f6c2455b86221bfead1b01e431a43832b63b76a/lua/rainbow/internal.lua#L165

So how do we change the parser using an autocmd?

p00f commented

nvm there's parsers.get_buf_lang(buf)

When the parser is initially created in M.attach it uses whatever the lang argument is. I was not able to find documentation on what lang exactly is, so I am going to assume that it is whatever the file type of the buffer maps to in 'nvim-treesitter.parsers.get_buf_lang. If get_parsers is called without lang argument the same happens.

p00f commented

distros haven't yet updated to 0.7, I need to rewrite the autocmd in vimscript 😭

distros haven't yet updated to 0.7, I need to rewrite the autocmd in vimscript sob

Nah, just wrap it inside if vim.fn.has('0.7') ~= 0, eventually all distros will update to 0.7 and it's such a rare edge case no one has noticed yet, that it's not worth torturing yourself with Vim script.

p00f commented

nice, thanks!

p00f commented

I'm merging that branch now

Are you sure you want to have this autocommand for every buffer, even if it is not attached? You should be able to use <buffer> as the pattern, or <buffer=N> where N is the number of the current buffer. You would then need to create the auto command inside the attach function and remove it inside the detach function. I would like to try it, but Void Linux has not yet updated their package, so I would have to compile Neovim from source first.

p00f commented

if state_table[bufnr] then

changed it to check if it is attached

p00f commented

Is this fixed?

p00f commented

Yep

p00f commented

as in, parsers.get_parser is returning the same object?

as in, parsers.get_parser is returning the same object?

Even worse, Neovim's own vim.treesitter.get_parser is returning the same object. It does not update the language of the parser object. parsers.get_parser is just a wrapper around the Neovim function.

I have created a new issue: neovim/neovim#18148. I'd say to leave this as it is for now, we have our code and when it's fixed in Neovim things should fall right in place.

p00f commented

👍🏽