Skip to content

Commit 81c5b67

Browse files
author
Yoshiki Takashima
authored
docs: EMACS setup instructions (rust-lang#1290)
* Added docs on setting up rust-analyzer for EMACS. * Wording fixes to the docs. * Added instructions for clippy linter. * Put back warning that was supposed to be in previous section.
1 parent 1d5bfa7 commit 81c5b67

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

docs/src/rustc-hacks.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,91 @@ rustc_interface = { path = "~/.rustup/toolchains/<toolchain>/lib/rustlib/rustc-s
5050

5151
**Don't forget to rollback the changes before you create your PR.**
5252

53+
### EMACS (with `use-package`)
54+
First, `Cargo.toml` and `rustup toolchain` steps are identical to VS
55+
Code. Install Rust-analyzer binary under `~/.cargo/bin/`.
56+
57+
On EMACS, add the following to your EMACS lisp files. They will
58+
install the necessary packages using the `use-package` manager.
59+
```elisp
60+
;; Install LSP
61+
(use-package lsp-mode
62+
:commands lsp)
63+
(use-package lsp-ui)
64+
65+
;; Install Rust mode
66+
(use-package toml-mode)
67+
(use-package rust-mode)
68+
69+
(setq lsp-rust-server 'rust-analyzer)
70+
(setenv "PATH" (concat (getenv "PATH") ":/home/USER/.cargo/bin/"))
71+
```
72+
If EMACS complains that it cannot find certain packages, try running
73+
`M-x package-refresh-contents`.
74+
75+
For LSP to be able to find `rustc_private` files used by Kani, you
76+
will need to modify variable `lsp-rust-analyzer-rustc-source`. Run
77+
`M-x customize-variable`, type in `lsp-rust-analyzer-rustc-source`,
78+
click `Value Menu` and change it to `Path`. Paste in the path to
79+
`Cargo.toml` of `rustc`'s source code. You can find the source code
80+
under `.rustup`, and the path should end with
81+
`compiler/rustc/Cargo.toml`. **Important**: make sure that this
82+
`rustc` is the same version and architecture as what Kani uses. If
83+
not, LSP features like definition lookup may be break.
84+
85+
This ends the basic install for EMACS. You can test your configuration
86+
with the following steps.
87+
1. Opening up a rust file with at least one `rustc_private` import.
88+
2. Activate LSP mode with `M-x lsp`.
89+
3. When asked about the root of the project, pick one of them. **Make
90+
sure** that whichever root you pick has a `Cargo.toml` with
91+
`rustc_private=true` added.
92+
4. If LSP asks if you want to watch all files, select yes. For less
93+
powerful machines, you may want to adjust that later.
94+
5. On the file with `rustc_private` imports, do the following. If both
95+
work, then you are set up.
96+
- Hover mouse over the `rustc_private` import. If LSP is working,
97+
you should get information about the imported item.
98+
- With text cursor over the same `rustc_private` import, run `M-x
99+
lsp-find-definition`. This should jump to the definition within
100+
`rustc`'s source code.
101+
102+
LSP mode can integrate with `flycheck` for instant error checking and
103+
`company` for auto-complete. Consider adding the following to the
104+
configuration.
105+
```elisp
106+
(use-package flycheck
107+
:hook (prog-mode . flycheck-mode))
108+
109+
(use-package company
110+
:hook (prog-mode . company-mode)
111+
:config
112+
(global-company-mode))
113+
```
114+
115+
`clippy` linter can be added by changing the LSP install to:
116+
```elisp
117+
(use-package lsp-mode
118+
:commands lsp
119+
:custom
120+
(lsp-rust-analyzer-cargo-watch-command "clippy"))
121+
```
122+
123+
Finally lsp-mode can run rust-analyzer via TRAMP for remote
124+
development. **We found this way of using rust-analyzer to be unstable
125+
as of 2022-06**. If you want to give it a try you will need to add a
126+
new LSP client for that remote with the following code.
127+
```elisp
128+
(lsp-register-client
129+
(make-lsp-client
130+
:new-connection (lsp-tramp-connection "/full/path/to/remote/machines/rust-analyzer")
131+
:major-modes '(rust-mode)
132+
:remote? t
133+
:server-id 'rust-analyzer-remote))
134+
```
135+
136+
For further details, please see https://emacs-lsp.github.io/lsp-mode/page/remote/.
137+
53138
## Custom `rustc`
54139

55140
There are a few reasons why you may want to use your own copy of `rustc`. E.g.:

0 commit comments

Comments
 (0)