Skip to content

Commit e5dc138

Browse files
committed
Buffer pending whitespace in printer so as not to introduce trailing whitespace lines.
1 parent 681f0c7 commit e5dc138

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/comp/pretty/pp.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ fn mk_printer(io::writer out, uint linewidth) -> printer {
131131
true, // scan_stack_empty
132132
0u, // top
133133
0u, // bottom
134-
print_stack);
134+
print_stack,
135+
0);
135136
}
136137

137138
/*
@@ -236,7 +237,10 @@ obj printer(io::writer out,
236237
mutable uint bottom, // index of bottom of scan_stack
237238

238239
// 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
240244
) {
241245

242246

@@ -430,16 +434,13 @@ obj printer(io::writer out,
430434
fn print_newline(int amount) {
431435
log #fmt("NEWLINE %d", amount);
432436
out.write_str("\n");
437+
pending_indentation = 0;
433438
self.indent(amount);
434439
}
435440

436441
fn indent(int amount) {
437442
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;
443444
}
444445

445446
fn top() -> print_stack_elt {
@@ -452,6 +453,14 @@ obj printer(io::writer out,
452453
ret top;
453454
}
454455

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+
455464
fn print(token x, int L) {
456465
log #fmt("print %s %d (remaining line space=%d)",
457466
tok_str(x), L, space);
@@ -515,7 +524,7 @@ obj printer(io::writer out,
515524
assert L == len;
516525
// assert L <= space;
517526
space -= len;
518-
out.write_str(s);
527+
self.write_str(s);
519528
}
520529

521530
case (EOF) {

0 commit comments

Comments
 (0)