Skip to content
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 tree-sitter-comment #1170

Closed
wants to merge 1 commit into from
Closed

Conversation

pickfire
Copy link
Contributor

Fix #1164

Not working yet.

cc @the-mikedavis can you please help me out? Not sure why it highlights the first word differently. I am also not sure how to debug tree-sitter.

Screenshot_20211126_094524

Comment on lines +10 to +20
((tag ((name) @warning))
(#any-of? @warning "TODO" "HACK" "WARNING"))

("text" @warning
(#any-of? @warning "TODO" "HACK" "WARNING"))

((tag ((name) @error))
(#any-of? @error "FIXME" "XXX" "BUG"))

("text" @error
(#any-of? @error "FIXME" "XXX" "BUG"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any-of? isn't actually supported by us: #887 (comment)

Should be possible to implement though

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should be able to replace any #any-of? with an equivalent (but kinda ugly) #match? like here:

(
(identifier) @constant.builtin
(#match? @constant.builtin "^(__MODULE__|__DIR__|__ENV__|__CALLER__|__STACKTRACE__)$")
)

it might be nice to add #any-of? eventually, I think I've seen it in a bunch of different languages' queries

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think #any-of? is the issue. I tried removing everything in the comment highlight file but still it didn't get comment highlight as fallback color.

@the-mikedavis
Copy link
Member

testing out & debugging queries is not super easy with tree-sitter

the way I tend to do it is by starting up a tree-sitter playground in whatever grammar's repo and trying out the queries in the query box there. afaik there isn't a way to use combined injections in the playground though :/

@the-mikedavis
Copy link
Member

the-mikedavis commented Dec 18, 2021

Ok sorry for the delay, I had some time to investigate and it looks like there might be two problems at play. The first is a precedence rule thing that happens when migrating queries from neovim and then other I think might be a bug in injections but I'm not sure yet.

The precedence problem is that neovim will give preference to the last matching stanza in highlights.scm but helix (and the tree-sitter cli) give preference to the first. So the

(tag (name) @ui.text (user)? @constant)

on line 8 which is coloring tags as text and seems to be a kind of "default" rule for tags is matching all tags, and so the subsequent stanzas for tag don't ever match.

Setting up a fake languages.toml block like so

[[language]]
name = "comment"
scope = "scope.comment"
roots = []
file-types = ["comment"]

I get a good-looking highlight on my f.comment here:

foo.comment

where I've touched up the queries so that the default tag rule goes to the bottom (as well as other misc changes to line up the queries with how helix expects them):

[
 "("
 ")"
] @punctuation.bracket

":" @punctuation.delimiter

((tag (name) @warning)
 (#match? @warning "^(TODO|HACK|WARNING)$"))

("text" @warning
 (#match? @warning "^(TODO|HACK|WARNING)$"))

((tag (name) @error)
 (match? @error "^(FIXME|XXX|BUG)$"))

("text" @error
 (match? @error "^(FIXME|XXX|BUG)$"))

(tag
 (name) @ui.text
 (user)? @constant)

; Issue number (#123)
("text" @constant.numeric
 (#match? @constant.numeric "^#[0-9]+$"))

; User mention (@user)
("text" @tag
 (#match? @tag "^[@][a-zA-Z0-9_-]+$"))

This is viewing a file just using the comment tree-sitter and highlights though, and those same highlights seem to change dramatically when using injection, including the problem where each first word on a comment line gets highlighted. I'll have to dig a little deeper to figure out how the injection stuff works to give a perspective on that

the-mikedavis added a commit to the-mikedavis/helix that referenced this pull request Dec 18, 2021
@the-mikedavis
Copy link
Member

ah actually the injection appears to work well with a languages.toml entry like so:

[[language]]
name = "comment"
scope = "scope.comment"
roots = []
file-types = ["comment"]
injection-regex = "comment"

we need the injection-regex to make the tree-sitter injections work (see this block).

scm-injection

@archseer
Copy link
Member

Great work! I also noticed we need to fix some of the injection definitions that use @comment:

@pickfire
Copy link
Contributor Author

Thanks @the-mikedavis for helping out on this, I guess I will close this for now since the new PR is out.

@pickfire pickfire closed this Dec 19, 2021
archseer pushed a commit that referenced this pull request Dec 19, 2021
* Add tree-sitter-comment

Fix #1164

* fix precedence in tree-sitter-comment highlights

connects #1170

* set injection-regex for comment language

* remove comment filetype

* fix comment injections for neovim-style injections tags

* add comment injections for elixir

* remove f.comment

* fix spacing in .gitmodules

* run 'cargo xtask docgen'

Co-authored-by: Ivan Tham <[email protected]>
@pickfire pickfire deleted the comment branch December 20, 2021 13:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

highlight TODO, FIXME and XXX in comments
3 participants