diff --git a/modules/tools/tree-sitter/autoload/compat-30.el b/modules/tools/tree-sitter/autoload/compat-30.el index a4fe39300..d37f936b6 100644 --- a/modules/tools/tree-sitter/autoload/compat-30.el +++ b/modules/tools/tree-sitter/autoload/compat-30.el @@ -40,4 +40,32 @@ Tree-sitter grammar for `%s' is missing; install it?" ;; Check that the grammar was installed successfully (treesit-ready-p lang)))) +;;; Introduced in later commits of 31.X +;;;###autoload +(unless (boundp 'treesit-major-mode-remap-alist) + (defvar treesit-major-mode-remap-alist nil)) + +;;;###autoload +(defcustom treesit-enabled-modes nil + "Specify what treesit modes to enable by default. +The value can be either a list of ts-modes to enable, +or t to enable all ts-modes." + :type `(choice + (const :tag "Disable all automatic associations" nil) + (const :tag "Enable all available ts-modes" t) + (set :tag "List of enabled ts-modes" + ,@(when (treesit-available-p) + (sort (mapcar (lambda (m) `(function-item ,m)) + (seq-uniq (mapcar #'cdr treesit-major-mode-remap-alist))))))) + :initialize #'custom-initialize-default + :set (lambda (sym val) + (set-default sym val) + (when (treesit-available-p) + (dolist (m treesit-major-mode-remap-alist) + (setq major-mode-remap-alist + (if (or (eq val t) (memq (cdr m) val)) + (cons m major-mode-remap-alist) + (delete m major-mode-remap-alist)))))) + :version "31.1") + ;;; compat-30.el ends here diff --git a/modules/tools/tree-sitter/autoload/tree-sitter.el b/modules/tools/tree-sitter/autoload/tree-sitter.el index 23f2d0b3d..b3dde5490 100644 --- a/modules/tools/tree-sitter/autoload/tree-sitter.el +++ b/modules/tools/tree-sitter/autoload/tree-sitter.el @@ -39,6 +39,8 @@ Note that COMMIT is only available in Emacs >=31." ((and (fboundp 'treesit-available-p) (treesit-available-p) (fboundp ts-mode) + (or (eq treesit-enabled-modes t) + (memq ts-mode treesit-enabled-modes)) ;; Only prompt once, and log other times. (cl-every (if ensured? (doom-rpartial #'treesit-ready-p 'message) diff --git a/modules/tools/tree-sitter/config.el b/modules/tools/tree-sitter/config.el index 62788322b..4c2cd9133 100644 --- a/modules/tools/tree-sitter/config.el +++ b/modules/tools/tree-sitter/config.el @@ -7,7 +7,32 @@ :when (fboundp 'treesit-available-p) :when (treesit-available-p) :defer t + :preface + (setq treesit-enabled-modes t) + + ;; HACK: These *-ts-mode-maybe functions all treat `treesit-enabled-modes' + ;; strangely in the event the language's grammar is unavailable. Plus, they + ;; add yet-another-layer of complexity for users to be cognicent of. Get rid + ;; of them. + ;; REVIEW: Handle this during the 'doom sync' process instead. + (setq auto-mode-alist + (save-match-data + (cl-loop for (src . fn) in auto-mode-alist + unless (and (functionp fn) + (string-match "-ts-mode-maybe$" (symbol-name fn))) + collect (cons src fn)))) + :config + ;; HACK: The implementation of `treesit-enabled-modes's setter and + ;; `treesit-major-mode-remap-alist' are intrusively opinionated, so I + ;; disable it altogether as to not unexpectedly modify + ;; `major-mode-remap-alist' at runtime. What's more, there's no guarantee + ;; this will be populated correctly unless the user is on a particular + ;; commit of Emacs 31 or newer. Best we simply ignore it. + (dolist (m treesit-major-mode-remap-alist) + (setq major-mode-remap-alist (delete m major-mode-remap-alist))) + (setq treesit-major-mode-remap-alist nil) + ;; HACK: treesit lacks any way to dictate where to install grammars. (add-to-list 'treesit-extra-load-path (concat doom-profile-data-dir "tree-sitter")) (defadvice! +tree-sitter--install-grammar-to-local-dir-a (fn &rest args)