diff options
author | Aleksey Kladov <[email protected]> | 2019-10-27 08:26:46 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-10-27 08:26:46 +0000 |
commit | a490ba06fa635ecb34b5ce0b7205621eefaee603 (patch) | |
tree | 3793da47dbe724189842cabeb5a7cef91ad592ef | |
parent | 85984b09e182adca4b03d9f7efab20d48b5b632a (diff) |
document some more assists
-rw-r--r-- | crates/ra_assists/src/assists/merge_match_arms.rs | 26 | ||||
-rw-r--r-- | crates/ra_assists/src/assists/move_bounds.rs | 17 | ||||
-rw-r--r-- | crates/ra_assists/src/assists/move_guard.rs | 52 | ||||
-rw-r--r-- | crates/ra_assists/src/doc_tests/generated.rs | 97 | ||||
-rw-r--r-- | docs/user/assists.md | 93 | ||||
-rw-r--r-- | docs/user/features.md | 84 |
6 files changed, 279 insertions, 90 deletions
diff --git a/crates/ra_assists/src/assists/merge_match_arms.rs b/crates/ra_assists/src/assists/merge_match_arms.rs index 17baa98f9..f5ddd7159 100644 --- a/crates/ra_assists/src/assists/merge_match_arms.rs +++ b/crates/ra_assists/src/assists/merge_match_arms.rs | |||
@@ -1,9 +1,31 @@ | |||
1 | //! FIXME: write short doc here | ||
2 | |||
3 | use crate::{Assist, AssistCtx, AssistId, TextRange, TextUnit}; | 1 | use crate::{Assist, AssistCtx, AssistId, TextRange, TextUnit}; |
4 | use hir::db::HirDatabase; | 2 | use hir::db::HirDatabase; |
5 | use ra_syntax::ast::{AstNode, MatchArm}; | 3 | use ra_syntax::ast::{AstNode, MatchArm}; |
6 | 4 | ||
5 | // Assist: merge_match_arms | ||
6 | // | ||
7 | // Merges identical match arms. | ||
8 | // | ||
9 | // ``` | ||
10 | // enum Action { Move { distance: u32 }, Stop } | ||
11 | // | ||
12 | // fn handle(action: Action) { | ||
13 | // match action { | ||
14 | // <|>Action::Move(..) => foo(), | ||
15 | // Action::Stop => foo(), | ||
16 | // } | ||
17 | // } | ||
18 | // ``` | ||
19 | // -> | ||
20 | // ``` | ||
21 | // enum Action { Move { distance: u32 }, Stop } | ||
22 | // | ||
23 | // fn handle(action: Action) { | ||
24 | // match action { | ||
25 | // Action::Move(..) | Action::Stop => foo(), | ||
26 | // } | ||
27 | // } | ||
28 | // ``` | ||
7 | pub(crate) fn merge_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 29 | pub(crate) fn merge_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
8 | let current_arm = ctx.node_at_offset::<MatchArm>()?; | 30 | let current_arm = ctx.node_at_offset::<MatchArm>()?; |
9 | 31 | ||
diff --git a/crates/ra_assists/src/assists/move_bounds.rs b/crates/ra_assists/src/assists/move_bounds.rs index d2444b6b9..f96e19a9f 100644 --- a/crates/ra_assists/src/assists/move_bounds.rs +++ b/crates/ra_assists/src/assists/move_bounds.rs | |||
@@ -1,5 +1,3 @@ | |||
1 | //! FIXME: write short doc here | ||
2 | |||
3 | use hir::db::HirDatabase; | 1 | use hir::db::HirDatabase; |
4 | use ra_syntax::{ | 2 | use ra_syntax::{ |
5 | ast::{self, edit, make, AstNode, NameOwner, TypeBoundsOwner}, | 3 | ast::{self, edit, make, AstNode, NameOwner, TypeBoundsOwner}, |
@@ -9,6 +7,21 @@ use ra_syntax::{ | |||
9 | 7 | ||
10 | use crate::{Assist, AssistCtx, AssistId}; | 8 | use crate::{Assist, AssistCtx, AssistId}; |
11 | 9 | ||
10 | // Assist: move_bounds_to_where_clause | ||
11 | // | ||
12 | // Moves inline type bounds to a where clause. | ||
13 | // | ||
14 | // ``` | ||
15 | // fn apply<T, U, <|>F: FnOnce(T) -> U>(f: F, x: T) -> U { | ||
16 | // f(x) | ||
17 | // } | ||
18 | // ``` | ||
19 | // -> | ||
20 | // ``` | ||
21 | // fn apply<T, U, F>(f: F, x: T) -> U where F: FnOnce(T) -> U { | ||
22 | // f(x) | ||
23 | // } | ||
24 | // ``` | ||
12 | pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 25 | pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
13 | let type_param_list = ctx.node_at_offset::<ast::TypeParamList>()?; | 26 | let type_param_list = ctx.node_at_offset::<ast::TypeParamList>()?; |
14 | 27 | ||
diff --git a/crates/ra_assists/src/assists/move_guard.rs b/crates/ra_assists/src/assists/move_guard.rs index 51aea6334..36c95128d 100644 --- a/crates/ra_assists/src/assists/move_guard.rs +++ b/crates/ra_assists/src/assists/move_guard.rs | |||
@@ -1,5 +1,3 @@ | |||
1 | //! FIXME: write short doc here | ||
2 | |||
3 | use hir::db::HirDatabase; | 1 | use hir::db::HirDatabase; |
4 | use ra_syntax::{ | 2 | use ra_syntax::{ |
5 | ast, | 3 | ast, |
@@ -9,6 +7,31 @@ use ra_syntax::{ | |||
9 | 7 | ||
10 | use crate::{Assist, AssistCtx, AssistId}; | 8 | use crate::{Assist, AssistCtx, AssistId}; |
11 | 9 | ||
10 | // Assist: move_guard_to_arm_body | ||
11 | // | ||
12 | // Moves match guard into match arm body. | ||
13 | // | ||
14 | // ``` | ||
15 | // enum Action { Move { distance: u32 }, Stop } | ||
16 | // | ||
17 | // fn handle(action: Action) { | ||
18 | // match action { | ||
19 | // Action::Move { distance } <|>if distance > 10 => foo(), | ||
20 | // _ => (), | ||
21 | // } | ||
22 | // } | ||
23 | // ``` | ||
24 | // -> | ||
25 | // ``` | ||
26 | // enum Action { Move { distance: u32 }, Stop } | ||
27 | // | ||
28 | // fn handle(action: Action) { | ||
29 | // match action { | ||
30 | // Action::Move { distance } => if distance > 10 { foo() }, | ||
31 | // _ => (), | ||
32 | // } | ||
33 | // } | ||
34 | // ``` | ||
12 | pub(crate) fn move_guard_to_arm_body(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 35 | pub(crate) fn move_guard_to_arm_body(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
13 | let match_arm = ctx.node_at_offset::<MatchArm>()?; | 36 | let match_arm = ctx.node_at_offset::<MatchArm>()?; |
14 | let guard = match_arm.guard()?; | 37 | let guard = match_arm.guard()?; |
@@ -42,6 +65,31 @@ pub(crate) fn move_guard_to_arm_body(mut ctx: AssistCtx<impl HirDatabase>) -> Op | |||
42 | ctx.build() | 65 | ctx.build() |
43 | } | 66 | } |
44 | 67 | ||
68 | // Assist: move_arm_cond_to_match_guard | ||
69 | // | ||
70 | // Moves if expression from match arm body into a guard. | ||
71 | // | ||
72 | // ``` | ||
73 | // enum Action { Move { distance: u32 }, Stop } | ||
74 | // | ||
75 | // fn handle(action: Action) { | ||
76 | // match action { | ||
77 | // Action::Move { distance } => <|>if distance > 10 { foo() }, | ||
78 | // _ => (), | ||
79 | // } | ||
80 | // } | ||
81 | // ``` | ||
82 | // -> | ||
83 | // ``` | ||
84 | // enum Action { Move { distance: u32 }, Stop } | ||
85 | // | ||
86 | // fn handle(action: Action) { | ||
87 | // match action { | ||
88 | // Action::Move { distance } if distance > 10 => foo(), | ||
89 | // _ => (), | ||
90 | // } | ||
91 | // } | ||
92 | // ``` | ||
45 | pub(crate) fn move_arm_cond_to_match_guard(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 93 | pub(crate) fn move_arm_cond_to_match_guard(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
46 | let match_arm: MatchArm = ctx.node_at_offset::<MatchArm>()?; | 94 | let match_arm: MatchArm = ctx.node_at_offset::<MatchArm>()?; |
47 | let last_match_pat = match_arm.pats().last()?; | 95 | let last_match_pat = match_arm.pats().last()?; |
diff --git a/crates/ra_assists/src/doc_tests/generated.rs b/crates/ra_assists/src/doc_tests/generated.rs index b96d5772e..09677af68 100644 --- a/crates/ra_assists/src/doc_tests/generated.rs +++ b/crates/ra_assists/src/doc_tests/generated.rs | |||
@@ -273,3 +273,100 @@ fn main() { | |||
273 | "#####, | 273 | "#####, |
274 | ) | 274 | ) |
275 | } | 275 | } |
276 | |||
277 | #[test] | ||
278 | fn doctest_merge_match_arms() { | ||
279 | check( | ||
280 | "merge_match_arms", | ||
281 | r#####" | ||
282 | enum Action { Move { distance: u32 }, Stop } | ||
283 | |||
284 | fn handle(action: Action) { | ||
285 | match action { | ||
286 | <|>Action::Move(..) => foo(), | ||
287 | Action::Stop => foo(), | ||
288 | } | ||
289 | } | ||
290 | "#####, | ||
291 | r#####" | ||
292 | enum Action { Move { distance: u32 }, Stop } | ||
293 | |||
294 | fn handle(action: Action) { | ||
295 | match action { | ||
296 | Action::Move(..) | Action::Stop => foo(), | ||
297 | } | ||
298 | } | ||
299 | "#####, | ||
300 | ) | ||
301 | } | ||
302 | |||
303 | #[test] | ||
304 | fn doctest_move_arm_cond_to_match_guard() { | ||
305 | check( | ||
306 | "move_arm_cond_to_match_guard", | ||
307 | r#####" | ||
308 | enum Action { Move { distance: u32 }, Stop } | ||
309 | |||
310 | fn handle(action: Action) { | ||
311 | match action { | ||
312 | Action::Move { distance } => <|>if distance > 10 { foo() }, | ||
313 | _ => (), | ||
314 | } | ||
315 | } | ||
316 | "#####, | ||
317 | r#####" | ||
318 | enum Action { Move { distance: u32 }, Stop } | ||
319 | |||
320 | fn handle(action: Action) { | ||
321 | match action { | ||
322 | Action::Move { distance } if distance > 10 => foo(), | ||
323 | _ => (), | ||
324 | } | ||
325 | } | ||
326 | "#####, | ||
327 | ) | ||
328 | } | ||
329 | |||
330 | #[test] | ||
331 | fn doctest_move_bounds_to_where_clause() { | ||
332 | check( | ||
333 | "move_bounds_to_where_clause", | ||
334 | r#####" | ||
335 | fn apply<T, U, <|>F: FnOnce(T) -> U>(f: F, x: T) -> U { | ||
336 | f(x) | ||
337 | } | ||
338 | "#####, | ||
339 | r#####" | ||
340 | fn apply<T, U, F>(f: F, x: T) -> U where F: FnOnce(T) -> U { | ||
341 | f(x) | ||
342 | } | ||
343 | "#####, | ||
344 | ) | ||
345 | } | ||
346 | |||
347 | #[test] | ||
348 | fn doctest_move_guard_to_arm_body() { | ||
349 | check( | ||
350 | "move_guard_to_arm_body", | ||
351 | r#####" | ||
352 | enum Action { Move { distance: u32 }, Stop } | ||
353 | |||
354 | fn handle(action: Action) { | ||
355 | match action { | ||
356 | Action::Move { distance } <|>if distance > 10 => foo(), | ||
357 | _ => (), | ||
358 | } | ||
359 | } | ||
360 | "#####, | ||
361 | r#####" | ||
362 | enum Action { Move { distance: u32 }, Stop } | ||
363 | |||
364 | fn handle(action: Action) { | ||
365 | match action { | ||
366 | Action::Move { distance } => if distance > 10 { foo() }, | ||
367 | _ => (), | ||
368 | } | ||
369 | } | ||
370 | "#####, | ||
371 | ) | ||
372 | } | ||
diff --git a/docs/user/assists.md b/docs/user/assists.md index ee1cfa142..34a95696c 100644 --- a/docs/user/assists.md +++ b/docs/user/assists.md | |||
@@ -265,3 +265,96 @@ fn main() { | |||
265 | var_name * 4; | 265 | var_name * 4; |
266 | } | 266 | } |
267 | ``` | 267 | ``` |
268 | |||
269 | ## `merge_match_arms` | ||
270 | |||
271 | Merges identical match arms. | ||
272 | |||
273 | ```rust | ||
274 | // BEFORE | ||
275 | enum Action { Move { distance: u32 }, Stop } | ||
276 | |||
277 | fn handle(action: Action) { | ||
278 | match action { | ||
279 | ┃Action::Move(..) => foo(), | ||
280 | Action::Stop => foo(), | ||
281 | } | ||
282 | } | ||
283 | |||
284 | // AFTER | ||
285 | enum Action { Move { distance: u32 }, Stop } | ||
286 | |||
287 | fn handle(action: Action) { | ||
288 | match action { | ||
289 | Action::Move(..) | Action::Stop => foo(), | ||
290 | } | ||
291 | } | ||
292 | ``` | ||
293 | |||
294 | ## `move_arm_cond_to_match_guard` | ||
295 | |||
296 | Moves if expression from match arm body into a guard. | ||
297 | |||
298 | ```rust | ||
299 | // BEFORE | ||
300 | enum Action { Move { distance: u32 }, Stop } | ||
301 | |||
302 | fn handle(action: Action) { | ||
303 | match action { | ||
304 | Action::Move { distance } => ┃if distance > 10 { foo() }, | ||
305 | _ => (), | ||
306 | } | ||
307 | } | ||
308 | |||
309 | // AFTER | ||
310 | enum Action { Move { distance: u32 }, Stop } | ||
311 | |||
312 | fn handle(action: Action) { | ||
313 | match action { | ||
314 | Action::Move { distance } if distance > 10 => foo(), | ||
315 | _ => (), | ||
316 | } | ||
317 | } | ||
318 | ``` | ||
319 | |||
320 | ## `move_bounds_to_where_clause` | ||
321 | |||
322 | Moves inline type bounds to a where clause. | ||
323 | |||
324 | ```rust | ||
325 | // BEFORE | ||
326 | fn apply<T, U, ┃F: FnOnce(T) -> U>(f: F, x: T) -> U { | ||
327 | f(x) | ||
328 | } | ||
329 | |||
330 | // AFTER | ||
331 | fn apply<T, U, F>(f: F, x: T) -> U where F: FnOnce(T) -> U { | ||
332 | f(x) | ||
333 | } | ||
334 | ``` | ||
335 | |||
336 | ## `move_guard_to_arm_body` | ||
337 | |||
338 | Moves match guard into match arm body. | ||
339 | |||
340 | ```rust | ||
341 | // BEFORE | ||
342 | enum Action { Move { distance: u32 }, Stop } | ||
343 | |||
344 | fn handle(action: Action) { | ||
345 | match action { | ||
346 | Action::Move { distance } ┃if distance > 10 => foo(), | ||
347 | _ => (), | ||
348 | } | ||
349 | } | ||
350 | |||
351 | // AFTER | ||
352 | enum Action { Move { distance: u32 }, Stop } | ||
353 | |||
354 | fn handle(action: Action) { | ||
355 | match action { | ||
356 | Action::Move { distance } => if distance > 10 { foo() }, | ||
357 | _ => (), | ||
358 | } | ||
359 | } | ||
360 | ``` | ||
diff --git a/docs/user/features.md b/docs/user/features.md index 39dab710d..2e213e34c 100644 --- a/docs/user/features.md +++ b/docs/user/features.md | |||
@@ -154,45 +154,6 @@ fn main() { | |||
154 | } | 154 | } |
155 | ``` | 155 | ``` |
156 | 156 | ||
157 | - Flip `,` | ||
158 | |||
159 | ```rust | ||
160 | // before: | ||
161 | fn foo(x: usize,<|> dim: (usize, usize)) {} | ||
162 | // after: | ||
163 | fn foo(dim: (usize, usize), x: usize) {} | ||
164 | ``` | ||
165 | |||
166 | - Introduce variable: | ||
167 | |||
168 | ```rust | ||
169 | // before: | ||
170 | fn foo() { | ||
171 | foo(<|>1 + 1<|>); | ||
172 | } | ||
173 | |||
174 | // after: | ||
175 | fn foo() { | ||
176 | let var_name = 1 + 1; | ||
177 | foo(var_name); | ||
178 | } | ||
179 | ``` | ||
180 | |||
181 | - Inline local variable: | ||
182 | |||
183 | ```rust | ||
184 | // before: | ||
185 | fn foo() { | ||
186 | let a<|> = 1 + 1; | ||
187 | let b = a * 10; | ||
188 | } | ||
189 | |||
190 | // after: | ||
191 | fn foo() { | ||
192 | let b = (1 + 1) * 10; | ||
193 | } | ||
194 | ``` | ||
195 | |||
196 | - Remove `dbg!` | 157 | - Remove `dbg!` |
197 | 158 | ||
198 | ```rust | 159 | ```rust |
@@ -245,41 +206,6 @@ use crate:<|>:db::{RootDatabase, FileSymbol}; | |||
245 | use crate::{<|>db::{RootDatabase, FileSymbol}}; | 206 | use crate::{<|>db::{RootDatabase, FileSymbol}}; |
246 | ``` | 207 | ``` |
247 | 208 | ||
248 | - Flip binary expression | ||
249 | |||
250 | ```rust | ||
251 | // before: | ||
252 | fn foo() { | ||
253 | if 1 <<|> 2 { | ||
254 | println!("Who would have thought?"); | ||
255 | } | ||
256 | } | ||
257 | // after: | ||
258 | fn foo() { | ||
259 | if 2 ><|> 1 { | ||
260 | println!("Who would have thought?"); | ||
261 | } | ||
262 | } | ||
263 | ``` | ||
264 | |||
265 | - Move guard expression to match arm body | ||
266 | ```rust | ||
267 | // before: | ||
268 | fn f() { | ||
269 | match x { | ||
270 | <|>y @ 4 | y @ 5 if y > 5 => true, | ||
271 | _ => false | ||
272 | } | ||
273 | } | ||
274 | // after: | ||
275 | fn f() { | ||
276 | match x { | ||
277 | y @ 4 | y @ 5 => if y > 5 { <|>true }, | ||
278 | _ => false | ||
279 | } | ||
280 | } | ||
281 | ``` | ||
282 | |||
283 | - Move if condition to match arm guard | 209 | - Move if condition to match arm guard |
284 | ```rust | 210 | ```rust |
285 | // before: | 211 | // before: |
@@ -309,16 +235,6 @@ fn f() { | |||
309 | } | 235 | } |
310 | ``` | 236 | ``` |
311 | 237 | ||
312 | - Move type bounds to where clause | ||
313 | |||
314 | ```rust | ||
315 | // before: | ||
316 | fn foo<T: u32, F: FnOnce(T) -> T>() {} | ||
317 | |||
318 | // after: | ||
319 | fn foo<T, F>() where T: u32, F: FnOnce(T) -> T {} | ||
320 | ``` | ||
321 | |||
322 | - Make raw string unescaped | 238 | - Make raw string unescaped |
323 | 239 | ||
324 | ```rust | 240 | ```rust |