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