From 8eb820f460f00ab20b9b8d952c600755ecadee2c Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Sun, 3 May 2020 15:07:57 +0300 Subject: Omit name hints for method names similar to parameter names --- crates/ra_ide/src/inlay_hints.rs | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/crates/ra_ide/src/inlay_hints.rs b/crates/ra_ide/src/inlay_hints.rs index 98483df32..8eb2c4412 100644 --- a/crates/ra_ide/src/inlay_hints.rs +++ b/crates/ra_ide/src/inlay_hints.rs @@ -144,7 +144,7 @@ fn get_param_name_hints( .iter() .skip(n_params_to_skip) .zip(args) - .filter(|(param, arg)| should_show_param_hint(&fn_signature, param, &arg)) + .filter(|(param, arg)| should_show_param_name_hint(&fn_signature, param, &arg)) .map(|(param_name, arg)| InlayHint { range: arg.syntax().text_range(), kind: InlayKind::ParameterHint, @@ -181,7 +181,7 @@ fn get_bind_pat_hints( fn pat_is_enum_variant(db: &RootDatabase, bind_pat: &ast::BindPat, pat_ty: &Type) -> bool { if let Some(Adt::Enum(enum_data)) = pat_ty.as_adt() { - let pat_text = bind_pat.syntax().to_string(); + let pat_text = bind_pat.to_string(); enum_data .variants(db) .into_iter() @@ -198,7 +198,7 @@ fn should_not_display_type_hint(db: &RootDatabase, bind_pat: &ast::BindPat, pat_ } if let Some(Adt::Struct(s)) = pat_ty.as_adt() { - if s.fields(db).is_empty() && s.name(db).to_string() == bind_pat.syntax().to_string() { + if s.fields(db).is_empty() && s.name(db).to_string() == bind_pat.to_string() { return true; } } @@ -230,13 +230,13 @@ fn should_not_display_type_hint(db: &RootDatabase, bind_pat: &ast::BindPat, pat_ false } -fn should_show_param_hint( +fn should_show_param_name_hint( fn_signature: &FunctionSignature, param_name: &str, argument: &ast::Expr, ) -> bool { if param_name.is_empty() - || is_argument_similar_to_param(argument, param_name) + || is_argument_similar_to_param_name(argument, param_name) || Some(param_name.trim_start_matches('_')) == fn_signature.name.as_ref().map(|s| s.trim_start_matches('_')) { @@ -254,20 +254,25 @@ fn should_show_param_hint( parameters_len != 1 || !is_obvious_param(param_name) } -fn is_argument_similar_to_param(argument: &ast::Expr, param_name: &str) -> bool { - let argument_string = remove_ref(argument.clone()).syntax().to_string(); +fn is_argument_similar_to_param_name(argument: &ast::Expr, param_name: &str) -> bool { + let argument_string = if let Some(repr) = get_string_representation(argument) { + repr + } else { + return false; + }; let param_name = param_name.trim_start_matches('_'); let argument_string = argument_string.trim_start_matches('_'); argument_string.starts_with(¶m_name) || argument_string.ends_with(¶m_name) } -fn remove_ref(expr: ast::Expr) -> ast::Expr { - if let ast::Expr::RefExpr(ref_expr) = &expr { - if let Some(inner) = ref_expr.expr() { - return inner; +fn get_string_representation(expr: &ast::Expr) -> Option { + match expr { + ast::Expr::MethodCallExpr(method_call_expr) => { + Some(method_call_expr.name_ref()?.to_string()) } + ast::Expr::RefExpr(ref_expr) => get_string_representation(&ref_expr.expr()?), + _ => Some(expr.to_string()), } - expr } fn is_obvious_param(param_name: &str) -> bool { @@ -1073,6 +1078,12 @@ struct TestVarContainer { test_var: i32, } +impl TestVarContainer { + fn test_var(&self) -> i32 { + self.test_var + } +} + struct Test {} impl Test { @@ -1114,12 +1125,13 @@ fn main() { let test_var: i32 = 55; test_processed.no_hints_expected(22, test_var); test_processed.no_hints_expected(33, container.test_var); + test_processed.no_hints_expected(44, container.test_var()); test_processed.frob(false); twiddle(true); doo(true); - let param_begin: Param = Param {}; + let mut param_begin: Param = Param {}; different_order(¶m_begin); different_order(&mut param_begin); -- cgit v1.2.3