aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_ty/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_ty/src')
-rw-r--r--crates/ra_hir_ty/src/display.rs63
-rw-r--r--crates/ra_hir_ty/src/tests/display_source_code.rs27
2 files changed, 63 insertions, 27 deletions
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 {
136 SourceCode { module_id: ModuleId }, 136 SourceCode { module_id: ModuleId },
137} 137}
138 138
139impl DisplayTarget {
140 fn is_source_code(&self) -> bool {
141 matches!(self, Self::SourceCode {..})
142 }
143}
144
139#[derive(Debug)] 145#[derive(Debug)]
140pub enum DisplaySourceCodeError { 146pub enum DisplaySourceCodeError {
141 PathNotFound, 147 PathNotFound,
@@ -303,37 +309,40 @@ impl HirDisplay for ApplicationTy {
303 309
304 if self.parameters.len() > 0 { 310 if self.parameters.len() > 0 {
305 let mut non_default_parameters = Vec::with_capacity(self.parameters.len()); 311 let mut non_default_parameters = Vec::with_capacity(self.parameters.len());
306 let parameters_to_write = if f.omit_verbose_types() { 312 let parameters_to_write =
307 match self 313 if f.display_target.is_source_code() || f.omit_verbose_types() {
308 .ctor 314 match self
309 .as_generic_def() 315 .ctor
310 .map(|generic_def_id| f.db.generic_defaults(generic_def_id)) 316 .as_generic_def()
311 .filter(|defaults| !defaults.is_empty()) 317 .map(|generic_def_id| f.db.generic_defaults(generic_def_id))
312 { 318 .filter(|defaults| !defaults.is_empty())
313 None => self.parameters.0.as_ref(), 319 {
314 Some(default_parameters) => { 320 None => self.parameters.0.as_ref(),
315 for (i, parameter) in self.parameters.iter().enumerate() { 321 Some(default_parameters) => {
316 match (parameter, default_parameters.get(i)) { 322 for (i, parameter) in self.parameters.iter().enumerate() {
317 (&Ty::Unknown, _) | (_, None) => { 323 match (parameter, default_parameters.get(i)) {
318 non_default_parameters.push(parameter.clone()) 324 (&Ty::Unknown, _) | (_, None) => {
319 } 325 non_default_parameters.push(parameter.clone())
320 (_, Some(default_parameter)) 326 }
321 if parameter != default_parameter => 327 (_, Some(default_parameter))
322 { 328 if parameter != default_parameter =>
323 non_default_parameters.push(parameter.clone()) 329 {
330 non_default_parameters.push(parameter.clone())
331 }
332 _ => (),
324 } 333 }
325 _ => (),
326 } 334 }
335 &non_default_parameters
327 } 336 }
328 &non_default_parameters
329 } 337 }
330 } 338 } else {
331 } else { 339 self.parameters.0.as_ref()
332 self.parameters.0.as_ref() 340 };
333 }; 341 if !parameters_to_write.is_empty() {
334 write!(f, "<")?; 342 write!(f, "<")?;
335 f.write_joined(parameters_to_write, ", ")?; 343 f.write_joined(parameters_to_write, ", ")?;
336 write!(f, ">")?; 344 write!(f, ">")?;
345 }
337 } 346 }
338 } 347 }
339 TypeCtor::AssociatedType(type_alias) => { 348 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() {
21 ); 21 );
22 assert_eq!("foo::Foo", displayed_source_at_pos(&db, pos)); 22 assert_eq!("foo::Foo", displayed_source_at_pos(&db, pos));
23} 23}
24
25#[test]
26fn omit_default_type_parameters() {
27 let (db, pos) = TestDB::with_position(
28 r"
29 //- /main.rs
30 struct Foo<T = u8> { t: T }
31 fn main() {
32 let foo = Foo { t: 5 };
33 foo<|>;
34 }
35 ",
36 );
37 assert_eq!("Foo", displayed_source_at_pos(&db, pos));
38
39 let (db, pos) = TestDB::with_position(
40 r"
41 //- /main.rs
42 struct Foo<K, T = u8> { k: K, t: T }
43 fn main() {
44 let foo = Foo { k: 400, t: 5 };
45 foo<|>;
46 }
47 ",
48 );
49 assert_eq!("Foo<i32>", displayed_source_at_pos(&db, pos));
50}