diff options
Diffstat (limited to 'docs/user/manual.adoc')
-rw-r--r-- | docs/user/manual.adoc | 282 |
1 files changed, 282 insertions, 0 deletions
diff --git a/docs/user/manual.adoc b/docs/user/manual.adoc new file mode 100644 index 000000000..202783fd9 --- /dev/null +++ b/docs/user/manual.adoc | |||
@@ -0,0 +1,282 @@ | |||
1 | = User Manual | ||
2 | :toc: preamble | ||
3 | :sectanchors: | ||
4 | :page-layout: post | ||
5 | :icons: font | ||
6 | :source-highlighter: rouge | ||
7 | :experimental: | ||
8 | |||
9 | // Master copy of this document lives in the https://github.com/rust-analyzer/rust-analyzer repository | ||
10 | |||
11 | At its core, rust-analyzer is a *library* for semantic analysis of Rust code as it changes over time. | ||
12 | This manual focuses on a specific usage of the library -- running it as part of a server that implements the | ||
13 | https://microsoft.github.io/language-server-protocol/[Language Server Protocol] (LSP). | ||
14 | The LSP allows various code editors, like VS Code, Emacs or Vim, to implement semantic features like completion or goto definition by talking to an external language server process. | ||
15 | |||
16 | To improve this document, send a pull request against | ||
17 | https://github.com/rust-analyzer/rust-analyzer/blob/master/docs/user/manual.adoc[this file]. | ||
18 | |||
19 | If you have questions about using rust-analyzer, please ask them in the https://users.rust-lang.org/c/ide/14["`IDEs and Editors`"] topic of Rust users forum. | ||
20 | |||
21 | == Installation | ||
22 | |||
23 | In theory, one should be able to just install the <<rust-analyzer-language-server-binary,`rust-analyzer` binary>> and have it automatically work with any editor. | ||
24 | We are not there yet, so some editor specific setup is required. | ||
25 | |||
26 | Additionally, rust-analyzer needs the sources of the standard library. | ||
27 | If the source code is not present, rust-analyzer will attempt to install it automatically. | ||
28 | |||
29 | To add the sources manually, run the following command: | ||
30 | |||
31 | ```bash | ||
32 | $ rustup component add rust-src | ||
33 | ``` | ||
34 | |||
35 | === VS Code | ||
36 | |||
37 | This is the best supported editor at the moment. | ||
38 | The rust-analyzer plugin for VS Code is maintained | ||
39 | https://github.com/rust-analyzer/rust-analyzer/tree/master/editors/code[in tree]. | ||
40 | |||
41 | You can install the latest release of the plugin from | ||
42 | https://marketplace.visualstudio.com/items?itemName=matklad.rust-analyzer[the marketplace]. | ||
43 | By default, the plugin will prompt you to download the matching version of the server as well: | ||
44 | |||
45 | image::https://user-images.githubusercontent.com/9021944/75067008-17502500-54ba-11ea-835a-f92aac50e866.png[] | ||
46 | |||
47 | [NOTE] | ||
48 | ==== | ||
49 | To disable this notification put the following to `settings.json` | ||
50 | |||
51 | [source,json] | ||
52 | ---- | ||
53 | { "rust-analyzer.updates.askBeforeDownload": false } | ||
54 | ---- | ||
55 | ==== | ||
56 | |||
57 | The server binary is stored in: | ||
58 | |||
59 | * Linux: `~/.config/Code/User/globalStorage/matklad.rust-analyzer` | ||
60 | * macOS: `~/Library/Application Support/Code/User/globalStorage/matklad.rust-analyzer` | ||
61 | * Windows: `%APPDATA%\Code\User\globalStorage\matklad.rust-analyzer` | ||
62 | |||
63 | Note that we only support two most recent versions of VS Code. | ||
64 | |||
65 | ==== Updates | ||
66 | |||
67 | The extension will be updated automatically as new versions become available. It will ask your permission to download the matching language server version binary if needed. | ||
68 | |||
69 | ===== Nightly | ||
70 | |||
71 | We ship nightly releases for VS Code. To help us out with testing the newest code and follow the bleeding edge of our `master`, please use the following config: | ||
72 | |||
73 | [source,json] | ||
74 | ---- | ||
75 | { "rust-analyzer.updates.channel": "nightly" } | ||
76 | ---- | ||
77 | |||
78 | You will be prompted to install the `nightly` extension version. Just click `Download now` and from that moment you will get automatic updates every 24 hours. | ||
79 | |||
80 | If you don't want to be asked for `Download now` every day when the new nightly version is released add the following to your `settings.json`: | ||
81 | [source,json] | ||
82 | ---- | ||
83 | { "rust-analyzer.updates.askBeforeDownload": false } | ||
84 | ---- | ||
85 | |||
86 | NOTE: Nightly extension should **only** be installed via the `Download now` action from VS Code. | ||
87 | |||
88 | ==== Building From Source | ||
89 | |||
90 | Alternatively, both the server and the plugin can be installed from source: | ||
91 | |||
92 | [source] | ||
93 | ---- | ||
94 | $ git clone https://github.com/rust-analyzer/rust-analyzer.git && cd rust-analyzer | ||
95 | $ cargo xtask install | ||
96 | ---- | ||
97 | |||
98 | You'll need Cargo, nodejs and npm for this. | ||
99 | |||
100 | Note that installing via `xtask install` does not work for VS Code Remote, instead you'll need to install the `.vsix` manually. | ||
101 | |||
102 | ==== Troubleshooting | ||
103 | |||
104 | Here are some useful self-diagnostic commands: | ||
105 | |||
106 | * **Rust Analyzer: Show RA Version** shows the version of `rust-analyzer` binary | ||
107 | * **Rust Analyzer: Status** prints some statistics about the server, like the few latest LSP requests | ||
108 | * To enable server-side logging, run with `env RA_LOG=info` and see `Output > Rust Analyzer Language Server` in VS Code's panel. | ||
109 | * To log all LSP requests, add `"rust-analyzer.trace.server": "verbose"` to the settings and look for `Server Trace` in the panel. | ||
110 | * To enable client-side logging, add `"rust-analyzer.trace.extension": true` to the settings and open the `Console` tab of VS Code developer tools. | ||
111 | |||
112 | ==== Special `when` clause context for keybindings. | ||
113 | You may use `inRustProject` context to configure keybindings for rust projects only. For example: | ||
114 | [source,json] | ||
115 | ---- | ||
116 | { | ||
117 | "key": "ctrl+i", | ||
118 | "command": "rust-analyzer.toggleInlayHints", | ||
119 | "when": "inRustProject" | ||
120 | } | ||
121 | ---- | ||
122 | More about `when` clause contexts https://code.visualstudio.com/docs/getstarted/keybindings#_when-clause-contexts[here]. | ||
123 | |||
124 | === rust-analyzer Language Server Binary | ||
125 | |||
126 | Other editors generally require the `rust-analyzer` binary to be in `$PATH`. | ||
127 | You can download the pre-built binary from the https://github.com/rust-analyzer/rust-analyzer/releases[releases] page. | ||
128 | Typically, you then need to rename the binary for your platform, e.g. `rust-analyzer-mac` if you're on Mac OS, to `rust-analyzer` and make it executable in addition to moving it into a directory in your `$PATH`. | ||
129 | |||
130 | On Linux to install the `rust-analyzer` binary into `~/.local/bin`, this commands could be used | ||
131 | |||
132 | [source,bash] | ||
133 | ---- | ||
134 | $ curl -L https://github.com/rust-analyzer/rust-analyzer/releases/latest/download/rust-analyzer-linux -o ~/.local/bin/rust-analyzer | ||
135 | $ chmod +x ~/.local/bin/rust-analyzer | ||
136 | ---- | ||
137 | |||
138 | Ensure `~/.local/bin` is listed in the `$PATH` variable. | ||
139 | |||
140 | Alternatively, you can install it from source using the following command: | ||
141 | |||
142 | [source,bash] | ||
143 | ---- | ||
144 | $ git clone https://github.com/rust-analyzer/rust-analyzer.git && cd rust-analyzer | ||
145 | $ cargo xtask install --server | ||
146 | ---- | ||
147 | |||
148 | If your editor can't find the binary even though the binary is on your `$PATH`, the likely explanation is that it doesn't see the same `$PATH` as the shell, see https://github.com/rust-analyzer/rust-analyzer/issues/1811[this issue]. | ||
149 | On Unix, running the editor from a shell or changing the `.desktop` file to set the environment should help. | ||
150 | |||
151 | ==== Arch Linux | ||
152 | |||
153 | The `rust-analyzer` binary can be installed from the repos or AUR (Arch User Repository): | ||
154 | |||
155 | - https://www.archlinux.org/packages/community/x86_64/rust-analyzer/[`rust-analyzer`] (built from latest tagged source) | ||
156 | - https://aur.archlinux.org/packages/rust-analyzer-git[`rust-analyzer-git`] (latest Git version) | ||
157 | |||
158 | Install it with pacman, for example: | ||
159 | |||
160 | [source,bash] | ||
161 | ---- | ||
162 | $ pacman -S rust-analyzer | ||
163 | ---- | ||
164 | |||
165 | === Emacs | ||
166 | |||
167 | Prerequisites: You have installed the <<rust-analyzer-language-server-binary,`rust-analyzer` binary>>. | ||
168 | |||
169 | Emacs support is maintained as part of the https://github.com/emacs-lsp/lsp-mode[Emacs-LSP] package in https://github.com/emacs-lsp/lsp-mode/blob/master/lsp-rust.el[lsp-rust.el]. | ||
170 | |||
171 | 1. Install the most recent version of `emacs-lsp` package by following the https://github.com/emacs-lsp/lsp-mode[Emacs-LSP instructions]. | ||
172 | 2. Set `lsp-rust-server` to `'rust-analyzer`. | ||
173 | 3. Run `lsp` in a Rust buffer. | ||
174 | 4. (Optionally) bind commands like `lsp-rust-analyzer-join-lines`, `lsp-extend-selection` and `lsp-rust-analyzer-expand-macro` to keys. | ||
175 | |||
176 | === Vim/NeoVim | ||
177 | |||
178 | Prerequisites: You have installed the <<rust-analyzer-language-server-binary,`rust-analyzer` binary>>. Not needed if the extension can install/update it on its own, coc-rust-analyzer is one example. | ||
179 | |||
180 | The are several LSP client implementations for vim or neovim: | ||
181 | |||
182 | ==== coc-rust-analyzer | ||
183 | |||
184 | 1. Install coc.nvim by following the instructions at | ||
185 | https://github.com/neoclide/coc.nvim[coc.nvim] | ||
186 | (Node.js required) | ||
187 | 2. Run `:CocInstall coc-rust-analyzer` to install | ||
188 | https://github.com/fannheyward/coc-rust-analyzer[coc-rust-analyzer], | ||
189 | this extension implements _most_ of the features supported in the VSCode extension: | ||
190 | * automatically install and upgrade stable/nightly releases | ||
191 | * same configurations as VSCode extension, `rust-analyzer.serverPath`, `rust-analyzer.cargo.features` etc. | ||
192 | * same commands too, `rust-analyzer.analyzerStatus`, `rust-analyzer.ssr` etc. | ||
193 | * inlay hints for method chaining support, _Neovim Only_ | ||
194 | * semantic highlighting is not implemented yet | ||
195 | |||
196 | ==== LanguageClient-neovim | ||
197 | |||
198 | 1. Install LanguageClient-neovim by following the instructions | ||
199 | https://github.com/autozimu/LanguageClient-neovim[here] | ||
200 | * The GitHub project wiki has extra tips on configuration | ||
201 | |||
202 | 2. Configure by adding this to your vim/neovim config file (replacing the existing Rust-specific line if it exists): | ||
203 | + | ||
204 | [source,vim] | ||
205 | ---- | ||
206 | let g:LanguageClient_serverCommands = { | ||
207 | \ 'rust': ['rust-analyzer'], | ||
208 | \ } | ||
209 | ---- | ||
210 | |||
211 | ==== YouCompleteMe | ||
212 | |||
213 | 1. Install YouCompleteMe by following the instructions | ||
214 | https://github.com/ycm-core/lsp-examples#rust-rust-analyzer[here] | ||
215 | |||
216 | 2. Configure by adding this to your vim/neovim config file (replacing the existing Rust-specific line if it exists): | ||
217 | + | ||
218 | [source,vim] | ||
219 | ---- | ||
220 | let g:ycm_language_server = | ||
221 | \ [ | ||
222 | \ { | ||
223 | \ 'name': 'rust', | ||
224 | \ 'cmdline': ['rust-analyzer'], | ||
225 | \ 'filetypes': ['rust'], | ||
226 | \ 'project_root_files': ['Cargo.toml'] | ||
227 | \ } | ||
228 | \ ] | ||
229 | ---- | ||
230 | |||
231 | ==== ALE | ||
232 | |||
233 | To use the LSP server in https://github.com/dense-analysis/ale[ale]: | ||
234 | |||
235 | [source,vim] | ||
236 | ---- | ||
237 | let g:ale_linters = {'rust': ['analyzer']} | ||
238 | ---- | ||
239 | |||
240 | ==== nvim-lsp | ||
241 | |||
242 | NeoVim 0.5 (not yet released) has built-in language server support. | ||
243 | For a quick start configuration of rust-analyzer, use https://github.com/neovim/nvim-lsp#rust_analyzer[neovim/nvim-lsp]. | ||
244 | Once `neovim/nvim-lsp` is installed, use `+lua require'nvim_lsp'.rust_analyzer.setup({})+` in your `init.vim`. | ||
245 | |||
246 | === Sublime Text 3 | ||
247 | |||
248 | Prerequisites: You have installed the <<rust-analyzer-language-server-binary,`rust-analyzer` binary>>. | ||
249 | |||
250 | You also need the `LSP` package. To install it: | ||
251 | |||
252 | 1. If you've never installed a Sublime Text package, install Package Control: | ||
253 | * Open the command palette (Win/Linux: `ctrl+shift+p`, Mac: `cmd+shift+p`) | ||
254 | * Type `Install Package Control`, press enter | ||
255 | 2. In the command palette, run `Package control: Install package`, and in the list that pops up, type `LSP` and press enter. | ||
256 | |||
257 | Finally, with your Rust project open, in the command palette, run `LSP: Enable Language Server In Project` or `LSP: Enable Language Server Globally`, then select `rust-analyzer` in the list that pops up to enable the rust-analyzer LSP. The latter means that rust-analyzer is enabled by default in Rust projects. | ||
258 | |||
259 | If it worked, you should see "rust-analyzer, Line X, Column Y" on the left side of the bottom bar, and after waiting a bit, functionality like tooltips on hovering over variables should become available. | ||
260 | |||
261 | If you get an error saying `No such file or directory: 'rust-analyzer'`, see the <<rust-analyzer-language-server-binary,`rust-analyzer` binary>> section on installing the language server binary. | ||
262 | |||
263 | === GNOME Builder | ||
264 | |||
265 | Prerequisites: You have installed the <<rust-analyzer-language-server-binary,`rust-analyzer` binary>>. | ||
266 | |||
267 | Gnome Builder currently has support for RLS, and there's no way to configure the language server executable. A future version might support `rust-analyzer` out of the box. | ||
268 | |||
269 | 1. Rename, symlink or copy the `rust-analyzer` binary to `rls` and place it somewhere Builder can find (in `PATH`, or under `~/.cargo/bin`). | ||
270 | 2. Enable the Rust Builder plugin. | ||
271 | |||
272 | == Features | ||
273 | |||
274 | include::./generated_features.adoc[] | ||
275 | |||
276 | == Assists (Code Actions) | ||
277 | |||
278 | Assists, or code actions, are small local refactorings, available in a particular context. | ||
279 | They are usually triggered by a shortcut or by clicking a light bulb icon in the editor. | ||
280 | Cursor position or selection is signified by `┃` character. | ||
281 | |||
282 | include::./generated_assists.adoc[] | ||