renzmann / treesit-auto

Automatic installation, usage, and fallback for tree-sitter major modes in Emacs 29
GNU General Public License v3.0
369 stars 28 forks source link

Optionally add the ability to run the hooks associated with a non ts partner when enabling the ts mode #52

Closed colonelpanic8 closed 1 year ago

colonelpanic8 commented 1 year ago

I wrote this code in my config to do this:

(defun treesit-auto-for-each (fn)
  (cl-loop for recipe in treesit-auto-recipe-list
           do
           (let ((from (treesit-auto-recipe-remap recipe))
                 (to (treesit-auto-recipe-ts-mode recipe)))
             (funcall fn from to))))

(defun treesit-auto-get-mode-hook-symbol (mode)
  (intern (concat (symbol-name mode) "-hook")))

(defvar treesit-auto-run-original-hooks t)
(defvar treesit-auto-hook-copy-blacklist '((rust-mode . rust-ts-mode)))

(treesit-auto-for-each
 (lambda (from to)
   (let ((targets (if (listp from) from (list from))))
     (cl-loop for from in targets
              do
              (letrec ((to-hook (treesit-auto-get-mode-hook-symbol to))
                       (from-hook (treesit-auto-get-mode-hook-symbol from))
                       (treesit-auto-hook-name
                        (intern
                         (concat "treesit-auto-run-"
                                 (symbol-name from-hook)
                                 "-for-" (symbol-name to)))))
                (defalias treesit-auto-hook-name
                  `(lambda ()
                     (when (and treesit-auto-run-original-hooks
                                (boundp ',from-hook)
                                (not (memq '(,from . ,to) treesit-auto-hook-copy-blacklist)))
                       (message "Running hooks from %s for %s" ',from-hook ',to)
                       (apply 'run-hooks ,from-hook))))
                (add-hook to-hook treesit-auto-hook-name))))))

I'd be happy to clean this up if it could be something that would make sense to include as part of the package.

colonelpanic8 commented 1 year ago

nevermind, seems that updating lsp-mode fixed this issue for me

zeorin commented 12 months ago

I think you might have meant to close https://github.com/renzmann/treesit-auto/issues/51 instead of this issue.