]> git.lizzy.rs Git - rust.git/blob - docs/user/README.md
1861c69abc854508619eb8f83d4cf09e8b42d8e2
[rust.git] / docs / user / README.md
1 The main interface to rust-analyzer is the
2 [LSP](https://microsoft.github.io/language-server-protocol/) implementation. To
3 install lsp server, use `cargo xtask install --server`, which is a shorthand for `cargo
4 install --package ra_lsp_server`. The binary is named `ra_lsp_server`, you
5 should be able to use it with any LSP-compatible editor. We use custom
6 extensions to LSP, so special client-side support is required to take full
7 advantage of rust-analyzer. This repository contains support code for VS Code
8 and Emacs.
9
10 Rust Analyzer needs sources of rust standard library to work, so you might need
11 to execute
12
13 ```
14 $ rustup component add rust-src
15 ```
16
17 See [./features.md](./features.md) document for a list of features that are available.
18
19 ## VS Code
20
21 Prerequisites:
22
23 In order to build the VS Code plugin, you need to have node.js and npm with
24 a minimum version of 10 installed. Please refer to
25 [node.js and npm documentation](https://nodejs.org) for installation instructions.
26
27 You will also need the most recent version of VS Code: we don't try to
28 maintain compatibility with older versions yet.
29
30 The experimental VS Code plugin can then be built and installed by executing the
31 following commands:
32
33 ```
34 $ git clone https://github.com/rust-analyzer/rust-analyzer.git --depth 1
35 $ cd rust-analyzer
36 $ cargo xtask install
37 ```
38
39 The automatic installation is expected to *just work* for common cases, if it
40 doesn't, report bugs!
41
42 If you have an unusual setup (for example, `code` is not in the `PATH`), you
43 should adapt these manual installation instructions:
44
45 ```
46 $ git clone https://github.com/rust-analyzer/rust-analyzer.git --depth 1
47 $ cd rust-analyzer
48 $ cargo install --path ./crates/ra_lsp_server/ --force --locked
49 $ cd ./editors/code
50 $ npm install
51 $ ./node_modules/vsce/out/vsce package
52 $ code --install-extension ./ra-lsp-0.0.1.vsix
53 ```
54
55 It's better to remove existing Rust plugins to avoid interference.
56
57 Beyond basic LSP features, there are some extension commands which you can
58 invoke via <kbd>Ctrl+Shift+P</kbd> or bind to a shortcut. See [./features.md](./features.md)
59 for details.
60
61 For updates, pull the latest changes from the master branch, run `cargo xtask install` again, and **restart** VS Code instance.
62 See [microsoft/vscode#72308](https://github.com/microsoft/vscode/issues/72308) for why a full restart is needed.
63
64 ### VS Code Remote
65
66 You can also use `rust-analyzer` with the Visual Studio Code Remote extensions
67 (Remote SSH, Remote WSL, Remote Containers). In this case, however, you have to
68 manually install the `.vsix` package:
69
70 1. Build the extension on the remote host using the instructions above (ignore the
71    error if `code` cannot be found in your PATH: VSCode doesn't need to be installed
72    on the remote host).
73 2. In Visual Studio Code open a connection to the remote host.
74 3. Open the Extensions View (`View > Extensions`, keyboard shortcut: `Ctrl+Shift+X`).
75 4. From the top-right kebab menu (`ยทยทยท`) select `Install from VSIX...`
76 5. Inside the `rust-analyzer` directory find the `editors/code` subdirectory and choose
77    the `ra-lsp-0.0.1.vsix` file.
78 6. Restart Visual Studio Code and re-establish the connection to the remote host.
79
80 In case of errors please make sure that `~/.cargo/bin` is in your `PATH` on the remote
81 host.
82
83 ### Settings
84
85 * `rust-analyzer.highlightingOn`: enables experimental syntax highlighting
86 * `rust-analyzer.enableEnhancedTyping`: by default, rust-analyzer intercepts
87   `Enter` key to make it easier to continue comments. Note that it may conflict with VIM emulation plugin.
88 * `rust-analyzer.raLspServerPath`: path to `ra_lsp_server` executable
89 * `rust-analyzer.enableCargoWatchOnStartup`: prompt to install & enable `cargo
90   watch` for live error highlighting (note, this **does not** use rust-analyzer)
91 * `rust-analyzer.excludeGlobs`: a list of glob-patterns for exclusion (see globset [docs](https://docs.rs/globset) for syntax).
92   Note: glob patterns are applied to all Cargo packages and a rooted at a package root.
93   This is not very intuitive and a limitation of a current implementation.
94 * `rust-analyzer.useClientWatching`: use client provided file watching instead
95   of notify watching.
96 * `rust-analyzer.cargo-watch.command`: `cargo-watch` command. (e.g: `clippy` will run as `cargo watch -x clippy` )
97 * `rust-analyzer.cargo-watch.arguments`: cargo-watch check arguments.
98   (e.g: `--features="shumway,pdf"` will run as `cargo watch -x "check --features="shumway,pdf""` )
99 * `rust-analyzer.cargo-watch.ignore`: list of patterns for cargo-watch to ignore (will be passed as `--ignore`)
100 * `rust-analyzer.trace.server`: enables internal logging
101 * `rust-analyzer.trace.cargo-watch`: enables cargo-watch logging
102 * `RUST_SRC_PATH`: environment variable that overwrites the sysroot
103 * `rust-analyzer.featureFlags` -- a JSON object to tweak fine-grained behavior:
104    ```js
105    {
106        // Show diagnostics produced by rust-analyzer itself.
107        "lsp.diagnostics": true,
108        // Automatically insert `()` and `<>` when completing functions and types.
109        "completion.insertion.add-call-parenthesis": true,
110        // Enable completions like `.if`, `.match`, etc.
111        "completion.enable-postfix": true,
112        // Show notification when workspace is fully loaded
113        "notifications.workspace-loaded": true,
114    }
115    ```
116
117
118 ## Emacs
119
120 Prerequisites:
121
122 `emacs-lsp`, `dash` and `ht` packages.
123
124 Installation:
125
126 * add
127 [ra-emacs-lsp.el](https://github.com/rust-analyzer/rust-analyzer/blob/69ee5c9c5ef212f7911028c9ddf581559e6565c3/editors/emacs/ra-emacs-lsp.el)
128 to load path and require it in `init.el`
129 * run `lsp` in a rust buffer
130 * (Optionally) bind commands like `rust-analyzer-join-lines` or `rust-analyzer-extend-selection` to keys, and enable `rust-analyzer-inlay-hints-mode` to get inline type hints
131
132
133 ## Vim and NeoVim
134
135 * Install coc.nvim by following the instructions at [coc.nvim]
136   - You will need nodejs installed.
137   - You may want to include some of the sample vim configurations [from here][coc-vim-conf]
138   - Note that if you use a plugin manager other than `vim-plug`, you may need to manually
139     checkout the `release` branch wherever your plugin manager cloned it. Otherwise you will
140     get errors about a missing javascript file.
141 * Run `:CocInstall coc-rust-analyzer` to install [coc-rust-analyzer], this extension implemented _most_ of the features supported in the VSCode extension:
142   - same configurations as VSCode extension, `rust-analyzer.raLspServerPath`, `rust-analyzer.enableCargoWatchOnStartup` etc.
143   - same commands too, `rust-analyzer.analyzerStatus`, `rust-analyzer.startCargoWatch` etc.
144   - highlighting and inlay_hints are not implemented yet
145
146 [coc.nvim]: https://github.com/neoclide/coc.nvim
147 [coc-vim-conf]: https://github.com/neoclide/coc.nvim/#example-vim-configuration
148 [coc-rust-analyzer]: https://github.com/fannheyward/coc-rust-analyzer
149
150 ## Vim and NeoVim Alternative
151
152 * Install LanguageClient-neovim by following the instructions [here][lang-client-neovim]
153   - No extra run-time is required as this server is written in Rust
154   - The github project wiki has extra tips on configuration
155
156 * Configure by adding this to your vim/neovim config file (replacing the existing rust specific line if it exists):
157
158 ```
159 let g:LanguageClient_serverCommands = {
160 \ 'rust': ['ra_lsp_server'],
161 \ }
162 ```
163
164 [lang-client-neovim]: https://github.com/autozimu/LanguageClient-neovim
165
166
167 ## Sublime Text 3
168
169 Prequisites:
170
171 `LSP` package.
172
173 Installation:
174
175 * Invoke the command palette with <kbd>Ctrl+Shift+P</kbd>
176 * Type `LSP Settings` to open the LSP preferences editor
177 * Add the following LSP client definition to your settings:
178
179 ```json
180 "rust-analyzer": {
181     "command": ["ra_lsp_server"],
182     "languageId": "rust",
183     "scopes": ["source.rust"],
184     "syntaxes": [
185         "Packages/Rust/Rust.sublime-syntax",
186         "Packages/Rust Enhanced/RustEnhanced.sublime-syntax"
187     ],
188     "initializationOptions": {
189       "featureFlags": {
190       }
191     },
192 }
193 ```
194
195 * You can now invoke the command palette and type LSP enable to locally/globally enable the rust-analyzer LSP (type LSP enable, then choose either locally or globally, then select rust-analyzer)
196
197 * Note that `ra_lsp_server` binary must be in `$PATH` for this to work. If it's not the case, you can specify full path to the binary, which is typically `.cargo/bin/ra_lsp_server`.