Skip to content

Jump around your source code in emacs using treesit and avy

License

Notifications You must be signed in to change notification settings

dmille56/treesit-jump

Repository files navigation

https://github.com/dmille56/treesit-jump/actions/workflows/main.yml/badge.svg

About

Jump around your source code in emacs using treesit and avy.

Demos

treesit-jump-jump (used to jump to match) https://i.imgur.com/bYeCHwd.gif

treesit-jump-select (used to select the region of a match) https://i.imgur.com/Ge5g7pb.gif

treesit-jump-delete (used to delete the region of a match) https://i.imgur.com/ofIUGTj.gif

treesit-jump-parent-jump (used to jump to a parent of the current node under the cursor) https://i.imgur.com/2222Jun.gif

treesit-jump-gptel-describe (used to describe the selected region using gptel (chatgpt/ollama/etc.))

Table of Contents

Installation

Dependencies

  1. Emacs compiled with treesit support (version 29+)
  2. Installed treesitter grammar (for the language you want to use)
    • You can use treesit-auto to install grammars and auto load treesitter language modes.
  3. Avy
  4. Transient
  5. Gptel

Config

(use-package treesit-jump
  :straight (:host github :repo "dmille56/treesit-jump" :files ("*.el" "treesit-queries"))
  :config
  ;; Optional: add some queries to filter out of results (since they can be too cluttered sometimes)
  (setq treesit-jump-queries-filter-list '("inner" "test" "param")))

Extra config options

  • Transient for all the functions: treesit-jump-transient
  • How to add extra treesit queries per mode:
(add-to-list 'treesit-jump-queries-extra-alist (cons 'powershell-ts-mode '("(flow_control_statement (_)) @flow")))
(add-to-list 'treesit-jump-queries-extra-alist (cons 'python-ts-mode '("(return_statement (_)) @return")))
  • How to add queries to filter out of results per mode:
(add-to-list 'treesit-jump-queries-filter-mode-alist (cons 'python-ts-mode '("class")))
  • Function to clear cache (useful if you’ve added to treesit-queries per node after running treesit-jump functions):
treesit-jump-queries-clear-cache

Queries

Queries are defined in treesit-queries directory. Queries must be defined separately for each language. List of the query capture types are defined here: https://github.com/nvim-treesitter/nvim-treesitter-textobjects?tab=readme-ov-file#built-in-textobjects.

For help defining your own queries refer to: https://github.com/emacs-mirror/emacs/blob/master/admin/notes/tree-sitter/starter-guide#query-syntax. Also recommend the following package for debugging your queries: https://git.sr.ht/~meow_king/ts-query-highlight.

Notes

I’ve tested that the queries compile for the following languages: Python, C, C++, Java, C#, JavaScript, TypeScript, Go, Rust.

Special Thanks

This package takes inspiration from avy, and evil-textobj-tree-sitter. Queries (and a bit of code to process them) were taken originally from evil-textobj-tree-sitter. The code here is licensed under the same license (Apache-2.0).

About

Jump around your source code in emacs using treesit and avy

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published