From cbbbd4832582256820695bb73fdb4ca125f1b1f9 Mon Sep 17 00:00:00 2001 From: Timo Freiberg Date: Sun, 10 May 2020 18:09:22 +0200 Subject: [PATCH] Omit default types in HirDisplay SourceCode mode --- .../src/handlers/add_explicit_type.rs | 2 +- crates/ra_hir_ty/src/display.rs | 63 +++++++++++-------- .../src/tests/display_source_code.rs | 27 ++++++++ 3 files changed, 64 insertions(+), 28 deletions(-) diff --git a/crates/ra_assists/src/handlers/add_explicit_type.rs b/crates/ra_assists/src/handlers/add_explicit_type.rs index 146cc75df2a..7ced006266f 100644 --- a/crates/ra_assists/src/handlers/add_explicit_type.rs +++ b/crates/ra_assists/src/handlers/add_explicit_type.rs @@ -209,7 +209,7 @@ struct Test { } fn main() { - let test<|>: Test = Test { t: 23, k: 33 }; + let test<|>: Test = Test { t: 23, k: 33 }; }"#, ); } diff --git a/crates/ra_hir_ty/src/display.rs b/crates/ra_hir_ty/src/display.rs index f5edaea8c80..b9c4d2e8978 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()) + 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) => { 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 ca17486152e..4088b1d22d9 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)); +}