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

@@ -132,7 +132,8 @@ information.")
(let ((pred (car rule)) (let ((pred (car rule))
(plist (cdr rule))) (plist (cdr rule)))
(and (or (and (symbolp pred) (and (or (and (symbolp pred)
(eq major-mode pred)) (or (eq major-mode pred)
(memq pred (get major-mode 'derived-mode-extra-parents))))
(and (stringp pred) (and (stringp pred)
(stringp buffer-file-name) (stringp buffer-file-name)
(string-match-p pred buffer-file-name))) (string-match-p pred buffer-file-name)))

View File

@@ -95,6 +95,7 @@ This can be passed nil as its second argument to unset handlers for MODES. e.g.
'xref-backend-functions) 'xref-backend-functions)
(make-list 5 async) (make-list 5 async)
(make-list 5 (or (eq major-mode mode) (make-list 5 (or (eq major-mode mode)
(memq mode (get major-mode 'derived-mode-extra-parents))
(and (boundp mode) (and (boundp mode)
(symbol-value mode)))))))) (symbol-value mode))))))))
(add-hook hook fn))))) (add-hook hook fn)))))

View File

@@ -5,12 +5,13 @@
(message "Old tree-sitter.el support is deprecated!")) (message "Old tree-sitter.el support is deprecated!"))
;;;###autodef (fset 'set-tree-sitter! #'ignore) ;;;###autodef (fset 'set-tree-sitter! #'ignore)
(defun set-tree-sitter! (mode ts-mode &optional recipes) (defun set-tree-sitter! (modes ts-mode &optional recipes)
"Remap major MODE to TS-MODE. "Remap major MODES to TS-MODE.
MODE and TS-MODE are major mode symbols. If RECIPES is provided, fall back to MODES and TS-MODE are major mode symbols. MODES can be a list thereof. If
MODE if RECIPES don't pass `treesit-ready-p' when activating TS-MODE. Use this RECIPES is provided, fall back to MODES if RECIPES don't pass `treesit-ready-p'
for ts modes that error out instead of failing gracefully. 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 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 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 (which describe what each of these keys mean). Note that COMMIT is ignored
pre-Emacs 31." pre-Emacs 31."
(declare (indent 2)) (declare (indent 2))
(cl-check-type mode (or list symbol)) (cl-check-type modes (or list symbol))
(cl-check-type ts-mode symbol) (cl-check-type ts-mode symbol)
(let ((recipes (mapcar #'ensure-list (ensure-list recipes)))) (let ((recipes (mapcar #'ensure-list (ensure-list recipes)))
(dolist (m (or (ensure-list mode) (list nil))) (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 (when m
(setf (alist-get m major-mode-remap-defaults) ts-mode)) (setf (alist-get m major-mode-remap-defaults) ts-mode))
(put ts-mode '+tree-sitter (cons m (mapcar #'car recipes)))) (put ts-mode '+tree-sitter (cons m (mapcar #'car recipes))))