From 2a582b78a5f9c1fa908fe5f4c9ff4ab2966adb2e Mon Sep 17 00:00:00 2001 From: IceSentry Date: Wed, 8 Apr 2020 17:48:16 -0400 Subject: Add more heuristics for hiding obvious param hints This will now hide "value", "pat", "rhs" and "other" These words were selected from the std because they are used in common functions with only a single param and are obvious by their use. I think it would be good to also hide "bytes" if the type is `[u8; n]` but I'm not sure how to get the param type signature It will also hide the hint if the passed param starts or end with the param_name --- crates/ra_ide/src/inlay_hints.rs | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) (limited to 'crates') diff --git a/crates/ra_ide/src/inlay_hints.rs b/crates/ra_ide/src/inlay_hints.rs index 4b133b19b..6a4fe15fd 100644 --- a/crates/ra_ide/src/inlay_hints.rs +++ b/crates/ra_ide/src/inlay_hints.rs @@ -1,4 +1,4 @@ -//! FIXME: write short doc here +//! This module defines multiple types of inlay hints and their visibility use hir::{Adt, HirDisplay, Semantics, Type}; use ra_ide_db::RootDatabase; @@ -236,7 +236,10 @@ fn should_show_param_hint( argument: &ast::Expr, ) -> bool { let argument_string = argument.syntax().to_string(); - if param_name.is_empty() || argument_string.ends_with(param_name) { + if param_name.is_empty() + || argument_string.ends_with(¶m_name) + || argument_string.starts_with(¶m_name) + { return false; } @@ -245,8 +248,15 @@ fn should_show_param_hint( } else { fn_signature.parameters.len() }; + // avoid displaying hints for common functions like map, filter, etc. - if parameters_len == 1 && (param_name.len() == 1 || param_name == "predicate") { + // or other obvious words used in std + // TODO ignore "bytes" if the type is [u8; n] + let is_obvious_param_name = match param_name { + "predicate" | "value" | "pat" | "rhs" | "other" => true, + _ => false, + }; + if parameters_len == 1 && (param_name.len() == 1 || is_obvious_param_name) { return false; } @@ -1059,9 +1069,17 @@ impl Test { self } + fn field(self, value: i32) -> Self { + self + } + fn no_hints_expected(&self, _: i32, test_var: i32) {} } +struct Param {} + +fn different_order(param: Param) {} + fn main() { let container: TestVarContainer = TestVarContainer { test_var: 42 }; let test: Test = Test {}; @@ -1069,11 +1087,19 @@ fn main() { map(22); filter(33); - let test_processed: Test = test.map(1).filter(2); + let test_processed: Test = test.map(1).filter(2).field(3); let test_var: i32 = 55; test_processed.no_hints_expected(22, test_var); test_processed.no_hints_expected(33, container.test_var); + + let param_begin: Param = Param {}; + different_order(param_begin); + + let a: f64 = 7.0; + let b: f64 = 4.0; + let _: f64 = a.div_euclid(b); + let _: f64 = a.abs_sub(b); }"#, ); -- cgit v1.2.3 From a2dc18f71acf83dd2946622603d3da00b456d42a Mon Sep 17 00:00:00 2001 From: IceSentry Date: Wed, 8 Apr 2020 18:11:24 -0400 Subject: remove TODO --- crates/ra_ide/src/inlay_hints.rs | 1 - 1 file changed, 1 deletion(-) (limited to 'crates') diff --git a/crates/ra_ide/src/inlay_hints.rs b/crates/ra_ide/src/inlay_hints.rs index 6a4fe15fd..a86e18fbb 100644 --- a/crates/ra_ide/src/inlay_hints.rs +++ b/crates/ra_ide/src/inlay_hints.rs @@ -251,7 +251,6 @@ fn should_show_param_hint( // avoid displaying hints for common functions like map, filter, etc. // or other obvious words used in std - // TODO ignore "bytes" if the type is [u8; n] let is_obvious_param_name = match param_name { "predicate" | "value" | "pat" | "rhs" | "other" => true, _ => false, -- cgit v1.2.3 From de6db0632228ae61f7ec1f87eaf520ccd4e46925 Mon Sep 17 00:00:00 2001 From: IceSentry Date: Wed, 8 Apr 2020 19:07:21 -0400 Subject: ignore `&mut ` and `&` when checking params --- crates/ra_ide/src/inlay_hints.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'crates') diff --git a/crates/ra_ide/src/inlay_hints.rs b/crates/ra_ide/src/inlay_hints.rs index a86e18fbb..6b8b5813d 100644 --- a/crates/ra_ide/src/inlay_hints.rs +++ b/crates/ra_ide/src/inlay_hints.rs @@ -235,7 +235,15 @@ fn should_show_param_hint( param_name: &str, argument: &ast::Expr, ) -> bool { - let argument_string = argument.syntax().to_string(); + let argument_string = { + let arg_string = argument.syntax().to_string(); + let arg_split: Vec = arg_string.chars().collect(); + match arg_split.as_slice() { + ['&', 'm', 'u', 't', ' ', arg_name @ ..] => arg_name.into_iter().collect::(), + ['&', arg_name @ ..] => arg_name.into_iter().collect::(), + _ => arg_string, + } + }; if param_name.is_empty() || argument_string.ends_with(¶m_name) || argument_string.starts_with(¶m_name) @@ -1077,7 +1085,8 @@ impl Test { struct Param {} -fn different_order(param: Param) {} +fn different_order(param: &Param) {} +fn different_order_mut(param: &mut Param) {} fn main() { let container: TestVarContainer = TestVarContainer { test_var: 42 }; @@ -1093,7 +1102,8 @@ fn main() { test_processed.no_hints_expected(33, container.test_var); let param_begin: Param = Param {}; - different_order(param_begin); + different_order(¶m_begin); + different_order(&mut param_begin); let a: f64 = 7.0; let b: f64 = 4.0; -- cgit v1.2.3 From cba694c60276f7543ee9ed1dddf3fe93209f527f Mon Sep 17 00:00:00 2001 From: IceSentry Date: Wed, 8 Apr 2020 19:26:47 -0400 Subject: better `&mut ` and `&` matching --- crates/ra_ide/src/inlay_hints.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'crates') diff --git a/crates/ra_ide/src/inlay_hints.rs b/crates/ra_ide/src/inlay_hints.rs index 6b8b5813d..1da61e4c4 100644 --- a/crates/ra_ide/src/inlay_hints.rs +++ b/crates/ra_ide/src/inlay_hints.rs @@ -236,13 +236,13 @@ fn should_show_param_hint( argument: &ast::Expr, ) -> bool { let argument_string = { - let arg_string = argument.syntax().to_string(); - let arg_split: Vec = arg_string.chars().collect(); - match arg_split.as_slice() { - ['&', 'm', 'u', 't', ' ', arg_name @ ..] => arg_name.into_iter().collect::(), - ['&', arg_name @ ..] => arg_name.into_iter().collect::(), - _ => arg_string, + let mut arg_string = argument.syntax().to_string(); + if arg_string.get(0..5) == Some("&mut ") { + arg_string = arg_string[5..].to_string(); + } else if arg_string.get(0..1) == Some("&") { + arg_string = arg_string[1..].to_string(); } + arg_string }; if param_name.is_empty() || argument_string.ends_with(¶m_name) -- cgit v1.2.3 From ae416f3c6e90a8d2b2f9d1713d2db4ddce12df65 Mon Sep 17 00:00:00 2001 From: IceSentry Date: Thu, 9 Apr 2020 10:35:07 -0400 Subject: clean up param hint checking --- crates/ra_ide/src/inlay_hints.rs | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) (limited to 'crates') diff --git a/crates/ra_ide/src/inlay_hints.rs b/crates/ra_ide/src/inlay_hints.rs index 1da61e4c4..b1661d238 100644 --- a/crates/ra_ide/src/inlay_hints.rs +++ b/crates/ra_ide/src/inlay_hints.rs @@ -235,19 +235,7 @@ fn should_show_param_hint( param_name: &str, argument: &ast::Expr, ) -> bool { - let argument_string = { - let mut arg_string = argument.syntax().to_string(); - if arg_string.get(0..5) == Some("&mut ") { - arg_string = arg_string[5..].to_string(); - } else if arg_string.get(0..1) == Some("&") { - arg_string = arg_string[1..].to_string(); - } - arg_string - }; - if param_name.is_empty() - || argument_string.ends_with(¶m_name) - || argument_string.starts_with(¶m_name) - { + if param_name.is_empty() || is_argument_similar_to_param(argument, param_name) { return false; } @@ -259,15 +247,27 @@ fn should_show_param_hint( // avoid displaying hints for common functions like map, filter, etc. // or other obvious words used in std + if parameters_len == 1 && is_obvious_param(param_name) { + return false; + } + true +} + +fn is_argument_similar_to_param(argument: &ast::Expr, param_name: &str) -> bool { + let argument_string = if let ast::Expr::RefExpr(ref_expr) = argument { + ref_expr.syntax().last_token().expect("RefExpr should have a last_token").to_string() + } else { + argument.syntax().to_string() + }; + argument_string.starts_with(¶m_name) || argument_string.ends_with(¶m_name) +} + +fn is_obvious_param(param_name: &str) -> bool { let is_obvious_param_name = match param_name { "predicate" | "value" | "pat" | "rhs" | "other" => true, _ => false, }; - if parameters_len == 1 && (param_name.len() == 1 || is_obvious_param_name) { - return false; - } - - true + param_name.len() == 1 || is_obvious_param_name } fn get_fn_signature(sema: &Semantics, expr: &ast::Expr) -> Option { -- cgit v1.2.3 From ebc61709c86a9bfceafecc0b6ed9cc4f4624e0fe Mon Sep 17 00:00:00 2001 From: IceSentry Date: Thu, 9 Apr 2020 12:26:49 -0400 Subject: use .expr() to remove ref --- crates/ra_ide/src/inlay_hints.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'crates') diff --git a/crates/ra_ide/src/inlay_hints.rs b/crates/ra_ide/src/inlay_hints.rs index b1661d238..da9f55a69 100644 --- a/crates/ra_ide/src/inlay_hints.rs +++ b/crates/ra_ide/src/inlay_hints.rs @@ -254,14 +254,19 @@ fn should_show_param_hint( } fn is_argument_similar_to_param(argument: &ast::Expr, param_name: &str) -> bool { - let argument_string = if let ast::Expr::RefExpr(ref_expr) = argument { - ref_expr.syntax().last_token().expect("RefExpr should have a last_token").to_string() - } else { - argument.syntax().to_string() - }; + let argument_string = remove_ref(argument.clone()).syntax().to_string(); 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; + } + } + expr +} + fn is_obvious_param(param_name: &str) -> bool { let is_obvious_param_name = match param_name { "predicate" | "value" | "pat" | "rhs" | "other" => true, -- cgit v1.2.3