Skip to content

Commit 823e919

Browse files
Merge #2657
2657: Omit closure parameters in closure type display strings r=flodiebold a=SomeoneToIgnore Part of #1946 I wonder, should we display the the closure trait (Fn/FnMut/FnOnce) in inlay hints instead of `|...|` at all? Co-authored-by: Kirill Bulatov <mail4score@gmail.com>
2 parents 97f0139 + 242be36 commit 823e919

File tree

3 files changed

+62
-18
lines changed

3 files changed

+62
-18
lines changed

crates/ra_hir_ty/src/display.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub struct HirFormatter<'a, 'b, DB> {
1010
buf: String,
1111
curr_size: usize,
1212
max_size: Option<usize>,
13-
should_display_default_types: bool,
13+
omit_verbose_types: bool,
1414
}
1515

1616
pub trait HirDisplay {
@@ -20,7 +20,7 @@ pub trait HirDisplay {
2020
where
2121
Self: Sized,
2222
{
23-
HirDisplayWrapper(db, self, None, true)
23+
HirDisplayWrapper(db, self, None, false)
2424
}
2525

2626
fn display_truncated<'a, DB>(
@@ -31,7 +31,7 @@ pub trait HirDisplay {
3131
where
3232
Self: Sized,
3333
{
34-
HirDisplayWrapper(db, self, max_size, false)
34+
HirDisplayWrapper(db, self, max_size, true)
3535
}
3636
}
3737

@@ -74,8 +74,8 @@ where
7474
}
7575
}
7676

77-
pub fn should_display_default_types(&self) -> bool {
78-
self.should_display_default_types
77+
pub fn omit_verbose_types(&self) -> bool {
78+
self.omit_verbose_types
7979
}
8080
}
8181

@@ -93,7 +93,7 @@ where
9393
buf: String::with_capacity(20),
9494
curr_size: 0,
9595
max_size: self.2,
96-
should_display_default_types: self.3,
96+
omit_verbose_types: self.3,
9797
})
9898
}
9999
}

crates/ra_hir_ty/src/lib.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,8 @@ impl TypeWalk for Ty {
821821
}
822822
}
823823

824+
const TYPE_HINT_TRUNCATION: &str = "…";
825+
824826
impl HirDisplay for &Ty {
825827
fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result {
826828
HirDisplay::hir_fmt(*self, f)
@@ -830,7 +832,7 @@ impl HirDisplay for &Ty {
830832
impl HirDisplay for ApplicationTy {
831833
fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result {
832834
if f.should_truncate() {
833-
return write!(f, "…");
835+
return write!(f, "{}", TYPE_HINT_TRUNCATION);
834836
}
835837

836838
match self.ctor {
@@ -908,9 +910,7 @@ impl HirDisplay for ApplicationTy {
908910
write!(f, "<")?;
909911

910912
let mut non_default_parameters = Vec::with_capacity(self.parameters.len());
911-
let parameters_to_write = if f.should_display_default_types() {
912-
self.parameters.0.as_ref()
913-
} else {
913+
let parameters_to_write = if f.omit_verbose_types() {
914914
match self
915915
.ctor
916916
.as_generic_def()
@@ -935,6 +935,8 @@ impl HirDisplay for ApplicationTy {
935935
&non_default_parameters
936936
}
937937
}
938+
} else {
939+
self.parameters.0.as_ref()
938940
};
939941

940942
f.write_joined(parameters_to_write, ", ")?;
@@ -959,9 +961,16 @@ impl HirDisplay for ApplicationTy {
959961
let sig = self.parameters[0]
960962
.callable_sig(f.db)
961963
.expect("first closure parameter should contain signature");
962-
write!(f, "|")?;
963-
f.write_joined(sig.params(), ", ")?;
964-
write!(f, "| -> {}", sig.ret().display(f.db))?;
964+
let return_type_hint = sig.ret().display(f.db);
965+
if sig.params().is_empty() {
966+
write!(f, "|| -> {}", return_type_hint)?;
967+
} else if f.omit_verbose_types() {
968+
write!(f, "|{}| -> {}", TYPE_HINT_TRUNCATION, return_type_hint)?;
969+
} else {
970+
write!(f, "|")?;
971+
f.write_joined(sig.params(), ", ")?;
972+
write!(f, "| -> {}", return_type_hint)?;
973+
};
965974
}
966975
}
967976
Ok(())
@@ -971,7 +980,7 @@ impl HirDisplay for ApplicationTy {
971980
impl HirDisplay for ProjectionTy {
972981
fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result {
973982
if f.should_truncate() {
974-
return write!(f, "…");
983+
return write!(f, "{}", TYPE_HINT_TRUNCATION);
975984
}
976985

977986
let trait_name = f.db.trait_data(self.trait_(f.db)).name.clone();
@@ -989,7 +998,7 @@ impl HirDisplay for ProjectionTy {
989998
impl HirDisplay for Ty {
990999
fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result {
9911000
if f.should_truncate() {
992-
return write!(f, "…");
1001+
return write!(f, "{}", TYPE_HINT_TRUNCATION);
9931002
}
9941003

9951004
match self {
@@ -1074,7 +1083,7 @@ impl HirDisplay for Ty {
10741083
impl TraitRef {
10751084
fn hir_fmt_ext(&self, f: &mut HirFormatter<impl HirDatabase>, use_as: bool) -> fmt::Result {
10761085
if f.should_truncate() {
1077-
return write!(f, "…");
1086+
return write!(f, "{}", TYPE_HINT_TRUNCATION);
10781087
}
10791088

10801089
self.substs[0].hir_fmt(f)?;
@@ -1108,7 +1117,7 @@ impl HirDisplay for &GenericPredicate {
11081117
impl HirDisplay for GenericPredicate {
11091118
fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result {
11101119
if f.should_truncate() {
1111-
return write!(f, "…");
1120+
return write!(f, "{}", TYPE_HINT_TRUNCATION);
11121121
}
11131122

11141123
match self {

crates/ra_ide/src/inlay_hints.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,14 +293,19 @@ fn main() {
293293
}
294294

295295
#[test]
296-
fn closure_parameter() {
296+
fn closure_parameters() {
297297
let (analysis, file_id) = single_file(
298298
r#"
299299
fn main() {
300300
let mut start = 0;
301301
(0..2).for_each(|increment| {
302302
start += increment;
303303
})
304+
305+
let multiply = |a, b, c, d| a * b * c * d;
306+
let _: i32 = multiply(1, 2, 3, 4);
307+
308+
let return_42 = || 42;
304309
}"#,
305310
);
306311

@@ -316,6 +321,36 @@ fn main() {
316321
kind: TypeHint,
317322
label: "i32",
318323
},
324+
InlayHint {
325+
range: [114; 122),
326+
kind: TypeHint,
327+
label: "|…| -> i32",
328+
},
329+
InlayHint {
330+
range: [126; 127),
331+
kind: TypeHint,
332+
label: "i32",
333+
},
334+
InlayHint {
335+
range: [129; 130),
336+
kind: TypeHint,
337+
label: "i32",
338+
},
339+
InlayHint {
340+
range: [132; 133),
341+
kind: TypeHint,
342+
label: "i32",
343+
},
344+
InlayHint {
345+
range: [135; 136),
346+
kind: TypeHint,
347+
label: "i32",
348+
},
349+
InlayHint {
350+
range: [201; 210),
351+
kind: TypeHint,
352+
label: "|| -> i32",
353+
},
319354
]
320355
"###
321356
);

0 commit comments

Comments
 (0)