feat(scala): add treesit support

This commit is contained in:
Henrik Lissner
2025-05-22 19:32:30 +02:00
parent 5db361b474
commit c5dd2847ff
5 changed files with 52 additions and 22 deletions

View File

@ -30,11 +30,12 @@ Through the power of [[https://scalameta.org/metals/docs/editors/overview.html][
(supports metals). (supports metals).
- +tree-sitter :: - +tree-sitter ::
Leverages tree-sitter for better syntax highlighting and structural text Leverages tree-sitter for better syntax highlighting and structural text
editing. Requires [[doom-module::tools tree-sitter]]. editing. Requires Emacs 29.1+ and [[doom-module::tools tree-sitter]].
** Packages ** Packages
- [[doom-package:sbt-mode]] - [[doom-package:sbt-mode]]
- [[doom-package:scala-mode]] - [[doom-package:scala-mode]]
- [[doom-package:scala-ts-mode]] if [[doom-module:+tree-sitter]]
- [[doom-package:lsp-metals]] if [[doom-module:+lsp]] - [[doom-package:lsp-metals]] if [[doom-module:+lsp]]
** Hacks ** Hacks

View File

@ -7,24 +7,12 @@
;; ;;
;;; Packages ;;; Packages
(after! scala-mode (defun +scala-common-config (mode)
(setq scala-indent:align-parameters t (set-formatter! 'scalafmt '("scalafmt" "--stdin")
;; indent block comments to first asterix, not second :modes (list mode))
scala-indent:use-javadoc-style t) (set-repl-handler! mode #'+scala/open-repl
:persist t)
(setq-hook! 'scala-mode-hook (set-ligatures! mode
comment-line-break-function #'+scala-comment-indent-new-line-fn)
(when (modulep! +lsp)
(setq-hook! 'scala-mode-hook lsp-enable-indentation nil)
(add-hook 'scala-mode-local-vars-hook #'lsp! 'append))
(when (modulep! +tree-sitter)
(add-hook 'scala-mode-local-vars-hook #'tree-sitter! 'append))
(set-formatter! 'scalafmt '("scalafmt" "--stdin") :modes '(scala-mode))
(set-ligatures! 'scala-mode
;; Functional ;; Functional
:def "def" :def "def"
:composition "compose" :composition "compose"
@ -49,9 +37,43 @@
;; Other ;; Other
:union "union" :union "union"
:intersect "intersect" :intersect "intersect"
:diff "diff")) :diff "diff")
(when (modulep! +lsp)
(add-hook (intern (format "%s-local-vars-hook" mode)) #'lsp! 'append)))
(after! scala-mode
(setq scala-indent:align-parameters t
;; indent block comments to first asterix, not second
scala-indent:use-javadoc-style t)
(setq-hook! 'scala-mode-hook
comment-line-break-function #'+scala-comment-indent-new-line-fn
lsp-enable-indentation nil)
(+scala-common-config 'scala-mode))
(use-package! scala-ts-mode
:when (modulep! +tree-sitter)
:when (fboundp 'treesit-available-p)
:defer t
:init
(set-tree-sitter! 'scala-mode 'scala-ts-mode
'((scala :url "https://github.com/tree-sitter/tree-sitter-scala")))
:config
(when (modulep! +lsp)
(setq-hook! 'scala-ts-mode-hook lsp-enable-indentation nil)
(add-hook 'scala-ts-mode-local-vars-hook #'lsp! 'append))
;; HACK: Rely on `major-mode-remap-defaults'.
(cl-callf2 assq-delete-all 'scala-ts-mode auto-mode-alist)
(+scala-common-config 'scala-ts-mode))
(use-package! sbt-mode (use-package! sbt-mode
:after scala-mode :after scala-mode
:config (set-repl-handler! 'scala-mode #'+scala/open-repl :persist t)) :config (set-repl-handler! '(scala-mode scala-ts-mode) #'+scala/open-repl :persist t))

View File

@ -8,6 +8,10 @@
(modulep! :tools tree-sitter)) (modulep! :tools tree-sitter))
"This module requires (:tools tree-sitter)") "This module requires (:tools tree-sitter)")
(assert! (or (not (modulep! +tree-sitter))
(version< emacs-version "29.1"))
"Emacs 29.1+ is required for tree-sitter support")
(if (and (modulep! +lsp) (if (and (modulep! +lsp)
(not (executable-find "metals"))) (not (executable-find "metals")))
(warn! "metals isn't installed")) (warn! "metals isn't installed"))

View File

@ -4,6 +4,10 @@
(package! sbt-mode :pin "cc68728a6ef0600aad369157b3a2d0ce56afba9b") (package! sbt-mode :pin "cc68728a6ef0600aad369157b3a2d0ce56afba9b")
(package! scala-mode :pin "661337d8aa0a0cb418184c83757661603de3b2e3") (package! scala-mode :pin "661337d8aa0a0cb418184c83757661603de3b2e3")
(when (and (modulep! +tree-sitter)
(fboundp 'treesit-available-p))
(package! scala-ts-mode :pin "c7671e10419261ef70b1820d3b970ad39f6fcfe2"))
(when (and (modulep! +lsp) (when (and (modulep! +lsp)
(modulep! :tools lsp -eglot)) (modulep! :tools lsp -eglot))
(package! lsp-metals :pin "e1d9d04f3bab7e6e74916054b36ab1a87e831367")) (package! lsp-metals :pin "e1d9d04f3bab7e6e74916054b36ab1a87e831367"))

View File

@ -42,7 +42,6 @@
(proto "https://github.com/mitchellh/tree-sitter-proto" nil nil nil nil) (proto "https://github.com/mitchellh/tree-sitter-proto" nil nil nil nil)
(r "https://github.com/r-lib/tree-sitter-r" nil nil nil nil) (r "https://github.com/r-lib/tree-sitter-r" nil nil nil nil)
(rust "https://github.com/tree-sitter/tree-sitter-rust" nil nil nil nil) (rust "https://github.com/tree-sitter/tree-sitter-rust" nil nil nil nil)
(scala "https://github.com/tree-sitter/tree-sitter-scala" nil nil nil nil)
(sql "https://github.com/DerekStride/tree-sitter-sql" "gh-pages" nil nil nil) (sql "https://github.com/DerekStride/tree-sitter-sql" "gh-pages" nil nil nil)
(surface "https://github.com/connorlay/tree-sitter-surface" nil nil nil nil) (surface "https://github.com/connorlay/tree-sitter-surface" nil nil nil nil)
(toml "https://github.com/tree-sitter/tree-sitter-toml" nil nil nil nil) (toml "https://github.com/tree-sitter/tree-sitter-toml" nil nil nil nil)