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,7 +309,8 @@ 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() {
let parameters_to_write =
if f.display_target.is_source_code() || f.omit_verbose_types() {
match self
.ctor
.as_generic_def()
@ -331,11 +338,13 @@ impl HirDisplay for ApplicationTy {
} 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) => {
let trait_ = match type_alias.lookup(f.db.upcast()).container {
AssocContainerId::TraitId(it) => it,

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));
}