Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: don't show inlay hint when param name is a_b_c and argument expr is a.b.c #19039

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions crates/ide/src/inlay_hints/param_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use either::Either;
use hir::{Callable, Semantics};
use ide_db::{famous_defs::FamousDefs, RootDatabase};

use itertools::{EitherOrBoth, Itertools};
use span::EditionedFileId;
use stdx::to_lower_snake_case;
use syntax::{
Expand Down Expand Up @@ -124,6 +125,7 @@ fn should_hide_param_name_hint(
};
let fn_name = fn_name.as_deref();
is_param_name_suffix_of_fn_name(param_name, callable, fn_name)
|| is_param_name_same_as_field_expr(argument, param_name)
|| is_argument_expr_similar_to_param_name(argument, param_name)
|| param_name.starts_with("ra_fixture")
|| (callable.n_params() == 1 && is_obvious_param(param_name))
Expand Down Expand Up @@ -154,6 +156,18 @@ fn is_param_name_suffix_of_fn_name(
}
}

/// `a_b_c_d` will match `a.b.c.d`
fn is_param_name_same_as_field_expr(argument: &ast::Expr, param_name: &str) -> bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be part of is_argument_similar_to_param_name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can't as it is: this is iterating the full field expression, whereas is_argument_similar_to_param_name uses only the last part of the field expr

I can modify it to take the whole expression in that case maybe ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, well at least move it to is_argument_expr_similar_to_param_name I'd say then, though given my other comment that might not be necessary either way

let ast::Expr::FieldExpr(argument) = argument else {
return false;
};

let argument = argument.to_string();
let param_split = param_name.split('_');
let argument_split = argument.split('.');
param_split.zip_longest(argument_split).all(|v| matches!(v, EitherOrBoth::Both(l, r) if l == r))
}

fn is_argument_expr_similar_to_param_name(argument: &ast::Expr, param_name: &str) -> bool {
let argument = match get_string_representation(argument) {
Some(argument) => argument,
Expand Down Expand Up @@ -508,6 +522,14 @@ enum CompletionKind {

fn non_ident_pat((a, b): (u32, u32)) {}

struct Abc {
def: Def
}
struct Def {
ijk: i32
}
fn baz(abc_def_ijk: i32) {}

fn main() {
const PARAM: u32 = 0;
foo(PARAM);
Expand Down Expand Up @@ -553,6 +575,13 @@ fn main() {
//^^^^^^^^^^^ param_eter

non_ident_pat((0, 0));

let abc = Abc { def: Def { ijk: 42 } };
baz(abc.def.ijk);

let a = Abc { def: Def { ijk: 43 } };
baz(a.def.ijk);
//^^^^^^^^^ abc_def_ijk
}"#,
);
}
Expand Down
Loading