From 4a7824f8648548bd87b46b2cabc7a60f3b1f7ab9 Mon Sep 17 00:00:00 2001 From: John Turner Date: Tue, 24 Jan 2023 00:37:25 -0500 Subject: [PATCH] added some treesitter setup stuff --- init.el | 2 + .../init-treesit-setup-available-languages.el | 55 +++++++++++++++++++ init/treesit/init-treesit.el | 3 + 3 files changed, 60 insertions(+) create mode 100644 init/treesit/init-treesit-setup-available-languages.el create mode 100644 init/treesit/init-treesit.el diff --git a/init.el b/init.el index 2140fdd..e8fb9d2 100644 --- a/init.el +++ b/init.el @@ -80,6 +80,8 @@ (when (require? 'pixel-scroll) (require 'init-pixel-scroll)) +(when (and (require? 'treesit) (treesit-available-p)) + (require 'init-treesit)) (require 'man-completion) (require 'license-boilerplate) diff --git a/init/treesit/init-treesit-setup-available-languages.el b/init/treesit/init-treesit-setup-available-languages.el new file mode 100644 index 0000000..5817e15 --- /dev/null +++ b/init/treesit/init-treesit-setup-available-languages.el @@ -0,0 +1,55 @@ +;; -*- lexical-binding: t; -*- + +(defvar -languages `((python-ts-mode + ((:langs . python) + (:file-ext . ,(rx ".py")) + (:interpreter . "python"))) + (rust-ts-mode + ((:langs . rust) + (:file-ext . ,(rx ".rs")))) + (bash-ts-mode + ((:langs . bash) + (:file-ext . ,(rx ".sh")) + (:interpreter . "bash"))) + (c-ts-mode + ((:langs . c) + (:file-ext . ,(rx ".c")))) + (c++-ts-mode + ((:langs . c++) + (:file-ext . ,(rx (or ".c++" ".cpp" ".cxx" + ".h++" ".hpp" ".hxx"))))) + (c-or-c++-ts-mode + ((:langs . (c c++)) + (:file-ext . ,(rx ".h")))) + (toml-ts-mode + ((:langs . toml) + (:file-ext . ,(rx ".toml")))))) + +(defun -ready-quiet-p (language) + (treesit-ready-p language t)) + +(cl-defun -load-language? (&key ts-mode + required-languages + file-extension + interpreter?) + (let ((ready? (if (listp required-languages) + (seq-every-p '-ready-quiet-p required-languages) + (-ready-quiet-p required-languages)))) + (when ready? + (add-to-list 'auto-mode-alist (cons file-extension ts-mode)) + (when interpreter? + (add-to-list 'interpreter-mode-alist (cons interpreter? ts-mode)))))) + +(seq-map (lambda (item) + (-load-language? + :ts-mode (car item) + :required-languages (alist-get :langs (cadr item)) + :file-extension (alist-get :file-ext (cadr item)) + :interpreter? (alist-get :interpreter (cadr item)))) + -languages) + +(provide 'init-treesit-setup-available-languages) + +;; Local Variables: +;; read-symbol-shorthands: (("-" . "init-treesit-setup-available-languages-")) +;; End: diff --git a/init/treesit/init-treesit.el b/init/treesit/init-treesit.el new file mode 100644 index 0000000..29ea075 --- /dev/null +++ b/init/treesit/init-treesit.el @@ -0,0 +1,3 @@ +(require 'init-treesit-setup-available-languages) + +(provide 'init-treesit)