Skip to content

Commit 9635d8b

Browse files
bors[bot]IceSentry
andauthored
Merge #3901
3901: Add more heuristics for hiding obvious param hints r=matklad a=IceSentry This will now hide `value`, `pat`, `rhs` and `other`. These words were selected from the std because they are used in commonly used functions with only a single param and are obvious by their use. It will also hide the hint if the passed param **starts** or end with the param_name. Maybe we could also split on '_' and check if one of the string is the param_name. 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. Closes #3900 Co-authored-by: IceSentry <[email protected]>
2 parents dde9488 + ebc6170 commit 9635d8b

File tree

1 file changed

+46
-6
lines changed

1 file changed

+46
-6
lines changed

crates/ra_ide/src/inlay_hints.rs

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! FIXME: write short doc here
1+
//! This module defines multiple types of inlay hints and their visibility
22
33
use hir::{Adt, HirDisplay, Semantics, Type};
44
use ra_ide_db::RootDatabase;
@@ -235,8 +235,7 @@ fn should_show_param_hint(
235235
param_name: &str,
236236
argument: &ast::Expr,
237237
) -> bool {
238-
let argument_string = argument.syntax().to_string();
239-
if param_name.is_empty() || argument_string.ends_with(param_name) {
238+
if param_name.is_empty() || is_argument_similar_to_param(argument, param_name) {
240239
return false;
241240
}
242241

@@ -245,14 +244,37 @@ fn should_show_param_hint(
245244
} else {
246245
fn_signature.parameters.len()
247246
};
247+
248248
// avoid displaying hints for common functions like map, filter, etc.
249-
if parameters_len == 1 && (param_name.len() == 1 || param_name == "predicate") {
249+
// or other obvious words used in std
250+
if parameters_len == 1 && is_obvious_param(param_name) {
250251
return false;
251252
}
252-
253253
true
254254
}
255255

256+
fn is_argument_similar_to_param(argument: &ast::Expr, param_name: &str) -> bool {
257+
let argument_string = remove_ref(argument.clone()).syntax().to_string();
258+
argument_string.starts_with(&param_name) || argument_string.ends_with(&param_name)
259+
}
260+
261+
fn remove_ref(expr: ast::Expr) -> ast::Expr {
262+
if let ast::Expr::RefExpr(ref_expr) = &expr {
263+
if let Some(inner) = ref_expr.expr() {
264+
return inner;
265+
}
266+
}
267+
expr
268+
}
269+
270+
fn is_obvious_param(param_name: &str) -> bool {
271+
let is_obvious_param_name = match param_name {
272+
"predicate" | "value" | "pat" | "rhs" | "other" => true,
273+
_ => false,
274+
};
275+
param_name.len() == 1 || is_obvious_param_name
276+
}
277+
256278
fn get_fn_signature(sema: &Semantics<RootDatabase>, expr: &ast::Expr) -> Option<FunctionSignature> {
257279
match expr {
258280
ast::Expr::CallExpr(expr) => {
@@ -1059,21 +1081,39 @@ impl Test {
10591081
self
10601082
}
10611083
1084+
fn field(self, value: i32) -> Self {
1085+
self
1086+
}
1087+
10621088
fn no_hints_expected(&self, _: i32, test_var: i32) {}
10631089
}
10641090
1091+
struct Param {}
1092+
1093+
fn different_order(param: &Param) {}
1094+
fn different_order_mut(param: &mut Param) {}
1095+
10651096
fn main() {
10661097
let container: TestVarContainer = TestVarContainer { test_var: 42 };
10671098
let test: Test = Test {};
10681099
10691100
map(22);
10701101
filter(33);
10711102
1072-
let test_processed: Test = test.map(1).filter(2);
1103+
let test_processed: Test = test.map(1).filter(2).field(3);
10731104
10741105
let test_var: i32 = 55;
10751106
test_processed.no_hints_expected(22, test_var);
10761107
test_processed.no_hints_expected(33, container.test_var);
1108+
1109+
let param_begin: Param = Param {};
1110+
different_order(&param_begin);
1111+
different_order(&mut param_begin);
1112+
1113+
let a: f64 = 7.0;
1114+
let b: f64 = 4.0;
1115+
let _: f64 = a.div_euclid(b);
1116+
let _: f64 = a.abs_sub(b);
10771117
}"#,
10781118
);
10791119

0 commit comments

Comments
 (0)