diff --git a/crates/ra_ide/src/inlay_hints.rs b/crates/ra_ide/src/inlay_hints.rs index 4b133b19ba..da9f55a696 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; @@ -235,8 +235,7 @@ fn should_show_param_hint( param_name: &str, 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() || is_argument_similar_to_param(argument, param_name) { return false; } @@ -245,14 +244,37 @@ 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 + 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 = 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, + _ => false, + }; + param_name.len() == 1 || is_obvious_param_name +} + fn get_fn_signature(sema: &Semantics, expr: &ast::Expr) -> Option { match expr { ast::Expr::CallExpr(expr) => { @@ -1059,9 +1081,18 @@ 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 different_order_mut(param: &mut Param) {} + fn main() { let container: TestVarContainer = TestVarContainer { test_var: 42 }; let test: Test = Test {}; @@ -1069,11 +1100,20 @@ 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(¶m_begin); + different_order(&mut param_begin); + + let a: f64 = 7.0; + let b: f64 = 4.0; + let _: f64 = a.div_euclid(b); + let _: f64 = a.abs_sub(b); }"#, );