fix(rust): unify under rustic-mode

My mistake: I'd overlooked that rustic-mode already provides treesit
support, so we don't need all this complexity, we can simply use
rustic-mode. This indirectly fixes the recent regressions around
localleader keybinds and LSP activation.

Fix: #8473
Amend: 086a0d30d0
This commit is contained in:
Henrik Lissner
2025-08-31 21:48:37 +02:00
parent b2e4e8e27c
commit 5ce2a29bf7

View File

@@ -17,7 +17,6 @@
(use-package! rust-mode (use-package! rust-mode
:mode ("\\.rs\\'" . rust-mode)
:defer t :defer t
:preface :preface
;; Ensure rust-mode derives from rust-ts-mode, b/c rustic-mode derives from ;; Ensure rust-mode derives from rust-ts-mode, b/c rustic-mode derives from
@@ -33,26 +32,10 @@
(require 'rustic-mode nil t))) (require 'rustic-mode nil t)))
(use-package! rust-ts-mode ; 29.1+ only
:when (modulep! +tree-sitter)
:defer t
:init
(set-tree-sitter! 'rust-mode 'rust-ts-mode
`((rust :url "https://github.com/tree-sitter/tree-sitter-rust"
:commit ,(if (and (treesit-available-p)
(< (treesit-library-abi-version) 15))
"1f63b33efee17e833e0ea29266dd3d713e27e321"
"18b0515fca567f5a10aee9978c6d2640e878671a"))))
(add-to-list 'major-mode-remap-defaults '(rust-mode . rust-ts-mode) t))
(use-package! rustic (use-package! rustic
:mode ("\\.rs\\'" . rustic-mode)
:defer t :defer t
:preface :preface
(unless (assq 'rust-mode major-mode-remap-defaults)
(add-to-list 'major-mode-remap-defaults '(rust-mode . rustic-mode)))
;; HACK `rustic' sets up some things too early. I'd rather disable it and let ;; HACK `rustic' sets up some things too early. I'd rather disable it and let
;; our respective modules standardize how they're initialized. ;; our respective modules standardize how they're initialized.
(setq rustic-lsp-client nil) (setq rustic-lsp-client nil)
@@ -64,6 +47,14 @@
(remove-hook 'flycheck-mode-hook #'rustic-flycheck-setup)) (remove-hook 'flycheck-mode-hook #'rustic-flycheck-setup))
:init :init
(when (modulep! +tree-sitter)
(set-tree-sitter! 'rust-mode 'rustic-mode
`((rust :url "https://github.com/tree-sitter/tree-sitter-rust"
:commit ,(if (and (treesit-available-p)
(< (treesit-library-abi-version) 15))
"1f63b33efee17e833e0ea29266dd3d713e27e321"
"18b0515fca567f5a10aee9978c6d2640e878671a")))))
;; HACK Certainly, `rustic-babel' does this, but the package (and many other ;; HACK Certainly, `rustic-babel' does this, but the package (and many other
;; rustic packages) must be loaded in order for them to take effect. To lazy ;; rustic packages) must be loaded in order for them to take effect. To lazy
;; load it all, it must be done earlier: ;; load it all, it must be done earlier:
@@ -89,34 +80,35 @@
'lsp-mode)) 'lsp-mode))
(add-hook 'rustic-mode-local-vars-hook #'rustic-setup-lsp 'append) (add-hook 'rustic-mode-local-vars-hook #'rustic-setup-lsp 'append)
;; HACK: Add @scturtle fix for signatures on hover on LSP mode. This code (when (modulep! :tools lsp -eglot)
;; has not been upstreamed because it depends on the exact format of the ;; HACK: Add @scturtle fix for signatures on hover on LSP mode. This code
;; response of Rust Analyzer, which is not stable enough for `lsp-mode' ;; has not been upstreamed because it depends on the exact format of the
;; maintainers (see emacs-lsp/lsp-mode#1740). ;; response of Rust Analyzer, which is not stable enough for `lsp-mode'
(unless (modulep! :tools lsp +eglot) ;; maintainers (see emacs-lsp/lsp-mode#1740).
(defadvice! +rust--dont-cache-results-from-ra-a (&rest _) (defadvice! +rust--dont-cache-results-from-ra-a (&rest _)
:after #'lsp-eldoc-function :after #'lsp-eldoc-function
(when (derived-mode-p 'rust-mode 'rust-ts-mode) (when (derived-mode-p 'rust-mode 'rust-ts-mode)
(setq lsp--hover-saved-bounds nil))) (setq lsp--hover-saved-bounds nil)))
;; extract and show short signature for rust-analyzer ;; Extract and show short signature for rust-analyzer.
(cl-defmethod lsp-clients-extract-signature-on-hover (contents (_server-id (eql rust-analyzer))) (after! lsp-rust
(let* ((value (if lsp-use-plists (plist-get contents :value) (gethash "value" contents))) (cl-defmethod lsp-clients-extract-signature-on-hover (contents (_server-id (eql rust-analyzer)))
(groups (--partition-by (s-blank? it) (s-lines (s-trim value)))) (let* ((value (if lsp-use-plists (plist-get contents :value) (gethash "value" contents)))
(mod-group (cond ((s-equals? "```rust" (car (-fifth-item groups))) (-third-item groups)) (groups (--partition-by (s-blank? it) (s-lines (s-trim value))))
((s-equals? "```rust" (car (-third-item groups))) (-first-item groups)) (mod-group (cond ((s-equals? "```rust" (car (-fifth-item groups))) (-third-item groups))
(t nil))) ((s-equals? "```rust" (car (-third-item groups))) (-first-item groups))
(cmt (if (null mod-group) "" (concat " // " (cadr mod-group)))) (t nil)))
(sig-group (cond ((s-equals? "```rust" (car (-fifth-item groups))) (-fifth-item groups)) (cmt (if (null mod-group) "" (concat " // " (cadr mod-group))))
((s-equals? "```rust" (car (-third-item groups))) (-third-item groups)) (sig-group (cond ((s-equals? "```rust" (car (-fifth-item groups))) (-fifth-item groups))
(t (-first-item groups)))) ((s-equals? "```rust" (car (-third-item groups))) (-third-item groups))
(sig (->> sig-group (t (-first-item groups))))
(--drop-while (s-equals? "```rust" it)) (sig (->> sig-group
(--take-while (not (s-equals? "```" it))) (--drop-while (s-equals? "```rust" it))
(--map (s-replace-regexp "//.*" "" it)) (--take-while (not (s-equals? "```" it)))
(--map (s-trim it)) (--map (s-replace-regexp "//.*" "" it))
(s-join " ")))) (--map (s-trim it))
(lsp--render-element (concat "```rust\n" sig cmt "\n```")))))) (s-join " "))))
(lsp--render-element (concat "```rust\n" sig cmt "\n```")))))))
;; HACK If lsp/eglot isn't available, it attempts to install lsp-mode via ;; HACK If lsp/eglot isn't available, it attempts to install lsp-mode via
;; package.el. Doom manages its own dependencies through straight so disable ;; package.el. Doom manages its own dependencies through straight so disable