Are highlighting queries in tree-sitter-XXX repositories considered canonical? #2381
-
For every language, there are highlighting queries being maintained in the I'm asking because for instance there is a repository maintaining highlighting queries for neovim (https://github.com/nvim-treesitter/nvim-treesitter/tree/master/queries), and another one for Emacs (https://github.com/emacs-tree-sitter/tree-sitter-langs/tree/master/queries). It feels like a lot of duplicated work to maintain (presumably very similar) highlighting queries in several different places. In particular, the Emacs repository justifies it in the following way:
What do people working on tree-sitter think? I'm using Emacs and I would like to contribute fixes to highlighting queries upstream, but I'm not sure where upstream is… (tagging @amaanq, as we interacted recently in the Javascript/Typescript grammars, and I see that you recently contributed to the neovim repository) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Queries are how you integrate a client with a specific tree-sitter grammar, so each client (neovim, emacs, etc.) owning the queries they use to integrate with each grammar makes sense. Different clients support different capture names and predicates. The queries you see living in each tree-sitter repo are only consumed by the tree-sitter highlight command and github itself (for the few languages it highlights with tree-sitter), as far as I know. They also serve as useful examples to adapt to different clients. Highlighting query files are not a very large amount of work to make and might take a day or two at most for a very complicated grammar. If new clients want to reuse existing work they can choose to support the same capture names and predicates as for example neovim. It's not really accurate to call the queries living in each tree-sitter repo canonical, but for my own grammar I do maintain a sort of correspondence between them and queries in the various supported clients. |
Beta Was this translation helpful? Give feedback.
-
Those that are owned by tree-sitter are meant for usage w/ the highlight subcommand in the cli iirc, and the captures are a bit simpler. Now for Neovim, one of the main differences between nvim-treesitter/neovim core and tree-sitter is that our queries are last-wins, or basically, all captures are applied and the latest + most inner capture is shown, e.g. if we have a tree like (field (identifier)), where the entire field is captured Emacs is also a wildcard which we know very little about in terms of captures, since they seem to embed the query-like syntax in Emacs-lisp code itself (would be easy enough and sort-of makes sense given the lisp-likeness, I guess) I for one would love to standardize a broader set of captures that make sense for all editors, so that a majority of queries can be shared - Max himself has agreed that last-wins is a better system for queries while working on the Zed editor, but currently upstream and Helix use first-wins/a short circuit logic. I plan to add a flag for the highlight crate to allow a "last-wins" precedence soon, something like "--apply-all-captures" probably. Upstream for you is probably Emacs, if you think tree-sitter would also benefit from lacking captures that's welcome as well (just peruse some of the repos queries for the style/standard) |
Beta Was this translation helpful? Give feedback.
-
Thanks you |
Beta Was this translation helpful? Give feedback.
Those that are owned by tree-sitter are meant for usage w/ the highlight subcommand in the cli iirc, and the captures are a bit simpler.
Now for Neovim, one of the main differences between nvim-treesitter/neovim core and tree-sitter is that our queries are last-wins, or basically, all captures are applied and the latest + most inner capture is shown, e.g. if we have a tree like (field (identifier)), where the entire field is captured
@field
and identi…