diff options
author | Benjamin Coenen <[email protected]> | 2020-10-28 16:58:16 +0000 |
---|---|---|
committer | Benjamin Coenen <[email protected]> | 2020-10-28 16:58:16 +0000 |
commit | 7322a69cf6947d22a07a0b6173dc50a3e4062916 (patch) | |
tree | 7426455f28a70ced3d61a83e18d74e09144c0073 /crates/hir_ty | |
parent | e34183218ceb840c3b20a707ed79e57bdf9f90b7 (diff) |
refactor(hir_ty): do not override DisplayTarget in hir_fmt
Signed-off-by: Benjamin Coenen <[email protected]>
Diffstat (limited to 'crates/hir_ty')
-rw-r--r-- | crates/hir_ty/src/display.rs | 113 |
1 files changed, 68 insertions, 45 deletions
diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index 822ef4477..14e8c0633 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs | |||
@@ -26,6 +26,20 @@ pub trait HirDisplay { | |||
26 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError>; | 26 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError>; |
27 | 27 | ||
28 | /// Returns a `Display`able type that is human-readable. | 28 | /// Returns a `Display`able type that is human-readable. |
29 | fn into_displayable<'a>( | ||
30 | &'a self, | ||
31 | db: &'a dyn HirDatabase, | ||
32 | max_size: Option<usize>, | ||
33 | omit_verbose_types: bool, | ||
34 | display_target: DisplayTarget, | ||
35 | ) -> HirDisplayWrapper<'a, Self> | ||
36 | where | ||
37 | Self: Sized, | ||
38 | { | ||
39 | HirDisplayWrapper { db, t: self, max_size, omit_verbose_types, display_target } | ||
40 | } | ||
41 | |||
42 | /// Returns a `Display`able type that is human-readable. | ||
29 | /// Use this for showing types to the user (e.g. diagnostics) | 43 | /// Use this for showing types to the user (e.g. diagnostics) |
30 | fn display<'a>(&'a self, db: &'a dyn HirDatabase) -> HirDisplayWrapper<'a, Self> | 44 | fn display<'a>(&'a self, db: &'a dyn HirDatabase) -> HirDisplayWrapper<'a, Self> |
31 | where | 45 | where |
@@ -140,7 +154,7 @@ impl<'a> HirFormatter<'a> { | |||
140 | } | 154 | } |
141 | 155 | ||
142 | #[derive(Clone, Copy)] | 156 | #[derive(Clone, Copy)] |
143 | enum DisplayTarget { | 157 | pub enum DisplayTarget { |
144 | /// Display types for inlays, doc popups, autocompletion, etc... | 158 | /// Display types for inlays, doc popups, autocompletion, etc... |
145 | /// Showing `{unknown}` or not qualifying paths is fine here. | 159 | /// Showing `{unknown}` or not qualifying paths is fine here. |
146 | /// There's no reason for this to fail. | 160 | /// There's no reason for this to fail. |
@@ -232,32 +246,32 @@ impl HirDisplay for ApplicationTy { | |||
232 | TypeCtor::Str => write!(f, "str")?, | 246 | TypeCtor::Str => write!(f, "str")?, |
233 | TypeCtor::Slice => { | 247 | TypeCtor::Slice => { |
234 | let t = self.parameters.as_single(); | 248 | let t = self.parameters.as_single(); |
235 | write!(f, "[{}]", t.display(f.db))?; | 249 | write!(f, "[")?; |
250 | t.hir_fmt(f)?; | ||
251 | write!(f, "]")?; | ||
236 | } | 252 | } |
237 | TypeCtor::Array => { | 253 | TypeCtor::Array => { |
238 | let t = self.parameters.as_single(); | 254 | let t = self.parameters.as_single(); |
239 | write!(f, "[{}; _]", t.display(f.db))?; | 255 | write!(f, "[")?; |
256 | t.hir_fmt(f)?; | ||
257 | write!(f, "; _]")?; | ||
240 | } | 258 | } |
241 | TypeCtor::RawPtr(m) => { | 259 | TypeCtor::RawPtr(m) => { |
242 | let t = self.parameters.as_single(); | 260 | let t = self.parameters.as_single(); |
243 | let ty_display = t.display(f.db); | ||
244 | 261 | ||
245 | write!(f, "*{}", m.as_keyword_for_ptr())?; | 262 | write!(f, "*{}", m.as_keyword_for_ptr())?; |
246 | if matches!(t, Ty::Dyn(predicates) if predicates.len() > 1) { | 263 | if matches!(t, Ty::Dyn(predicates) if predicates.len() > 1) { |
247 | write!(f, "(")?; | 264 | write!(f, "(")?; |
248 | write!(f, "{}", ty_display)?; | 265 | t.hir_fmt(f)?; |
249 | write!(f, ")")?; | 266 | write!(f, ")")?; |
250 | } else { | 267 | } else { |
251 | write!(f, "{}", ty_display)?; | 268 | t.hir_fmt(f)?; |
252 | } | 269 | } |
253 | } | 270 | } |
254 | TypeCtor::Ref(m) => { | 271 | TypeCtor::Ref(m) => { |
255 | let t = self.parameters.as_single(); | 272 | let t = self.parameters.as_single(); |
256 | let ty_display = if f.omit_verbose_types() { | 273 | let ty_display = |
257 | t.display_truncated(f.db, f.max_size) | 274 | t.into_displayable(f.db, f.max_size, f.omit_verbose_types, f.display_target); |
258 | } else { | ||
259 | t.display(f.db) | ||
260 | }; | ||
261 | 275 | ||
262 | write!(f, "&{}", m.as_keyword_for_ref())?; | 276 | write!(f, "&{}", m.as_keyword_for_ref())?; |
263 | if matches!(t, Ty::Dyn(predicates) if predicates.len() > 1) { | 277 | if matches!(t, Ty::Dyn(predicates) if predicates.len() > 1) { |
@@ -272,7 +286,9 @@ impl HirDisplay for ApplicationTy { | |||
272 | TypeCtor::Tuple { .. } => { | 286 | TypeCtor::Tuple { .. } => { |
273 | let ts = &self.parameters; | 287 | let ts = &self.parameters; |
274 | if ts.len() == 1 { | 288 | if ts.len() == 1 { |
275 | write!(f, "({},)", ts[0].display(f.db))?; | 289 | write!(f, "(")?; |
290 | ts[0].hir_fmt(f)?; | ||
291 | write!(f, ",)")?; | ||
276 | } else { | 292 | } else { |
277 | write!(f, "(")?; | 293 | write!(f, "(")?; |
278 | f.write_joined(&*ts.0, ", ")?; | 294 | f.write_joined(&*ts.0, ", ")?; |
@@ -293,11 +309,12 @@ impl HirDisplay for ApplicationTy { | |||
293 | write!(f, ")")?; | 309 | write!(f, ")")?; |
294 | let ret = sig.ret(); | 310 | let ret = sig.ret(); |
295 | if *ret != Ty::unit() { | 311 | if *ret != Ty::unit() { |
296 | let ret_display = if f.omit_verbose_types() { | 312 | let ret_display = ret.into_displayable( |
297 | ret.display_truncated(f.db, f.max_size) | 313 | f.db, |
298 | } else { | 314 | f.max_size, |
299 | ret.display(f.db) | 315 | f.omit_verbose_types, |
300 | }; | 316 | f.display_target, |
317 | ); | ||
301 | write!(f, " -> {}", ret_display)?; | 318 | write!(f, " -> {}", ret_display)?; |
302 | } | 319 | } |
303 | } | 320 | } |
@@ -329,15 +346,13 @@ impl HirDisplay for ApplicationTy { | |||
329 | write!(f, ")")?; | 346 | write!(f, ")")?; |
330 | let ret = sig.ret(); | 347 | let ret = sig.ret(); |
331 | if *ret != Ty::unit() { | 348 | if *ret != Ty::unit() { |
332 | let ret_display = if f.omit_verbose_types() { | 349 | let ret_display = ret.into_displayable( |
333 | ret.display_truncated(f.db, f.max_size) | 350 | f.db, |
334 | } else { | 351 | f.max_size, |
335 | if f.display_target.is_test() { | 352 | f.omit_verbose_types, |
336 | ret.display_test(f.db) | 353 | f.display_target, |
337 | } else { | 354 | ); |
338 | ret.display(f.db) | 355 | |
339 | } | ||
340 | }; | ||
341 | write!(f, " -> {}", ret_display)?; | 356 | write!(f, " -> {}", ret_display)?; |
342 | } | 357 | } |
343 | } | 358 | } |
@@ -473,15 +488,12 @@ impl HirDisplay for ApplicationTy { | |||
473 | write!(f, "|")?; | 488 | write!(f, "|")?; |
474 | }; | 489 | }; |
475 | 490 | ||
476 | let ret_display = if f.omit_verbose_types() { | 491 | let ret_display = sig.ret().into_displayable( |
477 | sig.ret().display_truncated(f.db, f.max_size) | 492 | f.db, |
478 | } else { | 493 | f.max_size, |
479 | if f.display_target.is_test() { | 494 | f.omit_verbose_types, |
480 | sig.ret().display_test(f.db) | 495 | f.display_target, |
481 | } else { | 496 | ); |
482 | sig.ret().display(f.db) | ||
483 | } | ||
484 | }; | ||
485 | write!(f, " -> {}", ret_display)?; | 497 | write!(f, " -> {}", ret_display)?; |
486 | } else { | 498 | } else { |
487 | write!(f, "{{closure}}")?; | 499 | write!(f, "{{closure}}")?; |
@@ -499,7 +511,13 @@ impl HirDisplay for ProjectionTy { | |||
499 | } | 511 | } |
500 | 512 | ||
501 | let trait_ = f.db.trait_data(self.trait_(f.db)); | 513 | let trait_ = f.db.trait_data(self.trait_(f.db)); |
502 | write!(f, "<{} as {}", self.parameters[0].display(f.db), trait_.name)?; | 514 | let first_parameter = self.parameters[0].into_displayable( |
515 | f.db, | ||
516 | f.max_size, | ||
517 | f.omit_verbose_types, | ||
518 | f.display_target, | ||
519 | ); | ||
520 | write!(f, "<{} as {}", first_parameter, trait_.name)?; | ||
503 | if self.parameters.len() > 1 { | 521 | if self.parameters.len() > 1 { |
504 | write!(f, "<")?; | 522 | write!(f, "<")?; |
505 | f.write_joined(&self.parameters[1..], ", ")?; | 523 | f.write_joined(&self.parameters[1..], ", ")?; |
@@ -678,10 +696,10 @@ impl HirDisplay for GenericPredicate { | |||
678 | projection_pred.projection_ty.trait_ref(f.db).hir_fmt_ext(f, true)?; | 696 | projection_pred.projection_ty.trait_ref(f.db).hir_fmt_ext(f, true)?; |
679 | write!( | 697 | write!( |
680 | f, | 698 | f, |
681 | ">::{} = {}", | 699 | ">::{} = ", |
682 | f.db.type_alias_data(projection_pred.projection_ty.associated_ty).name, | 700 | f.db.type_alias_data(projection_pred.projection_ty.associated_ty).name, |
683 | projection_pred.ty.display(f.db) | ||
684 | )?; | 701 | )?; |
702 | projection_pred.ty.hir_fmt(f)?; | ||
685 | } | 703 | } |
686 | GenericPredicate::Error => write!(f, "{{error}}")?, | 704 | GenericPredicate::Error => write!(f, "{{error}}")?, |
687 | } | 705 | } |
@@ -692,13 +710,18 @@ impl HirDisplay for GenericPredicate { | |||
692 | impl HirDisplay for Obligation { | 710 | impl HirDisplay for Obligation { |
693 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | 711 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { |
694 | match self { | 712 | match self { |
695 | Obligation::Trait(tr) => write!(f, "Implements({})", tr.display(f.db)), | 713 | Obligation::Trait(tr) => { |
696 | Obligation::Projection(proj) => write!( | 714 | write!(f, "Implements(")?; |
697 | f, | 715 | tr.hir_fmt(f)?; |
698 | "Normalize({} => {})", | 716 | write!(f, ")") |
699 | proj.projection_ty.display(f.db), | 717 | } |
700 | proj.ty.display(f.db) | 718 | Obligation::Projection(proj) => { |
701 | ), | 719 | write!(f, "Normalize(")?; |
720 | proj.projection_ty.hir_fmt(f)?; | ||
721 | write!(f, " => ")?; | ||
722 | proj.ty.hir_fmt(f)?; | ||
723 | write!(f, ")") | ||
724 | } | ||
702 | } | 725 | } |
703 | } | 726 | } |
704 | } | 727 | } |