cleaned up treesitter setup script

master
John Turner 2 years ago
parent 4a7824f864
commit a12a48a0a3
No known key found for this signature in database
GPG Key ID: 422FE10CC41A94A2

@ -1,52 +1,94 @@
;; -*- lexical-binding: t; -*- ;; -*- lexical-binding: t; -*-
(defvar -languages `((python-ts-mode ;; This script is a quick workaround for setting up treesitter modes.
((:langs . python) ;; Hopefully this won't be needed for long but it works for now!
(:file-ext . ,(rx ".py")) ;;
(:interpreter . "python"))) ;; This just runs through the list of modes below, checks if the
(rust-ts-mode ;; required treesitter parsers are installed, and if so,
((:langs . rust) ;; sets up auto-mode-alist and interpreter-mode-alist
(:file-ext . ,(rx ".rs")))) ;; to open the respective source files with the treesitter
(bash-ts-mode ;; mode for that language.
((:langs . bash) ;;
(:file-ext . ,(rx ".sh")) ;; To use, you should just add the modes and other info to the list
(:interpreter . "bash"))) ;; below, and then "require" this file in your init.
(c-ts-mode ;; The messages that get generated by this script may be burried
((:langs . c) ;; by other startup messages, so it may not appear to have done
(:file-ext . ,(rx ".c")))) ;; anything!
(c++-ts-mode ;;
((:langs . c++) ;; To test if it works, visit a file such as "x.py", then run
(:file-ext . ,(rx (or ".c++" ".cpp" ".cxx" ;; "M-x describe-mode". There should be some info about
".h++" ".hpp" ".hxx"))))) ;; the current major mode that looks like this:
(c-or-c++-ts-mode ;;
((:langs . (c c++)) ;; "The major mode is Python mode defined in python.el:
(:file-ext . ,(rx ".h")))) ;; Major mode for editing Python files, using tree-sitter library."
(toml-ts-mode
((:langs . toml) (defvar -quiet? nil
(:file-ext . ,(rx ".toml")))))) "Suppress messages when setting up treesitter modes.")
(defun -ready-quiet-p (language) (defvar -modes `((python-ts-mode
(treesit-ready-p language t)) ((:parsers . python)
(:file-ext . ,(rx ".py"))
(cl-defun -load-language? (&key ts-mode (:interpreters . ("python" "python3"))))
required-languages (rust-ts-mode
file-extension ((:parsers . rust)
interpreter?) (:file-ext . ,(rx ".rs"))))
(let ((ready? (if (listp required-languages) (bash-ts-mode
(seq-every-p '-ready-quiet-p required-languages) ((:parsers . bash)
(-ready-quiet-p required-languages)))) (:file-ext . ,(rx ".sh"))
(when ready? (:interpreters . ("bash" "sh" "openrc-run"))))
(add-to-list 'auto-mode-alist (cons file-extension ts-mode)) (c-ts-mode
(when interpreter? ((:parsers . c)
(add-to-list 'interpreter-mode-alist (cons interpreter? ts-mode)))))) (:file-ext . ,(rx ".c"))))
(c++-ts-mode
(seq-map (lambda (item) ((:parsers . cpp)
(-load-language? (:file-ext . ,(rx (or ".c++" ".cpp" ".cxx"
:ts-mode (car item) ".h++" ".hpp" ".hxx")))))
:required-languages (alist-get :langs (cadr item)) (c-or-c++-ts-mode
:file-extension (alist-get :file-ext (cadr item)) ((:parsers . (c c++))
:interpreter? (alist-get :interpreter (cadr item)))) (:file-ext . ,(rx ".h"))))
-languages) (toml-ts-mode
((:parsers . toml)
(:file-ext . ,(rx ".toml")))))
"Treesitter modes and some information required to set them up.
Parsers are the treesitter packages that need to be installed to
use the associated mode. python-ts-mode requires the python treesitter
parser package to be installed for example.
File extension is for example .py for python scripts. Some
modes like c++-ts-mode are associated with a few
extensions, and c-or-c++-mode is associated with .h
because the extension is ambiguous.
Interpreters are used to associate certain shebangs with the treesitter
mode. Files that start with '#!/usr/bin/env python' for example will
be associated with python-ts-mode if 'python' is included in the
interpreters list.")
(defun -ready-quiet? (parser)
(treesit-ready-p parser t))
(cl-defun -setup-mode? (&key ts-mode
required-parsers
file-extensions
interpreters)
(when (seq-every-p '-ready-quiet? (ensure-list required-parsers))
(add-to-list 'auto-mode-alist (cons file-extensions ts-mode))
(when interpreters
(seq-each (lambda (interpreter)
(add-to-list 'interpreter-mode-alist (cons interpreter ts-mode)))
(ensure-list interpreters)))
t))
(seq-each (lambda (mode)
(when (and (-setup-mode?
:ts-mode (car mode)
:required-parsers (alist-get :parsers (cadr mode))
:file-extensions (alist-get :file-ext (cadr mode))
:interpreters (alist-get :interpreters (cadr mode)))
(not -quiet?))
(message "Setup treesitter mode %s" (car mode))))
-modes)
(provide 'init-treesit-setup-available-languages) (provide 'init-treesit-setup-available-languages)

Loading…
Cancel
Save