diff options
Diffstat (limited to 'docs/user')
-rw-r--r-- | docs/user/features.md | 251 |
1 files changed, 203 insertions, 48 deletions
diff --git a/docs/user/features.md b/docs/user/features.md index 5df606aee..aa3bf5157 100644 --- a/docs/user/features.md +++ b/docs/user/features.md | |||
@@ -1,45 +1,47 @@ | |||
1 | This documents is an index of features that rust-analyzer language server provides. | 1 | This documents is an index of features that rust-analyzer language server |
2 | provides. Shortcuts are for the default VS Code layout. If there's no shortcut, | ||
3 | you can use <kbd>Ctrl+Shift+P</kbd> to search for the corresponding action. | ||
2 | 4 | ||
3 | ### Go to symbol in workspace <kbd>ctrl+t</kbd> | 5 | ### Workspace Symbol <kbd>ctrl+t</kbd> |
4 | 6 | ||
5 | It mostly works on top of the built-in LSP functionality, however `#` and `*` | 7 | Uses fuzzy-search to find types, modules and function by name across your |
6 | symbols can be used to narrow down the search. Specifically, | 8 | project and dependencies. This **the** most useful feature, which improves code |
9 | navigation tremendously. It mostly works on top of the built-in LSP | ||
10 | functionality, however `#` and `*` symbols can be used to narrow down the | ||
11 | search. Specifically, | ||
7 | 12 | ||
8 | - `#Foo` searches for `Foo` type in the current workspace | 13 | - `Foo` searches for `Foo` type in the current workspace |
9 | - `#foo#` searches for `foo` function in the current workspace | 14 | - `foo#` searches for `foo` function in the current workspace |
10 | - `#Foo*` searches for `Foo` type among dependencies, excluding `stdlib` | 15 | - `Foo*` searches for `Foo` type among dependencies, excluding `stdlib` |
11 | - `#foo#*` searches for `foo` function among dependencies. | 16 | - `foo#*` searches for `foo` function among dependencies. |
12 | 17 | ||
13 | That is, `#` switches from "types" to all symbols, `*` switches from the current | 18 | That is, `#` switches from "types" to all symbols, `*` switches from the current |
14 | workspace to dependencies. | 19 | workspace to dependencies. |
15 | 20 | ||
16 | ### Commands <kbd>ctrl+shift+p</kbd> | 21 | ### Document Symbol <kbd>ctrl+shift+o</kbd> |
17 | |||
18 | #### Show Rust Syntax Tree | ||
19 | |||
20 | Shows the parse tree of the current file. It exists mostly for debugging | ||
21 | rust-analyzer itself. | ||
22 | 22 | ||
23 | #### Extend Selection | 23 | Provides a tree of the symbols defined in the file. Can be used to |
24 | 24 | ||
25 | Extends the current selection to the encompassing syntactic construct | 25 | * fuzzy search symbol in a file (super useful) |
26 | (expression, statement, item, module, etc). It works with multiple cursors. Do | 26 | * draw breadcrumbs to describe the context around the cursor |
27 | bind this command to a key, its super-useful! Expected to be upstreamed to LSP soonish: | 27 | * draw outline of the file |
28 | https://github.com/Microsoft/language-server-protocol/issues/613 | ||
29 | 28 | ||
30 | #### Matching Brace | 29 | ### On Typing Assists |
31 | 30 | ||
32 | If the cursor is on any brace (`<>(){}[]`) which is a part of a brace-pair, | 31 | Some features trigger on typing certain characters: |
33 | moves cursor to the matching brace. It uses the actual parser to determine | ||
34 | braces, so it won't confuse generics with comparisons. | ||
35 | 32 | ||
36 | #### Parent Module | 33 | - typing `let =` tries to smartly add `;` if `=` is followed by an existing expression. |
34 | - Enter inside comments automatically inserts `///` | ||
35 | - typing `.` in a chain method call auto-indents | ||
37 | 36 | ||
38 | Navigates to the parent module of the current module. | 37 | ### Commands <kbd>ctrl+shift+p</kbd> |
39 | 38 | ||
40 | #### Join Lines | 39 | #### Extend Selection |
41 | 40 | ||
42 | Join selected lines into one, smartly fixing up whitespace and trailing commas. | 41 | Extends the current selection to the encompassing syntactic construct |
42 | (expression, statement, item, module, etc). It works with multiple cursors. Do | ||
43 | bind this command to a key, it's super-useful! Expected to be upstreamed to LSP | ||
44 | soonish: https://github.com/Microsoft/language-server-protocol/issues/613 | ||
43 | 45 | ||
44 | #### Run | 46 | #### Run |
45 | 47 | ||
@@ -47,33 +49,37 @@ Shows popup suggesting to run a test/benchmark/binary **at the current cursor | |||
47 | location**. Super useful for repeatedly running just a single test. Do bind this | 49 | location**. Super useful for repeatedly running just a single test. Do bind this |
48 | to a shortcut! | 50 | to a shortcut! |
49 | 51 | ||
52 | #### Parent Module | ||
50 | 53 | ||
51 | ### On Typing Assists | 54 | Navigates to the parent module of the current module. |
52 | 55 | ||
53 | Some features trigger on typing certain characters: | 56 | #### Matching Brace |
54 | 57 | ||
55 | - typing `let =` tries to smartly add `;` if `=` is followed by an existing expression. | 58 | If the cursor is on any brace (`<>(){}[]`) which is a part of a brace-pair, |
56 | - Enter inside comments automatically inserts `///` | 59 | moves cursor to the matching brace. It uses the actual parser to determine |
57 | - typing `.` in a chain method call auto-indents | 60 | braces, so it won't confuse generics with comparisons. |
58 | 61 | ||
62 | #### Join Lines | ||
59 | 63 | ||
64 | Join selected lines into one, smartly fixing up whitespace and trailing commas. | ||
60 | 65 | ||
66 | #### Show Syntax Tree | ||
61 | 67 | ||
68 | Shows the parse tree of the current file. It exists mostly for debugging | ||
69 | rust-analyzer itself. | ||
62 | 70 | ||
63 | ### Code Actions (Assists) | 71 | #### Status |
64 | 72 | ||
65 | These are triggered in a particular context via light bulb. We use custom code on | 73 | Shows internal statistic about memory usage of rust-analyzer |
66 | the VS Code side to be able to position cursor. | ||
67 | 74 | ||
75 | #### Run garbage collection | ||
68 | 76 | ||
69 | - Flip `,` | 77 | Manually triggers GC |
70 | 78 | ||
71 | ```rust | 79 | ### Code Actions (Assists) |
72 | // before: | 80 | |
73 | fn foo(x: usize,<|> dim: (usize, usize)) | 81 | These are triggered in a particular context via light bulb. We use custom code on |
74 | // after: | 82 | the VS Code side to be able to position cursor. `<|>` signifies cursor |
75 | fn foo(dim: (usize, usize), x: usize) | ||
76 | ``` | ||
77 | 83 | ||
78 | - Add `#[derive]` | 84 | - Add `#[derive]` |
79 | 85 | ||
@@ -106,14 +112,147 @@ impl<'a, T: Debug> Foo<'a, T> { | |||
106 | } | 112 | } |
107 | ``` | 113 | ``` |
108 | 114 | ||
109 | - Change visibility | 115 | - Add missing `impl` members |
110 | 116 | ||
111 | ```rust | 117 | ```rust |
112 | // before: | 118 | // before: |
113 | fn<|> foo() {} | 119 | trait Foo { |
120 | fn foo(&self); | ||
121 | fn bar(&self); | ||
122 | fn baz(&self); | ||
123 | } | ||
124 | |||
125 | struct S; | ||
126 | |||
127 | impl Foo for S { | ||
128 | fn bar(&self) {} | ||
129 | <|> | ||
130 | } | ||
131 | |||
132 | // after: | ||
133 | trait Foo { | ||
134 | fn foo(&self); | ||
135 | fn bar(&self); | ||
136 | fn baz(&self); | ||
137 | } | ||
114 | 138 | ||
115 | // after | 139 | struct S; |
116 | pub(crate) fn foo() {} | 140 | |
141 | impl Foo for S { | ||
142 | fn bar(&self) {} | ||
143 | fn foo(&self) { unimplemented!() } | ||
144 | fn baz(&self) { unimplemented!() }<|> | ||
145 | } | ||
146 | ``` | ||
147 | |||
148 | - Import path | ||
149 | |||
150 | ```rust | ||
151 | // before: | ||
152 | impl std::fmt::Debug<|> for Foo { | ||
153 | } | ||
154 | |||
155 | // after: | ||
156 | use std::fmt::Debug | ||
157 | |||
158 | impl Debug<|> for Foo { | ||
159 | } | ||
160 | ``` | ||
161 | |||
162 | - Change Visibility | ||
163 | |||
164 | ```rust | ||
165 | // before: | ||
166 | <|>fn foo() {} | ||
167 | |||
168 | // after: | ||
169 | <|>pub(crate) fn foo() {} | ||
170 | |||
171 | // after: | ||
172 | <|>pub fn foo() {} | ||
173 | ``` | ||
174 | |||
175 | - Fill match arms | ||
176 | |||
177 | ```rust | ||
178 | // before: | ||
179 | enum A { | ||
180 | As, | ||
181 | Bs, | ||
182 | Cs(String), | ||
183 | Ds(String, String), | ||
184 | Es{x: usize, y: usize} | ||
185 | } | ||
186 | |||
187 | fn main() { | ||
188 | let a = A::As; | ||
189 | match a<|> {} | ||
190 | } | ||
191 | |||
192 | // after: | ||
193 | enum A { | ||
194 | As, | ||
195 | Bs, | ||
196 | Cs(String), | ||
197 | Ds(String, String), | ||
198 | Es{x: usize, y: usize} | ||
199 | } | ||
200 | |||
201 | fn main() { | ||
202 | let a = A::As; | ||
203 | match <|>a { | ||
204 | A::As => (), | ||
205 | A::Bs => (), | ||
206 | A::Cs(_) => (), | ||
207 | A::Ds(_, _) => (), | ||
208 | A::Es{x, y} => (), | ||
209 | } | ||
210 | } | ||
211 | ``` | ||
212 | |||
213 | -- Fill struct fields | ||
214 | |||
215 | ```rust | ||
216 | // before: | ||
217 | struct S<'a, D> { | ||
218 | a: u32, | ||
219 | b: String, | ||
220 | c: (i32, i32), | ||
221 | d: D, | ||
222 | r: &'a str, | ||
223 | } | ||
224 | |||
225 | fn main() { | ||
226 | let s = S<|> {} | ||
227 | } | ||
228 | |||
229 | // after: | ||
230 | struct S<'a, D> { | ||
231 | a: u32, | ||
232 | b: String, | ||
233 | c: (i32, i32), | ||
234 | d: D, | ||
235 | r: &'a str, | ||
236 | } | ||
237 | |||
238 | fn main() { | ||
239 | let s = <|>S { | ||
240 | a: (), | ||
241 | b: (), | ||
242 | c: (), | ||
243 | d: (), | ||
244 | r: (), | ||
245 | } | ||
246 | } | ||
247 | ``` | ||
248 | |||
249 | - Flip `,` | ||
250 | |||
251 | ```rust | ||
252 | // before: | ||
253 | fn foo(x: usize,<|> dim: (usize, usize)) {} | ||
254 | // after: | ||
255 | fn foo(dim: (usize, usize), x: usize) {} | ||
117 | ``` | 256 | ``` |
118 | 257 | ||
119 | - Introduce variable: | 258 | - Introduce variable: |
@@ -131,6 +270,24 @@ fn foo() { | |||
131 | } | 270 | } |
132 | ``` | 271 | ``` |
133 | 272 | ||
273 | -- Remove `dbg!` | ||
274 | |||
275 | ```rust | ||
276 | // before: | ||
277 | fn foo(n: usize) { | ||
278 | if let Some(_) = dbg!(n.<|>checked_sub(4)) { | ||
279 | // ... | ||
280 | } | ||
281 | } | ||
282 | |||
283 | // after: | ||
284 | fn foo(n: usize) { | ||
285 | if let Some(_) = n.<|>checked_sub(4) { | ||
286 | // ... | ||
287 | } | ||
288 | } | ||
289 | ``` | ||
290 | |||
134 | - Replace if-let with match: | 291 | - Replace if-let with match: |
135 | 292 | ||
136 | ```rust | 293 | ```rust |
@@ -164,5 +321,3 @@ use algo:<|>:visitor::{Visitor, visit}; | |||
164 | //after: | 321 | //after: |
165 | use algo::{<|>visitor::{Visitor, visit}}; | 322 | use algo::{<|>visitor::{Visitor, visit}}; |
166 | ``` | 323 | ``` |
167 | |||
168 | |||