p00f/nvim-ts-rainbow

Colour is messed up/lost after formatting

wutianjun5858 opened this issue · 5 comments

Describe the bug
c code when use clang-format, then rainbow become gray

Steps to reproduce
use clang-format, rainbow->gray

Expected behavior
rainbow do not change

Screenshots
before clang-format
image

after clang-format
image

d12bb commented

This actually isn't only C code, it gets confused when formatting other code and even messes up highlighting beyond parens too, like Fennel:
Before :!fnlfmt --fix %:
image

After format:
image

mrdgo commented

When I manually run :TSDisable rainbow | TSEnable rainbow, it is fixed again. I use this formatter, which exposes event User FormatterPost.

So naturally:

augroup FormatGroup
	autocmd!
	autocmd BufWritePost *.java,*.ts,*.lua,*.py FormatWrite
	autocmd User FormatterPost TSDisable rainbow | TSEnable rainbow
augroup end

should solve the problem. However, this doesn't work on java. Therefore I added my current setup to further investigate. But no luck so far. Maybe someone here is smarter than I am?

Edit:

this solves it for my setup:

augroup FormatGroup
	autocmd!
	autocmd BufWritePost *.java,*.ts,*.lua,*.py FormatWrite
	autocmd FocusGained * TSDisable rainbow | TSEnable rainbow
augroup end

Edit 2:
only partially. It still happens, then I quickly change Workspace of my window manager somewhere else and back again. I repeat that until formatting and rainbow braces are displayed correctly. Still not really elegant.

Editing in general seems to confuse rainbow, not just formatting. It's particularly easy to trigger mismatches in haskell:

haskell

We can also sometimes get typescript to screw up:

ts

Interestingly, the behavior alters between TSDisable rainbow | TSEnable rainbow cycles. Matching errors may appear in different places for the same action, and annecdotally, cycling off/on several times in a given file actually seems to clear the issue up. This observation leads to a hack like the following:

autocmd BufNewFile,BufReadPost * TSDisable rainbow | TSEnable rainbow | TSDisable rainbow | TSEnable rainbow | TSDisable rainbow | TSEnable rainbow

I also cycle after every write in case the above isn't enough:

autocmd BufWritePost * TSDisable rainbow | TSEnable rainbow

These both seem to be quick enough that I don't notice, and while they do nothing to illuminate the underlying cause of the issue, they do seem to do a pretty good job of patching it over.

The same in Rust as well. I think it's very easy to trigger this bug. For example, I delete a left bracket and retype it and color will mass up.

Same for me in LaTeX