From 87b55c036365e502610e9a0b98518075f3cfae76 Mon Sep 17 00:00:00 2001 From: Uko Kokņevičs Date: Fri, 17 Oct 2025 08:06:36 +0300 Subject: emacs: Improve the treesit config by a lot --- emacs/.config/emacs/arkta/arkta-progmodes.el | 3 + emacs/.config/emacs/arkta/arkta-treesit.el | 152 +++++++++++++++++++++++---- 2 files changed, 132 insertions(+), 23 deletions(-) diff --git a/emacs/.config/emacs/arkta/arkta-progmodes.el b/emacs/.config/emacs/arkta/arkta-progmodes.el index 98cf58e..eddc4aa 100644 --- a/emacs/.config/emacs/arkta/arkta-progmodes.el +++ b/emacs/.config/emacs/arkta/arkta-progmodes.el @@ -1,6 +1,8 @@ ;; -*- lexical-binding: t -*- ;; Copyright © 2018-2025 Uko Koknevics +(require 'arkta-treesit) + (use-package adoc-mode :straight t :mode "\\.adoc\\'") @@ -299,6 +301,7 @@ (use-package typst-ts-mode :after treesit + :when (arkta/treesit-supports 14) :straight t :mode "\\.typ\\'") diff --git a/emacs/.config/emacs/arkta/arkta-treesit.el b/emacs/.config/emacs/arkta/arkta-treesit.el index e6fede7..9375245 100644 --- a/emacs/.config/emacs/arkta/arkta-treesit.el +++ b/emacs/.config/emacs/arkta/arkta-treesit.el @@ -4,6 +4,28 @@ (use-package treesit :straight '(treesit :type built-in) :config + (use-package dash + :straight t) + + (defun arkta/treesit-supports-single (wanted-version) + (and (>= wanted-version (treesit-library-abi-version t)) + (<= wanted-version (treesit-library-abi-version)))) + + (defun arkta/treesit-supports (&rest wanted-versions) + (-any? #'arkta/treesit-supports-single wanted-versions)) + + (unless (arkta/treesit-supports 13) + (error "Your treesitter library does not support ABI version 13!")) + + (defmacro arkta/treesit-cond (&rest body) + (declare (indent 0)) + `(cond ,@(cl-loop for (version . clause-body) in body + collect (let ((check (if (consp version) + `(arkta/treesit-supports ,@version) + `(arkta/treesit-supports-single ,version)))) + `(,check ,@clause-body))) + (t (error "Unsupported treesit version [%s;%s]!!" (treesit-library-abi-version t) (treesit-library-abi-version))))) + (defvar *arkta/treesit-current* nil) (defun arkta/treesit-current-file () @@ -43,7 +65,6 @@ (arkta/treesit-save-current))) (defun arkta/$after-treesit-install-language-grammar (lang &optional out-dir) - (declare (ignore out-dir)) (let ((spec (assq lang treesit-language-source-alist))) (arkta/treesit-append-current spec))) (advice-add 'treesit-install-language-grammar :after #'arkta/$after-treesit-install-language-grammar) @@ -57,8 +78,9 @@ (let ((name (car spec))) (unless (and (treesit-language-available-p name) (arkta/treesit-newest-p spec)) - (message "Treesit: Ensuring %s...\n" (car spec)) - (treesit-install-language-grammar name)))) + (message "Treesit: Ensuring %s..." name) + (treesit-install-language-grammar name)) + (message "Treesit: We have %s with ABI version %s" name (treesit-language-abi-version name)))) (defun arkta/treesit-ensure-languages () (mapc #'arkta/treesit-ensure-language treesit-language-source-alist)) @@ -67,26 +89,110 @@ (list (expand-file-name "tree-sitter" local-config-dir))) (setq treesit-language-source-alist - '((c "https://github.com/tree-sitter/tree-sitter-c.git" "v0.24.1") - (cmake "https://github.com/uyha/tree-sitter-cmake.git" "v0.7.1") - (cpp "https://github.com/tree-sitter/tree-sitter-cpp.git" "v0.23.4") - (c-sharp "https://github.com/tree-sitter/tree-sitter-c-sharp.git" "v0.23.1") - (css "https://github.com/tree-sitter/tree-sitter-css.git" "v0.25.0") - (dockerfile "https://github.com/camdencheek/tree-sitter-dockerfile.git" "v0.2.0") - (go "https://github.com/tree-sitter/tree-sitter-go.git" "v0.25.0") - (gomod "https://github.com/camdencheek/tree-sitter-go-mod.git" "v1.1.0") - (java "https://github.com/tree-sitter/tree-sitter-java.git" "v0.23.5") - (javascript "https://github.com/tree-sitter/tree-sitter-javascript.git" "v0.25.0") - (json "https://github.com/tree-sitter/tree-sitter-json.git" "v0.24.8") - (kotlin "https://github.com/fwcd/tree-sitter-kotlin.git" "0.3.8") - (python "https://github.com/tree-sitter/tree-sitter-python.git" "v0.25.0") - (ruby "https://github.com/tree-sitter/tree-sitter-ruby.git" "v0.23.1") - (rust "https://github.com/tree-sitter/tree-sitter-rust.git" "v0.24.0") - (toml "https://github.com/ikatyang/tree-sitter-toml.git" "v0.5.1") - (tsx "https://github.com/tree-sitter/tree-sitter-typescript.git" "v0.23.2" "tsx/src") - (typescript "https://github.com/tree-sitter/tree-sitter-typescript.git" "v0.23.2" "typescript/src") - (typst "https://github.com/uben0/tree-sitter-typst.git" "v0.11.0") - (yaml "https://github.com/ikatyang/tree-sitter-yaml.git" "v0.5.0"))) + `((c "https://github.com/tree-sitter/tree-sitter-c.git" + ,(arkta/treesit-cond + (15 "v0.24.1") + (14 "v0.23.6") + (13 "v0.19.0"))) + + (cmake "https://github.com/uyha/tree-sitter-cmake.git" + ,(arkta/treesit-cond + (14 "v0.7.1") + (13 "0.1.0"))) + + (cpp "https://github.com/tree-sitter/tree-sitter-cpp.git" + ,(arkta/treesit-cond + (14 "v0.23.4") + (13 "v0.19.0"))) + + (c-sharp "https://github.com/tree-sitter/tree-sitter-c-sharp.git" + ,(arkta/treesit-cond + (14 "v0.23.1") + (13 "v0.20.0"))) + + (css "https://github.com/tree-sitter/tree-sitter-css.git" + ,(arkta/treesit-cond + (15 "v0.25.0") + (14 "v0.23.2") + (13 "v0.19.0"))) + + (dockerfile "https://github.com/camdencheek/tree-sitter-dockerfile.git" + ,(arkta/treesit-cond + (14 "v0.2.0") + (13 "v0.1.2"))) + + (go "https://github.com/tree-sitter/tree-sitter-go.git" + ,(arkta/treesit-cond + (15 "v0.25.0") + (14 "v0.23.4") + (13 "v0.19.1"))) + + (gomod "https://github.com/camdencheek/tree-sitter-go-mod.git" + ,(arkta/treesit-cond + (14 "v1.1.0") + (13 "v1.0.0"))) + + (java "https://github.com/tree-sitter/tree-sitter-java.git" + ,(arkta/treesit-cond + (14 "v0.23.5") + (13 "v0.19.1"))) + + (javascript "https://github.com/tree-sitter/tree-sitter-javascript.git" + ,(arkta/treesit-cond + (15 "v0.25.0") + (14 "v0.23.1") + (13 "v0.19.0"))) + + (json "https://github.com/tree-sitter/tree-sitter-json.git" + ,(arkta/treesit-cond + (14 "v0.24.8") + (13 "v0.19.0"))) + + (kotlin "https://github.com/fwcd/tree-sitter-kotlin.git" + ,(arkta/treesit-cond + (14 "0.3.8") + (13 "0.2.11"))) + + (python "https://github.com/tree-sitter/tree-sitter-python.git" + ,(arkta/treesit-cond + (15 "v0.25.0") + (14 "v0.23.6") + (13 "v0.19.0"))) + + (ruby "https://github.com/tree-sitter/tree-sitter-ruby.git" + ,(arkta/treesit-cond + (14 "v0.23.1") + (13 "v0.19.0"))) + + (rust "https://github.com/tree-sitter/tree-sitter-rust.git" + ,(arkta/treesit-cond + (15 "v0.24.0") + (14 "v0.23.3") + (13 "v0.19.1"))) + + (toml "https://github.com/ikatyang/tree-sitter-toml.git" + ,(arkta/treesit-cond + (13 "v0.5.1"))) + + (tsx "https://github.com/tree-sitter/tree-sitter-typescript.git" + ,(arkta/treesit-cond + (14 "v0.23.2") + (13 "v0.19.0")) + "tsx/src") + (typescript "https://github.com/tree-sitter/tree-sitter-typescript.git" + ,(arkta/treesit-cond + (14 "v0.23.2") + (13 "v0.19.0")) + "typescript/src") + + (yaml "https://github.com/ikatyang/tree-sitter-yaml.git" + ,(arkta/treesit-cond + (13 "v0.5.0"))) + + ,@(when (arkta/treesit-supports 14) + `((typst "https://github.com/uben0/tree-sitter-typst.git" + ,(arkta/treesit-cond + (14 "v0.11.0"))))))) (arkta/treesit-ensure-languages)) -- cgit v1.2.3