Skip to content
This repository has been archived by the owner on Jan 2, 2023. It is now read-only.

Highlight only current sub-tree? #110

Open
HiPhish opened this issue Apr 18, 2022 · 4 comments
Open

Highlight only current sub-tree? #110

HiPhish opened this issue Apr 18, 2022 · 4 comments

Comments

@HiPhish
Copy link
Contributor

HiPhish commented Apr 18, 2022

As a user I sometimes find it too noisy when the entire buffer is highlighted. I would like if it was possible to only highlight the current sub-tree of the buffer, i.e. the delimiters of the current node, the delimited of all its descendants and the delimiter of all its ancestors. Take the following s-expression as an example

(/ (+ (- b)
      (sqrt (- (* b b)
               (* 4 a c))))
   (* 2 a))

Now suppose the cursor is on the r of sqrt (or anywhere else inside that node). The highlighting should look as follows, where the square brackets denote highlighted parentheses:

[/ [+ (- b)
      [sqrt [- [* b b)
               [* 4 a c]]]]
   (* 2 a)]

All s-expressions inside sqrt are highlighted, but the sibling (- b) and uncle (* 2 a) are not. Since the highlighting can change whenever the cursor moves this would require re-computing the highlight groups on every cursor movement. I have hacked something together, but it only sort of works. My understanding of Tree-sitter is still too limited.

Screenshot_20220418_160935

The idea was to get the range of the parent of the query result and check whether the cursor is within that range. Obviously this won't work for descendants of the current node. I would need somehow to get the cursor node and then check whether the query node is inside it.

-- Inside the `for _, node, _ in ...` loop:
local y, x, Y, X = node:parent():range()
if y <= r and r <= Y and x <= c and c <= X then
    -- Do the highlighting
end

My hack does not work in HTML, but that might be because I am using the extended mode.

What's your opinion? Should we have such a feature? Would it be too costly? Advice on how to implement it?

@p00f
Copy link
Owner

p00f commented Apr 18, 2022

The feature would be nice to have, I can't take a look rn - probably next week. It doesn't appear(???) costly

@HiPhish
Copy link
Contributor Author

HiPhish commented Apr 19, 2022

I managed to hack something together.

vokoscreen-2022-04-20_00-11-35.mp4

The code is quite a mess, but it works. I will have to test it with other languages and clean it up properly. It's a first step.

@damnever
Copy link

This feature is useful, any progress on this?

@HiPhish
Copy link
Contributor Author

HiPhish commented Aug 10, 2022

Right, I have been shoving this one under the rug. Here is work-in-progress pull request: #131

I don't know enough about Tree-sitter do it properly. Is there more in terms of resources other than the Tree-sitter website and the Neovim documentation? Something that walks me through the baby steps first and explains what all the terms mean.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants