[[!sidebar content=""]] [[!meta title="Tree-sitter beyond syntax highlighting"]] [[!meta copyright="Copyright © 2022 Abin Simon"]] [[!inline pages="internal(2022/info/treesitter-nav)" raw="yes"]] <!-- Initially generated with emacsconf-generate-talk-page and then left alone for manual editing --> <!-- You can manually edit this file to update the abstract, add links, etc. ---> # Tree-sitter beyond syntax highlighting Abin Simon (IRC: meain on libera.chat, Matrix: @meain:matrix.org, <mailto:mail@meain.io>) [[!inline pages="internal(2022/info/treesitter-before)" raw="yes"]] Tree sitter has seen a lot of development recently, but more often than not folks are only aware of its use for syntax highliting. The idea of this talk is to introduce some other usecases where they could benefit from tree-sitter. This talk will be an overview of the kind of things that they will be able to do with tree-sitter with demos but won't go in depth into how they would all of them. The presentation will link to the resources mentioned during the talk where folks can learn more about each of them. This session will introduce them to things like (not final list): - textobjects using tree-sitter: <https://github.com/meain/evil-textobj-tree-sitter/> - Folding using tree-sitter: <https://github.com/emacs-tree-sitter/ts-fold> - Navigating config headings: <https://blog.meain.io/2022/navigating-config-files-using-tree-sitter/> - Using tree-sitter for narrowing: <https://blog.meain.io/2022/more-treesitter-emacs/#narrow-to-language-level-constructs> - Intelligent snippets using tree-sitter: <https://blog.meain.io/2021/intelligent-snippets-treesitter/> - Using tree-sitter to get which-func like functionality: <https://blog.meain.io/2022/more-treesitter-emacs/#show-current-class%2Ffunction-name-in-modeline> - Some useful tree-sitter functions: tree-sitter-save-excursion # Discussion ## Notes - The speaker's blog: <https://blog.meain.io/> - Fancy Narrow: <https://github.com/Malabarba/fancy-narrow> - Text objects using tree-sitter in evil-mode: <https://github.com/meain/evil-textobj-tree-sitter/> - Notes/Slides: <https://github.com/meain/emacsconf-talk-tree-sitter> ## Questions and answers - Q: What treesitter package is being used I think there is 3 different ones - A: Most of what is demoed here is using <https://github.com/emacs-tree-sitter/elisp-tree-sitter> - Q: Can the folds be treated as outlines as in outline-minor-mode folds? - A: I don't think the package ts-fold which I showcased works with outline mode, but it should be simple enough to add something like that (<https://github.com/emacs-tree-sitter/ts-fold>) - Q: Is there any benefit to use tree-sitter for sexp-based languages? +1 - A: Being able to query for specific things like variables / conditions might come in handy - Q:Do you have to have an LSP set up in order to use tree-sitter? - A:I still use eglot for lsp. While tree-sitter help with highlighting, folding, nav etc . . tree-sitter can be more thought of to be working on a single file. So when I need to do project wide things like jump to defenition, find reference or renames lsp comes in handy. - Q: Is there any example configuration for the transition from traditional major mode to new *-ts-major-mode? It seems that configuration of major mode (xxx-mode-hook, yasnippet, etc) has to been rewritten - A: I am just starting to work with builtin tree-sitter, so don't have much input here unfortunately :( - Q: So, is there a tree-sitter language definition for elisp? - A: I'm just starting to look into built-in tree sitter, but I feel like we should be able to do all of them. - Q: awesome stuff. i always wonder when itll appear in my fingers. sure the lib is in 29 but i guess some glue is required? - Q: thanks for the great talk. I have one question. Will tree sitter able to highlight syntax in sourceblocks of org files? - A: there is nothing technically stopping one from enabling highlighting in config blocks. Since I don't use org, I've not really looked into how it currently is. - Q: So Emacs 29 includes the original tree sitter C library by Max Brunsfeld or is it a custom rewrite? - Q: what about the relationship between emacs-tree-sitter and treesitter in core emacs - A: there are just two Emacs side implementaions for the tree-sitter lib <https://tree-sitter.github.io/tree-sitter/> . The first one is in Rust and the second in C within core - A: I have not extensively tested out the builtin one, but both should be more or less the same. The builtin one is less mature as of now and has a slightly different api. - A: Plus most plugins that work with tree-sitter will be working with elisp-tree-sitter only as of now - I'm speaking of the the third party package. Perhaps it has been fixed. IIUC the issue is not the emacs package but rather tree-sitter itself. - A: You might wanna open an issue at https://github.com/emacs-tree-sitter/tree-sitter-langs if you are having issues with tree-sitter highlighting - Q: biggest difference between the treesitter functionality built into Emacs 29 and the emacs-tree-sitter github package? - Q: Are there any sample configurations about *-ts-mode integrated with default major-mode? - Q: Building Emacs 29 with native tree-sitter support seem challanging any useful tip. - building emacs ... tips from Xah Lee <http://xahlee.info/emacs/emacs/building_emacs_on_linux.html> - Q: How much of what you showed can be done with the build-in tree-sitter? - Q: How easy is it to hack the syntax definition? - A: It is super easy once you learn a bit about tree-sitter. This is how the highlight queries looks like. <https://github.com/emacs-tree-sitter/tree-sitter-langs/blob/master/queries/python/highlights.scm> Once we have this, the tree-sitter integration can take care of the rest. - Q: So Emacs 29 includes the original tree sitter C library by Max Brunsfeld or is it a custom rewrite? - both elisp-tree-sitter and tree-sitter in emacs core are emacs side wrappers on top of the tree-sitter lib from Max. - Q: What was the name of the module used for doing AST queries on the current buffer? - A: for viewing and querying the tree, they are commands built into `tree-sitter-debug-mode` and `tree-sitter-query-builder` ## Other IRC discussions - thanks for the great talk meain! - thank you for the talk - Great talk. Can I use this with Python? Bash? - Amazing stuff!!! I need that YAML thing! - When I am writing lisp macros I'm always having problems with the highlighting. I'm seeing this can be achieve with tree-sitter, is there a more streamlined way of doing it with treesitter - with less code? - thank you for tree-sitter talk, It's awesome - Now I definitely need to try tree-sitter - very inspiring talk, the future looks bright! - Very well done talk. Thank you. - I've actually added a lot of highlighting for rust mode on my editor, using tree sitter. It's very powerful once you get into it - with the new *-ts-mode, seems that a lot of configurations of language specific major mode have to be rewritten - A: yup, there are quite a few things that being rewritten a bit like indent, highlight etc. - yup, previously I would just jump to top after reformatting code. These days I've been also trying https://github.com/radian-software/apheleia which has been pretty good at keeping the position by using some other methods - I use your tree-sitter package for a long time, It works very stable. - I use tree-sitter write plugin to replace paredit: https://github.com/manateelazycat/grammatical-edit [[!inline pages="internal(2022/info/treesitter-after)" raw="yes"]] [[!inline pages="internal(2022/info/treesitter-nav)" raw="yes"]] [[!taglink CategoryTreeSitter]]