rainbow icon indicating copy to clipboard operation
rainbow copied to clipboard

Ignore match in certain syntax groups (e.g. C defines)

Open dazzywi opened this issue 3 months ago • 3 comments

Hello! Firstly, thank you for this plugin - I've been using it extensively!

I have a problem for which I didn't find plausible solution after searching through this repository.

To be precise, I want to disable matching parentheses, curly brackets, etc within C define commands. The reason for this behavior is that rainbow's own highlights will break vim syntax highlights (this is especially apparent in multi-line defines) as seen in example image below

Current behavior
Image

I want to keep highlights for all pairs of parentheses, brackets, etc while not matching ones in define statements. Example of desired behavior can be seen on image below

Desired behavior
Image

Can this be achieved just through configuration, or alternatively, a patch I can apply for this plugin? Just as a note, I don't wish to completely disable matching for C filetype.

dazzywi avatar Aug 30 '25 16:08 dazzywi

Vim shows that C defines have highlight group cDefine which (I think) is a link to Define group

dazzywi avatar Aug 30 '25 16:08 dazzywi

I made a temporary solution with the following match patterns:

let g:rainbow_conf.separately['c'] = { 'parentheses': [
	\ 'start=/\%(.*\\\)\@!\%(#.*\)\@<!{/ end=/}/ fold',
	\ 'start=/\%(.*\\\)\@!\%(#.*\)\@<!(/ end=/)/ fold',
	\ 'start=/\%(.*\\\)\@!\%(#.*\)\@<!\[/ end=/\]/ fold',
	\ ]
\}

See :h @<! and :h @!


IMO, It's not even half good:

  • The set of operators in rainbow configuration will be highlighted inside define statement (note the && operators);
  • The last line of multi-line define is still going to be matched;
  • This affects how all parentheses, brackets, etc in file are interpreted, and is probably slower.
After applying new pattern
Image

I think this should really be done with some kind of Vim syntax exclusion (i.e., if {start|end} not in group), but I have no idea how to do it right now.

dazzywi avatar Sep 04 '25 17:09 dazzywi

The :syntax command shows the syntax rule about cDefine is:

cDefine        xxx start=/^\s*\zs\%(%:\|#\)\s*\%(define\|undef\)\>/ skip=/\\$/ end=/$
/  keepend contains=ALLBUT,@cPreProcGroup,@Spell
                   links to Macro

and the top level rainbow rules is

cRainbow_lv0_r0 xxx matchgroup=cRainbow_lv0_p0 start=/(/ end=/)/  fold contains=TOP,@
NoSpell containedin=@cRainbowRegions_lv3
                   matchgroup=cRainbow_lv0_p0 start=/(/ end=/)/  fold contains=TOP,@N
oSpell containedin=@cRainbowRegions_lv3

I'm not sure how to solve this yet, but that might help.

luochen1990 avatar Oct 29 '25 13:10 luochen1990