-
-
Notifications
You must be signed in to change notification settings - Fork 142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add dynamic query for coloring n-depth headlines #780
Add dynamic query for coloring n-depth headlines #780
Conversation
Thanks for the PR! |
The reason is mainly threefold:
|
After which numbers colors start to repeat? I'm ok solving this, but not in this way. |
is it possible in the predicate language of treesitter to define a query that applies a modulo operation on the length of the query? I saw that the predicate could be a regex pattern, but I could not figure out how to define a dynamic query |
Yes, something like this should work (didn' tested it though): "highlights.scm
(headline (stars) @_stars (#org-is-headline-level? @_stars "1")) @org.headline.level1
(headline (stars) @_stars (#org-is-headline-level? @_stars "2")) @org.headline.level2
"etc vim.treesitter.query.add_predicate('org-is-headline-level?', function(match, _, source, predicate)
---@type TSNode
local node = match[predicate[2]]
local level = tonumber(match[predicate[3]])
if not node then
return false
end
local _, _, _, node_end_col = node:range()
return node_end_col % level == 0
end, { force = true }) |
wow, that is super powerful! I didn't realize that you could define your own predicate like that! I thought we were limited to the ones listed in the treesitter doc pages. This totally changes the way I'm going to consider using treesitter in the future. |
Yes, there are several custom ones here https://github.com/nvim-orgmode/orgmode/blob/master/lua/orgmode/config/init.lua?plain=1#L342 You can adapt this PR to use the custom predicate similar to the one I posted in a previous comment. |
would you like me to apply that new predicate to this PR and test? or will you put it in your own commit, and I should close this one? |
1a929b0
to
c614615
Compare
You can go ahead and update this PR. |
c614615
to
7ba3717
Compare
accomplished using predicate
7ba3717
to
17249fa
Compare
No worries, I wrote it that way in my example without testing. |
My bad, I wasn't implying a failure on your part, just that I didn't fully understand how the arguments to the predicate handler worked, despite reading the documentation several times. It was basically trial and error for me, and I felt I needed to explain because when the github actions run each time I force push I assume that it is costing some amount of compute time for the project. I really appreciate that you wrote the solution to begin with, as it has launched my understanding of how to work with treesitter. |
accomplished using predicate Co-authored-by: qaptoR <rocco.ruscitti@outlook.com>
rather than define queries for set n=8 depth headlines, set autocommand that searches for stars and uses length of stars text to determine depth using modulo operation to wrap for number of color groups (hardcoded still at 8, but would be nice to define any number of groups easily in the futur