HiPhish / rainbow-delimiters.nvim

Rainbow delimiters for Neovim with Tree-sitter
https://gitlab.com/HiPhish/rainbow-delimiters.nvim
Apache License 2.0
532 stars 39 forks source link

Add missing rust query #122

Open Danielkonge opened 3 months ago

Danielkonge commented 3 months ago

Note that the current neovim treesitter implementation only captures anything inside a rust macro via token_tree, so we need this new query to capture <T>.

I added some examples in the test file, but the only thing the new query fixes is the line with:

vec![Generic::<u8>::new(1)]

I only just now figured out that we don't actually capture the stuff inside macros via the standard queries, we only capture things inside e.g. a vec! via token_tree.

I thought that knowing this might help fixing the highlighting in C/C++ too (see #80), but it seems that the "self-injections" don't work the same there, so I couldn't see any easy fix to the current problems there.

HiPhish commented 2 months ago

I don't know Rust, so I need some help here. Looking at the syntax tree of the test file I don't see any token_tree followed by < or > in Neovim 0.10. The <T> is represented as

(type_parameters
  "<"
  ">")

On a side note, I get ten errors in the syntax tree when I use :InspectTree, but none with :TSPlayground. Do you get the same? Do you have an idea of what could be going on? Maybe it's a bug in Neovim's :InspectTree implementation.

Danielkonge commented 2 months ago

The <, > here are usually captured by

(type_arguments
  "<"
  ">")

but that doesn't seem to work inside macros in Rust. In general, I can't get correct highlighting inside macros without token_tree. To get correct highlighting in Rust, the only solution I have found so far is using token_tree, which I think is what "marks" the macro. I am not sure why captures work in this different way in Rust (or if I am doing something wrong), but this solution was the easiest workaround I found.

I also get the ERRORs, so maybe that is what causes problems here? I haven't really looked into this in more detail since this commit fixed my issue.

Danielkonge commented 2 months ago

I looked into this a bit more and I think I remember why we added the other token_tree containers originally. The problem is the way rust handles macros, where it injects itself, and if we capture them as usual, then we get wrong highlighting in a couple of ways:

This is also the reason we have the parent_lang ~= lang check in the global strategy, when deciding on the update ranges. (This check should probably have included or lang ~= rust).

HiPhish commented 2 months ago

I have opened a new issue for the discrepancy in error reports: https://github.com/neovim/neovim/issues/29726

I suggest we put this issue on hold for now until we know wheter there is a bug in Neovim or the parser, or if things really are that complicated. If there is a but we get the fix for free, and if the problem is on our end we can still solve it the hard way.