Merge #2594
2594: Omit default parameter types r=matklad a=SomeoneToIgnore Part of https://github.com/rust-analyzer/rust-analyzer/issues/1946 Co-authored-by: Kirill Bulatov <mail4score@gmail.com>
This commit is contained in:
commit
a766f63650
@ -10,6 +10,7 @@ pub struct HirFormatter<'a, 'b, DB> {
|
||||
buf: String,
|
||||
curr_size: usize,
|
||||
max_size: Option<usize>,
|
||||
should_display_default_types: bool,
|
||||
}
|
||||
|
||||
pub trait HirDisplay {
|
||||
@ -19,7 +20,7 @@ pub trait HirDisplay {
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
HirDisplayWrapper(db, self, None)
|
||||
HirDisplayWrapper(db, self, None, true)
|
||||
}
|
||||
|
||||
fn display_truncated<'a, DB>(
|
||||
@ -30,7 +31,7 @@ pub trait HirDisplay {
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
HirDisplayWrapper(db, self, max_size)
|
||||
HirDisplayWrapper(db, self, max_size, false)
|
||||
}
|
||||
}
|
||||
|
||||
@ -72,9 +73,13 @@ where
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
pub fn should_display_default_types(&self) -> bool {
|
||||
self.should_display_default_types
|
||||
}
|
||||
}
|
||||
|
||||
pub struct HirDisplayWrapper<'a, DB, T>(&'a DB, &'a T, Option<usize>);
|
||||
pub struct HirDisplayWrapper<'a, DB, T>(&'a DB, &'a T, Option<usize>, bool);
|
||||
|
||||
impl<'a, DB, T> fmt::Display for HirDisplayWrapper<'a, DB, T>
|
||||
where
|
||||
@ -88,6 +93,7 @@ where
|
||||
buf: String::with_capacity(20),
|
||||
curr_size: 0,
|
||||
max_size: self.2,
|
||||
should_display_default_types: self.3,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -906,7 +906,38 @@ impl HirDisplay for ApplicationTy {
|
||||
write!(f, "{}", name)?;
|
||||
if self.parameters.len() > 0 {
|
||||
write!(f, "<")?;
|
||||
f.write_joined(&*self.parameters.0, ", ")?;
|
||||
|
||||
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 {
|
||||
match self
|
||||
.ctor
|
||||
.as_generic_def()
|
||||
.map(|generic_def_id| f.db.generic_defaults(generic_def_id))
|
||||
.filter(|defaults| !defaults.is_empty())
|
||||
{
|
||||
Option::None => self.parameters.0.as_ref(),
|
||||
Option::Some(default_parameters) => {
|
||||
for (i, parameter) in self.parameters.into_iter().enumerate() {
|
||||
match (parameter, default_parameters.get(i)) {
|
||||
(&Ty::Unknown, _) | (_, None) => {
|
||||
non_default_parameters.push(parameter.clone())
|
||||
}
|
||||
(_, Some(default_parameter))
|
||||
if parameter != default_parameter =>
|
||||
{
|
||||
non_default_parameters.push(parameter.clone())
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
&non_default_parameters
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
f.write_joined(parameters_to_write, ", ")?;
|
||||
write!(f, ">")?;
|
||||
}
|
||||
}
|
||||
|
@ -250,7 +250,7 @@ pub(crate) fn type_of(db: &RootDatabase, frange: FileRange) -> Option<String> {
|
||||
} else {
|
||||
return None;
|
||||
};
|
||||
Some(ty.display(db).to_string())
|
||||
Some(ty.display_truncated(db, None).to_string())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@ -424,6 +424,23 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hover_omits_default_generic_types() {
|
||||
check_hover_result(
|
||||
r#"
|
||||
//- /main.rs
|
||||
struct Test<K, T = u8> {
|
||||
k: K,
|
||||
t: T,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let zz<|> = Test { t: 23, k: 33 };
|
||||
}"#,
|
||||
&["Test<i32>"],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hover_some() {
|
||||
let (analysis, position) = single_file_with_position(
|
||||
|
@ -159,6 +159,32 @@ mod tests {
|
||||
|
||||
use crate::mock_analysis::single_file;
|
||||
|
||||
#[test]
|
||||
fn default_generic_types_should_not_be_displayed() {
|
||||
let (analysis, file_id) = single_file(
|
||||
r#"
|
||||
struct Test<K, T = u8> {
|
||||
k: K,
|
||||
t: T,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let zz = Test { t: 23, k: 33 };
|
||||
}"#,
|
||||
);
|
||||
|
||||
assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###"
|
||||
[
|
||||
InlayHint {
|
||||
range: [69; 71),
|
||||
kind: TypeHint,
|
||||
label: "Test<i32>",
|
||||
},
|
||||
]
|
||||
"###
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn let_statement() {
|
||||
let (analysis, file_id) = single_file(
|
||||
|
Loading…
x
Reference in New Issue
Block a user