diff options
Diffstat (limited to 'docs/user/assists.md')
-rw-r--r-- | docs/user/assists.md | 505 |
1 files changed, 505 insertions, 0 deletions
diff --git a/docs/user/assists.md b/docs/user/assists.md new file mode 100644 index 000000000..303353e74 --- /dev/null +++ b/docs/user/assists.md | |||
@@ -0,0 +1,505 @@ | |||
1 | # Assists | ||
2 | |||
3 | Cursor position or selection is signified by `┃` character. | ||
4 | |||
5 | |||
6 | ## `add_derive` | ||
7 | |||
8 | Adds a new `#[derive()]` clause to a struct or enum. | ||
9 | |||
10 | ```rust | ||
11 | // BEFORE | ||
12 | struct Point { | ||
13 | x: u32, | ||
14 | y: u32,┃ | ||
15 | } | ||
16 | |||
17 | // AFTER | ||
18 | #[derive()] | ||
19 | struct Point { | ||
20 | x: u32, | ||
21 | y: u32, | ||
22 | } | ||
23 | ``` | ||
24 | |||
25 | ## `add_explicit_type` | ||
26 | |||
27 | Specify type for a let binding. | ||
28 | |||
29 | ```rust | ||
30 | // BEFORE | ||
31 | fn main() { | ||
32 | let x┃ = 92; | ||
33 | } | ||
34 | |||
35 | // AFTER | ||
36 | fn main() { | ||
37 | let x: i32 = 92; | ||
38 | } | ||
39 | ``` | ||
40 | |||
41 | ## `add_hash` | ||
42 | |||
43 | Adds a hash to a raw string literal. | ||
44 | |||
45 | ```rust | ||
46 | // BEFORE | ||
47 | fn main() { | ||
48 | r#"Hello,┃ World!"#; | ||
49 | } | ||
50 | |||
51 | // AFTER | ||
52 | fn main() { | ||
53 | r##"Hello, World!"##; | ||
54 | } | ||
55 | ``` | ||
56 | |||
57 | ## `add_impl` | ||
58 | |||
59 | Adds a new inherent impl for a type. | ||
60 | |||
61 | ```rust | ||
62 | // BEFORE | ||
63 | struct Ctx<T: Clone> { | ||
64 | data: T,┃ | ||
65 | } | ||
66 | |||
67 | // AFTER | ||
68 | struct Ctx<T: Clone> { | ||
69 | data: T, | ||
70 | } | ||
71 | |||
72 | impl<T: Clone> Ctx<T> { | ||
73 | |||
74 | } | ||
75 | ``` | ||
76 | |||
77 | ## `add_impl_default_members` | ||
78 | |||
79 | Adds scaffold for overriding default impl members. | ||
80 | |||
81 | ```rust | ||
82 | // BEFORE | ||
83 | trait T { | ||
84 | Type X; | ||
85 | fn foo(&self); | ||
86 | fn bar(&self) {} | ||
87 | } | ||
88 | |||
89 | impl T for () { | ||
90 | Type X = (); | ||
91 | fn foo(&self) {}┃ | ||
92 | |||
93 | } | ||
94 | |||
95 | // AFTER | ||
96 | trait T { | ||
97 | Type X; | ||
98 | fn foo(&self); | ||
99 | fn bar(&self) {} | ||
100 | } | ||
101 | |||
102 | impl T for () { | ||
103 | Type X = (); | ||
104 | fn foo(&self) {} | ||
105 | fn bar(&self) {} | ||
106 | |||
107 | } | ||
108 | ``` | ||
109 | |||
110 | ## `add_impl_missing_members` | ||
111 | |||
112 | Adds scaffold for required impl members. | ||
113 | |||
114 | ```rust | ||
115 | // BEFORE | ||
116 | trait T { | ||
117 | Type X; | ||
118 | fn foo(&self); | ||
119 | fn bar(&self) {} | ||
120 | } | ||
121 | |||
122 | impl T for () {┃ | ||
123 | |||
124 | } | ||
125 | |||
126 | // AFTER | ||
127 | trait T { | ||
128 | Type X; | ||
129 | fn foo(&self); | ||
130 | fn bar(&self) {} | ||
131 | } | ||
132 | |||
133 | impl T for () { | ||
134 | fn foo(&self) { unimplemented!() } | ||
135 | |||
136 | } | ||
137 | ``` | ||
138 | |||
139 | ## `add_import` | ||
140 | |||
141 | Adds a use statement for a given fully-qualified path. | ||
142 | |||
143 | ```rust | ||
144 | // BEFORE | ||
145 | fn process(map: std::collections::┃HashMap<String, String>) {} | ||
146 | |||
147 | // AFTER | ||
148 | use std::collections::HashMap; | ||
149 | |||
150 | fn process(map: HashMap<String, String>) {} | ||
151 | ``` | ||
152 | |||
153 | ## `apply_demorgan` | ||
154 | |||
155 | Apply [De Morgan's law](https://en.wikipedia.org/wiki/De_Morgan%27s_laws). | ||
156 | This transforms expressions of the form `!l || !r` into `!(l && r)`. | ||
157 | This also works with `&&`. This assist can only be applied with the cursor | ||
158 | on either `||` or `&&`, with both operands being a negation of some kind. | ||
159 | This means something of the form `!x` or `x != y`. | ||
160 | |||
161 | ```rust | ||
162 | // BEFORE | ||
163 | fn main() { | ||
164 | if x != 4 ||┃ !y {} | ||
165 | } | ||
166 | |||
167 | // AFTER | ||
168 | fn main() { | ||
169 | if !(x == 4 && y) {} | ||
170 | } | ||
171 | ``` | ||
172 | |||
173 | ## `change_visibility` | ||
174 | |||
175 | Adds or changes existing visibility specifier. | ||
176 | |||
177 | ```rust | ||
178 | // BEFORE | ||
179 | ┃fn frobnicate() {} | ||
180 | |||
181 | // AFTER | ||
182 | pub(crate) fn frobnicate() {} | ||
183 | ``` | ||
184 | |||
185 | ## `convert_to_guarded_return` | ||
186 | |||
187 | Replace a large conditional with a guarded return. | ||
188 | |||
189 | ```rust | ||
190 | // BEFORE | ||
191 | fn main() { | ||
192 | ┃if cond { | ||
193 | foo(); | ||
194 | bar(); | ||
195 | } | ||
196 | } | ||
197 | |||
198 | // AFTER | ||
199 | fn main() { | ||
200 | if !cond { | ||
201 | return; | ||
202 | } | ||
203 | foo(); | ||
204 | bar(); | ||
205 | } | ||
206 | ``` | ||
207 | |||
208 | ## `fill_match_arms` | ||
209 | |||
210 | Adds missing clauses to a `match` expression. | ||
211 | |||
212 | ```rust | ||
213 | // BEFORE | ||
214 | enum Action { Move { distance: u32 }, Stop } | ||
215 | |||
216 | fn handle(action: Action) { | ||
217 | match action { | ||
218 | ┃ | ||
219 | } | ||
220 | } | ||
221 | |||
222 | // AFTER | ||
223 | enum Action { Move { distance: u32 }, Stop } | ||
224 | |||
225 | fn handle(action: Action) { | ||
226 | match action { | ||
227 | Action::Move { distance } => (), | ||
228 | Action::Stop => (), | ||
229 | } | ||
230 | } | ||
231 | ``` | ||
232 | |||
233 | ## `flip_binexpr` | ||
234 | |||
235 | Flips operands of a binary expression. | ||
236 | |||
237 | ```rust | ||
238 | // BEFORE | ||
239 | fn main() { | ||
240 | let _ = 90 +┃ 2; | ||
241 | } | ||
242 | |||
243 | // AFTER | ||
244 | fn main() { | ||
245 | let _ = 2 + 90; | ||
246 | } | ||
247 | ``` | ||
248 | |||
249 | ## `flip_comma` | ||
250 | |||
251 | Flips two comma-separated items. | ||
252 | |||
253 | ```rust | ||
254 | // BEFORE | ||
255 | fn main() { | ||
256 | ((1, 2),┃ (3, 4)); | ||
257 | } | ||
258 | |||
259 | // AFTER | ||
260 | fn main() { | ||
261 | ((3, 4), (1, 2)); | ||
262 | } | ||
263 | ``` | ||
264 | |||
265 | ## `flip_trait_bound` | ||
266 | |||
267 | Flips two trait bounds. | ||
268 | |||
269 | ```rust | ||
270 | // BEFORE | ||
271 | fn foo<T: Clone +┃ Copy>() { } | ||
272 | |||
273 | // AFTER | ||
274 | fn foo<T: Copy + Clone>() { } | ||
275 | ``` | ||
276 | |||
277 | ## `inline_local_variable` | ||
278 | |||
279 | Inlines local variable. | ||
280 | |||
281 | ```rust | ||
282 | // BEFORE | ||
283 | fn main() { | ||
284 | let x┃ = 1 + 2; | ||
285 | x * 4; | ||
286 | } | ||
287 | |||
288 | // AFTER | ||
289 | fn main() { | ||
290 | (1 + 2) * 4; | ||
291 | } | ||
292 | ``` | ||
293 | |||
294 | ## `introduce_variable` | ||
295 | |||
296 | Extracts subexpression into a variable. | ||
297 | |||
298 | ```rust | ||
299 | // BEFORE | ||
300 | fn main() { | ||
301 | ┃(1 + 2)┃ * 4; | ||
302 | } | ||
303 | |||
304 | // AFTER | ||
305 | fn main() { | ||
306 | let var_name = (1 + 2); | ||
307 | var_name * 4; | ||
308 | } | ||
309 | ``` | ||
310 | |||
311 | ## `make_raw_string` | ||
312 | |||
313 | Adds `r#` to a plain string literal. | ||
314 | |||
315 | ```rust | ||
316 | // BEFORE | ||
317 | fn main() { | ||
318 | "Hello,┃ World!"; | ||
319 | } | ||
320 | |||
321 | // AFTER | ||
322 | fn main() { | ||
323 | r#"Hello, World!"#; | ||
324 | } | ||
325 | ``` | ||
326 | |||
327 | ## `make_usual_string` | ||
328 | |||
329 | Turns a raw string into a plain string. | ||
330 | |||
331 | ```rust | ||
332 | // BEFORE | ||
333 | fn main() { | ||
334 | r#"Hello,┃ "World!""#; | ||
335 | } | ||
336 | |||
337 | // AFTER | ||
338 | fn main() { | ||
339 | "Hello, \"World!\""; | ||
340 | } | ||
341 | ``` | ||
342 | |||
343 | ## `merge_match_arms` | ||
344 | |||
345 | Merges identical match arms. | ||
346 | |||
347 | ```rust | ||
348 | // BEFORE | ||
349 | enum Action { Move { distance: u32 }, Stop } | ||
350 | |||
351 | fn handle(action: Action) { | ||
352 | match action { | ||
353 | ┃Action::Move(..) => foo(), | ||
354 | Action::Stop => foo(), | ||
355 | } | ||
356 | } | ||
357 | |||
358 | // AFTER | ||
359 | enum Action { Move { distance: u32 }, Stop } | ||
360 | |||
361 | fn handle(action: Action) { | ||
362 | match action { | ||
363 | Action::Move(..) | Action::Stop => foo(), | ||
364 | } | ||
365 | } | ||
366 | ``` | ||
367 | |||
368 | ## `move_arm_cond_to_match_guard` | ||
369 | |||
370 | Moves if expression from match arm body into a guard. | ||
371 | |||
372 | ```rust | ||
373 | // BEFORE | ||
374 | enum Action { Move { distance: u32 }, Stop } | ||
375 | |||
376 | fn handle(action: Action) { | ||
377 | match action { | ||
378 | Action::Move { distance } => ┃if distance > 10 { foo() }, | ||
379 | _ => (), | ||
380 | } | ||
381 | } | ||
382 | |||
383 | // AFTER | ||
384 | enum Action { Move { distance: u32 }, Stop } | ||
385 | |||
386 | fn handle(action: Action) { | ||
387 | match action { | ||
388 | Action::Move { distance } if distance > 10 => foo(), | ||
389 | _ => (), | ||
390 | } | ||
391 | } | ||
392 | ``` | ||
393 | |||
394 | ## `move_bounds_to_where_clause` | ||
395 | |||
396 | Moves inline type bounds to a where clause. | ||
397 | |||
398 | ```rust | ||
399 | // BEFORE | ||
400 | fn apply<T, U, ┃F: FnOnce(T) -> U>(f: F, x: T) -> U { | ||
401 | f(x) | ||
402 | } | ||
403 | |||
404 | // AFTER | ||
405 | fn apply<T, U, F>(f: F, x: T) -> U where F: FnOnce(T) -> U { | ||
406 | f(x) | ||
407 | } | ||
408 | ``` | ||
409 | |||
410 | ## `move_guard_to_arm_body` | ||
411 | |||
412 | Moves match guard into match arm body. | ||
413 | |||
414 | ```rust | ||
415 | // BEFORE | ||
416 | enum Action { Move { distance: u32 }, Stop } | ||
417 | |||
418 | fn handle(action: Action) { | ||
419 | match action { | ||
420 | Action::Move { distance } ┃if distance > 10 => foo(), | ||
421 | _ => (), | ||
422 | } | ||
423 | } | ||
424 | |||
425 | // AFTER | ||
426 | enum Action { Move { distance: u32 }, Stop } | ||
427 | |||
428 | fn handle(action: Action) { | ||
429 | match action { | ||
430 | Action::Move { distance } => if distance > 10 { foo() }, | ||
431 | _ => (), | ||
432 | } | ||
433 | } | ||
434 | ``` | ||
435 | |||
436 | ## `remove_dbg` | ||
437 | |||
438 | Removes `dbg!()` macro call. | ||
439 | |||
440 | ```rust | ||
441 | // BEFORE | ||
442 | fn main() { | ||
443 | ┃dbg!(92); | ||
444 | } | ||
445 | |||
446 | // AFTER | ||
447 | fn main() { | ||
448 | 92; | ||
449 | } | ||
450 | ``` | ||
451 | |||
452 | ## `remove_hash` | ||
453 | |||
454 | Removes a hash from a raw string literal. | ||
455 | |||
456 | ```rust | ||
457 | // BEFORE | ||
458 | fn main() { | ||
459 | r#"Hello,┃ World!"#; | ||
460 | } | ||
461 | |||
462 | // AFTER | ||
463 | fn main() { | ||
464 | r"Hello, World!"; | ||
465 | } | ||
466 | ``` | ||
467 | |||
468 | ## `replace_if_let_with_match` | ||
469 | |||
470 | Replaces `if let` with an else branch with a `match` expression. | ||
471 | |||
472 | ```rust | ||
473 | // BEFORE | ||
474 | enum Action { Move { distance: u32 }, Stop } | ||
475 | |||
476 | fn handle(action: Action) { | ||
477 | ┃if let Action::Move { distance } = action { | ||
478 | foo(distance) | ||
479 | } else { | ||
480 | bar() | ||
481 | } | ||
482 | } | ||
483 | |||
484 | // AFTER | ||
485 | enum Action { Move { distance: u32 }, Stop } | ||
486 | |||
487 | fn handle(action: Action) { | ||
488 | match action { | ||
489 | Action::Move { distance } => foo(distance), | ||
490 | _ => bar(), | ||
491 | } | ||
492 | } | ||
493 | ``` | ||
494 | |||
495 | ## `split_import` | ||
496 | |||
497 | Wraps the tail of import into braces. | ||
498 | |||
499 | ```rust | ||
500 | // BEFORE | ||
501 | use std::┃collections::HashMap; | ||
502 | |||
503 | // AFTER | ||
504 | use std::{collections::HashMap}; | ||
505 | ``` | ||