aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ide/src/inlay_hints.rs155
1 files changed, 68 insertions, 87 deletions
diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs
index 9f2f6c80a..821c61403 100644
--- a/crates/ide/src/inlay_hints.rs
+++ b/crates/ide/src/inlay_hints.rs
@@ -315,6 +315,7 @@ fn should_hide_param_name_hint(
315 param_name: &str, 315 param_name: &str,
316 argument: &ast::Expr, 316 argument: &ast::Expr,
317) -> bool { 317) -> bool {
318 // These are to be tested in the `parameter_hint_heuristics` test
318 // hide when: 319 // hide when:
319 // - the parameter name is a suffix of the function's name 320 // - the parameter name is a suffix of the function's name
320 // - the argument is an enum whose name is equal to the parameter 321 // - the argument is an enum whose name is equal to the parameter
@@ -395,7 +396,7 @@ fn get_string_representation(expr: &ast::Expr) -> Option<String> {
395 ast::Expr::MethodCallExpr(method_call_expr) => { 396 ast::Expr::MethodCallExpr(method_call_expr) => {
396 let name_ref = method_call_expr.name_ref()?; 397 let name_ref = method_call_expr.name_ref()?;
397 match name_ref.text().as_str() { 398 match name_ref.text().as_str() {
398 "clone" => method_call_expr.receiver().map(|rec| rec.to_string()), 399 "clone" | "as_ref" => method_call_expr.receiver().map(|rec| rec.to_string()),
399 name_ref => Some(name_ref.to_owned()), 400 name_ref => Some(name_ref.to_owned()),
400 } 401 }
401 } 402 }
@@ -521,6 +522,8 @@ fn main() {
521 ); 522 );
522 } 523 }
523 524
525 // Parameter hint tests
526
524 #[test] 527 #[test]
525 fn param_hints_only() { 528 fn param_hints_only() {
526 check_params( 529 check_params(
@@ -564,6 +567,15 @@ fn main() {
564 ); 567 );
565}"#, 568}"#,
566 ); 569 );
570 check_params(
571 r#"
572fn param_with_underscore(underscore: i32) -> i32 { underscore }
573fn main() {
574 let _x = param_with_underscore(
575 4,
576 );
577}"#,
578 );
567 } 579 }
568 580
569 #[test] 581 #[test]
@@ -583,30 +595,32 @@ fn main() {
583 fn never_hide_param_when_multiple_params() { 595 fn never_hide_param_when_multiple_params() {
584 check_params( 596 check_params(
585 r#" 597 r#"
586fn foo(bar: i32, baz: i32) -> i32 { bar + baz } 598fn foo(foo: i32, bar: i32) -> i32 { bar + baz }
587fn main() { 599fn main() {
588 let _x = foo( 600 let _x = foo(
589 4, 601 4,
590 //^ bar 602 //^ foo
591 8, 603 8,
592 //^ baz 604 //^ bar
593 ); 605 );
594}"#, 606}"#,
595 ); 607 );
596 } 608 }
597 609
598 #[test] 610 #[test]
599 fn hide_param_hints_for_clones() { 611 fn param_hints_look_through_as_ref_and_clone() {
600 check_params( 612 check_params(
601 r#" 613 r#"
602fn foo(bar: i32, baz: String, qux: f32) {} 614fn foo(bar: i32, baz: f32) {}
603 615
604fn main() { 616fn main() {
605 let bar = 3; 617 let bar = 3;
606 let baz = &"baz"; 618 let baz = &"baz";
607 let fez = 1.0; 619 let fez = 1.0;
608 foo(bar.clone(), baz.clone(), fez.clone()); 620 foo(bar.clone(), bar.clone());
609 //^^^^^^^^^^^ qux 621 //^^^^^^^^^^^ baz
622 foo(bar.as_ref(), bar.as_ref());
623 //^^^^^^^^^^^^ baz
610} 624}
611"#, 625"#,
612 ); 626 );
@@ -639,10 +653,10 @@ fn main() {
639 r#"pub fn test(a: i32, b: i32) -> [i32; 2] { [a, b] } 653 r#"pub fn test(a: i32, b: i32) -> [i32; 2] { [a, b] }
640fn main() { 654fn main() {
641 test( 655 test(
642 0x0fab272b, 656 0xa_b,
643 //^^^^^^^^^^ a 657 //^^^^^ a
644 0x0fab272b 658 0xa_b,
645 //^^^^^^^^^^ b 659 //^^^^^ b
646 ); 660 );
647}"#, 661}"#,
648 ) 662 )
@@ -650,7 +664,7 @@ fn main() {
650 664
651 #[test] 665 #[test]
652 fn function_call_parameter_hint() { 666 fn function_call_parameter_hint() {
653 check( 667 check_params(
654 r#" 668 r#"
655enum Option<T> { None, Some(T) } 669enum Option<T> { None, Some(T) }
656use Option::*; 670use Option::*;
@@ -685,7 +699,6 @@ fn test_func(mut foo: i32, bar: i32, msg: &str, _: i32, last: i32) -> i32 {
685 699
686fn main() { 700fn main() {
687 let not_literal = 1; 701 let not_literal = 1;
688 //^^^^^^^^^^^ i32
689 let _: i32 = test_func(1, 2, "hello", 3, not_literal); 702 let _: i32 = test_func(1, 2, "hello", 3, not_literal);
690 //^ foo ^ bar ^^^^^^^ msg ^^^^^^^^^^^ last 703 //^ foo ^ bar ^^^^^^^ msg ^^^^^^^^^^^ last
691 let t: Test = Test {}; 704 let t: Test = Test {};
@@ -712,97 +725,65 @@ fn main() {
712 } 725 }
713 726
714 #[test] 727 #[test]
715 fn omitted_parameters_hints_heuristics() { 728 fn parameter_hint_heuristics() {
716 check_with_config( 729 check_params(
717 InlayHintsConfig { max_length: Some(8), ..TEST_CONFIG },
718 r#" 730 r#"
731fn check(ra_fixture_thing: &str) {}
732
719fn map(f: i32) {} 733fn map(f: i32) {}
720fn filter(predicate: i32) {} 734fn filter(predicate: i32) {}
721 735
722struct TestVarContainer { 736fn strip_suffix(suffix: &str) {}
723 test_var: i32, 737fn stripsuffix(suffix: &str) {}
724} 738fn same(same: u32) {}
725 739fn same2(_same2: u32) {}
726impl TestVarContainer {
727 fn test_var(&self) -> i32 {
728 self.test_var
729 }
730}
731 740
732struct Test {}
733
734impl Test {
735 fn map(self, f: i32) -> Self {
736 self
737 }
738
739 fn filter(self, predicate: i32) -> Self {
740 self
741 }
742
743 fn field(self, value: i32) -> Self {
744 self
745 }
746
747 fn no_hints_expected(&self, _: i32, test_var: i32) {}
748
749 fn frob(&self, frob: bool) {}
750}
751
752struct Param {}
753
754fn different_order(param: &Param) {}
755fn different_order_mut(param: &mut Param) {}
756fn has_underscore(_param: bool) {}
757fn enum_matches_param_name(completion_kind: CompletionKind) {} 741fn enum_matches_param_name(completion_kind: CompletionKind) {}
758fn param_destructuring_omitted_1((a, b): (u32, u32)) {}
759fn param_destructuring_omitted_2(TestVarContainer { test_var: _ }: TestVarContainer) {}
760 742
761fn twiddle(twiddle: bool) {} 743fn foo(param: u32) {}
762fn doo(_doo: bool) {} 744fn bar(param_eter: u32) {}
763 745
764enum CompletionKind { 746enum CompletionKind {
765 Keyword, 747 Keyword,
766} 748}
767 749
768fn main() { 750fn non_ident_pat((a, b): (u32, u32)) {}
769 let container: TestVarContainer = TestVarContainer { test_var: 42 };
770 let test: Test = Test {};
771
772 map(22);
773 filter(33);
774 751
775 let test_processed: Test = test.map(1).filter(2).field(3); 752fn main() {
776 753 check("");
777 let test_var: i32 = 55;
778 test_processed.no_hints_expected(22, test_var);
779 test_processed.no_hints_expected(33, container.test_var);
780 test_processed.no_hints_expected(44, container.test_var());
781 test_processed.frob(false);
782
783 twiddle(true);
784 doo(true);
785
786 const TWIDDLE_UPPERCASE: bool = true;
787 twiddle(TWIDDLE_UPPERCASE);
788 754
789 let mut param_begin: Param = Param {}; 755 map(0);
790 different_order(&param_begin); 756 filter(0);
791 different_order(&mut param_begin);
792 757
793 let param: bool = true; 758 strip_suffix("");
794 has_underscore(param); 759 stripsuffix("");
760 //^^ suffix
761 same(0);
762 same2(0);
795 763
796 enum_matches_param_name(CompletionKind::Keyword); 764 enum_matches_param_name(CompletionKind::Keyword);
797 765
798 let a: f64 = 7.0; 766 let param = 0;
799 let b: f64 = 4.0; 767 foo(param);
800 let _: f64 = a.div_euclid(b); 768 let param_end = 0;
801 let _: f64 = a.abs_sub(b); 769 foo(param_end);
802 770 let start_param = 0;
803 let range: (u32, u32) = (3, 5); 771 foo(start_param);
804 param_destructuring_omitted_1(range); 772 let param2 = 0;
805 param_destructuring_omitted_2(container); 773 foo(param2);
774 //^^^^^^ param
775
776 let param_eter = 0;
777 bar(param_eter);
778 let param_eter_end = 0;
779 bar(param_eter_end);
780 let start_param_eter = 0;
781 bar(start_param_eter);
782 let param_eter2 = 0;
783 bar(param_eter2);
784 //^^^^^^^^^^^ param_eter
785
786 non_ident_pat((0, 0));
806}"#, 787}"#,
807 ); 788 );
808 } 789 }