@@ -25,6 +25,20 @@ pub struct HirFormatter<'a> {
2525pub trait HirDisplay {
2626 fn hir_fmt ( & self , f : & mut HirFormatter ) -> Result < ( ) , HirDisplayError > ;
2727
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+
2842 /// Returns a `Display`able type that is human-readable.
2943 /// Use this for showing types to the user (e.g. diagnostics)
3044 fn display < ' a > ( & ' a self , db : & ' a dyn HirDatabase ) -> HirDisplayWrapper < ' a , Self >
@@ -140,7 +154,7 @@ impl<'a> HirFormatter<'a> {
140154}
141155
142156#[ derive( Clone , Copy ) ]
143- enum DisplayTarget {
157+ pub enum DisplayTarget {
144158 /// Display types for inlays, doc popups, autocompletion, etc...
145159 /// Showing `{unknown}` or not qualifying paths is fine here.
146160 /// There's no reason for this to fail.
@@ -232,32 +246,32 @@ impl HirDisplay for ApplicationTy {
232246 TypeCtor :: Str => write ! ( f, "str" ) ?,
233247 TypeCtor :: Slice => {
234248 let t = self . parameters . as_single ( ) ;
235- write ! ( f, "[{}]" , t. display( f. db) ) ?;
249+ write ! ( f, "[" ) ?;
250+ t. hir_fmt ( f) ?;
251+ write ! ( f, "]" ) ?;
236252 }
237253 TypeCtor :: Array => {
238254 let t = self . parameters . as_single ( ) ;
239- write ! ( f, "[{}; _]" , t. display( f. db) ) ?;
255+ write ! ( f, "[" ) ?;
256+ t. hir_fmt ( f) ?;
257+ write ! ( f, "; _]" ) ?;
240258 }
241259 TypeCtor :: RawPtr ( m) => {
242260 let t = self . parameters . as_single ( ) ;
243- let ty_display = t. display ( f. db ) ;
244261
245262 write ! ( f, "*{}" , m. as_keyword_for_ptr( ) ) ?;
246263 if matches ! ( t, Ty :: Dyn ( predicates) if predicates. len( ) > 1 ) {
247264 write ! ( f, "(" ) ?;
248- write ! ( f , "{}" , ty_display ) ?;
265+ t . hir_fmt ( f ) ?;
249266 write ! ( f, ")" ) ?;
250267 } else {
251- write ! ( f , "{}" , ty_display ) ?;
268+ t . hir_fmt ( f ) ?;
252269 }
253270 }
254271 TypeCtor :: Ref ( m) => {
255272 let t = self . parameters . as_single ( ) ;
256- let ty_display = if f. omit_verbose_types ( ) {
257- t. display_truncated ( f. db , f. max_size )
258- } else {
259- t. display ( f. db )
260- } ;
273+ let ty_display =
274+ t. into_displayable ( f. db , f. max_size , f. omit_verbose_types , f. display_target ) ;
261275
262276 write ! ( f, "&{}" , m. as_keyword_for_ref( ) ) ?;
263277 if matches ! ( t, Ty :: Dyn ( predicates) if predicates. len( ) > 1 ) {
@@ -272,7 +286,9 @@ impl HirDisplay for ApplicationTy {
272286 TypeCtor :: Tuple { .. } => {
273287 let ts = & self . parameters ;
274288 if ts. len ( ) == 1 {
275- write ! ( f, "({},)" , ts[ 0 ] . display( f. db) ) ?;
289+ write ! ( f, "(" ) ?;
290+ ts[ 0 ] . hir_fmt ( f) ?;
291+ write ! ( f, ",)" ) ?;
276292 } else {
277293 write ! ( f, "(" ) ?;
278294 f. write_joined ( & * ts. 0 , ", " ) ?;
@@ -293,11 +309,12 @@ impl HirDisplay for ApplicationTy {
293309 write ! ( f, ")" ) ?;
294310 let ret = sig. ret ( ) ;
295311 if * ret != Ty :: unit ( ) {
296- let ret_display = if f. omit_verbose_types ( ) {
297- ret. display_truncated ( f. db , f. max_size )
298- } else {
299- ret. display ( f. db )
300- } ;
312+ let ret_display = ret. into_displayable (
313+ f. db ,
314+ f. max_size ,
315+ f. omit_verbose_types ,
316+ f. display_target ,
317+ ) ;
301318 write ! ( f, " -> {}" , ret_display) ?;
302319 }
303320 }
@@ -329,15 +346,13 @@ impl HirDisplay for ApplicationTy {
329346 write ! ( f, ")" ) ?;
330347 let ret = sig. ret ( ) ;
331348 if * ret != Ty :: unit ( ) {
332- let ret_display = if f. omit_verbose_types ( ) {
333- ret. display_truncated ( f. db , f. max_size )
334- } else {
335- if f. display_target . is_test ( ) {
336- ret. display_test ( f. db )
337- } else {
338- ret. display ( f. db )
339- }
340- } ;
349+ let ret_display = ret. into_displayable (
350+ f. db ,
351+ f. max_size ,
352+ f. omit_verbose_types ,
353+ f. display_target ,
354+ ) ;
355+
341356 write ! ( f, " -> {}" , ret_display) ?;
342357 }
343358 }
@@ -473,15 +488,12 @@ impl HirDisplay for ApplicationTy {
473488 write ! ( f, "|" ) ?;
474489 } ;
475490
476- let ret_display = if f. omit_verbose_types ( ) {
477- sig. ret ( ) . display_truncated ( f. db , f. max_size )
478- } else {
479- if f. display_target . is_test ( ) {
480- sig. ret ( ) . display_test ( f. db )
481- } else {
482- sig. ret ( ) . display ( f. db )
483- }
484- } ;
491+ let ret_display = sig. ret ( ) . into_displayable (
492+ f. db ,
493+ f. max_size ,
494+ f. omit_verbose_types ,
495+ f. display_target ,
496+ ) ;
485497 write ! ( f, " -> {}" , ret_display) ?;
486498 } else {
487499 write ! ( f, "{{closure}}" ) ?;
@@ -499,7 +511,13 @@ impl HirDisplay for ProjectionTy {
499511 }
500512
501513 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) ?;
503521 if self . parameters . len ( ) > 1 {
504522 write ! ( f, "<" ) ?;
505523 f. write_joined ( & self . parameters [ 1 ..] , ", " ) ?;
@@ -678,10 +696,10 @@ impl HirDisplay for GenericPredicate {
678696 projection_pred. projection_ty . trait_ref ( f. db ) . hir_fmt_ext ( f, true ) ?;
679697 write ! (
680698 f,
681- ">::{} = {} " ,
699+ ">::{} = " ,
682700 f. db. type_alias_data( projection_pred. projection_ty. associated_ty) . name,
683- projection_pred. ty. display( f. db)
684701 ) ?;
702+ projection_pred. ty . hir_fmt ( f) ?;
685703 }
686704 GenericPredicate :: Error => write ! ( f, "{{error}}" ) ?,
687705 }
@@ -692,13 +710,18 @@ impl HirDisplay for GenericPredicate {
692710impl HirDisplay for Obligation {
693711 fn hir_fmt ( & self , f : & mut HirFormatter ) -> Result < ( ) , HirDisplayError > {
694712 match self {
695- Obligation :: Trait ( tr) => write ! ( f, "Implements({})" , tr. display( f. db) ) ,
696- Obligation :: Projection ( proj) => write ! (
697- f,
698- "Normalize({} => {})" ,
699- proj. projection_ty. display( f. db) ,
700- proj. ty. display( f. db)
701- ) ,
713+ Obligation :: Trait ( tr) => {
714+ write ! ( f, "Implements(" ) ?;
715+ tr. hir_fmt ( f) ?;
716+ write ! ( f, ")" )
717+ }
718+ Obligation :: Projection ( proj) => {
719+ write ! ( f, "Normalize(" ) ?;
720+ proj. projection_ty . hir_fmt ( f) ?;
721+ write ! ( f, " => " ) ?;
722+ proj. ty . hir_fmt ( f) ?;
723+ write ! ( f, ")" )
724+ }
702725 }
703726 }
704727}
0 commit comments