HiPhish / nvim-ts-rainbow2

Rainbow delimiters for Neovim through Tree-sitter
https://gitlab.com/HiPhish/nvim-ts-rainbow2
Apache License 2.0
339 stars 35 forks source link

Top level brackets are not highlighted when toggling rainbow #24

Closed mystilleef closed 1 year ago

mystilleef commented 1 year ago

Describe the bug

I disable rainbow on InsertEnter for Zen Mode. When I re-enable it on InsertLeave, top-level brackets are not highlighted, especially on function lines.

Steps to reproduce

1). Open a Lua document with top-level functions. 2). Disable rainbow with TSBufDisable rainbow. 3). Enable rainbow with TSBufEnable rainbow. 4). Brackets on the function line are not highlighted.

See the video below for an illustration.

Expected behavior

Rainbow should correctly highlight all brackets during toggles.

Video

https://user-images.githubusercontent.com/273399/228194565-8dda6cfe-ea6d-4945-812e-f04bacdb3883.mp4

HiPhish commented 1 year ago

It is actually working as intended, the question is whether the intended behaviour makes sense. Please allow me to explain; consider the following snippet of Lua code:

local function derp()
    local x = (((4)))
    print(x)
end

Now let's look at the syntax tree:

local_declaration: function_declaration [1, 0] - [4, 3]
  name: identifier [1, 15] - [1, 19]
  parameters: parameters [1, 19] - [1, 21]
  body: block [2, 1] - [3, 9]
    local_declaration: variable_declaration [2, 1] - [2, 18]
      assignment_statement [2, 7] - [2, 18]
        variable_list [2, 7] - [2, 8]
          name: identifier [2, 7] - [2, 8]
        expression_list [2, 11] - [2, 18]
          value: parenthesized_expression [2, 11] - [2, 18]
            parenthesized_expression [2, 12] - [2, 17]
              parenthesized_expression [2, 13] - [2, 16]
                number [2, 14] - [2, 15]
    function_call [3, 1] - [3, 9]
      name: identifier [3, 1] - [3, 6]
      arguments: arguments [3, 6] - [3, 9]
        identifier [3, 7] - [3, 8]

The parentheses after derp are part of the parameters node. However, the query does not define a pattern for that node type. These parentheses will never be highlighted by ts-rainbow.

But wait, they are clearly highlighted red, aren't they? Actually, they aren't. It just so happens that the default highlight colour for parentheses is the same as or at least similar to the first colour of the rainbow spectrum. If you were to change the spectrum the parentheses after derp would keep their colour.

So this raises two questions:

The first answer is that since parameters cannot contain nested parentheses I did not consider it worth adding them. Maybe I was wrong. What do you think?

The second question I don't have an answer for. I will need to investigate it.

mystilleef commented 1 year ago

My opinion is that rainbow handles coloring all pairs, parentheses, and brackets, regardless of where they exist.

I believe that's how the older rainbow package worked, but give me time to test it.

The older package allowed you to choose the order of how brackets were colored. The outermost or topmost brackets where always the same color, so maybe that's why I didn't notice any discrepancies.

By the way, I noticed this issue working on Lua files, I didn't see this issue with TypeScript files I was working on earlier.

HiPhish commented 1 year ago

You are right and my assumption was wrong. It is possible for the parentheses of a function definition to be nested:

local thunks = {
    function() return 1 end,
    function() return 2 end,
    function() return 3 end,
}

Here each of the pairs is at a nesting level of two, so it needs to have the second colour. I have updated the plugin, please check the current master branch.

I believe that's how the older rainbow package worked, but give me time to test it.

Correct, but that was mainly a side effect of how its queries would just match any anonymous node that was a parenthesis. I have changed fundamentally how queries work, which means that supporting languages is more work, but it gives more control over the result and allows for cool patterns like HTML tags or begin/end blocks. The downside is that sometimes a pattern like the argument list can slip through the cracks.

By the way, I noticed this issue working on Lua files, I didn't see this issue with TypeScript files I was working on earlier.

Correct, the Typescript query does actually describe a pattern for the function argument list. It's only the Lua query that was weird.

mystilleef commented 1 year ago

Thanks for the fix. Closing this issue.