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

Using tree sitter modes does not play well with lsp #51

Closed colonelpanic8 closed 9 months ago

colonelpanic8 commented 1 year ago

This is probably more of an issue with lsp-mode than treesit-auto, and to that end I have filed https://github.com/emacs-lsp/lsp-mode/issues/4117

Still, it could be worth considering adding something here, or at least making treesit-auto users aware of this issue.

Also, here's my config that hackily fixes the issue, in case any one wants something like this:

(use-package treesit-auto
  :demand t
  :config
  (progn
    (setq treesit-auto-install t)
    (global-treesit-auto-mode +1)

    (defun treesit-auto-copy-all-lsp-languages ()
      (cl-loop for recipe in treesit-auto-recipe-list
               do
               (let ((from (treesit-auto-recipe-remap recipe))
                     (to (treesit-auto-recipe-ts-mode recipe)))
                 (treesit-auto-copy-lsp-language from to)
                 (treesit-auto-copy-lsp-client-languages from to))))

    (defun treesit-auto-copy-lsp-language (from to)
      (let ((from-value (alist-get from lsp-language-id-configuration)))
        (when from-value
          (add-to-list 'lsp-language-id-configuration `(,to . ,from-value))
          from-value)))

    (defun treesit-auto-copy-lsp-client-languages (from to)
      (cl-loop for client in (hash-table-values lsp-clients)
               do (let ((major-modes (lsp--client-major-modes client)))
                    (when (memq from major-modes)
                      (message (concat "Matched " (symbol-name from) " with " (symbol-name (lsp--client-server-id client))))
                      (setf (lsp--client-major-modes client) (append major-modes (list to)))))))

    (use-package lsp-mode
      :config
      (treesit-auto-copy-all-lsp-languages))))
renzmann commented 10 months ago

I can't quote discern if there's a bug report about treesit-auto in here; I'll likely close this if there isn't something specific about this package that we need to address.