rainbow-delimiters.nvim icon indicating copy to clipboard operation
rainbow-delimiters.nvim copied to clipboard

Cannot highlight parentheses in real-time

Open lzlcs opened this issue 1 year ago • 14 comments

Neovim version

0.9.1

Language affected

scheme

Query

No response

Strategy

No response

Description

The same as [this](https://github.com/HiPhish/nvim-ts-rainbow2/issues/52)

lzlcs avatar Jul 04 '23 02:07 lzlcs

Seems related to another bug I reported on: https://github.com/lukas-reineke/indent-blankline.nvim/issues/555

Blankline gets scope information from treesitter, but it's like it's working off of stale cached info and the scope returned is messed up once an edit is made. ":edit" also fixes things there as well. However Blankline is also fixed with ":update", but that doesn't seem to have effect for rainbow-delimiters.

It seems like something to do with cache invalidation or re-scanning to sync the treesitter state is not working as expected for external plugins using treesitter that were not an issue with the depreciated treesitter modules.

ReubenM avatar Jul 11 '23 20:07 ReubenM

The same thing is happening for me. In both Racket and Clojure when using nvim-surround, parenthesis don't update color until another delimiter type is modified, such as another parenthesis. It is not limited to the Lisp language family, and happens with all other delimiters (tags included).

def-SpaceWar avatar Aug 08 '23 03:08 def-SpaceWar

It this still a problem on the current master?

HiPhish avatar Aug 27 '23 22:08 HiPhish

This problem is still present, but only with global strategy.

alexmozaidze avatar Jan 25 '24 10:01 alexmozaidze

This problem is still present, but only with global strategy.

Do you have Tree-sitter highlighting enabled? Call vim.treesitter.start() in your buffer and tell me whether real-time highlighting works.

For the technical background, there is a callback registered which will re-apply rainbow highlighting whenever the tree structure changes. However, this callback is only called if general Tree-sitter highlighting is enabled for that buffer. And yes, it has to be enabled per buffer. I was not aware of this because by nvim-treesitter configuration does it implicitly.

HiPhish avatar Jan 28 '24 16:01 HiPhish

I think the issue has been resolved on the latest plugin version, since I didn't encounter it in quite a while. At least for me.

alexmozaidze avatar Feb 02 '24 15:02 alexmozaidze

This issue has not been resolved!!! I have the exact same issue: the highlighting does not update in real time.

It starts working as intended and updates in real time when I call vim.treesitter.start(). It also seems to update once if I run :edit

I am on rainbow-delimiters v0.4.0 on nvim v0.9.5

Hope this can be fixed!!

TheTrueWhiteOwl avatar May 14 '24 18:05 TheTrueWhiteOwl

Yeah, it hasn't been resolved. I noticed that it can be triggered by visual selecting a region with brackets and deleting and then undoing the deletion.

alexmozaidze avatar May 15 '24 23:05 alexmozaidze

For anyone looking for a temporary 'fix' for this problem, the following autocommand seems to work:

-- HACK: temporary fix to ensure rainbow delimiters are highlighted in real-time
vim.api.nvim_create_autocmd(
   "BufRead",
   {
      desc = "Ensure treesitter is initialized???",
      callback = function()
         -- if this fails then it means no parser is available for current buffer
         if pcall(vim.treesitter.start) then
            vim.treesitter.start()
         end
      end,
   }
)

TheTrueWhiteOwl avatar May 22 '24 05:05 TheTrueWhiteOwl

To everyone who is wondering why nothing has happened: I still cannot replicate the problem, so I cannot "fix" it either. That's why this issue is still lingering on.

@TheTrueWhiteOwl I don't understand this autocommand. You call vim.treesitter.start twice, but why?

HiPhish avatar May 29 '24 12:05 HiPhish

@HiPhish I am really embarrassed about the fact that I published the code like that publicly and I must apologize for the confusion...

You see, I wrote that temporary fix at something like 1:00 AM in the morning. The only important part of that autocmd was the vim.treesitter.start() since it ensures treesitter is working properly and the parentheses are highlighted in real-time. However, that raises an error when called inside a buffer where no parser for that language is available, so I placed it inside a pcall: hence the pcall(vim.treesiter.start).

At the time I also decided to try running other pieces of code inside buffers parsed with treesitter and since the pcall already served as a kind of way to check if I was inside such a buffer I just used it in an if statement. However later I removed those extra function calls and when I saw that the if statement was empty I just put the vim.treesitter.start() inside again...

I feel like an absolute fool especially due to the confusion I caused... Here is the autocmd without the duplicate call.

-- HACK: temporary fix to ensure rainbow delimiters are highlighted in real-time
vim.api.nvim_create_autocmd(
   "BufRead",
   {
      desc = "Ensure treesitter is initialized???",
      callback = function()
         -- if this fails then it means no parser is available for current buffer
         pcall(vim.treesitter.start)
      end,
   }
)

On another note, I think it is important to also consider the possibility that this issue does not stem from the rainbow-delimiters extension, but rather from nvim-treesitter itself, since the issue is fixed by calling vim.treesitter.start()?

TheTrueWhiteOwl avatar May 29 '24 20:05 TheTrueWhiteOwl

@TheTrueWhiteOwl Haha, it's OK, no need to aplogize this hard.

If the problem is indeed the lack of vim.treesitter.start(), then that give me an idea: I have nvim-treesitter/nvim-treesitter installed and set up. I think calling the setup function installs an autocommand similar to to the hack. Do any of the people who do not get live updated have it installed and set up as well?

HiPhish avatar May 29 '24 23:05 HiPhish

It's been fixed for me (IDK when I use kakoune now) but I just felt like contributing at some point an update fixed it for my neovim.

def-SpaceWar avatar Jun 04 '24 02:06 def-SpaceWar

I checked like exactly right now. ^

def-SpaceWar avatar Jun 04 '24 02:06 def-SpaceWar

I encountered a very strange problem, that is, if I disable highlight in the treesitter configuration, and then run vim.treesitter.start() in the open buffer, the colored brackets can be updated in real time. However, if I set highlight = {enable = true} in the treesitter configuration, the colored brackets will not be updated in real time even if I run vim.treesitter.start() in the open buffer. But aren't both essentially running the vim.treesitter.start() command? Why is there such a difference? @HiPhish

narutozxp avatar Nov 05 '24 16:11 narutozxp

Can you please try with a fresh Neovim configuration, i.e. only this plugin and no other plugins? See :h $NVIM_APPNAME and :h xdg for how to create a separate configuration. I use the same technique for testing the plugin isolated from my own configuration and highlighting works in my tests.

HiPhish avatar Nov 05 '24 22:11 HiPhish

When using $NVIM_APPNAME, my configuration will not be able to download lazy.nvim, so it is not easy to implement the above test. Maybe the behavior is due to this code

https://github.com/nvim-treesitter/nvim-treesitter/blob/7dc8aabe86db8c2f23520e8334f7584f83e84342/lua/nvim-treesitter/configs.lua#L110-L116

narutozxp avatar Nov 06 '24 07:11 narutozxp

You are not supposed to use Lazy, that would defeat the point of a fresh and minimal configuration. Here is how I do it when I want to create a minimal setup:

# Replace 'derp' and 'whatever' with whatever you want

# First create your fresh directories
mkdir -p derp/local/share/nvim/site/pack/whatever/start
mkdir -p derp/config/nvim/

# Now add this plugin to a location where it can be found
git clone https://github.com/HiPhish/rainbow-delimiters.nvim/ derp/local/share/nvim/site/pack/whatever/start/rainbow-delimiters.nvim

# Add your settings to derp/config/nvim/init.{vim,lua}

# Now instruct Neovim to use these directories
export XDG_CONFIG_HOME=derp/config
export XDG_DATA_HOME=derp/local/share

# Run Neovim with your fresh configuration

Setting up a fresh configuration is generally a good thing to do when you want to find out if a bug is really a bug in the plugin or if something else is interfering with it.

HiPhish avatar Nov 06 '24 18:11 HiPhish

Maybe the behavior is due to this code

https://github.com/nvim-treesitter/nvim-treesitter/blob/7dc8aabe86db8c2f23520e8334f7584f83e84342/lua/nvim-treesitter/configs.lua#L110-L116

Very unlikely. I don't use the module system of nvim-treesitter, it has been deprecated.

HiPhish avatar Nov 06 '24 18:11 HiPhish

Thank you for your guidance, the behavior shown by minimal configuration is indeed correct

narutozxp avatar Nov 07 '24 01:11 narutozxp

In my original environment, the best solution is to refer to the above solution, add an automatic command, and disable highlight in treesitter.

require 'nvim-treesitter.configs'.setup {
        -- ensure_installed = "maintained", -- for installing all maintained parsers
        ensure_installed = { "c", "cpp", "python", "verilog","bash","vim","regex","lua","vimdoc","markdown","markdown_inline","latex","html","xml"}, -- for installing specific parsers 如果cmp的补全信息不渲染markdown,就在这里加上markdown和makrdown_inline
        sync_install = false, -- install synchronously
        ignore_install = {}, -- parsers to not install
        highlight = {
            enable = false,
            additional_vim_regex_highlighting = false, -- disable standard vim highlighting
        },
        indent = {
            enable = true,
        },
}


vim.api.nvim_create_autocmd(
   "BufRead",
   {
      desc = "Enable highlight",
      callback = function()
         vim.cmd("TSBufEnable highlight")
      end,
   }
)

narutozxp avatar Nov 07 '24 03:11 narutozxp

I'm afraid I cannot help you further. I don't use package managers, so I don't know how to properly configure Lazy. Some from the Lazy community could probably help you.

HiPhish avatar Nov 07 '24 06:11 HiPhish

I was having the same issue and none of the fixes here were working. I ended up needing an additional delay for some reason:

vim.api.nvim_create_autocmd("BufEnter", {
    pattern = "*",
    callback = function()
        vim.defer_fn(function()
            vim.cmd("TSBufEnable highlight")
        end, 100) -- Delay by 100 milliseconds
    end,
})

AaronVerDow avatar Dec 29 '24 00:12 AaronVerDow