From 80a43c75b96b58c62e966c7e676119eaee0aadc3 Mon Sep 17 00:00:00 2001 From: John Turner Date: Mon, 25 Sep 2023 05:48:09 -0400 Subject: [PATCH] conditionally configure treesitter modes if their parsers are avail --- packages/treesit.el | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/packages/treesit.el b/packages/treesit.el index d9326f8..d5af6b9 100644 --- a/packages/treesit.el +++ b/packages/treesit.el @@ -1,16 +1,24 @@ (use-package treesit :if (ignore-errors (treesit-available-p)) - :mode - ((rx (literal ".") "rs" eos) . rust-ts-mode) - ((rx (literal ".") "py" eos) . python-ts-mode) - ((rx (literal ".") "sh" eos) . bash-ts-mode) - :interpreter - ("python" . python-ts-mode) - ("python3" . python-ts-mode) - ("bash" . bash-ts-mode) - ("sh" . bash-ts-mode) - ("openrc-run" . bash-ts-mode)) - + :config + (let ((setup-treesit-mode (lambda (parser treesit-mode modes &optional interpreters) + (if (not (treesit-language-available-p parser)) + (error "treesit language parser not available for %s" (symbol-name language)) + (seq-each (lambda (mode) + (add-to-list 'auto-mode-alist + (cons (rx (literal ".") + (literal mode) eos) + treesit-mode))) + modes) + (seq-each (lambda (interpreter) + (add-to-list 'interpreter-mode-alist + (cons interpreter treesit-mode))) + interpreters))))) + (funcall setup-treesit-mode 'python 'python-ts-mode '("py") '("python" "python3")) + (funcall setup-treesit-mode 'bash 'bash-ts-mode '("sh") '("bash" "sh" "openrc-run")) + (funcall setup-treesit-mode 'rust 'rust-ts-mode '("rs")) + (funcall setup-treesit-mode 'cpp 'c++-ts-mode '("cpp" "cxx" "c++" "hpp" "hxx" "h++")))) + (use-package rust-ts-mode :requires treesit :hook