From f4693a048acd3dbaa287d23e2fb27681c18bed9e Mon Sep 17 00:00:00 2001 From: Dimitri Lozeve Date: Fri, 23 Jun 2023 14:20:24 +0200 Subject: [PATCH] Automatic tree-sitter setup Install grammars automatically and use tree-sitter modes by default --- init.el | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/init.el b/init.el index 29af8d3..5ca555e 100644 --- a/init.el +++ b/init.el @@ -548,6 +548,41 @@ ;;; Programming tools ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +(use-package treesit + :straight nil + :preface + (defun dl/setup-install-grammars () + "Install Tree-sitter grammars if they are absent." + (interactive) + (dolist (grammar + '((c "https://github.com/tree-sitter/tree-sitter-c") + (json "https://github.com/tree-sitter/tree-sitter-json") + (python "https://github.com/tree-sitter/tree-sitter-python") + (rust "https://github.com/tree-sitter/tree-sitter-rust") + (toml "https://github.com/tree-sitter/tree-sitter-toml") + (yaml "https://github.com/ikatyang/tree-sitter-yaml"))) + (add-to-list 'treesit-language-source-alist grammar) + ;; Only install `grammar' if we don't already have it + ;; installed. However, if you want to *update* a grammar then + ;; this obviously prevents that from happening. + (unless (treesit-language-available-p (car grammar)) + (treesit-install-language-grammar (car grammar))))) + ;; Optional, but recommended. Tree-sitter enabled major modes are + ;; distinct from their ordinary counterparts. + ;; + ;; You can remap major modes with `major-mode-remap-alist'. Note + ;; that this does *not* extend to hooks! Make sure you migrate them + ;; also + (dolist (mapping '((c-mode . c-ts-mode) + (json-mode . json-ts-mode) + (python-mode . python-ts-mode) + (rust-mode . rust-ts-mode) + (toml-mode . toml-ts-mode) + (yaml-mode . yaml-ts-mode))) + (add-to-list 'major-mode-remap-alist mapping)) + :config + (dl/setup-install-grammars)) + (use-package expand-region :straight t :bind ("C-=" . er/expand-region))