Skip to content

Commit 05723cb

Browse files
matthewsanetralnicola
authored andcommitted
Add check if param name is similar to fn name
1 parent 53c7aea commit 05723cb

File tree

1 file changed

+106
-2
lines changed

1 file changed

+106
-2
lines changed

crates/ide/src/inlay_hints.rs

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use assists::utils::FamousDefs;
22
use either::Either;
3-
use hir::{known, HirDisplay, Semantics};
3+
use hir::{known, Callable, HirDisplay, Semantics};
44
use ide_db::RootDatabase;
55
use stdx::to_lower_snake_case;
66
use syntax::{
@@ -170,7 +170,7 @@ fn get_param_name_hints(
170170
};
171171
Some((param_name, arg))
172172
})
173-
.filter(|(param_name, arg)| should_show_param_name_hint(sema, &callable, &param_name, &arg))
173+
.filter(|(param_name, arg)| should_show_param_name_hint(sema, &callable, param_name, &arg))
174174
.map(|(param_name, arg)| InlayHint {
175175
range: arg.syntax().text_range(),
176176
kind: InlayKind::ParameterHint,
@@ -334,9 +334,11 @@ fn should_show_param_name_hint(
334334
| hir::CallableKind::TupleEnumVariant(_)
335335
| hir::CallableKind::Closure => None,
336336
};
337+
337338
if param_name.is_empty()
338339
|| Some(param_name) == fn_name.as_ref().map(|s| s.trim_start_matches('_'))
339340
|| is_argument_similar_to_param_name(sema, argument, param_name)
341+
|| is_param_name_similar_to_fn_name(param_name, callable, fn_name.as_ref())
340342
|| param_name.starts_with("ra_fixture")
341343
{
342344
return false;
@@ -364,6 +366,26 @@ fn is_argument_similar_to_param_name(
364366
}
365367
}
366368

369+
fn is_param_name_similar_to_fn_name(
370+
param_name: &str,
371+
callable: &Callable,
372+
fn_name: Option<&String>,
373+
) -> bool {
374+
// if it's the only parameter, don't show it if:
375+
// - is the same as the function name, or
376+
// - the function ends with '_' + param_name
377+
378+
match (callable.n_params(), fn_name) {
379+
(1, Some(function)) => {
380+
function == param_name
381+
|| (function.len() > param_name.len()
382+
&& function.ends_with(param_name)
383+
&& function[..function.len() - param_name.len()].ends_with('_'))
384+
}
385+
_ => false,
386+
}
387+
}
388+
367389
fn is_enum_name_similar_to_param_name(
368390
sema: &Semantics<RootDatabase>,
369391
argument: &ast::Expr,
@@ -456,6 +478,88 @@ fn main() {
456478
);
457479
}
458480

481+
#[test]
482+
fn param_name_similar_to_fn_name_still_hints() {
483+
check_with_config(
484+
InlayHintsConfig {
485+
parameter_hints: true,
486+
type_hints: false,
487+
chaining_hints: false,
488+
max_length: None,
489+
},
490+
r#"
491+
fn max(x: i32, y: i32) -> i32 { x + y }
492+
fn main() {
493+
let _x = max(
494+
4,
495+
//^ x
496+
4,
497+
//^ y
498+
);
499+
}"#,
500+
);
501+
}
502+
503+
#[test]
504+
fn param_name_similar_to_fn_name() {
505+
check_with_config(
506+
InlayHintsConfig {
507+
parameter_hints: true,
508+
type_hints: false,
509+
chaining_hints: false,
510+
max_length: None,
511+
},
512+
r#"
513+
fn param_with_underscore(with_underscore: i32) -> i32 { with_underscore }
514+
fn main() {
515+
let _x = param_with_underscore(
516+
4,
517+
);
518+
}"#,
519+
);
520+
}
521+
522+
#[test]
523+
fn param_name_same_as_fn_name() {
524+
check_with_config(
525+
InlayHintsConfig {
526+
parameter_hints: true,
527+
type_hints: false,
528+
chaining_hints: false,
529+
max_length: None,
530+
},
531+
r#"
532+
fn foo(foo: i32) -> i32 { foo }
533+
fn main() {
534+
let _x = foo(
535+
4,
536+
);
537+
}"#,
538+
);
539+
}
540+
541+
#[test]
542+
fn never_hide_param_when_multiple_params() {
543+
check_with_config(
544+
InlayHintsConfig {
545+
parameter_hints: true,
546+
type_hints: false,
547+
chaining_hints: false,
548+
max_length: None,
549+
},
550+
r#"
551+
fn foo(bar: i32, baz: i32) -> i32 { bar + baz }
552+
fn main() {
553+
let _x = foo(
554+
4,
555+
//^ bar
556+
8,
557+
//^ baz
558+
);
559+
}"#,
560+
);
561+
}
562+
459563
#[test]
460564
fn hints_disabled() {
461565
check_with_config(

0 commit comments

Comments
 (0)