Treesit 配合 expand-region.el

适用于 Emacs 内置的 treesit

(defun treesit-mark-bigger-node ()
  (let* ((root (treesit-buffer-root-node))
         (node (treesit-node-descendant-for-range root (region-beginning) (region-end)))
         (node-start (treesit-node-start node))
         (node-end (treesit-node-end node)))
    ;; Node fits the region exactly. Try its parent node instead.
    (when (and (= (region-beginning) node-start) (= (region-end) node-end))
      (when-let ((node (treesit-node-parent node)))
        (setq node-start (treesit-node-start node)
              node-end (treesit-node-end node))))
    (set-mark node-end)
    (goto-char node-start)))

(add-to-list 'er/try-expand-list 'treesit-mark-bigger-node)

代码修改自 Integration with expand-region · Issue #20 · emacs-tree-sitter/elisp-tree-sitter · GitHub

5 个赞

感谢,早就想要基于treesiter的expand region,年后再试试了