diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2024-11-21 15:25:03 +0100 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2025-03-17 11:09:49 +0100 |
| commit | 8de51ae845f067dcebedab5cc2d66c7e05e8aca9 (patch) | |
| tree | 89bd9e42f034e9713fbcf227f8275221c1afc4fb | |
| parent | 7604c5aeef469e51e8dd19a514b883e415f462fb (diff) | |
added own tree-sitter configuration
| -rw-r--r-- | config/emacs/init.el | 1 | ||||
| -rw-r--r-- | config/emacs/tree-sitter.el | 44 |
2 files changed, 45 insertions, 0 deletions
diff --git a/config/emacs/init.el b/config/emacs/init.el index 9de677e..ac89b44 100644 --- a/config/emacs/init.el +++ b/config/emacs/init.el @@ -3,6 +3,7 @@ ;;; TODO(nlordell): ;; 1. Update remaining init scripts: ;; (load "~/.config/emacs/gtd.el" nil t) +;; (load "~/.config/emacs/tree-sitter.el" nil t) ;; 2. Emacs over devcontainers/docker with TRAMP ;; <https://happihacking.com/blog/posts/2023/dev-containers-emacs/> ;; 3. Investigate other potential sane Emacs defaults diff --git a/config/emacs/tree-sitter.el b/config/emacs/tree-sitter.el new file mode 100644 index 0000000..402d15c --- /dev/null +++ b/config/emacs/tree-sitter.el @@ -0,0 +1,44 @@ +(require 'treesit) + +(setq + tree-sitter-install-dir "~/.cache/emacs/tree-sitter" + tree-sitter-languages '((rust :version "v0.23.1") + (ocaml :version "v0.23.2" + :grammars ((ocaml :src "grammars/ocaml/src") + (ocaml_interface :src "grammars/interface/src") + (ocaml_type :src "grammars/type/src"))))) + +(defun tree-sitter--install-language (lang) + "Installs tree-sitter language grammars for LANG." + (let* ((lconf (or (alist-get lang tree-sitter-languages) + (error "unknown language %s" lang))) + (url (or (plist-get lconf :url) + (format "https://github.com/tree-sitter/tree-sitter-%s" lang))) + (version (plist-get lconf :version)) + (grammars (or (plist-get lconf :grammars) + `((,lang))))) + (cl-loop for (grammar . gconf) in grammars do + (let* ((src (or (plist-get gconf :src) "src")) + (recipe (list "~/.cache/emacs/tree-sitter" + grammar url version src))) + (apply #'treesit--install-language-grammar-1 recipe))))) + +(defun tree-sitter--major-mode-remaps (lang) + "Gets the remap configuration for LANG or `nil' when not available" + (let* ((lconf (or (alist-get lang tree-sitter-languages) + (error "unknown language %s" lang))) + (remaps (plist-get lconf :remaps))) + (if (treesit-language-available-p lang) remaps nil))) + +(defun use-tree-sitter-language (lang) + "Installs and configures tree-sitter language grammars for LANG." + (unless (treesit-language-available-p lang) + (tree-sitter--install-language lang) + (let (remaps (tree-sitter--major-mode-remaps lang)) + (append major-mode-remap-alist remaps)))) + +(setq + treesit-extra-load-path `(,tree-sitter-install-dir) + major-mode-remap-alist (mapcan (lambda (lang) + (tree-sitter--major-mode-remaps (car lang))) + tree-sitter-languages)) |
