tree-sitter-langs icon indicating copy to clipboard operation
tree-sitter-langs copied to clipboard

Hacky support for treesit in Emacs core

Open ymarco opened this issue 3 years ago • 4 comments
trafficstars

CC @casouri

I wanted to use tree-sitter-langs packaged grammars and queries with treesit in Emacs core.

I'm not expecting these bunch of hacks to be accepted, it's more of a talking point on compatibility/collaboration between elisp-tree-sitter and treesit.

Points:

  • elisp-tree-sitter maps capture name X to face tree-sitter-hl-face:X, while treesit expects X to be a face by itself. I tried to replace X with tree-sitter-hl-face:X, but seems like colons end identifier names in tree-sitter's query language. What I ended up doing is replacing X with the parent face of tree-sitter-hl-face:X
  • .match? and .eq? predicates in elisp-tree-sitter are renamed to #match and #euqal in treesit, and #match has its arguments swapped in relation to .match?
  • treesit expects grammars to be files in the format libtree-sitter-X, so it can't find tree-sitter-lang's grammars, so I symlinked each grammar file with itself prefixed with libtree-sitter-.

ymarco avatar May 15 '22 10:05 ymarco

@ymarco question: does the treesit integration in core support multi-language documents?

jsmestad avatar Sep 30 '22 14:09 jsmestad

I haven't delved much into that, it would be doable with functions like treesit-set-ranges in the API. Yuan wrote a texinfo node about it in the feature/tree-sitter branch (see Parsing Text in Multiple Languages and treesit-parser-set-included-ranges), but I think it'd be better to ask him on emacs-devel what an actual mode using these would look like.

ymarco avatar Sep 30 '22 14:09 ymarco

@kiennq I just saw your fork on reddit, what does it do? I couldn't make sense of the commit history...

ymarco avatar Oct 10 '22 06:10 ymarco

@ymarco My fork is similar to your commit with additional to:

  • doesn't take dependencies on emacs-tree-sitter, the only dependency is the builtin treesit. The recipe for it is (treesit-langs :repo "kiennq/tree-sitter-langs" :fetcher github :files ("tree-sitter-langs-build.el" "treesit-*.el" "queries"))
  • Add some more parsers with their highlight queries (taken from the parser's repo or neovim's tree-sitter repo)

kiennq avatar Oct 18 '22 04:10 kiennq