HiPhish / nvim-ts-rainbow2

Rainbow delimiters for Neovim through Tree-sitter
https://gitlab.com/HiPhish/nvim-ts-rainbow2
Apache License 2.0
338 stars 35 forks source link

Highlight errors inside #if 0 #17

Closed hungnguyen1503 closed 1 year ago

hungnguyen1503 commented 1 year ago

Describe the bug

If brackets are inside 0 then the rainbow will highlight it.

Expected behavior

Could you please support me with a config rainbow plugin?

Screenshots

image

HiPhish commented 1 year ago

I don't understand the question. Are you saying that you want the braces to be highlighted when the cursor is on top of the zero, or do you not want the braces to be highlighted?

hungnguyen1503 commented 1 year ago

Yes, That's what I want.

HiPhish commented 1 year ago

Which of the two?

hungnguyen1503 commented 1 year ago

I don't want the braces to be highlighted in #if 0 because #if 0 is equivalent to comment code in C language

HiPhish commented 1 year ago

Your code won't work because it is invalid C. You need a name for the new type after the structure declaration:

typedef struct {
    struct_b b;
} my_struct_t;

But even with that out of the way, I don't know how to make it work. Here is a query which matches your requirements:

(preproc_if
  condition: (number_literal) @cond (#not-eq? @cond "0")
  (type_definition
    (struct_specifier
      (field_declaration_list
        "{" @opening
        "}" @closing) @container)))

The key is the predicate check (#not-eq? @cond "0") which makes sure that the number (which must be a literal) is not zero. However, you will still get highlighting because this other query still matches:

(field_declaration_list
   (("{" @opening)
    ("}" @closing))) @container

You could try to remove that query, but then highlighting of structure brackets won't work anywhere but an #if context. The only solution I could imagine is to write a custom predicate (see :h treesitter-predicates) which checks that the syntax highlight group of the @container capture to see if it is the comment group.

hungnguyen1503 commented 1 year ago

Hi, I checked the issues of my source code, and it seems that the error is from clangd, I tried config Clangd on Visual Studio Code and it gives me the same error. Your plugin works great. Thank you for your support, this ticket can be closed.