diff options
Diffstat (limited to 'docs/user/assists.md')
-rw-r--r-- | docs/user/assists.md | 769 |
1 files changed, 0 insertions, 769 deletions
diff --git a/docs/user/assists.md b/docs/user/assists.md deleted file mode 100644 index 4ad7ea59d..000000000 --- a/docs/user/assists.md +++ /dev/null | |||
@@ -1,769 +0,0 @@ | |||
1 | # Assists | ||
2 | |||
3 | Cursor position or selection is signified by `┃` character. | ||
4 | |||
5 | |||
6 | ## `add_custom_impl` | ||
7 | |||
8 | Adds impl block for derived trait. | ||
9 | |||
10 | ```rust | ||
11 | // BEFORE | ||
12 | #[derive(Deb┃ug, Display)] | ||
13 | struct S; | ||
14 | |||
15 | // AFTER | ||
16 | #[derive(Display)] | ||
17 | struct S; | ||
18 | |||
19 | impl Debug for S { | ||
20 | $0 | ||
21 | } | ||
22 | ``` | ||
23 | |||
24 | ## `add_derive` | ||
25 | |||
26 | Adds a new `#[derive()]` clause to a struct or enum. | ||
27 | |||
28 | ```rust | ||
29 | // BEFORE | ||
30 | struct Point { | ||
31 | x: u32, | ||
32 | y: u32,┃ | ||
33 | } | ||
34 | |||
35 | // AFTER | ||
36 | #[derive($0)] | ||
37 | struct Point { | ||
38 | x: u32, | ||
39 | y: u32, | ||
40 | } | ||
41 | ``` | ||
42 | |||
43 | ## `add_explicit_type` | ||
44 | |||
45 | Specify type for a let binding. | ||
46 | |||
47 | ```rust | ||
48 | // BEFORE | ||
49 | fn main() { | ||
50 | let x┃ = 92; | ||
51 | } | ||
52 | |||
53 | // AFTER | ||
54 | fn main() { | ||
55 | let x: i32 = 92; | ||
56 | } | ||
57 | ``` | ||
58 | |||
59 | ## `add_function` | ||
60 | |||
61 | Adds a stub function with a signature matching the function under the cursor. | ||
62 | |||
63 | ```rust | ||
64 | // BEFORE | ||
65 | struct Baz; | ||
66 | fn baz() -> Baz { Baz } | ||
67 | fn foo() { | ||
68 | bar┃("", baz()); | ||
69 | } | ||
70 | |||
71 | |||
72 | // AFTER | ||
73 | struct Baz; | ||
74 | fn baz() -> Baz { Baz } | ||
75 | fn foo() { | ||
76 | bar("", baz()); | ||
77 | } | ||
78 | |||
79 | fn bar(arg: &str, baz: Baz) { | ||
80 | ${0:todo!()} | ||
81 | } | ||
82 | |||
83 | ``` | ||
84 | |||
85 | ## `add_hash` | ||
86 | |||
87 | Adds a hash to a raw string literal. | ||
88 | |||
89 | ```rust | ||
90 | // BEFORE | ||
91 | fn main() { | ||
92 | r#"Hello,┃ World!"#; | ||
93 | } | ||
94 | |||
95 | // AFTER | ||
96 | fn main() { | ||
97 | r##"Hello, World!"##; | ||
98 | } | ||
99 | ``` | ||
100 | |||
101 | ## `add_impl` | ||
102 | |||
103 | Adds a new inherent impl for a type. | ||
104 | |||
105 | ```rust | ||
106 | // BEFORE | ||
107 | struct Ctx<T: Clone> { | ||
108 | data: T,┃ | ||
109 | } | ||
110 | |||
111 | // AFTER | ||
112 | struct Ctx<T: Clone> { | ||
113 | data: T, | ||
114 | } | ||
115 | |||
116 | impl<T: Clone> Ctx<T> { | ||
117 | $0 | ||
118 | } | ||
119 | ``` | ||
120 | |||
121 | ## `add_impl_default_members` | ||
122 | |||
123 | Adds scaffold for overriding default impl members. | ||
124 | |||
125 | ```rust | ||
126 | // BEFORE | ||
127 | trait Trait { | ||
128 | Type X; | ||
129 | fn foo(&self); | ||
130 | fn bar(&self) {} | ||
131 | } | ||
132 | |||
133 | impl Trait for () { | ||
134 | Type X = (); | ||
135 | fn foo(&self) {}┃ | ||
136 | |||
137 | } | ||
138 | |||
139 | // AFTER | ||
140 | trait Trait { | ||
141 | Type X; | ||
142 | fn foo(&self); | ||
143 | fn bar(&self) {} | ||
144 | } | ||
145 | |||
146 | impl Trait for () { | ||
147 | Type X = (); | ||
148 | fn foo(&self) {} | ||
149 | $0fn bar(&self) {} | ||
150 | |||
151 | } | ||
152 | ``` | ||
153 | |||
154 | ## `add_impl_missing_members` | ||
155 | |||
156 | Adds scaffold for required impl members. | ||
157 | |||
158 | ```rust | ||
159 | // BEFORE | ||
160 | trait Trait<T> { | ||
161 | Type X; | ||
162 | fn foo(&self) -> T; | ||
163 | fn bar(&self) {} | ||
164 | } | ||
165 | |||
166 | impl Trait<u32> for () {┃ | ||
167 | |||
168 | } | ||
169 | |||
170 | // AFTER | ||
171 | trait Trait<T> { | ||
172 | Type X; | ||
173 | fn foo(&self) -> T; | ||
174 | fn bar(&self) {} | ||
175 | } | ||
176 | |||
177 | impl Trait<u32> for () { | ||
178 | fn foo(&self) -> u32 { | ||
179 | ${0:todo!()} | ||
180 | } | ||
181 | |||
182 | } | ||
183 | ``` | ||
184 | |||
185 | ## `add_new` | ||
186 | |||
187 | Adds a new inherent impl for a type. | ||
188 | |||
189 | ```rust | ||
190 | // BEFORE | ||
191 | struct Ctx<T: Clone> { | ||
192 | data: T,┃ | ||
193 | } | ||
194 | |||
195 | // AFTER | ||
196 | struct Ctx<T: Clone> { | ||
197 | data: T, | ||
198 | } | ||
199 | |||
200 | impl<T: Clone> Ctx<T> { | ||
201 | fn $0new(data: T) -> Self { Self { data } } | ||
202 | } | ||
203 | |||
204 | ``` | ||
205 | |||
206 | ## `add_turbo_fish` | ||
207 | |||
208 | Adds `::<_>` to a call of a generic method or function. | ||
209 | |||
210 | ```rust | ||
211 | // BEFORE | ||
212 | fn make<T>() -> T { todo!() } | ||
213 | fn main() { | ||
214 | let x = make┃(); | ||
215 | } | ||
216 | |||
217 | // AFTER | ||
218 | fn make<T>() -> T { todo!() } | ||
219 | fn main() { | ||
220 | let x = make::<${0:_}>(); | ||
221 | } | ||
222 | ``` | ||
223 | |||
224 | ## `apply_demorgan` | ||
225 | |||
226 | Apply [De Morgan's law](https://en.wikipedia.org/wiki/De_Morgan%27s_laws). | ||
227 | This transforms expressions of the form `!l || !r` into `!(l && r)`. | ||
228 | This also works with `&&`. This assist can only be applied with the cursor | ||
229 | on either `||` or `&&`, with both operands being a negation of some kind. | ||
230 | This means something of the form `!x` or `x != y`. | ||
231 | |||
232 | ```rust | ||
233 | // BEFORE | ||
234 | fn main() { | ||
235 | if x != 4 ||┃ !y {} | ||
236 | } | ||
237 | |||
238 | // AFTER | ||
239 | fn main() { | ||
240 | if !(x == 4 && y) {} | ||
241 | } | ||
242 | ``` | ||
243 | |||
244 | ## `auto_import` | ||
245 | |||
246 | If the name is unresolved, provides all possible imports for it. | ||
247 | |||
248 | ```rust | ||
249 | // BEFORE | ||
250 | fn main() { | ||
251 | let map = HashMap┃::new(); | ||
252 | } | ||
253 | |||
254 | // AFTER | ||
255 | use std::collections::HashMap; | ||
256 | |||
257 | fn main() { | ||
258 | let map = HashMap::new(); | ||
259 | } | ||
260 | ``` | ||
261 | |||
262 | ## `change_return_type_to_result` | ||
263 | |||
264 | Change the function's return type to Result. | ||
265 | |||
266 | ```rust | ||
267 | // BEFORE | ||
268 | fn foo() -> i32┃ { 42i32 } | ||
269 | |||
270 | // AFTER | ||
271 | fn foo() -> Result<i32, ${0:_}> { Ok(42i32) } | ||
272 | ``` | ||
273 | |||
274 | ## `change_visibility` | ||
275 | |||
276 | Adds or changes existing visibility specifier. | ||
277 | |||
278 | ```rust | ||
279 | // BEFORE | ||
280 | ┃fn frobnicate() {} | ||
281 | |||
282 | // AFTER | ||
283 | pub(crate) fn frobnicate() {} | ||
284 | ``` | ||
285 | |||
286 | ## `convert_to_guarded_return` | ||
287 | |||
288 | Replace a large conditional with a guarded return. | ||
289 | |||
290 | ```rust | ||
291 | // BEFORE | ||
292 | fn main() { | ||
293 | ┃if cond { | ||
294 | foo(); | ||
295 | bar(); | ||
296 | } | ||
297 | } | ||
298 | |||
299 | // AFTER | ||
300 | fn main() { | ||
301 | if !cond { | ||
302 | return; | ||
303 | } | ||
304 | foo(); | ||
305 | bar(); | ||
306 | } | ||
307 | ``` | ||
308 | |||
309 | ## `fill_match_arms` | ||
310 | |||
311 | Adds missing clauses to a `match` expression. | ||
312 | |||
313 | ```rust | ||
314 | // BEFORE | ||
315 | enum Action { Move { distance: u32 }, Stop } | ||
316 | |||
317 | fn handle(action: Action) { | ||
318 | match action { | ||
319 | ┃ | ||
320 | } | ||
321 | } | ||
322 | |||
323 | // AFTER | ||
324 | enum Action { Move { distance: u32 }, Stop } | ||
325 | |||
326 | fn handle(action: Action) { | ||
327 | match action { | ||
328 | $0Action::Move { distance } => {} | ||
329 | Action::Stop => {} | ||
330 | } | ||
331 | } | ||
332 | ``` | ||
333 | |||
334 | ## `fix_visibility` | ||
335 | |||
336 | Makes inaccessible item public. | ||
337 | |||
338 | ```rust | ||
339 | // BEFORE | ||
340 | mod m { | ||
341 | fn frobnicate() {} | ||
342 | } | ||
343 | fn main() { | ||
344 | m::frobnicate┃() {} | ||
345 | } | ||
346 | |||
347 | // AFTER | ||
348 | mod m { | ||
349 | $0pub(crate) fn frobnicate() {} | ||
350 | } | ||
351 | fn main() { | ||
352 | m::frobnicate() {} | ||
353 | } | ||
354 | ``` | ||
355 | |||
356 | ## `flip_binexpr` | ||
357 | |||
358 | Flips operands of a binary expression. | ||
359 | |||
360 | ```rust | ||
361 | // BEFORE | ||
362 | fn main() { | ||
363 | let _ = 90 +┃ 2; | ||
364 | } | ||
365 | |||
366 | // AFTER | ||
367 | fn main() { | ||
368 | let _ = 2 + 90; | ||
369 | } | ||
370 | ``` | ||
371 | |||
372 | ## `flip_comma` | ||
373 | |||
374 | Flips two comma-separated items. | ||
375 | |||
376 | ```rust | ||
377 | // BEFORE | ||
378 | fn main() { | ||
379 | ((1, 2),┃ (3, 4)); | ||
380 | } | ||
381 | |||
382 | // AFTER | ||
383 | fn main() { | ||
384 | ((3, 4), (1, 2)); | ||
385 | } | ||
386 | ``` | ||
387 | |||
388 | ## `flip_trait_bound` | ||
389 | |||
390 | Flips two trait bounds. | ||
391 | |||
392 | ```rust | ||
393 | // BEFORE | ||
394 | fn foo<T: Clone +┃ Copy>() { } | ||
395 | |||
396 | // AFTER | ||
397 | fn foo<T: Copy + Clone>() { } | ||
398 | ``` | ||
399 | |||
400 | ## `inline_local_variable` | ||
401 | |||
402 | Inlines local variable. | ||
403 | |||
404 | ```rust | ||
405 | // BEFORE | ||
406 | fn main() { | ||
407 | let x┃ = 1 + 2; | ||
408 | x * 4; | ||
409 | } | ||
410 | |||
411 | // AFTER | ||
412 | fn main() { | ||
413 | (1 + 2) * 4; | ||
414 | } | ||
415 | ``` | ||
416 | |||
417 | ## `introduce_variable` | ||
418 | |||
419 | Extracts subexpression into a variable. | ||
420 | |||
421 | ```rust | ||
422 | // BEFORE | ||
423 | fn main() { | ||
424 | ┃(1 + 2)┃ * 4; | ||
425 | } | ||
426 | |||
427 | // AFTER | ||
428 | fn main() { | ||
429 | let $0var_name = (1 + 2); | ||
430 | var_name * 4; | ||
431 | } | ||
432 | ``` | ||
433 | |||
434 | ## `invert_if` | ||
435 | |||
436 | Apply invert_if | ||
437 | This transforms if expressions of the form `if !x {A} else {B}` into `if x {B} else {A}` | ||
438 | This also works with `!=`. This assist can only be applied with the cursor | ||
439 | on `if`. | ||
440 | |||
441 | ```rust | ||
442 | // BEFORE | ||
443 | fn main() { | ||
444 | if┃ !y { A } else { B } | ||
445 | } | ||
446 | |||
447 | // AFTER | ||
448 | fn main() { | ||
449 | if y { B } else { A } | ||
450 | } | ||
451 | ``` | ||
452 | |||
453 | ## `make_raw_string` | ||
454 | |||
455 | Adds `r#` to a plain string literal. | ||
456 | |||
457 | ```rust | ||
458 | // BEFORE | ||
459 | fn main() { | ||
460 | "Hello,┃ World!"; | ||
461 | } | ||
462 | |||
463 | // AFTER | ||
464 | fn main() { | ||
465 | r#"Hello, World!"#; | ||
466 | } | ||
467 | ``` | ||
468 | |||
469 | ## `make_usual_string` | ||
470 | |||
471 | Turns a raw string into a plain string. | ||
472 | |||
473 | ```rust | ||
474 | // BEFORE | ||
475 | fn main() { | ||
476 | r#"Hello,┃ "World!""#; | ||
477 | } | ||
478 | |||
479 | // AFTER | ||
480 | fn main() { | ||
481 | "Hello, \"World!\""; | ||
482 | } | ||
483 | ``` | ||
484 | |||
485 | ## `merge_imports` | ||
486 | |||
487 | Merges two imports with a common prefix. | ||
488 | |||
489 | ```rust | ||
490 | // BEFORE | ||
491 | use std::┃fmt::Formatter; | ||
492 | use std::io; | ||
493 | |||
494 | // AFTER | ||
495 | use std::{fmt::Formatter, io}; | ||
496 | ``` | ||
497 | |||
498 | ## `merge_match_arms` | ||
499 | |||
500 | Merges identical match arms. | ||
501 | |||
502 | ```rust | ||
503 | // BEFORE | ||
504 | enum Action { Move { distance: u32 }, Stop } | ||
505 | |||
506 | fn handle(action: Action) { | ||
507 | match action { | ||
508 | ┃Action::Move(..) => foo(), | ||
509 | Action::Stop => foo(), | ||
510 | } | ||
511 | } | ||
512 | |||
513 | // AFTER | ||
514 | enum Action { Move { distance: u32 }, Stop } | ||
515 | |||
516 | fn handle(action: Action) { | ||
517 | match action { | ||
518 | Action::Move(..) | Action::Stop => foo(), | ||
519 | } | ||
520 | } | ||
521 | ``` | ||
522 | |||
523 | ## `move_arm_cond_to_match_guard` | ||
524 | |||
525 | Moves if expression from match arm body into a guard. | ||
526 | |||
527 | ```rust | ||
528 | // BEFORE | ||
529 | enum Action { Move { distance: u32 }, Stop } | ||
530 | |||
531 | fn handle(action: Action) { | ||
532 | match action { | ||
533 | Action::Move { distance } => ┃if distance > 10 { foo() }, | ||
534 | _ => (), | ||
535 | } | ||
536 | } | ||
537 | |||
538 | // AFTER | ||
539 | enum Action { Move { distance: u32 }, Stop } | ||
540 | |||
541 | fn handle(action: Action) { | ||
542 | match action { | ||
543 | Action::Move { distance } if distance > 10 => foo(), | ||
544 | _ => (), | ||
545 | } | ||
546 | } | ||
547 | ``` | ||
548 | |||
549 | ## `move_bounds_to_where_clause` | ||
550 | |||
551 | Moves inline type bounds to a where clause. | ||
552 | |||
553 | ```rust | ||
554 | // BEFORE | ||
555 | fn apply<T, U, ┃F: FnOnce(T) -> U>(f: F, x: T) -> U { | ||
556 | f(x) | ||
557 | } | ||
558 | |||
559 | // AFTER | ||
560 | fn apply<T, U, F>(f: F, x: T) -> U where F: FnOnce(T) -> U { | ||
561 | f(x) | ||
562 | } | ||
563 | ``` | ||
564 | |||
565 | ## `move_guard_to_arm_body` | ||
566 | |||
567 | Moves match guard into match arm body. | ||
568 | |||
569 | ```rust | ||
570 | // BEFORE | ||
571 | enum Action { Move { distance: u32 }, Stop } | ||
572 | |||
573 | fn handle(action: Action) { | ||
574 | match action { | ||
575 | Action::Move { distance } ┃if distance > 10 => foo(), | ||
576 | _ => (), | ||
577 | } | ||
578 | } | ||
579 | |||
580 | // AFTER | ||
581 | enum Action { Move { distance: u32 }, Stop } | ||
582 | |||
583 | fn handle(action: Action) { | ||
584 | match action { | ||
585 | Action::Move { distance } => if distance > 10 { foo() }, | ||
586 | _ => (), | ||
587 | } | ||
588 | } | ||
589 | ``` | ||
590 | |||
591 | ## `remove_dbg` | ||
592 | |||
593 | Removes `dbg!()` macro call. | ||
594 | |||
595 | ```rust | ||
596 | // BEFORE | ||
597 | fn main() { | ||
598 | ┃dbg!(92); | ||
599 | } | ||
600 | |||
601 | // AFTER | ||
602 | fn main() { | ||
603 | 92; | ||
604 | } | ||
605 | ``` | ||
606 | |||
607 | ## `remove_hash` | ||
608 | |||
609 | Removes a hash from a raw string literal. | ||
610 | |||
611 | ```rust | ||
612 | // BEFORE | ||
613 | fn main() { | ||
614 | r#"Hello,┃ World!"#; | ||
615 | } | ||
616 | |||
617 | // AFTER | ||
618 | fn main() { | ||
619 | r"Hello, World!"; | ||
620 | } | ||
621 | ``` | ||
622 | |||
623 | ## `remove_mut` | ||
624 | |||
625 | Removes the `mut` keyword. | ||
626 | |||
627 | ```rust | ||
628 | // BEFORE | ||
629 | impl Walrus { | ||
630 | fn feed(&mut┃ self, amount: u32) {} | ||
631 | } | ||
632 | |||
633 | // AFTER | ||
634 | impl Walrus { | ||
635 | fn feed(&self, amount: u32) {} | ||
636 | } | ||
637 | ``` | ||
638 | |||
639 | ## `reorder_fields` | ||
640 | |||
641 | Reorder the fields of record literals and record patterns in the same order as in | ||
642 | the definition. | ||
643 | |||
644 | ```rust | ||
645 | // BEFORE | ||
646 | struct Foo {foo: i32, bar: i32}; | ||
647 | const test: Foo = ┃Foo {bar: 0, foo: 1} | ||
648 | |||
649 | // AFTER | ||
650 | struct Foo {foo: i32, bar: i32}; | ||
651 | const test: Foo = Foo {foo: 1, bar: 0} | ||
652 | ``` | ||
653 | |||
654 | ## `replace_if_let_with_match` | ||
655 | |||
656 | Replaces `if let` with an else branch with a `match` expression. | ||
657 | |||
658 | ```rust | ||
659 | // BEFORE | ||
660 | enum Action { Move { distance: u32 }, Stop } | ||
661 | |||
662 | fn handle(action: Action) { | ||
663 | ┃if let Action::Move { distance } = action { | ||
664 | foo(distance) | ||
665 | } else { | ||
666 | bar() | ||
667 | } | ||
668 | } | ||
669 | |||
670 | // AFTER | ||
671 | enum Action { Move { distance: u32 }, Stop } | ||
672 | |||
673 | fn handle(action: Action) { | ||
674 | match action { | ||
675 | Action::Move { distance } => foo(distance), | ||
676 | _ => bar(), | ||
677 | } | ||
678 | } | ||
679 | ``` | ||
680 | |||
681 | ## `replace_let_with_if_let` | ||
682 | |||
683 | Replaces `let` with an `if-let`. | ||
684 | |||
685 | ```rust | ||
686 | // BEFORE | ||
687 | |||
688 | fn main(action: Action) { | ||
689 | ┃let x = compute(); | ||
690 | } | ||
691 | |||
692 | fn compute() -> Option<i32> { None } | ||
693 | |||
694 | // AFTER | ||
695 | |||
696 | fn main(action: Action) { | ||
697 | if let Some(x) = compute() { | ||
698 | } | ||
699 | } | ||
700 | |||
701 | fn compute() -> Option<i32> { None } | ||
702 | ``` | ||
703 | |||
704 | ## `replace_qualified_name_with_use` | ||
705 | |||
706 | Adds a use statement for a given fully-qualified name. | ||
707 | |||
708 | ```rust | ||
709 | // BEFORE | ||
710 | fn process(map: std::collections::┃HashMap<String, String>) {} | ||
711 | |||
712 | // AFTER | ||
713 | use std::collections::HashMap; | ||
714 | |||
715 | fn process(map: HashMap<String, String>) {} | ||
716 | ``` | ||
717 | |||
718 | ## `replace_unwrap_with_match` | ||
719 | |||
720 | Replaces `unwrap` a `match` expression. Works for Result and Option. | ||
721 | |||
722 | ```rust | ||
723 | // BEFORE | ||
724 | enum Result<T, E> { Ok(T), Err(E) } | ||
725 | fn main() { | ||
726 | let x: Result<i32, i32> = Result::Ok(92); | ||
727 | let y = x.┃unwrap(); | ||
728 | } | ||
729 | |||
730 | // AFTER | ||
731 | enum Result<T, E> { Ok(T), Err(E) } | ||
732 | fn main() { | ||
733 | let x: Result<i32, i32> = Result::Ok(92); | ||
734 | let y = match x { | ||
735 | Ok(a) => a, | ||
736 | $0_ => unreachable!(), | ||
737 | }; | ||
738 | } | ||
739 | ``` | ||
740 | |||
741 | ## `split_import` | ||
742 | |||
743 | Wraps the tail of import into braces. | ||
744 | |||
745 | ```rust | ||
746 | // BEFORE | ||
747 | use std::┃collections::HashMap; | ||
748 | |||
749 | // AFTER | ||
750 | use std::{collections::HashMap}; | ||
751 | ``` | ||
752 | |||
753 | ## `unwrap_block` | ||
754 | |||
755 | This assist removes if...else, for, while and loop control statements to just keep the body. | ||
756 | |||
757 | ```rust | ||
758 | // BEFORE | ||
759 | fn foo() { | ||
760 | if true {┃ | ||
761 | println!("foo"); | ||
762 | } | ||
763 | } | ||
764 | |||
765 | // AFTER | ||
766 | fn foo() { | ||
767 | println!("foo"); | ||
768 | } | ||
769 | ``` | ||