aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/handlers
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-05-20 13:00:37 +0100
committerAleksey Kladov <[email protected]>2020-05-20 13:01:10 +0100
commitc446fd76a2a6191adce87b20707a37bd46cb85a9 (patch)
tree3ec14623c37c8c34f85ac40528c354d6d2379712 /crates/ra_assists/src/handlers
parenta622b54ac029994328a99e30ee3169c21de498b7 (diff)
Snippetify fill_match_arms
Diffstat (limited to 'crates/ra_assists/src/handlers')
-rw-r--r--crates/ra_assists/src/handlers/fill_match_arms.rs193
1 files changed, 77 insertions, 116 deletions
diff --git a/crates/ra_assists/src/handlers/fill_match_arms.rs b/crates/ra_assists/src/handlers/fill_match_arms.rs
index bbdcdc626..cc303285b 100644
--- a/crates/ra_assists/src/handlers/fill_match_arms.rs
+++ b/crates/ra_assists/src/handlers/fill_match_arms.rs
@@ -6,7 +6,10 @@ use ra_ide_db::RootDatabase;
6use ra_syntax::ast::{self, make, AstNode, MatchArm, NameOwner, Pat}; 6use ra_syntax::ast::{self, make, AstNode, MatchArm, NameOwner, Pat};
7use test_utils::mark; 7use test_utils::mark;
8 8
9use crate::{utils::FamousDefs, AssistContext, AssistId, Assists}; 9use crate::{
10 utils::{render_snippet, Cursor, FamousDefs},
11 AssistContext, AssistId, Assists,
12};
10 13
11// Assist: fill_match_arms 14// Assist: fill_match_arms
12// 15//
@@ -27,7 +30,7 @@ use crate::{utils::FamousDefs, AssistContext, AssistId, Assists};
27// 30//
28// fn handle(action: Action) { 31// fn handle(action: Action) {
29// match action { 32// match action {
30// Action::Move { distance } => {} 33// $0Action::Move { distance } => {}
31// Action::Stop => {} 34// Action::Stop => {}
32// } 35// }
33// } 36// }
@@ -100,10 +103,23 @@ pub(crate) fn fill_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Option<
100 } 103 }
101 104
102 let target = match_expr.syntax().text_range(); 105 let target = match_expr.syntax().text_range();
103 acc.add(AssistId("fill_match_arms"), "Fill match arms", target, |edit| { 106 acc.add(AssistId("fill_match_arms"), "Fill match arms", target, |builder| {
104 let new_arm_list = match_arm_list.remove_placeholder().append_arms(missing_arms); 107 let new_arm_list = match_arm_list.remove_placeholder();
105 edit.set_cursor(expr.syntax().text_range().start()); 108 let n_old_arms = new_arm_list.arms().count();
106 edit.replace_ast(match_arm_list, new_arm_list); 109 let new_arm_list = new_arm_list.append_arms(missing_arms);
110 let first_new_arm = new_arm_list.arms().nth(n_old_arms);
111 let old_range = match_arm_list.syntax().text_range();
112 match (first_new_arm, ctx.config.snippet_cap) {
113 (Some(first_new_arm), Some(cap)) => {
114 let snippet = render_snippet(
115 cap,
116 new_arm_list.syntax(),
117 Cursor::Before(first_new_arm.syntax()),
118 );
119 builder.replace_snippet(cap, old_range, snippet);
120 }
121 _ => builder.replace(old_range, new_arm_list.to_string()),
122 }
107 }) 123 })
108} 124}
109 125
@@ -226,12 +242,12 @@ mod tests {
226 r#" 242 r#"
227 enum A { 243 enum A {
228 As, 244 As,
229 Bs{x:i32, y:Option<i32>}, 245 Bs { x: i32, y: Option<i32> },
230 Cs(i32, Option<i32>), 246 Cs(i32, Option<i32>),
231 } 247 }
232 fn main() { 248 fn main() {
233 match A::As<|> { 249 match A::As<|> {
234 A::Bs{x,y:Some(_)} => {} 250 A::Bs { x, y: Some(_) } => {}
235 A::Cs(_, Some(_)) => {} 251 A::Cs(_, Some(_)) => {}
236 } 252 }
237 } 253 }
@@ -239,14 +255,14 @@ mod tests {
239 r#" 255 r#"
240 enum A { 256 enum A {
241 As, 257 As,
242 Bs{x:i32, y:Option<i32>}, 258 Bs { x: i32, y: Option<i32> },
243 Cs(i32, Option<i32>), 259 Cs(i32, Option<i32>),
244 } 260 }
245 fn main() { 261 fn main() {
246 match <|>A::As { 262 match A::As {
247 A::Bs{x,y:Some(_)} => {} 263 A::Bs { x, y: Some(_) } => {}
248 A::Cs(_, Some(_)) => {} 264 A::Cs(_, Some(_)) => {}
249 A::As => {} 265 $0A::As => {}
250 } 266 }
251 } 267 }
252 "#, 268 "#,
@@ -276,9 +292,9 @@ mod tests {
276 Cs(Option<i32>), 292 Cs(Option<i32>),
277 } 293 }
278 fn main() { 294 fn main() {
279 match <|>A::As { 295 match A::As {
280 A::Cs(_) | A::Bs => {} 296 A::Cs(_) | A::Bs => {}
281 A::As => {} 297 $0A::As => {}
282 } 298 }
283 } 299 }
284 "#, 300 "#,
@@ -322,11 +338,11 @@ mod tests {
322 Ys, 338 Ys,
323 } 339 }
324 fn main() { 340 fn main() {
325 match <|>A::As { 341 match A::As {
326 A::Bs if 0 < 1 => {} 342 A::Bs if 0 < 1 => {}
327 A::Ds(_value) => { let x = 1; } 343 A::Ds(_value) => { let x = 1; }
328 A::Es(B::Xs) => (), 344 A::Es(B::Xs) => (),
329 A::As => {} 345 $0A::As => {}
330 A::Cs => {} 346 A::Cs => {}
331 } 347 }
332 } 348 }
@@ -344,7 +360,7 @@ mod tests {
344 Bs, 360 Bs,
345 Cs(String), 361 Cs(String),
346 Ds(String, String), 362 Ds(String, String),
347 Es{ x: usize, y: usize } 363 Es { x: usize, y: usize }
348 } 364 }
349 365
350 fn main() { 366 fn main() {
@@ -358,13 +374,13 @@ mod tests {
358 Bs, 374 Bs,
359 Cs(String), 375 Cs(String),
360 Ds(String, String), 376 Ds(String, String),
361 Es{ x: usize, y: usize } 377 Es { x: usize, y: usize }
362 } 378 }
363 379
364 fn main() { 380 fn main() {
365 let a = A::As; 381 let a = A::As;
366 match <|>a { 382 match a {
367 A::As => {} 383 $0A::As => {}
368 A::Bs => {} 384 A::Bs => {}
369 A::Cs(_) => {} 385 A::Cs(_) => {}
370 A::Ds(_, _) => {} 386 A::Ds(_, _) => {}
@@ -380,14 +396,8 @@ mod tests {
380 check_assist( 396 check_assist(
381 fill_match_arms, 397 fill_match_arms,
382 r#" 398 r#"
383 enum A { 399 enum A { One, Two }
384 One, 400 enum B { One, Two }
385 Two,
386 }
387 enum B {
388 One,
389 Two,
390 }
391 401
392 fn main() { 402 fn main() {
393 let a = A::One; 403 let a = A::One;
@@ -396,20 +406,14 @@ mod tests {
396 } 406 }
397 "#, 407 "#,
398 r#" 408 r#"
399 enum A { 409 enum A { One, Two }
400 One, 410 enum B { One, Two }
401 Two,
402 }
403 enum B {
404 One,
405 Two,
406 }
407 411
408 fn main() { 412 fn main() {
409 let a = A::One; 413 let a = A::One;
410 let b = B::One; 414 let b = B::One;
411 match <|>(a, b) { 415 match (a, b) {
412 (A::One, B::One) => {} 416 $0(A::One, B::One) => {}
413 (A::One, B::Two) => {} 417 (A::One, B::Two) => {}
414 (A::Two, B::One) => {} 418 (A::Two, B::One) => {}
415 (A::Two, B::Two) => {} 419 (A::Two, B::Two) => {}
@@ -424,14 +428,8 @@ mod tests {
424 check_assist( 428 check_assist(
425 fill_match_arms, 429 fill_match_arms,
426 r#" 430 r#"
427 enum A { 431 enum A { One, Two }
428 One, 432 enum B { One, Two }
429 Two,
430 }
431 enum B {
432 One,
433 Two,
434 }
435 433
436 fn main() { 434 fn main() {
437 let a = A::One; 435 let a = A::One;
@@ -440,20 +438,14 @@ mod tests {
440 } 438 }
441 "#, 439 "#,
442 r#" 440 r#"
443 enum A { 441 enum A { One, Two }
444 One, 442 enum B { One, Two }
445 Two,
446 }
447 enum B {
448 One,
449 Two,
450 }
451 443
452 fn main() { 444 fn main() {
453 let a = A::One; 445 let a = A::One;
454 let b = B::One; 446 let b = B::One;
455 match <|>(&a, &b) { 447 match (&a, &b) {
456 (A::One, B::One) => {} 448 $0(A::One, B::One) => {}
457 (A::One, B::Two) => {} 449 (A::One, B::Two) => {}
458 (A::Two, B::One) => {} 450 (A::Two, B::One) => {}
459 (A::Two, B::Two) => {} 451 (A::Two, B::Two) => {}
@@ -468,14 +460,8 @@ mod tests {
468 check_assist_not_applicable( 460 check_assist_not_applicable(
469 fill_match_arms, 461 fill_match_arms,
470 r#" 462 r#"
471 enum A { 463 enum A { One, Two }
472 One, 464 enum B { One, Two }
473 Two,
474 }
475 enum B {
476 One,
477 Two,
478 }
479 465
480 fn main() { 466 fn main() {
481 let a = A::One; 467 let a = A::One;
@@ -493,14 +479,8 @@ mod tests {
493 check_assist_not_applicable( 479 check_assist_not_applicable(
494 fill_match_arms, 480 fill_match_arms,
495 r#" 481 r#"
496 enum A { 482 enum A { One, Two }
497 One, 483 enum B { One, Two }
498 Two,
499 }
500 enum B {
501 One,
502 Two,
503 }
504 484
505 fn main() { 485 fn main() {
506 let a = A::One; 486 let a = A::One;
@@ -524,10 +504,7 @@ mod tests {
524 check_assist_not_applicable( 504 check_assist_not_applicable(
525 fill_match_arms, 505 fill_match_arms,
526 r#" 506 r#"
527 enum A { 507 enum A { One, Two }
528 One,
529 Two,
530 }
531 508
532 fn main() { 509 fn main() {
533 let a = A::One; 510 let a = A::One;
@@ -543,9 +520,7 @@ mod tests {
543 check_assist( 520 check_assist(
544 fill_match_arms, 521 fill_match_arms,
545 r#" 522 r#"
546 enum A { 523 enum A { As }
547 As,
548 }
549 524
550 fn foo(a: &A) { 525 fn foo(a: &A) {
551 match a<|> { 526 match a<|> {
@@ -553,13 +528,11 @@ mod tests {
553 } 528 }
554 "#, 529 "#,
555 r#" 530 r#"
556 enum A { 531 enum A { As }
557 As,
558 }
559 532
560 fn foo(a: &A) { 533 fn foo(a: &A) {
561 match <|>a { 534 match a {
562 A::As => {} 535 $0A::As => {}
563 } 536 }
564 } 537 }
565 "#, 538 "#,
@@ -569,7 +542,7 @@ mod tests {
569 fill_match_arms, 542 fill_match_arms,
570 r#" 543 r#"
571 enum A { 544 enum A {
572 Es{ x: usize, y: usize } 545 Es { x: usize, y: usize }
573 } 546 }
574 547
575 fn foo(a: &mut A) { 548 fn foo(a: &mut A) {
@@ -579,12 +552,12 @@ mod tests {
579 "#, 552 "#,
580 r#" 553 r#"
581 enum A { 554 enum A {
582 Es{ x: usize, y: usize } 555 Es { x: usize, y: usize }
583 } 556 }
584 557
585 fn foo(a: &mut A) { 558 fn foo(a: &mut A) {
586 match <|>a { 559 match a {
587 A::Es { x, y } => {} 560 $0A::Es { x, y } => {}
588 } 561 }
589 } 562 }
590 "#, 563 "#,
@@ -623,8 +596,8 @@ mod tests {
623 enum E { X, Y } 596 enum E { X, Y }
624 597
625 fn main() { 598 fn main() {
626 match <|>E::X { 599 match E::X {
627 E::X => {} 600 $0E::X => {}
628 E::Y => {} 601 E::Y => {}
629 } 602 }
630 } 603 }
@@ -651,8 +624,8 @@ mod tests {
651 use foo::E::X; 624 use foo::E::X;
652 625
653 fn main() { 626 fn main() {
654 match <|>X { 627 match X {
655 X => {} 628 $0X => {}
656 foo::E::Y => {} 629 foo::E::Y => {}
657 } 630 }
658 } 631 }
@@ -665,10 +638,7 @@ mod tests {
665 check_assist( 638 check_assist(
666 fill_match_arms, 639 fill_match_arms,
667 r#" 640 r#"
668 enum A { 641 enum A { One, Two }
669 One,
670 Two,
671 }
672 fn foo(a: A) { 642 fn foo(a: A) {
673 match a { 643 match a {
674 // foo bar baz<|> 644 // foo bar baz<|>
@@ -678,16 +648,13 @@ mod tests {
678 } 648 }
679 "#, 649 "#,
680 r#" 650 r#"
681 enum A { 651 enum A { One, Two }
682 One,
683 Two,
684 }
685 fn foo(a: A) { 652 fn foo(a: A) {
686 match <|>a { 653 match a {
687 // foo bar baz 654 // foo bar baz
688 A::One => {} 655 A::One => {}
689 // This is where the rest should be 656 // This is where the rest should be
690 A::Two => {} 657 $0A::Two => {}
691 } 658 }
692 } 659 }
693 "#, 660 "#,
@@ -699,10 +666,7 @@ mod tests {
699 check_assist( 666 check_assist(
700 fill_match_arms, 667 fill_match_arms,
701 r#" 668 r#"
702 enum A { 669 enum A { One, Two }
703 One,
704 Two,
705 }
706 fn foo(a: A) { 670 fn foo(a: A) {
707 match a { 671 match a {
708 // foo bar baz<|> 672 // foo bar baz<|>
@@ -710,14 +674,11 @@ mod tests {
710 } 674 }
711 "#, 675 "#,
712 r#" 676 r#"
713 enum A { 677 enum A { One, Two }
714 One,
715 Two,
716 }
717 fn foo(a: A) { 678 fn foo(a: A) {
718 match <|>a { 679 match a {
719 // foo bar baz 680 // foo bar baz
720 A::One => {} 681 $0A::One => {}
721 A::Two => {} 682 A::Two => {}
722 } 683 }
723 } 684 }
@@ -740,8 +701,8 @@ mod tests {
740 r#" 701 r#"
741 enum A { One, Two, } 702 enum A { One, Two, }
742 fn foo(a: A) { 703 fn foo(a: A) {
743 match <|>a { 704 match a {
744 A::One => {} 705 $0A::One => {}
745 A::Two => {} 706 A::Two => {}
746 } 707 }
747 } 708 }
@@ -765,8 +726,8 @@ fn foo(opt: Option<i32>) {
765 before, 726 before,
766 r#" 727 r#"
767fn foo(opt: Option<i32>) { 728fn foo(opt: Option<i32>) {
768 match <|>opt { 729 match opt {
769 Some(_) => {} 730 $0Some(_) => {}
770 None => {} 731 None => {}
771 } 732 }
772} 733}