@@ -131,7 +131,8 @@ fn mk_printer(io::writer out, uint linewidth) -> printer {
131
131
true , // scan_stack_empty
132
132
0 u, // top
133
133
0 u, // bottom
134
- print_stack) ;
134
+ print_stack,
135
+ 0 ) ;
135
136
}
136
137
137
138
/*
@@ -236,7 +237,10 @@ obj printer(io::writer out,
236
237
mutable uint bottom, // index of bottom of scan_stack
237
238
238
239
// stack of blocks-in-progress being flushed by print
239
- mutable vec[ print_stack_elt] print_stack
240
+ mutable vec[ print_stack_elt] print_stack,
241
+
242
+ // buffered indentation to avoid writing trailing whitespace
243
+ mutable int pending_indentation
240
244
) {
241
245
242
246
@@ -430,16 +434,13 @@ obj printer(io::writer out,
430
434
fn print_newline ( int amount ) {
431
435
log #fmt( "NEWLINE %d" , amount) ;
432
436
out. write_str ( "\n " ) ;
437
+ pending_indentation = 0 ;
433
438
self . indent ( amount) ;
434
439
}
435
440
436
441
fn indent ( int amount ) {
437
442
log #fmt( "INDENT %d" , amount) ;
438
- auto u = 0 ;
439
- while ( u < amount) {
440
- out. write_str ( " " ) ;
441
- u += 1 ;
442
- }
443
+ pending_indentation += amount;
443
444
}
444
445
445
446
fn top ( ) -> print_stack_elt {
@@ -452,6 +453,14 @@ obj printer(io::writer out,
452
453
ret top;
453
454
}
454
455
456
+ fn write_str ( str s) {
457
+ while ( pending_indentation > 0 ) {
458
+ out. write_str ( " " ) ;
459
+ pending_indentation -= 1 ;
460
+ }
461
+ out. write_str ( s) ;
462
+ }
463
+
455
464
fn print ( token x, int L ) {
456
465
log #fmt( "print %s %d (remaining line space=%d)" ,
457
466
tok_str ( x) , L , space) ;
@@ -515,7 +524,7 @@ obj printer(io::writer out,
515
524
assert L == len;
516
525
// assert L <= space;
517
526
space -= len;
518
- out . write_str ( s) ;
527
+ self . write_str ( s) ;
519
528
}
520
529
521
530
case ( EOF ) {
0 commit comments