Skip to content

Commit

Permalink
Fix inconsistencies and broken key bindings in the Rust layer (syl20b…
Browse files Browse the repository at this point in the history
…nr#16217)

* [rust] Resurrect rust-backend variable

rust-backend was removed but is still meaningful (needs to be set to
the symbol lsp in order for LSP support to be enabled).

This commit restores the variable to config.el and removes boundp
checks because it will always be bound now (except in layers.el which
is loaded before config.el).

* [rust] Fully remove support for unmaintained rls backend

* [rust] Bind SPC m c . to rustic-cargo-run-rerun

This command has different behavior than cargo-process-repeat (the
previous meaning of this binding), but it is still a reasonable
binding and is the most similar command that exists in the rustic
package.

* [rust] Fix wrong command name

* [rust] Remove wrapper commands for package cargo

This package was removed in the Rust layer overhaul, so these commands
are not used anywhere.  In fact, the reloading behavior cannot easily
be implemented with rustic, because rustic runs `cargo add` and the
like in a compilation-mode buffer rather than synchronously.

As a result, we also remove references to automatically reloading the
workspace in rust-analyzer and the corresponding config variable.

* [rust] Remove unnecessary post-init stage

Layer rust owns package rustic, so there is no need to use post-init.

* [rust] Ensure rustic-mode key bindings are always available

Using :after in use-package prevents the :init forms from being
evaluated until lsp-mode and flycheck are loaded.  However, if the
user has disabled lsp-mode or flycheck, they should still be able to
use the rustic-cargo-* key bindings, so :after is not appropriate
here.

Instead, directly use with-eval-after-load where necessary for the
lsp-mode- and flycheck-specific configuration.

* [rust] Add rustic-clippy as an available flycheck checker

This just uncomments some code that was already there.  Not sure why
it was commented in the first place.

* [rust] Remove redundant progn
  • Loading branch information
bcc32 authored Jan 6, 2024
1 parent 35cc1a6 commit d7d314a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 106 deletions.
23 changes: 9 additions & 14 deletions layers/+lang/rust/README.org
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
- [[#lsp][LSP]]
- [[#autocompletion][Autocompletion]]
- [[#debugger-dap-integration][Debugger (dap integration)]]
- [[#automatically-reload-workspace][Automatically Reload Workspace]]
- [[#reloading-workspace][Reloading Workspace]]
- [[#cargo][Cargo]]
- [[#cargo-edit][cargo-edit]]
- [[#cargo-outdated][cargo-outdated]]
Expand Down Expand Up @@ -45,20 +45,14 @@ To enable auto-completion, ensure that the =auto-completion= layer is enabled.
*** Debugger (dap integration)
To install the debug adapter you may run =M-x dap-gdb-lldb-setup= when you are on Linux or download it manually from [[https://marketplace.visualstudio.com/items?itemName=webfreak.debug][Native Debug]] and adjust =dap-gdb-lldb-path=.

*** Automatically Reload Workspace
When the LSP server is =rust-analyzer=, it may needs to reload the workspace to pickup changes made in =Cargo.toml=.
You can call =spacemacs/lsp-rust-analyzer-reload-workspace=, which would be faster than restarting the LSP backend.
*** Reloading Workspace
By default, rust-analyzer will automatically reload the workspace
after you make changes to =Cargo.toml= (such as adding dependencies).

You can also set =cargo-process-reload-on-modify= to =t=, then it will automatically reload the workspace after
one of the following is run:
- =rustic-cargo-add=
- =rustic-cargo-rm=
- =rustic-cargo-upgrade=

#+BEGIN_SRC elisp
(setq-default dotspacemacs-configuration-layers
'(lsp :variables cargo-process-reload-on-modify t))
#+END_SRC
However, if =lsp-rust-analyzer-cargo-auto-reload= is set to nil, you
will need to reload the workspace manually for the LSP to notice those
changes. You can call =spacemacs/lsp-rust-analyzer-reload-workspace=,
which would be faster than restarting the LSP backend.

** Cargo
[[http://doc.crates.io/index.html][Cargo]] is a project management command line tool for Rust. Installation
Expand Down Expand Up @@ -100,6 +94,7 @@ To enable automatic buffer formatting on save, set the variable =rustic-format-o
|-------------+--------------------------------------------------------|
| ~SPC m = =~ | reformat the buffer |
| ~SPC m b R~ | reload Rust-Analyzer workspace |
| ~SPC m c .~ | rerun the default binary with the same arguments |
| ~SPC m c =~ | format all project files with rustfmt |
| ~SPC m c a~ | add a new dependency with cargo-edit |
| ~SPC m c c~ | compile project |
Expand Down
9 changes: 7 additions & 2 deletions layers/+lang/rust/config.el
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,10 @@

(spacemacs|define-jump-handlers rustic-mode)

(defvar cargo-process-reload-on-modify nil
"When non-nil, reload workspace after a cargo-process command modifies Cargo.toml.")
(defvar rust-backend (and (configuration-layer/layer-used-p 'lsp) 'lsp)
"The backend to use for completion.
Currently, only `lsp' is supported. It is automatically used if
the `lsp' layer is enabled.
If `nil', LSP support is disabled.")
58 changes: 4 additions & 54 deletions layers/+lang/rust/funcs.el
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,13 @@
(message (concat "`lsp' layer is not installed, "
"please add `lsp' layer to your dotfile.")))

(defun spacemacs/lsp-rust-switch-server ()
"Switch between rust-analyzer and rls."
(interactive)
(lsp-rust-switch-server)
(call-interactively 'lsp-workspace-restart))

(defun spacemacs//rust-setup-lsp ()
"Setup lsp backend."
(if (configuration-layer/layer-used-p 'lsp)
(progn
(lsp-deferred)
(spacemacs/declare-prefix-for-mode 'rustic-mode "ms" "switch")
(spacemacs/set-leader-keys-for-major-mode 'rustic-mode
"ss" 'spacemacs/lsp-rust-switch-server
(if lsp-use-upstream-bindings "wR" "bR") 'spacemacs/lsp-rust-analyzer-reload-workspace))
(spacemacs//lsp-layer-not-installed-message)))

Expand All @@ -68,55 +61,12 @@
:dap-compilation-dir "${workspaceFolder}"
:cwd "${workspaceFolder}")))



(defun spacemacs/lsp-rust-analyzer-reload-workspace ()
"Reload workspaces to pick up changes in Cargo.toml.
Only applies to rust-analyzer, since rls automatically picks up changes already."
"Reload workspaces to pick up changes in Cargo.toml."
(interactive)
(if (member 'rust-analyzer (spacemacs//lsp-client-server-id))
(progn
(lsp-rust-analyzer-reload-workspace)
(message "Reloaded workspace"))
(message "RLS reloads automatically, and doesn't require an explicit reload")))

(when (configuration-layer/package-used-p 'cargo)
(defun spacemacs//cargo-maybe-reload ()
"Reload the workspace conditionally.
When one of the following is true, it won't reload:
- Backend is not rust-analyzer.
- `cargo-process-reload-on-modify' is nil."
(when (and cargo-process-reload-on-modify
(and (boundp 'rust-backend)
(eq rust-backend 'lsp))
(member 'rust-analyzer (spacemacs//lsp-client-server-id)))
(lsp-rust-analyzer-reload-workspace)))

(defun spacemacs/cargo-process-repeat ()
"Run last cargo process command, and conditionally reload the workspace."
(interactive)
(call-interactively 'cargo-process-repeat)
(when (member (car cargo-process-last-command)
'("Add" "Remove" "Upgrade"))
(spacemacs//cargo-maybe-reload)))

(defun spacemacs/cargo-process-add ()
"Run the cargo add command, and conditionally reload the workspace."
(interactive)
(call-interactively 'cargo-process-add)
(spacemacs//cargo-maybe-reload))

(defun spacemacs/cargo-process-rm()
"Run the cargo rm command, and conditionally reload the workspace."
(interactive)
(call-interactively 'cargo-process-rm)
(spacemacs//cargo-maybe-reload))

(defun spacemacs/cargo-process-upgrade()
"Run the cargo upgrade command, and conditionally reload the workspace."
(interactive)
(call-interactively 'cargo-process-upgrade)
(spacemacs//cargo-maybe-reload)))
(when (member 'rust-analyzer (spacemacs//lsp-client-server-id))
(lsp-rust-analyzer-reload-workspace)
(message "Reloaded workspace")))


;; Misc
Expand Down
69 changes: 33 additions & 36 deletions layers/+lang/rust/packages.el
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@
(spacemacs/counsel-gtags-define-keys-for-mode 'rustic-mode))

(defun rust/pre-init-dap-mode ()
(when (and (boundp 'rust-backend)
(eq rust-backend 'lsp))
(when (eq rust-backend 'lsp)
(add-to-list 'spacemacs--dap-supported-modes 'rustic-mode)
(add-hook 'rustic-mode-local-vars-hook #'spacemacs//rust-setup-dap)))

Expand All @@ -47,42 +46,43 @@
(defun rust/init-rustic ()
(use-package rustic
:defer t
:after (lsp-mode flycheck)
:mode ("\\.rs\\'" . rustic-mode)
:init
(progn
(add-hook 'rustic-mode-hook #'spacemacs//rust-setup-backend)
(add-hook 'rustic-mode-hook #'spacemacs//rust-setup-backend)

;; (push 'rustic-clippy flycheck-checkers)
(spacemacs/declare-prefix-for-mode 'rustic-mode "mc" "cargo")
(spacemacs/declare-prefix-for-mode 'rustic-mode "mt" "tests")
(spacemacs/declare-prefix-for-mode 'rustic-mode "mg" "goto")
(spacemacs/declare-prefix-for-mode 'rustic-mode "mh" "help")
(spacemacs/declare-prefix-for-mode 'rustic-mode "m=" "format")
(spacemacs/set-leader-keys-for-major-mode 'rustic-mode
"c." 'rustic-cargo-run-rerun
"c=" 'rustic-cargo-fmt
"ca" 'rustic-cargo-add
"cc" 'rustic-cargo-build
"cC" 'rustic-cargo-clean
"cd" 'rustic-cargo-doc
"cs" 'rustic-doc-search
"ce" 'rustic-cargo-bench
"ci" 'rustic-cargo-init
"cl" 'rustic-cargo-clippy
"cf" 'rustic-cargo-clippy-fix
"cn" 'rustic-cargo-new
"co" 'rustic-cargo-outdated
"cr" 'rustic-cargo-rm
"cu" 'rustic-cargo-update
"cU" 'rustic-cargo-upgrade
"cv" 'rustic-cargo-check
"cx" 'rustic-cargo-run
"ta" 'rustic-cargo-test
"tt" 'rustic-cargo-current-test)

(spacemacs/declare-prefix-for-mode 'rustic-mode "mc" "cargo")
(spacemacs/declare-prefix-for-mode 'rustic-mode "mt" "tests")
(spacemacs/declare-prefix-for-mode 'rustic-mode "mg" "goto")
(spacemacs/declare-prefix-for-mode 'rustic-mode "mh" "help")
(spacemacs/declare-prefix-for-mode 'rustic-mode "m=" "format")
(spacemacs/set-leader-keys-for-major-mode 'rustic-mode
;; Deactivated for now see https://github.com/syl20bnr/spacemacs/issues/16203 for details
;; "c." 'spacemacs/rustic-cargo-repeat
"c=" 'rustic-cargo-fmt
"ca" 'rustic-cargo-add
"cc" 'rustic-cargo-build
"cC" 'rustic-cargo-clean
"cd" 'rustic-cargo-doc
"cs" 'rustic-cargo-doc-search
"ce" 'rustic-cargo-bench
"ci" 'rustic-cargo-init
"cl" 'rustic-cargo-clippy
"cf" 'rustic-cargo-clippy-fix
"cn" 'rustic-cargo-new
"co" 'rustic-cargo-outdated
"cr" 'spacemacs/rustic-cargo-rm
"cu" 'rustic-cargo-update
"cU" 'rustic-cargo-upgrade
"cv" 'rustic-cargo-check
"cx" 'rustic-cargo-run
"ta" 'rustic-cargo-test
"tt" 'rustic-cargo-current-test
(with-eval-after-load 'flycheck
(spacemacs/enable-flycheck 'rustic-mode)
(push 'rustic-clippy flycheck-checkers))

(with-eval-after-load 'lsp-mode
(spacemacs/set-leader-keys-for-major-mode 'rustic-mode
"=j" 'lsp-rust-analyzer-join-lines
"==" 'lsp-format-buffer
"Ti" 'lsp-inlay-hints-mode
Expand All @@ -97,9 +97,6 @@
"," 'lsp-rust-analyzer-rerun
"." 'lsp-rust-analyzer-run))))

(defun rust/post-init-rustic ()
(spacemacs/enable-flycheck 'rustic-mode))

(defun rust/post-init-smartparens ()
(with-eval-after-load 'smartparens
;; Don't pair lifetime specifiers
Expand Down

0 comments on commit d7d314a

Please sign in to comment.