From cbbbd4832582256820695bb73fdb4ca125f1b1f9 Mon Sep 17 00:00:00 2001 From: Timo Freiberg Date: Sun, 10 May 2020 18:09:22 +0200 Subject: Omit default types in HirDisplay SourceCode mode --- crates/ra_hir_ty/src/display.rs | 63 +++++++++++++---------- crates/ra_hir_ty/src/tests/display_source_code.rs | 27 ++++++++++ 2 files changed, 63 insertions(+), 27 deletions(-) (limited to 'crates/ra_hir_ty/src') diff --git a/crates/ra_hir_ty/src/display.rs b/crates/ra_hir_ty/src/display.rs index f5edaea8c..b9c4d2e89 100644 --- a/crates/ra_hir_ty/src/display.rs +++ b/crates/ra_hir_ty/src/display.rs @@ -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()) - } - (_, Some(default_parameter)) - if parameter != default_parameter => - { - 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()) + } + _ => (), } - _ => (), } + &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) => { diff --git a/crates/ra_hir_ty/src/tests/display_source_code.rs b/crates/ra_hir_ty/src/tests/display_source_code.rs index ca1748615..4088b1d22 100644 --- a/crates/ra_hir_ty/src/tests/display_source_code.rs +++ b/crates/ra_hir_ty/src/tests/display_source_code.rs @@ -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: 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: K, t: T } + fn main() { + let foo = Foo { k: 400, t: 5 }; + foo<|>; + } + ", + ); + assert_eq!("Foo", displayed_source_at_pos(&db, pos)); +} -- cgit v1.2.3