fix(tree-sitter): add derived-mode parents to ts-modes

ts-modes do this already in 30/31+ (`derived-mode-add-parents` was added
in 30), but is still needed for 29.x users and any ts-modes that haven't
adapted (many of them).

Fix: doomemacs/community#29
This commit is contained in:
Henrik Lissner
2025-09-07 10:21:10 -04:00
parent 8f55404781
commit 1adec8b10c
3 changed files with 17 additions and 9 deletions

View File

@@ -5,12 +5,13 @@
(message "Old tree-sitter.el support is deprecated!"))
;;;###autodef (fset 'set-tree-sitter! #'ignore)
(defun set-tree-sitter! (mode ts-mode &optional recipes)
"Remap major MODE to TS-MODE.
(defun set-tree-sitter! (modes ts-mode &optional recipes)
"Remap major MODES to TS-MODE.
MODE and TS-MODE are major mode symbols. If RECIPES is provided, fall back to
MODE if RECIPES don't pass `treesit-ready-p' when activating TS-MODE. Use this
for ts modes that error out instead of failing gracefully.
MODES and TS-MODE are major mode symbols. MODES can be a list thereof. If
RECIPES is provided, fall back to MODES if RECIPES don't pass `treesit-ready-p'
when activating TS-MODE. Use this for ts modes that error out instead of failing
gracefully.
RECIPES is a symbol (a grammar language name), list thereof, or alist of plists
with the format (LANG &key URL REV SOURCE-DIR CC CPP COMMIT). If an alist of
@@ -18,10 +19,15 @@ plists, it will be transformed into entries for `treesit-language-source-alist'
(which describe what each of these keys mean). Note that COMMIT is ignored
pre-Emacs 31."
(declare (indent 2))
(cl-check-type mode (or list symbol))
(cl-check-type modes (or list symbol))
(cl-check-type ts-mode symbol)
(let ((recipes (mapcar #'ensure-list (ensure-list recipes))))
(dolist (m (or (ensure-list mode) (list nil)))
(let ((recipes (mapcar #'ensure-list (ensure-list recipes)))
(modes (ensure-list modes)))
(when modes
;; Most ts-modes do not register their base modes as parents until Emacs
;; 30/31; this backports that.
(put ts-mode 'derived-mode-extra-parents modes))
(dolist (m (or modes (list nil)))
(when m
(setf (alist-get m major-mode-remap-defaults) ts-mode))
(put ts-mode '+tree-sitter (cons m (mapcar #'car recipes))))