Omit default types in HirDisplay SourceCode mode

This commit is contained in:
Timo Freiberg 2020-05-10 18:09:22 +02:00
parent 225f353aa2
commit cbbbd48325
3 changed files with 64 additions and 28 deletions

View File

@ -209,7 +209,7 @@ struct Test<K, T = u8> {
}
fn main() {
let test<|>: Test<i32, u8> = Test { t: 23, k: 33 };
let test<|>: Test<i32> = Test { t: 23, k: 33 };
}"#,
);
}

View File

@ -136,6 +136,12 @@ enum DisplayTarget {
SourceCode { module_id: ModuleId },
}
impl DisplayTarget {
fn is_source_code(&self) -> bool {
matches!(self, Self::SourceCode {..})
}
}
#[derive(Debug)]
pub enum DisplaySourceCodeError {
PathNotFound,
@ -303,37 +309,40 @@ impl HirDisplay for ApplicationTy {
if self.parameters.len() > 0 {
let mut non_default_parameters = Vec::with_capacity(self.parameters.len());
let parameters_to_write = if f.omit_verbose_types() {
match self
.ctor
.as_generic_def()
.map(|generic_def_id| f.db.generic_defaults(generic_def_id))
.filter(|defaults| !defaults.is_empty())
{
None => self.parameters.0.as_ref(),
Some(default_parameters) => {
for (i, parameter) in self.parameters.iter().enumerate() {
match (parameter, default_parameters.get(i)) {
(&Ty::Unknown, _) | (_, None) => {
non_default_parameters.push(parameter.clone())
let parameters_to_write =
if f.display_target.is_source_code() || f.omit_verbose_types() {
match self
.ctor
.as_generic_def()
.map(|generic_def_id| f.db.generic_defaults(generic_def_id))
.filter(|defaults| !defaults.is_empty())
{
None => self.parameters.0.as_ref(),
Some(default_parameters) => {
for (i, parameter) in self.parameters.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())
}
_ => (),
}
(_, Some(default_parameter))
if parameter != default_parameter =>
{
non_default_parameters.push(parameter.clone())
}
_ => (),
}
&non_default_parameters
}
&non_default_parameters
}
}
} else {
self.parameters.0.as_ref()
};
write!(f, "<")?;
f.write_joined(parameters_to_write, ", ")?;
write!(f, ">")?;
} else {
self.parameters.0.as_ref()
};
if !parameters_to_write.is_empty() {
write!(f, "<")?;
f.write_joined(parameters_to_write, ", ")?;
write!(f, ">")?;
}
}
}
TypeCtor::AssociatedType(type_alias) => {

View File

@ -21,3 +21,30 @@ fn bar() {
);
assert_eq!("foo::Foo", displayed_source_at_pos(&db, pos));
}
#[test]
fn omit_default_type_parameters() {
let (db, pos) = TestDB::with_position(
r"
//- /main.rs
struct Foo<T = u8> { t: T }
fn main() {
let foo = Foo { t: 5 };
foo<|>;
}
",
);
assert_eq!("Foo", displayed_source_at_pos(&db, pos));
let (db, pos) = TestDB::with_position(
r"
//- /main.rs
struct Foo<K, T = u8> { k: K, t: T }
fn main() {
let foo = Foo { k: 400, t: 5 };
foo<|>;
}
",
);
assert_eq!("Foo<i32>", displayed_source_at_pos(&db, pos));
}