luochen1990 / rainbow

Rainbow Parentheses Improved, shorter code, no level limit, smooth and fast, powerful configuration.
Apache License 2.0
1.78k stars 95 forks source link

[Bug] Brake comments colorization #183

Closed Curs3W4ll closed 2 years ago

Curs3W4ll commented 2 years ago

Describe the bug

Removing comments grey colorizations when comments contain operators

To Reproduce

File: bug.cpp, filetype=cpp

int main() {
    int x = 1;
    // This is a comment

    int y = 2;
    /*
     * Multiline comment
     */
    return 0;
}

Steps:

  1. Open file via vim bug.js
  2. See the colors of commented sections

Expected behavior

Comments should be grey as they are without the rainbow plugin

Screenshots

Expected image

With rainbow toggled OFF image

With rainbow toggled ON image

Additional context

I use that config

let g:rainbow_active = 1 " Load vim-rainbow for all the files

let g:rainbow_conf = {
\   'guifgs': ['magenta', 'coral2', 'gold', 'deepskyblue', 'slateblue'],
\   'ctermfgs': ['lightblue', 'lightyellow', 'lightcyan', 'lightmagenta'],
\   'guis': [''],
\   'cterms': [''],
\   'operators': '_,\|\.\|+\|-\|*\|\/\|%\|;\|=\|!\|<\|>_',
\   'parentheses': ['start=/(/ end=/)/ fold', 'start=/\[/ end=/\]/ fold', 'start=/{/ end=/}/ fold'],
\   'separately': {
\       '*': {},
\       'markdown': {
\           'parentheses_options': 'containedin=markdownCode contained',
\       },
\       'lisp': {
\           'guifgs': ['royalblue3', 'darkorange3', 'seagreen3', 'firebrick', 'darkorchid3'],
\       },
\       'haskell': {
\           'parentheses': ['start=/(/ end=/)/ fold', 'start=/\[/ end=/\]/ fold', 'start=/\v\{\ze[^-]/ end=/}/ fold'],
\       },
\       'vim': {
\           'parentheses_options': 'containedin=vimFuncBody',
\       },
\       'perl': {
\           'syn_name_prefix': 'perlBlockFoldRainbow',
\       },
\       'stylus': {
\           'parentheses': ['start=/{/ end=/}/ fold contains=@colorableGroup'],
\       },
\       'nerdtree': 0,
\       'css': 0,
\   }
\}
luochen1990 commented 2 years ago

Try to find out (you can use the tool in README.md) the syntax name of comment (when rainbow is disabled), and add something like following to your configuration ( replace xxxComment to the real one ) :

let g:rainbow_conf = {
\   'separately': {
\       'cpp': {
\           'parentheses_options': 'contains=@xxxComment',
\       }
\   }
\}
Curs3W4ll commented 2 years ago

with :hi, I found out that comments are identified with the syntax name Comment

image

So I updated my config to this:

let g:rainbow_conf = {
\   'guifgs': ['magenta', 'coral2', 'gold', 'deepskyblue', 'slateblue'],
\   'ctermfgs': ['lightblue', 'lightyellow', 'lightcyan', 'lightmagenta'],
\   'guis': [''],
\   'cterms': [''],
\   'operators': '_,\|\.\|+\|-\|*\|\/\|%\|;\|=\|!\|<\|>_',
\   'parentheses': ['start=/(/ end=/)/ fold', 'start=/\[/ end=/\]/ fold', 'start=/{/ end=/}/ fold'],
\   'separately': {
\       '*': {},
\       'cpp': {
\           'parentheses_options': 'contains=@Comment',
\       },
\       'markdown': {
\           'parentheses_options': 'containedin=markdownCode contained',
\       },
\       'lisp': {
\           'guifgs': ['royalblue3', 'darkorange3', 'seagreen3', 'firebrick', 'darkorchid3'],
\       },
\       'haskell': {
\           'parentheses': ['start=/(/ end=/)/ fold', 'start=/\[/ end=/\]/ fold', 'start=/\v\{\ze[^-]/ end=/}/ fold'],
\       },
\       'vim': {
\           'parentheses_options': 'containedin=vimFuncBody',
\       },
\       'perl': {
\           'syn_name_prefix': 'perlBlockFoldRainbow',
\       },
\       'stylus': {
\           'parentheses': ['start=/{/ end=/}/ fold contains=@colorableGroup'],
\       },
\       'nerdtree': 0,
\       'css': 0,
\   }
\}

But it has no effect

luochen1990 commented 2 years ago

I cannot produce your issue, even without any extra rainbow configuration.

image

Have you enabled any 3rd-party syntax plugins for cpp?

Curs3W4ll commented 2 years ago

I tried on a newly installed machine (with docker), using neovim v0.5.1, but the comment colorization keep being broke in .cpp files Only installed plugins are vimplug and the rainbow plugin.

The only configuration is the init.vim file containing the following:

call plug#begin()

Plug 'luochen1990/rainbow'

call plug#end()

hi Comment cterm=NONE ctermfg=red ctermbg=yellow

let g:rainbow_active = 1 " Load vim-rainbow for all the files

let g:rainbow_conf = {
\       'guifgs': ['magenta', 'coral2', 'gold', 'deepskyblue', 'slateblue'],
\       'ctermfgs': ['lightblue', 'lightyellow', 'lightcyan', 'lightmagenta'],
\       'guis': [''],
\       'cterms': [''],
\       'operators': '_,\|\.\|+\|-\|*\|\/\|%\|;\|=\|!\|<\|>_',
\       'parentheses': ['start=/(/ end=/)/ fold', 'start=/\[/ end=/\]/ fold', 'start=/{/ end=/}/ fold'],
\       'separately': {
\               '*': {},
\       'cpp': {
\           'parentheses_options': 'contains=@Comment',
\       },
\               'markdown': {
\                       'parentheses_options': 'containedin=markdownCode contained',
\               },
\               'lisp': {
\                       'guifgs': ['royalblue3', 'darkorange3', 'seagreen3', 'firebrick', 'darkorchid3'],
\               },
\               'haskell': {
\                       'parentheses': ['start=/(/ end=/)/ fold', 'start=/\[/ end=/\]/ fold', 'start=/\v\{\ze[^-]/ end=/}/ fold'],
\               },
\               'vim': {
\                       'parentheses_options': 'containedin=vimFuncBody',
\               },
\               'perl': {
\                       'syn_name_prefix': 'perlBlockFoldRainbow',
\               },
\               'stylus': {
\                       'parentheses': ['start=/{/ end=/}/ fold contains=@colorableGroup'],
\               },
\       'nerdtree': 0,
\               'css': 0,
\       }
\}

I changed comments highlight for better identification

Curs3W4ll commented 2 years ago

Try to play a bit with the operators, I found out that the only operator causing problems is the / one If I switch my operators configuration to

\       'operators': '_,\|\.\|+\|-\|*\|%\|;\|=\|!\|<\|>_',

Comments colorization is no more broken

Curs3W4ll commented 2 years ago

Ok I don't know if this can help you, but I identify in which case the comment colorization get broke

An idea poped in my head when I found out that the problematic operator in cpp was /, so I thought that this could be the 'comment identifier' (the character identifying a comment, // in c/cpp, # in bash/python, " in vim....) that was causing problems when it was in the operators

So I tested it, I added the # character in the operators list

And that's it ! Without the # as an operator, python comment colorization is ok image

But when I add the # character as an operator in the rainbow config, it then broke the comment colorization image

So the condition to broke comment colorization are

Curs3W4ll commented 2 years ago

Have you any idea of how can I fix it ?

luochen1990 commented 2 years ago

I suggest just remove / from operators, the operators feature is not designed for mathmatic operators, but for seperators of items in parentheses, (maybe it should be called seperators)

Curs3W4ll commented 2 years ago

Alright thanks a lot !