aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKirill Bulatov <[email protected]>2020-05-03 13:07:57 +0100
committerKirill Bulatov <[email protected]>2020-05-03 13:55:08 +0100
commit8eb820f460f00ab20b9b8d952c600755ecadee2c (patch)
tree1b33340a396d93273941288dbb5236852d9533d8
parentbaa35b02b2f1a197beec65bcd68f56ffb89abfa1 (diff)
Omit name hints for method names similar to parameter names
-rw-r--r--crates/ra_ide/src/inlay_hints.rs38
1 files 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(
144 .iter() 144 .iter()
145 .skip(n_params_to_skip) 145 .skip(n_params_to_skip)
146 .zip(args) 146 .zip(args)
147 .filter(|(param, arg)| should_show_param_hint(&fn_signature, param, &arg)) 147 .filter(|(param, arg)| should_show_param_name_hint(&fn_signature, param, &arg))
148 .map(|(param_name, arg)| InlayHint { 148 .map(|(param_name, arg)| InlayHint {
149 range: arg.syntax().text_range(), 149 range: arg.syntax().text_range(),
150 kind: InlayKind::ParameterHint, 150 kind: InlayKind::ParameterHint,
@@ -181,7 +181,7 @@ fn get_bind_pat_hints(
181 181
182fn pat_is_enum_variant(db: &RootDatabase, bind_pat: &ast::BindPat, pat_ty: &Type) -> bool { 182fn pat_is_enum_variant(db: &RootDatabase, bind_pat: &ast::BindPat, pat_ty: &Type) -> bool {
183 if let Some(Adt::Enum(enum_data)) = pat_ty.as_adt() { 183 if let Some(Adt::Enum(enum_data)) = pat_ty.as_adt() {
184 let pat_text = bind_pat.syntax().to_string(); 184 let pat_text = bind_pat.to_string();
185 enum_data 185 enum_data
186 .variants(db) 186 .variants(db)
187 .into_iter() 187 .into_iter()
@@ -198,7 +198,7 @@ fn should_not_display_type_hint(db: &RootDatabase, bind_pat: &ast::BindPat, pat_
198 } 198 }
199 199
200 if let Some(Adt::Struct(s)) = pat_ty.as_adt() { 200 if let Some(Adt::Struct(s)) = pat_ty.as_adt() {
201 if s.fields(db).is_empty() && s.name(db).to_string() == bind_pat.syntax().to_string() { 201 if s.fields(db).is_empty() && s.name(db).to_string() == bind_pat.to_string() {
202 return true; 202 return true;
203 } 203 }
204 } 204 }
@@ -230,13 +230,13 @@ fn should_not_display_type_hint(db: &RootDatabase, bind_pat: &ast::BindPat, pat_
230 false 230 false
231} 231}
232 232
233fn should_show_param_hint( 233fn should_show_param_name_hint(
234 fn_signature: &FunctionSignature, 234 fn_signature: &FunctionSignature,
235 param_name: &str, 235 param_name: &str,
236 argument: &ast::Expr, 236 argument: &ast::Expr,
237) -> bool { 237) -> bool {
238 if param_name.is_empty() 238 if param_name.is_empty()
239 || is_argument_similar_to_param(argument, param_name) 239 || is_argument_similar_to_param_name(argument, param_name)
240 || Some(param_name.trim_start_matches('_')) 240 || Some(param_name.trim_start_matches('_'))
241 == fn_signature.name.as_ref().map(|s| s.trim_start_matches('_')) 241 == fn_signature.name.as_ref().map(|s| s.trim_start_matches('_'))
242 { 242 {
@@ -254,20 +254,25 @@ fn should_show_param_hint(
254 parameters_len != 1 || !is_obvious_param(param_name) 254 parameters_len != 1 || !is_obvious_param(param_name)
255} 255}
256 256
257fn is_argument_similar_to_param(argument: &ast::Expr, param_name: &str) -> bool { 257fn is_argument_similar_to_param_name(argument: &ast::Expr, param_name: &str) -> bool {
258 let argument_string = remove_ref(argument.clone()).syntax().to_string(); 258 let argument_string = if let Some(repr) = get_string_representation(argument) {
259 repr
260 } else {
261 return false;
262 };
259 let param_name = param_name.trim_start_matches('_'); 263 let param_name = param_name.trim_start_matches('_');
260 let argument_string = argument_string.trim_start_matches('_'); 264 let argument_string = argument_string.trim_start_matches('_');
261 argument_string.starts_with(&param_name) || argument_string.ends_with(&param_name) 265 argument_string.starts_with(&param_name) || argument_string.ends_with(&param_name)
262} 266}
263 267
264fn remove_ref(expr: ast::Expr) -> ast::Expr { 268fn get_string_representation(expr: &ast::Expr) -> Option<String> {
265 if let ast::Expr::RefExpr(ref_expr) = &expr { 269 match expr {
266 if let Some(inner) = ref_expr.expr() { 270 ast::Expr::MethodCallExpr(method_call_expr) => {
267 return inner; 271 Some(method_call_expr.name_ref()?.to_string())
268 } 272 }
273 ast::Expr::RefExpr(ref_expr) => get_string_representation(&ref_expr.expr()?),
274 _ => Some(expr.to_string()),
269 } 275 }
270 expr
271} 276}
272 277
273fn is_obvious_param(param_name: &str) -> bool { 278fn is_obvious_param(param_name: &str) -> bool {
@@ -1073,6 +1078,12 @@ struct TestVarContainer {
1073 test_var: i32, 1078 test_var: i32,
1074} 1079}
1075 1080
1081impl TestVarContainer {
1082 fn test_var(&self) -> i32 {
1083 self.test_var
1084 }
1085}
1086
1076struct Test {} 1087struct Test {}
1077 1088
1078impl Test { 1089impl Test {
@@ -1114,12 +1125,13 @@ fn main() {
1114 let test_var: i32 = 55; 1125 let test_var: i32 = 55;
1115 test_processed.no_hints_expected(22, test_var); 1126 test_processed.no_hints_expected(22, test_var);
1116 test_processed.no_hints_expected(33, container.test_var); 1127 test_processed.no_hints_expected(33, container.test_var);
1128 test_processed.no_hints_expected(44, container.test_var());
1117 test_processed.frob(false); 1129 test_processed.frob(false);
1118 1130
1119 twiddle(true); 1131 twiddle(true);
1120 doo(true); 1132 doo(true);
1121 1133
1122 let param_begin: Param = Param {}; 1134 let mut param_begin: Param = Param {};
1123 different_order(&param_begin); 1135 different_order(&param_begin);
1124 different_order(&mut param_begin); 1136 different_order(&mut param_begin);
1125 1137