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.
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?
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.
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.
nice, thanks!
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.
nvim-ts-rainbow/lua/rainbow/internal.lua
Line 198 in 04284dc
changed it to check if it is attached
Is this fixed?
Yep
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.
👍🏽