Skip to content

Latest commit

 

History

History
59 lines (44 loc) · 2.13 KB

using-tree-edit.org

File metadata and controls

59 lines (44 loc) · 2.13 KB

Documentation on using the tree-edit library. More to come!

Rolling your own tree-edit commands

Evil style

Something like a lispyville may be of interest for those who prefer to be in normal mode. Here’s some simple examples of how you could implement that:

(evil-define-operator my/tree-edit-delete (beg end)
  "Delete node between BEG and END, if possible."
  (let ((node (tsc-get-named-descendant-for-position-range
               (tsc-root-node tree-sitter-tree) beg end)))
    (tree-edit-delete node)))

(evil-define-operator my/tree-edit-raise (beg end)
  "Raise node between BEG and END, if possible."
  (let ((node (tsc-get-named-descendant-for-position-range
               (tsc-root-node tree-sitter-tree) beg end)))
    (tree-edit-raise node)))

(evil-define-key '(normal visual) global-map "gk" #'my/tree-edit-delete)
(evil-define-key '(normal visual) global-map "g/" #'my/tree-edit-raise)

One thing to be careful of is whitespace: for example if you want to raise an identifier, typing g/w on a word would include the whitespace and would select the surrounding node instead (So g/e should be used). Similarly with text objects selecting the entire line.

Emacs style

(defun my/tree-edit-raise-word ()
  (interactive)
  (pcase-let* ((`(,beg . ,end) (bounds-of-thing-at-point 'word))
               (node (tsc-get-named-descendant-for-position-range
                      (tsc-root-node tree-sitter-tree) beg end)))
    (tree-edit-raise node)))

(define-key (current-global-map) (kbd "M-r") #'my/tree-edit-raise-word)

Adding new languages to tree-edit

  1. Install Cask
  2. Install grammar using tree-edit-install-grammar
  3. Copy one of the language files (tree-edit-python.el or similar) and adapt it to the new language
  4. Add language to tree-edit-language-alist
  5. See what breaks!

Customizing languages

Check out the docstrings of the variables used in tree-sitter-python.el and the pre-existing language files to see how to customize languages.