summaryrefslogtreecommitdiff
path: root/.config/emacs/modules/mm-treesit.el
diff options
context:
space:
mode:
Diffstat (limited to '.config/emacs/modules/mm-treesit.el')
-rw-r--r--.config/emacs/modules/mm-treesit.el130
1 files changed, 82 insertions, 48 deletions
diff --git a/.config/emacs/modules/mm-treesit.el b/.config/emacs/modules/mm-treesit.el
index df3d85a..1d4defa 100644
--- a/.config/emacs/modules/mm-treesit.el
+++ b/.config/emacs/modules/mm-treesit.el
@@ -15,49 +15,65 @@
(setopt treesit-font-lock-level 4)
(setopt treesit-language-source-alist
- '((c "https://github.com/tree-sitter/tree-sitter-c")
- (cpp "https://github.com/tree-sitter/tree-sitter-cpp")
- (css "https://github.com/tree-sitter/tree-sitter-css")
- (go "https://github.com/tree-sitter/tree-sitter-go")
- (gomod "https://github.com/camdencheek/tree-sitter-go-mod")
- (gsp "git://git.thomasvoss.com/tree-sitter-gsp.git")
- (html "https://github.com/tree-sitter/tree-sitter-html")
- (javascript "https://github.com/tree-sitter/tree-sitter-javascript")
- (python "https://github.com/tree-sitter/tree-sitter-python")
- (typescript "https://github.com/tree-sitter/tree-sitter-typescript"
- "master" "typescript/src")
- (vim "https://github.com/tree-sitter-grammars/tree-sitter-vim")
- (vue "https://github.com/ikatyang/tree-sitter-vue")))
+ '((awk
+ "https://github.com/Beaglefoot/tree-sitter-awk")
+ (c
+ "https://github.com/tree-sitter/tree-sitter-c")
+ (cpp
+ "https://github.com/tree-sitter/tree-sitter-cpp")
+ (css
+ "https://github.com/tree-sitter/tree-sitter-css")
+ (go
+ "https://github.com/tree-sitter/tree-sitter-go")
+ (gomod
+ "https://github.com/camdencheek/tree-sitter-go-mod")
+ (gsp
+ "git://git.thomasvoss.com/tree-sitter-gsp.git")
+ (html
+ "https://github.com/tree-sitter/tree-sitter-html")
+ (java
+ "https://github.com/tree-sitter/tree-sitter-java")
+ (javascript
+ "https://github.com/tree-sitter/tree-sitter-javascript")
+ (markdown
+ "https://github.com/tree-sitter-grammars/tree-sitter-markdown"
+ "split_parser" "tree-sitter-markdown/src")
+ (markdown-inline
+ "https://github.com/tree-sitter-grammars/tree-sitter-markdown"
+ "split_parser" "tree-sitter-markdown-inline/src")
+ (python
+ "https://github.com/tree-sitter/tree-sitter-python")
+ (tsx
+ "https://github.com/tree-sitter/tree-sitter-typescript"
+ "master" "tsx/src")
+ (typescript
+ "https://github.com/tree-sitter/tree-sitter-typescript"
+ "master" "typescript/src")
+ (vim
+ "https://github.com/tree-sitter-grammars/tree-sitter-vim")
+ (vue
+ "https://github.com/ikatyang/tree-sitter-vue")))
;;; Install Missing Parsers
-(defun mm-treesit-sync-sources ()
- "Sync Tree-Sitter parsers.
-Reinstall the Tree-Sitter parsers specified by
- `treesit-language-source-alist'."
+(defun mm-treesit-install-all ()
+ "Install all Tree-Sitter parsers.
+This is like `mm-treesit-install-missing' but also reinstalls parsers
+that are already installed."
(interactive)
- (let ((total (length treesit-language-source-alist))
- (count 0)
- (work treesit-language-source-alist)
- (processors-to-use (max 1 (1- (num-processors)))))
- (while work
- (let ((specs (seq-take work processors-to-use)))
- (dolist (spec specs)
- (async-start
- `(lambda ()
- ,(async-inject-variables "\\`treesit-language-source-alist\\'")
- (treesit-install-language-grammar ',(car spec)))
- (lambda (_)
- (setq count (1+ count))
- (message "Done syncing Tree-Sitter grammar for `%s' [%d/%d]"
- (car spec) count total))))
- (setq work (seq-drop work processors-to-use))))))
-
-(thread-last
- (mapcar #'car treesit-language-source-alist)
- (seq-remove #'treesit-language-available-p)
- (mapc #'treesit-install-language-grammar))
+ (cl-loop for (lang) in treesit-language-source-alist
+ do (treesit-install-language-grammar lang)))
+
+(defun mm-treesit-install-missing ()
+ "Install missing Tree-Sitter parsers.
+The parsers are taken from `treesit-language-source-alist'."
+ (interactive)
+ (cl-loop for (lang) in treesit-language-source-alist
+ unless (treesit-language-available-p lang)
+ do (treesit-install-language-grammar lang)))
+
+(mm-treesit-install-missing)
;;; Install Additional TS Modes
@@ -79,13 +95,18 @@ Reinstall the Tree-Sitter parsers specified by
;; NOTE: This package doesn’t autoload its ‘auto-mode-alist’ entries
(use-package vue-ts-mode
- :vc (:url "https://github.com/8uff3r/vue-ts-mode.git"
- :branch "main"
- :rev :newest
- :vc-backend Git)
+ :vc ( :url "https://github.com/8uff3r/vue-ts-mode.git"
+ :branch "main"
+ :rev :newest
+ :vc-backend Git)
:ensure t
:mode "\\.vue\\'")
+;; NOTE: This package doesn’t autoload its ‘auto-mode-alist’ entries
+(use-package markdown-ts-mode
+ :ensure t
+ :mode "\\.md\\'")
+
;;; Prefer Tree-Sitter Modes
@@ -94,28 +115,38 @@ Reinstall the Tree-Sitter parsers specified by
;; anyway. Same goes for ‘typescript-ts-mode’.
(defvar mm-treesit-language-file-name-alist
'((go . "\\.go\\'")
- (go-mod . "/go\\.mod\\'")
+ (gomod . "/go\\.mod\\'")
+ (tsx . "\\.tsx\\'")
(typescript . "\\.ts\\'"))
"Alist mapping languages to their associated file-names.
This alist is a set of pairs of the form (LANG . REGEXP) where LANG is
-the symbol corresponding to a major mode with the ‘-ts-mode’ suffix
+the symbol corresponding to a major mode with the `-ts-mode' suffix
removed. REGEXP is a regular expression matching filenames for which
the associated language’s major-mode should be enabled.
This alist is used to configure `auto-mode-alist'.")
+(defvar mm-treesit-dont-have-modes
+ '(markdown-inline)
+ "List of languages that don't have modes.
+Some languages may come with multiple parsers, (e.g. `markdown' and
+`markdown-inline') and as a result one-or-more of the parsers won't be
+associated with a mode. To avoid breaking the configuration, these
+languages should be listed here.")
+
(dolist (spec treesit-language-source-alist)
(let* ((lang (car spec))
- (lang (alist-get lang mm-treesit-language-remap-alist lang))
- (symbol-name (symbol-name lang))
- (name-mode (intern (concat symbol-name "-mode")))
- (name-ts-mode (intern (concat symbol-name "-ts-mode"))))
+ (lang-remap (alist-get lang mm-treesit-language-remap-alist lang))
+ (name-mode (intern (format "%s-mode" lang-remap)))
+ (name-ts-mode (intern (format "%s-ts-mode" lang-remap))))
;; If ‘name-ts-mode’ is already in ‘auto-mode-alist’ then we don’t
;; need to do anything, however if that’s not the case then if
;; ‘name-ts-mode’ and ‘name-mode’ are both bound we do a simple
;; remap. If the above is not true then we lookup the extensions in
;; ‘mm-treesit-language-file-name-alist’.
(cond
+ ((memq lang mm-treesit-dont-have-modes)
+ nil)
((not (fboundp name-ts-mode))
(warn "`%s' is missing." name-ts-mode))
((rassq name-ts-mode auto-mode-alist)
@@ -128,6 +159,9 @@ This alist is used to configure `auto-mode-alist'.")
(add-to-list 'auto-mode-alist (cons file-regexp name-ts-mode))
(warn "Unable to determine the extension for `%s'." name-ts-mode))))))
+;; JavaScript being difficult as usual
+(add-to-list 'major-mode-remap-alist '(javascript-mode . js-ts-mode))
+
;;; Hack For C23