diff options
Diffstat (limited to 'crates/ide_assists/src/handlers')
-rw-r--r-- | crates/ide_assists/src/handlers/extract_variable.rs | 208 |
1 files changed, 202 insertions, 6 deletions
diff --git a/crates/ide_assists/src/handlers/extract_variable.rs b/crates/ide_assists/src/handlers/extract_variable.rs index 98f3dc6ca..312ac7ac4 100644 --- a/crates/ide_assists/src/handlers/extract_variable.rs +++ b/crates/ide_assists/src/handlers/extract_variable.rs | |||
@@ -8,7 +8,7 @@ use syntax::{ | |||
8 | }; | 8 | }; |
9 | use test_utils::mark; | 9 | use test_utils::mark; |
10 | 10 | ||
11 | use crate::{AssistContext, AssistId, AssistKind, Assists}; | 11 | use crate::{utils::suggest_name, AssistContext, AssistId, AssistKind, Assists}; |
12 | 12 | ||
13 | // Assist: extract_variable | 13 | // Assist: extract_variable |
14 | // | 14 | // |
@@ -54,7 +54,7 @@ pub(crate) fn extract_variable(acc: &mut Assists, ctx: &AssistContext) -> Option | |||
54 | 54 | ||
55 | let var_name = match &field_shorthand { | 55 | let var_name = match &field_shorthand { |
56 | Some(it) => it.to_string(), | 56 | Some(it) => it.to_string(), |
57 | None => "var_name".to_string(), | 57 | None => suggest_name::variable(&to_extract, &ctx.sema), |
58 | }; | 58 | }; |
59 | let expr_range = match &field_shorthand { | 59 | let expr_range = match &field_shorthand { |
60 | Some(it) => it.syntax().text_range().cover(to_extract.syntax().text_range()), | 60 | Some(it) => it.syntax().text_range().cover(to_extract.syntax().text_range()), |
@@ -274,8 +274,8 @@ fn foo() { | |||
274 | "#, | 274 | "#, |
275 | r#" | 275 | r#" |
276 | fn foo() { | 276 | fn foo() { |
277 | let $0var_name = bar(1 + 1); | 277 | let $0bar = bar(1 + 1); |
278 | var_name | 278 | bar |
279 | } | 279 | } |
280 | "#, | 280 | "#, |
281 | ) | 281 | ) |
@@ -401,8 +401,8 @@ fn main() { | |||
401 | ", | 401 | ", |
402 | " | 402 | " |
403 | fn main() { | 403 | fn main() { |
404 | let $0var_name = bar.foo(); | 404 | let $0foo = bar.foo(); |
405 | let v = var_name; | 405 | let v = foo; |
406 | } | 406 | } |
407 | ", | 407 | ", |
408 | ); | 408 | ); |
@@ -557,6 +557,202 @@ fn main() { | |||
557 | } | 557 | } |
558 | 558 | ||
559 | #[test] | 559 | #[test] |
560 | fn extract_var_name_from_type() { | ||
561 | check_assist( | ||
562 | extract_variable, | ||
563 | r#" | ||
564 | struct Test(i32); | ||
565 | |||
566 | fn foo() -> Test { | ||
567 | $0{ Test(10) }$0 | ||
568 | } | ||
569 | "#, | ||
570 | r#" | ||
571 | struct Test(i32); | ||
572 | |||
573 | fn foo() -> Test { | ||
574 | let $0test = { Test(10) }; | ||
575 | test | ||
576 | } | ||
577 | "#, | ||
578 | ) | ||
579 | } | ||
580 | |||
581 | #[test] | ||
582 | fn extract_var_name_from_parameter() { | ||
583 | check_assist( | ||
584 | extract_variable, | ||
585 | r#" | ||
586 | fn bar(test: u32, size: u32) | ||
587 | |||
588 | fn foo() { | ||
589 | bar(1, $01+1$0); | ||
590 | } | ||
591 | "#, | ||
592 | r#" | ||
593 | fn bar(test: u32, size: u32) | ||
594 | |||
595 | fn foo() { | ||
596 | let $0size = 1+1; | ||
597 | bar(1, size); | ||
598 | } | ||
599 | "#, | ||
600 | ) | ||
601 | } | ||
602 | |||
603 | #[test] | ||
604 | fn extract_var_parameter_name_has_precedence_over_type() { | ||
605 | check_assist( | ||
606 | extract_variable, | ||
607 | r#" | ||
608 | struct TextSize(u32); | ||
609 | fn bar(test: u32, size: TextSize) | ||
610 | |||
611 | fn foo() { | ||
612 | bar(1, $0{ TextSize(1+1) }$0); | ||
613 | } | ||
614 | "#, | ||
615 | r#" | ||
616 | struct TextSize(u32); | ||
617 | fn bar(test: u32, size: TextSize) | ||
618 | |||
619 | fn foo() { | ||
620 | let $0size = { TextSize(1+1) }; | ||
621 | bar(1, size); | ||
622 | } | ||
623 | "#, | ||
624 | ) | ||
625 | } | ||
626 | |||
627 | #[test] | ||
628 | fn extract_var_name_from_function() { | ||
629 | check_assist( | ||
630 | extract_variable, | ||
631 | r#" | ||
632 | fn is_required(test: u32, size: u32) -> bool | ||
633 | |||
634 | fn foo() -> bool { | ||
635 | $0is_required(1, 2)$0 | ||
636 | } | ||
637 | "#, | ||
638 | r#" | ||
639 | fn is_required(test: u32, size: u32) -> bool | ||
640 | |||
641 | fn foo() -> bool { | ||
642 | let $0is_required = is_required(1, 2); | ||
643 | is_required | ||
644 | } | ||
645 | "#, | ||
646 | ) | ||
647 | } | ||
648 | |||
649 | #[test] | ||
650 | fn extract_var_name_from_method() { | ||
651 | check_assist( | ||
652 | extract_variable, | ||
653 | r#" | ||
654 | struct S; | ||
655 | impl S { | ||
656 | fn bar(&self, n: u32) -> u32 { n } | ||
657 | } | ||
658 | |||
659 | fn foo() -> u32 { | ||
660 | $0S.bar(1)$0 | ||
661 | } | ||
662 | "#, | ||
663 | r#" | ||
664 | struct S; | ||
665 | impl S { | ||
666 | fn bar(&self, n: u32) -> u32 { n } | ||
667 | } | ||
668 | |||
669 | fn foo() -> u32 { | ||
670 | let $0bar = S.bar(1); | ||
671 | bar | ||
672 | } | ||
673 | "#, | ||
674 | ) | ||
675 | } | ||
676 | |||
677 | #[test] | ||
678 | fn extract_var_name_from_method_param() { | ||
679 | check_assist( | ||
680 | extract_variable, | ||
681 | r#" | ||
682 | struct S; | ||
683 | impl S { | ||
684 | fn bar(&self, n: u32, size: u32) { n } | ||
685 | } | ||
686 | |||
687 | fn foo() { | ||
688 | S.bar($01 + 1$0, 2) | ||
689 | } | ||
690 | "#, | ||
691 | r#" | ||
692 | struct S; | ||
693 | impl S { | ||
694 | fn bar(&self, n: u32, size: u32) { n } | ||
695 | } | ||
696 | |||
697 | fn foo() { | ||
698 | let $0n = 1 + 1; | ||
699 | S.bar(n, 2) | ||
700 | } | ||
701 | "#, | ||
702 | ) | ||
703 | } | ||
704 | |||
705 | #[test] | ||
706 | fn extract_var_name_from_ufcs_method_param() { | ||
707 | check_assist( | ||
708 | extract_variable, | ||
709 | r#" | ||
710 | struct S; | ||
711 | impl S { | ||
712 | fn bar(&self, n: u32, size: u32) { n } | ||
713 | } | ||
714 | |||
715 | fn foo() { | ||
716 | S::bar(&S, $01 + 1$0, 2) | ||
717 | } | ||
718 | "#, | ||
719 | r#" | ||
720 | struct S; | ||
721 | impl S { | ||
722 | fn bar(&self, n: u32, size: u32) { n } | ||
723 | } | ||
724 | |||
725 | fn foo() { | ||
726 | let $0n = 1 + 1; | ||
727 | S::bar(&S, n, 2) | ||
728 | } | ||
729 | "#, | ||
730 | ) | ||
731 | } | ||
732 | |||
733 | #[test] | ||
734 | fn extract_var_parameter_name_has_precedence_over_function() { | ||
735 | check_assist( | ||
736 | extract_variable, | ||
737 | r#" | ||
738 | fn bar(test: u32, size: u32) | ||
739 | |||
740 | fn foo() { | ||
741 | bar(1, $0symbol_size(1, 2)$0); | ||
742 | } | ||
743 | "#, | ||
744 | r#" | ||
745 | fn bar(test: u32, size: u32) | ||
746 | |||
747 | fn foo() { | ||
748 | let $0size = symbol_size(1, 2); | ||
749 | bar(1, size); | ||
750 | } | ||
751 | "#, | ||
752 | ) | ||
753 | } | ||
754 | |||
755 | #[test] | ||
560 | fn test_extract_var_for_return_not_applicable() { | 756 | fn test_extract_var_for_return_not_applicable() { |
561 | check_assist_not_applicable(extract_variable, "fn foo() { $0return$0; } "); | 757 | check_assist_not_applicable(extract_variable, "fn foo() { $0return$0; } "); |
562 | } | 758 | } |