2657: Omit closure parameters in closure type display strings r=flodiebold a=SomeoneToIgnore

Part of https://github.com/rust-analyzer/rust-analyzer/issues/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>
This commit is contained in:
bors[bot] 2019-12-27 10:10:07 +00:00 committed by GitHub
commit 823e9193fe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 18 deletions

View file

@ -10,7 +10,7 @@ pub struct HirFormatter<'a, 'b, DB> {
buf: String,
curr_size: usize,
max_size: Option<usize>,
should_display_default_types: bool,
omit_verbose_types: bool,
}
pub trait HirDisplay {
@ -20,7 +20,7 @@ pub trait HirDisplay {
where
Self: Sized,
{
HirDisplayWrapper(db, self, None, true)
HirDisplayWrapper(db, self, None, false)
}
fn display_truncated<'a, DB>(
@ -31,7 +31,7 @@ pub trait HirDisplay {
where
Self: Sized,
{
HirDisplayWrapper(db, self, max_size, false)
HirDisplayWrapper(db, self, max_size, true)
}
}
@ -74,8 +74,8 @@ where
}
}
pub fn should_display_default_types(&self) -> bool {
self.should_display_default_types
pub fn omit_verbose_types(&self) -> bool {
self.omit_verbose_types
}
}
@ -93,7 +93,7 @@ where
buf: String::with_capacity(20),
curr_size: 0,
max_size: self.2,
should_display_default_types: self.3,
omit_verbose_types: self.3,
})
}
}

View file

@ -821,6 +821,8 @@ impl TypeWalk for Ty {
}
}
const TYPE_HINT_TRUNCATION: &str = "";
impl HirDisplay for &Ty {
fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result {
HirDisplay::hir_fmt(*self, f)
@ -830,7 +832,7 @@ impl HirDisplay for &Ty {
impl HirDisplay for ApplicationTy {
fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result {
if f.should_truncate() {
return write!(f, "");
return write!(f, "{}", TYPE_HINT_TRUNCATION);
}
match self.ctor {
@ -908,9 +910,7 @@ impl HirDisplay for ApplicationTy {
write!(f, "<")?;
let mut non_default_parameters = Vec::with_capacity(self.parameters.len());
let parameters_to_write = if f.should_display_default_types() {
self.parameters.0.as_ref()
} else {
let parameters_to_write = if f.omit_verbose_types() {
match self
.ctor
.as_generic_def()
@ -935,6 +935,8 @@ impl HirDisplay for ApplicationTy {
&non_default_parameters
}
}
} else {
self.parameters.0.as_ref()
};
f.write_joined(parameters_to_write, ", ")?;
@ -959,9 +961,16 @@ impl HirDisplay for ApplicationTy {
let sig = self.parameters[0]
.callable_sig(f.db)
.expect("first closure parameter should contain signature");
write!(f, "|")?;
f.write_joined(sig.params(), ", ")?;
write!(f, "| -> {}", sig.ret().display(f.db))?;
let return_type_hint = sig.ret().display(f.db);
if sig.params().is_empty() {
write!(f, "|| -> {}", return_type_hint)?;
} else if f.omit_verbose_types() {
write!(f, "|{}| -> {}", TYPE_HINT_TRUNCATION, return_type_hint)?;
} else {
write!(f, "|")?;
f.write_joined(sig.params(), ", ")?;
write!(f, "| -> {}", return_type_hint)?;
};
}
}
Ok(())
@ -971,7 +980,7 @@ impl HirDisplay for ApplicationTy {
impl HirDisplay for ProjectionTy {
fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result {
if f.should_truncate() {
return write!(f, "");
return write!(f, "{}", TYPE_HINT_TRUNCATION);
}
let trait_name = f.db.trait_data(self.trait_(f.db)).name.clone();
@ -989,7 +998,7 @@ impl HirDisplay for ProjectionTy {
impl HirDisplay for Ty {
fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result {
if f.should_truncate() {
return write!(f, "");
return write!(f, "{}", TYPE_HINT_TRUNCATION);
}
match self {
@ -1074,7 +1083,7 @@ impl HirDisplay for Ty {
impl TraitRef {
fn hir_fmt_ext(&self, f: &mut HirFormatter<impl HirDatabase>, use_as: bool) -> fmt::Result {
if f.should_truncate() {
return write!(f, "");
return write!(f, "{}", TYPE_HINT_TRUNCATION);
}
self.substs[0].hir_fmt(f)?;
@ -1108,7 +1117,7 @@ impl HirDisplay for &GenericPredicate {
impl HirDisplay for GenericPredicate {
fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result {
if f.should_truncate() {
return write!(f, "");
return write!(f, "{}", TYPE_HINT_TRUNCATION);
}
match self {

View file

@ -293,7 +293,7 @@ fn main() {
}
#[test]
fn closure_parameter() {
fn closure_parameters() {
let (analysis, file_id) = single_file(
r#"
fn main() {
@ -301,6 +301,11 @@ fn main() {
(0..2).for_each(|increment| {
start += increment;
})
let multiply = |a, b, c, d| a * b * c * d;
let _: i32 = multiply(1, 2, 3, 4);
let return_42 = || 42;
}"#,
);
@ -316,6 +321,36 @@ fn main() {
kind: TypeHint,
label: "i32",
},
InlayHint {
range: [114; 122),
kind: TypeHint,
label: "|…| -> i32",
},
InlayHint {
range: [126; 127),
kind: TypeHint,
label: "i32",
},
InlayHint {
range: [129; 130),
kind: TypeHint,
label: "i32",
},
InlayHint {
range: [132; 133),
kind: TypeHint,
label: "i32",
},
InlayHint {
range: [135; 136),
kind: TypeHint,
label: "i32",
},
InlayHint {
range: [201; 210),
kind: TypeHint,
label: "|| -> i32",
},
]
"###
);