Skip to content

Commit 5f1fac4

Browse files
committed
Cleanup parameter_hint_heuristics inlay hints test
1 parent b0f6d88 commit 5f1fac4

File tree

1 file changed

+68
-87
lines changed

1 file changed

+68
-87
lines changed

crates/ide/src/inlay_hints.rs

Lines changed: 68 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ fn should_hide_param_name_hint(
315315
param_name: &str,
316316
argument: &ast::Expr,
317317
) -> bool {
318+
// These are to be tested in the `parameter_hint_heuristics` test
318319
// hide when:
319320
// - the parameter name is a suffix of the function's name
320321
// - 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> {
395396
ast::Expr::MethodCallExpr(method_call_expr) => {
396397
let name_ref = method_call_expr.name_ref()?;
397398
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()),
399400
name_ref => Some(name_ref.to_owned()),
400401
}
401402
}
@@ -521,6 +522,8 @@ fn main() {
521522
);
522523
}
523524

525+
// Parameter hint tests
526+
524527
#[test]
525528
fn param_hints_only() {
526529
check_params(
@@ -558,6 +561,15 @@ fn main() {
558561
check_params(
559562
r#"
560563
fn param_with_underscore(with_underscore: i32) -> i32 { with_underscore }
564+
fn main() {
565+
let _x = param_with_underscore(
566+
4,
567+
);
568+
}"#,
569+
);
570+
check_params(
571+
r#"
572+
fn param_with_underscore(underscore: i32) -> i32 { underscore }
561573
fn main() {
562574
let _x = param_with_underscore(
563575
4,
@@ -583,30 +595,32 @@ fn main() {
583595
fn never_hide_param_when_multiple_params() {
584596
check_params(
585597
r#"
586-
fn foo(bar: i32, baz: i32) -> i32 { bar + baz }
598+
fn foo(foo: i32, bar: i32) -> i32 { bar + baz }
587599
fn main() {
588600
let _x = foo(
589601
4,
590-
//^ bar
602+
//^ foo
591603
8,
592-
//^ baz
604+
//^ bar
593605
);
594606
}"#,
595607
);
596608
}
597609

598610
#[test]
599-
fn hide_param_hints_for_clones() {
611+
fn param_hints_look_through_as_ref_and_clone() {
600612
check_params(
601613
r#"
602-
fn foo(bar: i32, baz: String, qux: f32) {}
614+
fn foo(bar: i32, baz: f32) {}
603615
604616
fn main() {
605617
let bar = 3;
606618
let baz = &"baz";
607619
let fez = 1.0;
608-
foo(bar.clone(), baz.clone(), fez.clone());
609-
//^^^^^^^^^^^ qux
620+
foo(bar.clone(), bar.clone());
621+
//^^^^^^^^^^^ baz
622+
foo(bar.as_ref(), bar.as_ref());
623+
//^^^^^^^^^^^^ baz
610624
}
611625
"#,
612626
);
@@ -639,18 +653,18 @@ fn main() {
639653
r#"pub fn test(a: i32, b: i32) -> [i32; 2] { [a, b] }
640654
fn main() {
641655
test(
642-
0x0fab272b,
643-
//^^^^^^^^^^ a
644-
0x0fab272b
645-
//^^^^^^^^^^ b
656+
0xa_b,
657+
//^^^^^ a
658+
0xa_b,
659+
//^^^^^ b
646660
);
647661
}"#,
648662
)
649663
}
650664

651665
#[test]
652666
fn function_call_parameter_hint() {
653-
check(
667+
check_params(
654668
r#"
655669
enum Option<T> { None, Some(T) }
656670
use Option::*;
@@ -685,7 +699,6 @@ fn test_func(mut foo: i32, bar: i32, msg: &str, _: i32, last: i32) -> i32 {
685699
686700
fn main() {
687701
let not_literal = 1;
688-
//^^^^^^^^^^^ i32
689702
let _: i32 = test_func(1, 2, "hello", 3, not_literal);
690703
//^ foo ^ bar ^^^^^^^ msg ^^^^^^^^^^^ last
691704
let t: Test = Test {};
@@ -712,97 +725,65 @@ fn main() {
712725
}
713726

714727
#[test]
715-
fn omitted_parameters_hints_heuristics() {
716-
check_with_config(
717-
InlayHintsConfig { max_length: Some(8), ..TEST_CONFIG },
728+
fn parameter_hint_heuristics() {
729+
check_params(
718730
r#"
731+
fn check(ra_fixture_thing: &str) {}
732+
719733
fn map(f: i32) {}
720734
fn filter(predicate: i32) {}
721735
722-
struct TestVarContainer {
723-
test_var: i32,
724-
}
725-
726-
impl TestVarContainer {
727-
fn test_var(&self) -> i32 {
728-
self.test_var
729-
}
730-
}
736+
fn strip_suffix(suffix: &str) {}
737+
fn stripsuffix(suffix: &str) {}
738+
fn same(same: u32) {}
739+
fn same2(_same2: u32) {}
731740
732-
struct Test {}
733-
734-
impl 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-
752-
struct Param {}
753-
754-
fn different_order(param: &Param) {}
755-
fn different_order_mut(param: &mut Param) {}
756-
fn has_underscore(_param: bool) {}
757741
fn enum_matches_param_name(completion_kind: CompletionKind) {}
758-
fn param_destructuring_omitted_1((a, b): (u32, u32)) {}
759-
fn param_destructuring_omitted_2(TestVarContainer { test_var: _ }: TestVarContainer) {}
760742
761-
fn twiddle(twiddle: bool) {}
762-
fn doo(_doo: bool) {}
743+
fn foo(param: u32) {}
744+
fn bar(param_eter: u32) {}
763745
764746
enum CompletionKind {
765747
Keyword,
766748
}
767749
768-
fn main() {
769-
let container: TestVarContainer = TestVarContainer { test_var: 42 };
770-
let test: Test = Test {};
771-
772-
map(22);
773-
filter(33);
750+
fn non_ident_pat((a, b): (u32, u32)) {}
774751
775-
let test_processed: Test = test.map(1).filter(2).field(3);
776-
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);
752+
fn main() {
753+
check("");
788754
789-
let mut param_begin: Param = Param {};
790-
different_order(&param_begin);
791-
different_order(&mut param_begin);
755+
map(0);
756+
filter(0);
792757
793-
let param: bool = true;
794-
has_underscore(param);
758+
strip_suffix("");
759+
stripsuffix("");
760+
//^^ suffix
761+
same(0);
762+
same2(0);
795763
796764
enum_matches_param_name(CompletionKind::Keyword);
797765
798-
let a: f64 = 7.0;
799-
let b: f64 = 4.0;
800-
let _: f64 = a.div_euclid(b);
801-
let _: f64 = a.abs_sub(b);
802-
803-
let range: (u32, u32) = (3, 5);
804-
param_destructuring_omitted_1(range);
805-
param_destructuring_omitted_2(container);
766+
let param = 0;
767+
foo(param);
768+
let param_end = 0;
769+
foo(param_end);
770+
let start_param = 0;
771+
foo(start_param);
772+
let param2 = 0;
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));
806787
}"#,
807788
);
808789
}

0 commit comments

Comments
 (0)