HiPhish / rainbow-delimiters.nvim

Rainbow delimiters for Neovim with Tree-sitter
https://gitlab.com/HiPhish/rainbow-delimiters.nvim
Apache License 2.0
491 stars 37 forks source link

[Bug]: Doesn't seem to work for JSX filetypes #14

Closed schardev closed 1 year ago

schardev commented 1 year ago

Neovim version

0.9.1

Language affected

all I guess

Query

No response

Strategy

No response

Description

- Install [kickstart.nvim](https://github.com/nvim-lua/kickstart.nvim)
- Add `rainbow-delimiters.nvim` to `nvim-treesitter`'s dependencies like:

 {
    -- Highlight, edit, and navigate code
    'nvim-treesitter/nvim-treesitter',
    dependencies = {
      'nvim-treesitter/nvim-treesitter-textobjects',
      'HiPhish/rainbow-delimiters.nvim'
    },
    build = ':TSUpdate',
  },

- See if it works ... which it doesn't in my case

The health checks also says:

`- ERROR The healthcheck report for "rainbow-delimiters" plugin is empty.`
HiPhish commented 1 year ago

You could be experiencing the same problem as #2. Does highlighting work if you :write and then :edit the file?

As for the health check, if you don't have any custom configuration (which you don't need to have) there is nothing to check, so the health report will be empty. Perhaps I should add an explicit message for that case.

schardev commented 1 year ago

2 seems like a different problem as it was present in nvim-ts-rainbow2 too (which worked totally fine here). I recently decided to switch to rainbow-delimiters as I was getting some query errors in nvim-ts-rainbow2 (maybe because of outdated API calls?).

Does the repro works on your machine? Or it's working fine on yours?

EDIT: oh and yes, I did try :write and :edit still doesn't work. I also invoked the plugin on startup (rather than defining it as a treesitter dependency) still nope.

HiPhish commented 1 year ago

Can you please update to the current master, then set the log level to TRACE in your settings, then post the contents of the log file here? See :h rb-delimiters-logging for information on how to adjust the log level.

vim.g.rainbow_delimiters = {
    log = {
        level = vim.log.levels.TRACE
    }
}
schardev commented 1 year ago

For tsx file:

2023-07-22T21:05+0530   ERROR       Error attaching strategy to buffer 1: /usr/share/nvim/runtime/lua/vim/treesitter/query.lua:259: query: invalid structure at position 1394 for language tsx
2023-07-22T21:05+0530   ERROR       Error attaching strategy to buffer 1: /usr/share/nvim/runtime/lua/vim/treesitter/query.lua:259: query: invalid structure at position 1394 for language tsx
2023-07-22T21:05+0530   ERROR       Error attaching strategy to buffer 1: /usr/share/nvim/runtime/lua/vim/treesitter/query.lua:259: query: invalid structure at position 1394 for language tsx

For jsx file:

2023-07-22T21:09+0530   ERROR       Error attaching strategy to buffer 1: /usr/share/nvim/runtime/lua/vim/treesitter/query.lua:259: query: invalid structure at position 1065 for language javascript
2023-07-22T21:09+0530   ERROR       Error attaching strategy to buffer 1: /usr/share/nvim/runtime/lua/vim/treesitter/query.lua:259: query: invalid structure at position 1065 for language javascript

Interestingly, it started working for ts, html (and maybe other) files, but not js files.

HiPhish commented 1 year ago

Oops, I had a typo in my logger logic. Can you please update again and post the news logs?

The error message indicates that something is wrong with the query. Please run :TSUpdate to updated all parsers, then try running the following code:

local q = vim.fn.join(vim.fn.readfile('path/to/plugins/rainbow-delimiters.nvim/queries/javascript/rainbow-delimiters-react.scm'), '\n')
local query = vim._ts_parse_query('javascript', s)
print(query)

The first line loads the text of the query from file. The second line call the function which fails in your error message. If everything went correctly the last line should print something like <userdata 1>.

schardev commented 1 year ago

Sorry for the delay, was busy with work.

When running those commands it fails at the vim._ts_parse_query('javascript', q) function with error:

E5108: Error executing lua [string ":lua"]:1: no such language: javascript
stack traceback:
        [C]: in function '_ts_parse_query'
        [string ":lua"]:1: in main chunk

Which is weird as I made sure that the parser for javascript, tsx and typescript are properly installed (and yes I even re-installed them just to be sure).

Running the commands with tsx as the language, gives me previously reported error:

E5108: Error executing lua [string ":lua"]:1: query: invalid structure at posi
tion 1065 for language tsx
stack traceback:
        [C]: in function '_ts_parse_query'
        [string ":lua"]:1: in main chunk

And here are the logs:

2023-07-24T19:34+0530   TRACE       Define highlight groups
2023-07-24T19:34+0530   TRACE       Attaching to buffer 1 with language tsx.
2023-07-24T19:34+0530   TRACE   strategy.global global strategy on_attach
2023-07-24T19:34+0530   TRACE   strategy.global Set up parser for buffer 1
2023-07-24T19:34+0530   TRACE   strategy.global Set up parser for tsx
2023-07-24T19:34+0530   TRACE       Cannot attach to buffer 2, no parser for nil
2023-07-24T19:34+0530   TRACE       Cannot attach to buffer 3, no parser for nil
2023-07-24T19:34+0530   TRACE       Detaching from buffer 1.
2023-07-24T19:34+0530   TRACE       Attaching to buffer 1 with language tsx.
2023-07-24T19:34+0530   TRACE   strategy.global global strategy on_attach
2023-07-24T19:34+0530   TRACE   strategy.global Set up parser for buffer 1
2023-07-24T19:34+0530   TRACE   strategy.global Set up parser for tsx
2023-07-24T19:34+0530   TRACE       Detaching from buffer 1.
2023-07-24T19:34+0530   TRACE       Attaching to buffer 1 with language tsx.
2023-07-24T19:34+0530   TRACE   strategy.global global strategy on_attach
2023-07-24T19:34+0530   TRACE   strategy.global Set up parser for buffer 1
2023-07-24T19:34+0530   TRACE   strategy.global Set up parser for tsx
2023-07-24T19:35+0530   TRACE       Detaching from buffer 3.
2023-07-24T19:35+0530   TRACE       Detaching from buffer 2.
2023-07-24T19:42+0530   TRACE       Detaching from buffer 1.
HiPhish commented 1 year ago

What do you get if you try a language that does work for you? (Remember that you have to adjust the path to the query as well, otherwise you will get an error.)

schardev commented 1 year ago

for css, running:

local q = vim.fn.join(vim.fn.readfile('/home/schardev/.local/share/nvim/lazy/rai
nbow-delimiters.nvim/queries/css/rainbow-delimiters.scm'), '\n')
local query = vim._ts_parse_query('css', q)
print(query) -- returns `<query>`

and the logs says:

2023-07-25T16:44+0530   TRACE       Detaching from buffer 1.
2023-07-25T16:44+0530   TRACE       Attaching to buffer 1 with language scss.
2023-07-25T16:44+0530   TRACE   strategy.global global strategy on_attach
2023-07-25T16:44+0530   TRACE   strategy.global Set up parser for buffer 1
2023-07-25T16:44+0530   TRACE   strategy.global Set up parser for scss
2023-07-25T16:44+0530   TRACE   lib Query rainbow-delimiters found for scss
2023-07-25T16:44+0530   TRACE   lib Query rainbow-delimiters found for scss

and the same for json too:

2023-07-25T16:51+0530   TRACE       Define highlight groups
2023-07-25T16:51+0530   TRACE       Detaching from buffer 1.
2023-07-25T16:51+0530   TRACE       Attaching to buffer 1 with language json.
2023-07-25T16:51+0530   TRACE   strategy.global global strategy on_attach
2023-07-25T16:51+0530   TRACE   strategy.global Set up parser for buffer 1
2023-07-25T16:51+0530   TRACE   strategy.global Set up parser for json
2023-07-25T16:51+0530   TRACE   lib Query rainbow-delimiters found for json
2023-07-25T16:51+0530   TRACE   lib Query rainbow-delimiters found for json
2023-07-25T16:52+0530   TRACE       Detaching from buffer 2.
2023-07-25T16:52+0530   TRACE       Detaching from buffer 1.
HiPhish commented 1 year ago

So CSS and JSON are working correctly. Then the problem must be either with my query or with your Neovim, because by calling vim._ts_parse_query directly we are bypassing any of my logic. Here is an idea: open up the offending query file, comment everything out and then re-enable one pattern at a time until you find the offending pattern.

schardev commented 1 year ago

So, I was able to get it to work now. Apparently, this query from tsx and javascript schema is causing the parser to fail (the @closing capture to be exact). Changing that query to something like this works:

(jsx_self_closing_element
  name: (identifier) @opening
  "/>" @closing) @container ; <-- here

It works but the highlighting is a little bit off (see <NavArrowLeft /> in the below screenshot). I don't mind it though but there is that. image

EDIT: Here are the logs (just for the sake of it):

2023-07-27T15:58+0530   TRACE       Define highlight groups
2023-07-27T15:58+0530   TRACE       Attaching to buffer 1 with language tsx.
2023-07-27T15:58+0530   TRACE   strategy.global global strategy on_attach
2023-07-27T15:58+0530   TRACE   strategy.global Set up parser for buffer 1
2023-07-27T15:58+0530   TRACE   strategy.global Set up parser for tsx
2023-07-27T15:58+0530   TRACE   lib Query rainbow-delimiters found for tsx
2023-07-27T15:58+0530   TRACE   lib Query rainbow-delimiters found for tsx
2023-07-27T15:58+0530   TRACE       Cannot attach to buffer 2, no parser for nil
2023-07-27T15:58+0530   TRACE       Cannot attach to buffer 3, no parser for nil
2023-07-27T15:58+0530   TRACE   strategy.global Changed tree in buffer 1 with languages tsx
2023-07-27T15:58+0530   TRACE   lib Query rainbow-delimiters found for tsx
2023-07-27T15:58+0530   TRACE       Detaching from buffer 1.
2023-07-27T15:58+0530   TRACE       Detaching from buffer 1.
2023-07-27T15:58+0530   TRACE       Attaching to buffer 1 with language tsx.
2023-07-27T15:58+0530   TRACE   strategy.global global strategy on_attach
2023-07-27T15:58+0530   TRACE   strategy.global Set up parser for buffer 1
2023-07-27T15:58+0530   TRACE   strategy.global Set up parser for tsx
2023-07-27T15:58+0530   TRACE   lib Query rainbow-delimiters found for tsx
2023-07-27T15:58+0530   TRACE   lib Query rainbow-delimiters found for tsx
2023-07-27T15:58+0530   TRACE       Detaching from buffer 1.
2023-07-27T15:58+0530   TRACE       Detaching from buffer 2.
2023-07-27T15:58+0530   TRACE       Detaching from buffer 3.
2023-07-27T15:58+0530   TRACE       Define highlight groups
2023-07-27T15:58+0530   TRACE       Cannot attach to buffer 2, no parser for nil
2023-07-27T15:58+0530   TRACE       Detaching from buffer 2.
2023-07-27T15:58+0530   TRACE       Cannot attach to buffer 4, no parser for nil
2023-07-27T15:58+0530   TRACE       Detaching from buffer 1.
2023-07-27T15:58+0530   TRACE       Detaching from buffer 4.
HiPhish commented 1 year ago

Please try the current master branch. It looks like I had an older version of the parser installed and at some point the tree structure changed. There is no way of supporting both patterns, so I'm going with the more recent one.

It works but the highlighting is a little bit off

Yes, that is unfortunate. Currently Neovim can only have one match per capture, so I can have only one opening and one closing node. The three nodes are <, NavArrowLeft and />. The closing one is obvious, but which one should be the opening node? I went with the tag name, it seemed the lest bad option to me.

Ideally both < and NavArrowLeft should be opening nodes, but I cannot do that yet.

schardev commented 1 year ago

Updating to master obviously fixed it. Just hoping that https://github.com/neovim/neovim/pull/17099 get's merged so we can have proper highlighting.

You can close the issue now. Thanks for the support :)