refactor!(tree-sitter): replace tree-sitter w/ treesit

BREAKING CHANGE: This rewrites the :tools tree-sitter module to use
treesit instead of tree-sitter.el. Users will need to adapt to treesit
and remove any tree-sitter-specific config in their dotfiles.

Ref: #7623
Ref: #7742
Ref: #8197
This commit is contained in:
Henrik Lissner
2025-05-07 18:08:24 -04:00
parent 50200762cd
commit 1ac1b70d4e
7 changed files with 262 additions and 117 deletions

View File

@@ -0,0 +1,43 @@
;;; tools/tree-sitter/autoload/compat-30.el -*- lexical-binding: t; -*-
;;;###if (and (fboundp 'treesit-available-p) (version< emacs-version "31.1"))
;;
;; Backported from 31.1
;;
;;; Code:
(autoload 'treesit-ready-p "treesit")
;;;###autoload
(defcustom treesit-auto-install-grammar 'ask
"Whether to install tree-sitter language grammar libraries when needed.
This controls whether Emacs will install missing grammar libraries
when they are needed by some tree-sitter based mode.
If `ask', ask for confirmation before installing the required grammar library.
If `always', install the grammar library without asking.
If nil or `never' or anything else, don't install the grammar library
even while visiting a file in the mode that requires such grammar; this
might display a warning and/or fail to turn on the mode."
:type '(choice (const :tag "Never install grammar libraries" never)
(const :tag "Always automatically install grammar libraries"
always)
(const :tag "Ask whether to install missing grammar libraries"
ask))
:version "31.1"
:group 'treesit)
;;;###autoload
(defun treesit-ensure-installed (lang)
"Ensure that the grammar library for the language LANG is installed.
The option `treesit-auto-install-grammar' defines whether to install
the grammar library if it's unavailable."
(or (treesit-ready-p lang t)
(when (or (eq treesit-auto-install-grammar 'always)
(and (eq treesit-auto-install-grammar 'ask)
(y-or-n-p (format "\
Tree-sitter grammar for `%s' is missing; install it?"
lang))))
(treesit-install-language-grammar lang)
;; Check that the grammar was installed successfully
(treesit-ready-p lang))))
;;; compat-30.el ends here

View File

@@ -0,0 +1,71 @@
;;; tools/tree-sitter/autoload/tree-sitter.el -*- lexical-binding: t; -*-
;;;###autodef (fset 'tree-sitter! #'ignore)
(defun tree-sitter! ()
(message "Old tree-sitter.el support is deprecated!"))
;;;###autodef (fset 'set-tree-sitter! #'ignore)
(defun set-tree-sitter! (mode ts-mode &optional langs)
"Remap major MODE to TS-MODE.
MODE and TS-MODE are major mode symbols. If LANGS is provided, fall back to MODE
if LANGS don't pass `treesit-ready-p' when activating TS-MODE. Use this for ts
modes that error out instead of failing gracefully."
(declare (indent 2))
(cl-check-type mode symbol)
(cl-check-type ts-mode symbol)
(setq langs (ensure-list langs))
(dolist (m (ensure-list mode))
(add-to-list
'major-mode-remap-defaults
(cons
m (let (ensured?)
(lambda ()
(funcall
;; Because standard major-mode remapping doesn't offer graceful
;; failure in some cases, I implement it myself:
(cond ((null langs) m)
((not (fboundp ts-mode))
(message "Couldn't find %S, using %S instead" ts-mode m)
m)
((and (fboundp 'treesit-available-p)
(treesit-available-p)
(fboundp ts-mode)
;; Only prompt once, and log other times.
(cl-every (if ensured?
(doom-rpartial #'treesit-ready-p 'message)
#'treesit-ensure-installed)
(cl-loop for lang in langs
if (listp lang)
collect (car lang)
else collect (list lang))))
ts-mode)
((setq ensured? t)
m))))))))
(with-eval-after-load 'treesit
(dolist (lang langs)
(when (and lang (listp lang))
(cl-destructuring-bind (name &key url rev source-dir cc cpp commit) lang
(setf (alist-get name treesit-language-source-alist)
(list url rev source-dir cc cpp commit)))))))
;; ;; HACK: Remove and refactor when `use-package' eager macro expansion is solved or `use-package!' is removed
;; ;;;###autoload
;; (defun +tree-sitter-get-textobj (group &optional query)
;; "A wrapper around `evil-textobj-tree-sitter-get-textobj' to
;; prevent eager expansion."
;; (eval `(evil-textobj-tree-sitter-get-textobj ,group ,query)))
;; ;;;###autoload
;; (defun +tree-sitter-goto-textobj (group &optional previous end query)
;; "Thin wrapper that returns the symbol of a named function, used in keybindings."
;; (let ((sym (intern (format "+goto%s%s-%s" (if previous "-previous" "") (if end "-end" "") group))))
;; (fset sym (lambda ()
;; (interactive)
;; (evil-textobj-tree-sitter-goto-textobj group previous end query)))
;; sym))
;;; TODO: Backwards compatibility
;;; tree-sitter.el ends here