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