8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
- use syntax:: ast;
11
+ use syntax:: ast:: { self , Mutability } ;
12
12
use syntax:: print:: pprust;
13
13
use syntax:: codemap:: { self , Span , BytePos , CodeMap } ;
14
14
15
15
use Indent ;
16
16
use lists:: { format_item_list, itemize_list, format_fn_args, list_helper, ListTactic } ;
17
17
use rewrite:: { Rewrite , RewriteContext } ;
18
18
use utils:: { extra_offset, span_after, format_mutability, wrap_str} ;
19
+ use expr:: { rewrite_unary_prefix, rewrite_pair} ;
19
20
20
21
impl Rewrite for ast:: Path {
21
22
fn rewrite ( & self , context : & RewriteContext , width : usize , offset : Indent ) -> Option < String > {
@@ -129,21 +130,25 @@ impl<'a> SegmentParam<'a> {
129
130
}
130
131
131
132
impl < ' a > Rewrite for SegmentParam < ' a > {
132
- // FIXME: doesn't always use width, offset.
133
133
fn rewrite ( & self , context : & RewriteContext , width : usize , offset : Indent ) -> Option < String > {
134
- Some ( match * self {
134
+ match * self {
135
135
SegmentParam :: LifeTime ( ref lt) => {
136
- pprust:: lifetime_to_string ( lt)
136
+ wrap_str ( pprust:: lifetime_to_string ( lt) ,
137
+ context. config . max_width ,
138
+ width,
139
+ offset)
137
140
}
138
141
SegmentParam :: Type ( ref ty) => {
139
- try_opt ! ( ty. rewrite( context, width, offset) )
142
+ ty. rewrite ( context, width, offset)
140
143
}
141
144
SegmentParam :: Binding ( ref binding) => {
142
- format ! ( "{} = {}" ,
143
- binding. ident,
144
- try_opt!( binding. ty. rewrite( context, width, offset) ) )
145
+ let mut result = format ! ( "{} = " , binding. ident) ;
146
+ let budget = try_opt ! ( width. checked_sub( result. len( ) ) ) ;
147
+ let rewrite = try_opt ! ( binding. ty. rewrite( context, budget, offset + result. len( ) ) ) ;
148
+ result. push_str ( & rewrite) ;
149
+ Some ( result)
145
150
}
146
- } )
151
+ }
147
152
}
148
153
}
149
154
@@ -163,9 +168,7 @@ fn get_path_separator(codemap: &CodeMap,
163
168
for c in snippet. chars ( ) . rev ( ) {
164
169
if c == ':' {
165
170
return "::" ;
166
- } else if c. is_whitespace ( ) || c == '<' {
167
- continue ;
168
- } else {
171
+ } else if !c. is_whitespace ( ) && c != '<' {
169
172
return "" ;
170
173
}
171
174
}
@@ -271,8 +274,7 @@ fn rewrite_segment(segment: &ast::PathSegment,
271
274
impl Rewrite for ast:: WherePredicate {
272
275
fn rewrite ( & self , context : & RewriteContext , width : usize , offset : Indent ) -> Option < String > {
273
276
// TODO: dead spans?
274
- // TODO: don't assume we'll always fit on one line...
275
- Some ( match * self {
277
+ let result = match * self {
276
278
ast:: WherePredicate :: BoundPredicate ( ast:: WhereBoundPredicate { ref bound_lifetimes,
277
279
ref bounded_ty,
278
280
ref bounds,
@@ -335,23 +337,27 @@ impl Rewrite for ast::WherePredicate {
335
337
let path_str = try_opt ! ( path. rewrite( context, budget, offset + used_width) ) ;
336
338
format ! ( "{} = {}" , path_str, ty_str)
337
339
}
338
- } )
340
+ } ;
341
+
342
+ wrap_str ( result, context. config . max_width , width, offset)
339
343
}
340
344
}
341
345
342
346
impl Rewrite for ast:: LifetimeDef {
343
- fn rewrite ( & self , _ : & RewriteContext , _ : usize , _ : Indent ) -> Option < String > {
344
- if self . bounds . is_empty ( ) {
345
- Some ( pprust:: lifetime_to_string ( & self . lifetime ) )
347
+ fn rewrite ( & self , context : & RewriteContext , width : usize , offset : Indent ) -> Option < String > {
348
+ let result = if self . bounds . is_empty ( ) {
349
+ pprust:: lifetime_to_string ( & self . lifetime )
346
350
} else {
347
- Some ( format ! ( "{}: {}" ,
348
- pprust:: lifetime_to_string( & self . lifetime) ,
349
- self . bounds
350
- . iter( )
351
- . map( pprust:: lifetime_to_string)
352
- . collect:: <Vec <_>>( )
353
- . join( " + " ) ) )
354
- }
351
+ format ! ( "{}: {}" ,
352
+ pprust:: lifetime_to_string( & self . lifetime) ,
353
+ self . bounds
354
+ . iter( )
355
+ . map( pprust:: lifetime_to_string)
356
+ . collect:: <Vec <_>>( )
357
+ . join( " + " ) )
358
+ } ;
359
+
360
+ wrap_str ( result, context. config . max_width , width, offset)
355
361
}
356
362
}
357
363
@@ -366,7 +372,10 @@ impl Rewrite for ast::TyParamBound {
366
372
Some ( format ! ( "?{}" , try_opt!( tref. rewrite( context, budget, offset + 1 ) ) ) )
367
373
}
368
374
ast:: TyParamBound :: RegionTyParamBound ( ref l) => {
369
- Some ( pprust:: lifetime_to_string ( l) )
375
+ wrap_str ( pprust:: lifetime_to_string ( l) ,
376
+ context. config . max_width ,
377
+ width,
378
+ offset)
370
379
}
371
380
}
372
381
}
@@ -377,11 +386,10 @@ impl Rewrite for ast::TyParamBounds {
377
386
let strs: Vec < _ > = try_opt ! ( self . iter( )
378
387
. map( |b| b. rewrite( context, width, offset) )
379
388
. collect( ) ) ;
380
- Some ( strs. join ( " + " ) )
389
+ wrap_str ( strs. join ( " + " ) , context . config . max_width , width , offset )
381
390
}
382
391
}
383
392
384
- // FIXME: this assumes everything will fit on one line
385
393
impl Rewrite for ast:: TyParam {
386
394
fn rewrite ( & self , context : & RewriteContext , width : usize , offset : Indent ) -> Option < String > {
387
395
let mut result = String :: with_capacity ( 128 ) ;
@@ -404,11 +412,10 @@ impl Rewrite for ast::TyParam {
404
412
result. push_str ( & rewrite) ;
405
413
}
406
414
407
- Some ( result)
415
+ wrap_str ( result, context . config . max_width , width , offset )
408
416
}
409
417
}
410
418
411
- // FIXME: this assumes everything will fit on one line
412
419
impl Rewrite for ast:: PolyTraitRef {
413
420
fn rewrite ( & self , context : & RewriteContext , width : usize , offset : Indent ) -> Option < String > {
414
421
if !self . bound_lifetimes . is_empty ( ) {
@@ -432,12 +439,8 @@ impl Rewrite for ast::PolyTraitRef {
432
439
}
433
440
434
441
impl Rewrite for ast:: Ty {
435
- // FIXME doesn't always use width, offset
436
442
fn rewrite ( & self , context : & RewriteContext , width : usize , offset : Indent ) -> Option < String > {
437
443
match self . node {
438
- ast:: TyPath ( None , ref p) => {
439
- p. rewrite ( context, width, offset)
440
- }
441
444
ast:: TyObjectSum ( ref ty, ref bounds) => {
442
445
let ty_str = try_opt ! ( ty. rewrite( context, width, offset) ) ;
443
446
let overhead = ty_str. len ( ) + 3 ;
@@ -447,6 +450,14 @@ impl Rewrite for ast::Ty {
447
450
try_opt!( width. checked_sub( overhead) ) ,
448
451
offset + overhead) ) ) )
449
452
}
453
+ ast:: TyPtr ( ref mt) => {
454
+ let prefix = match mt. mutbl {
455
+ Mutability :: MutMutable => "*mut " ,
456
+ Mutability :: MutImmutable => "*const " ,
457
+ } ;
458
+
459
+ rewrite_unary_prefix ( context, prefix, & * mt. ty , width, offset)
460
+ }
450
461
ast:: TyRptr ( ref lifetime, ref mt) => {
451
462
let mut_str = format_mutability ( mt. mutbl ) ;
452
463
let mut_len = mut_str. len ( ) ;
@@ -470,37 +481,60 @@ impl Rewrite for ast::Ty {
470
481
}
471
482
} )
472
483
}
484
+ // FIXME: we drop any comments here, even though it's a silly place to put
485
+ // comments.
473
486
ast:: TyParen ( ref ty) => {
474
487
let budget = try_opt ! ( width. checked_sub( 2 ) ) ;
475
488
ty. rewrite ( context, budget, offset + 1 ) . map ( |ty_str| format ! ( "({})" , ty_str) )
476
489
}
477
- ast:: TyTup ( ref tup_ret ) => {
490
+ ast:: TyVec ( ref ty ) => {
478
491
let budget = try_opt ! ( width. checked_sub( 2 ) ) ;
492
+ ty. rewrite ( context, budget, offset + 1 ) . map ( |ty_str| format ! ( "[{}]" , ty_str) )
493
+ }
494
+ ast:: TyTup ( ref tup_ret) => {
479
495
if tup_ret. is_empty ( ) {
480
- Some ( "()" . to_string ( ) )
496
+ Some ( "()" . to_owned ( ) )
481
497
} else if let [ ref item] = & * * tup_ret {
498
+ let budget = try_opt ! ( width. checked_sub( 3 ) ) ;
482
499
let inner = try_opt ! ( item. rewrite( context, budget, offset + 1 ) ) ;
483
500
let ret = format ! ( "({},)" , inner) ;
484
501
wrap_str ( ret, context. config . max_width , budget, offset + 1 )
485
502
} else {
503
+ let budget = try_opt ! ( width. checked_sub( 2 ) ) ;
486
504
let items = itemize_list ( context. codemap ,
487
505
tup_ret. iter ( ) ,
488
506
")" ,
489
507
|item| item. span . lo ,
490
508
|item| item. span . hi ,
491
509
|item| item. rewrite ( context, budget, offset + 1 ) ,
492
- tup_ret [ 0 ] . span . lo ,
510
+ span_after ( self . span , "(" , context . codemap ) ,
493
511
self . span . hi ) ;
494
512
495
-
496
513
list_helper ( items, budget, offset + 1 , context. config , ListTactic :: Mixed )
497
514
. map ( |s| format ! ( "({})" , s) )
498
515
}
499
516
}
500
- _ => wrap_str ( pprust:: ty_to_string ( self ) ,
501
- context. config . max_width ,
502
- width,
503
- offset) ,
517
+ ast:: TyPolyTraitRef ( ref trait_ref) => trait_ref. rewrite ( context, width, offset) ,
518
+ ast:: TyPath ( ref q_self, ref path) => {
519
+ rewrite_path ( context, q_self. as_ref ( ) , path, width, offset)
520
+ }
521
+ ast:: TyFixedLengthVec ( ref ty, ref repeats) => {
522
+ rewrite_pair ( & * * ty, & * * repeats, "[" , "; " , "]" , context, width, offset)
523
+ }
524
+ ast:: TyInfer => {
525
+ if width >= 1 {
526
+ Some ( "_" . to_owned ( ) )
527
+ } else {
528
+ None
529
+ }
530
+ }
531
+ ast:: TyBareFn ( ..) => {
532
+ wrap_str ( pprust:: ty_to_string ( self ) ,
533
+ context. config . max_width ,
534
+ width,
535
+ offset)
536
+ }
537
+ ast:: TyMac ( ..) | ast:: TyTypeof ( ..) => unreachable ! ( ) ,
504
538
}
505
539
}
506
540
}
0 commit comments