diff options
author | Timo Freiberg <[email protected]> | 2020-05-10 17:09:22 +0100 |
---|---|---|
committer | Timo Freiberg <[email protected]> | 2020-05-10 17:10:15 +0100 |
commit | cbbbd4832582256820695bb73fdb4ca125f1b1f9 (patch) | |
tree | 1d223a7e9f995faa2188efd02115ec21cbaf3913 | |
parent | 225f353aa26329260b8c7f69305f616a9edaad70 (diff) |
Omit default types in HirDisplay SourceCode mode
-rw-r--r-- | crates/ra_assists/src/handlers/add_explicit_type.rs | 2 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/display.rs | 63 | ||||
-rw-r--r-- | crates/ra_hir_ty/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 146cc75df..7ced00626 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<K, T = u8> { | |||
209 | } | 209 | } |
210 | 210 | ||
211 | fn main() { | 211 | fn main() { |
212 | let test<|>: Test<i32, u8> = Test { t: 23, k: 33 }; | 212 | let test<|>: Test<i32> = Test { t: 23, k: 33 }; |
213 | }"#, | 213 | }"#, |
214 | ); | 214 | ); |
215 | } | 215 | } |
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 | ||
139 | impl DisplayTarget { | ||
140 | fn is_source_code(&self) -> bool { | ||
141 | matches!(self, Self::SourceCode {..}) | ||
142 | } | ||
143 | } | ||
144 | |||
139 | #[derive(Debug)] | 145 | #[derive(Debug)] |
140 | pub enum DisplaySourceCodeError { | 146 | pub 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] | ||
26 | fn 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 | } | ||