Skip to content

Commit 9ebb55d

Browse files
committed
Hide more unnecessary parameter hints for constructors
1 parent 5fe2844 commit 9ebb55d

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

crates/ide/src/inlay_hints.rs

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,7 @@ fn should_hide_param_name_hint(
894894
// These are to be tested in the `parameter_hint_heuristics` test
895895
// hide when:
896896
// - the parameter name is a suffix of the function's name
897-
// - the argument is an enum whose name is equal to the parameter
897+
// - the argument is a qualified constructing or call expression where the qualifier is an ADT
898898
// - exact argument<->parameter match(ignoring leading underscore) or parameter is a prefix/suffix
899899
// of argument with _ splitting it off
900900
// - param starts with `ra_fixture`
@@ -915,10 +915,10 @@ fn should_hide_param_name_hint(
915915
};
916916
let fn_name = fn_name.as_deref();
917917
is_param_name_suffix_of_fn_name(param_name, callable, fn_name)
918-
|| is_enum_name_similar_to_param_name(sema, argument, param_name)
919918
|| is_argument_similar_to_param_name(argument, param_name)
920919
|| param_name.starts_with("ra_fixture")
921920
|| (callable.n_params() == 1 && is_obvious_param(param_name))
921+
|| is_adt_constructor_similar_to_param_name(sema, argument, param_name)
922922
}
923923

924924
fn is_argument_similar_to_param_name(argument: &ast::Expr, param_name: &str) -> bool {
@@ -974,17 +974,43 @@ fn is_param_name_suffix_of_fn_name(
974974
}
975975
}
976976

977-
fn is_enum_name_similar_to_param_name(
977+
fn is_adt_constructor_similar_to_param_name(
978978
sema: &Semantics<RootDatabase>,
979979
argument: &ast::Expr,
980980
param_name: &str,
981981
) -> bool {
982-
match sema.type_of_expr(argument).and_then(|t| t.original.as_adt()) {
983-
Some(hir::Adt::Enum(e)) => {
984-
to_lower_snake_case(&e.name(sema.db).to_smol_str()) == param_name
982+
let path = match argument {
983+
ast::Expr::CallExpr(c) => c.expr().and_then(|e| match e {
984+
ast::Expr::PathExpr(p) => p.path(),
985+
_ => None,
986+
}),
987+
ast::Expr::PathExpr(p) => p.path(),
988+
ast::Expr::RecordExpr(r) => r.path(),
989+
_ => return false,
990+
};
991+
let path = match path {
992+
Some(it) => it,
993+
None => return false,
994+
};
995+
(|| match sema.resolve_path(&path)? {
996+
hir::PathResolution::Def(hir::ModuleDef::Adt(_)) => {
997+
Some(to_lower_snake_case(&path.segment()?.name_ref()?.text()) == param_name)
985998
}
986-
_ => false,
987-
}
999+
hir::PathResolution::Def(hir::ModuleDef::Function(_) | hir::ModuleDef::Variant(_)) => {
1000+
if to_lower_snake_case(&path.segment()?.name_ref()?.text()) == param_name {
1001+
return Some(true);
1002+
}
1003+
let qual = path.qualifier()?;
1004+
match sema.resolve_path(&qual)? {
1005+
hir::PathResolution::Def(hir::ModuleDef::Adt(_)) => {
1006+
Some(to_lower_snake_case(&qual.segment()?.name_ref()?.text()) == param_name)
1007+
}
1008+
_ => None,
1009+
}
1010+
}
1011+
_ => None,
1012+
})()
1013+
.unwrap_or(false)
9881014
}
9891015

9901016
fn get_string_representation(expr: &ast::Expr) -> Option<String> {
@@ -1309,7 +1335,6 @@ fn main() {
13091335
//^^ self ^^^^ param
13101336
Test::from_syntax(
13111337
FileId {},
1312-
//^^^^^^^^^ file_id
13131338
"impl".into(),
13141339
//^^^^^^^^^^^^^ name
13151340
None,

0 commit comments

Comments
 (0)