Skip to content

Commit 9b6ae59

Browse files
committed
Fix pp blank-line insertion after isolated comments in cboxes.
1 parent 3f7380c commit 9b6ae59

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

src/comp/pretty/pp.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,11 @@ obj printer(io::writer out,
234234

235235
// buffered indentation to avoid writing trailing whitespace
236236
mutable int pending_indentation) {
237+
238+
fn last_token() -> token {
239+
ret token.(right);
240+
}
241+
237242
fn pretty_print(token t) {
238243
log #fmt("pp [%u,%u]", left, right);
239244
alt (t) {
@@ -508,6 +513,11 @@ fn zerobreak(printer p) { spaces(p, 0u); }
508513
fn space(printer p) { spaces(p, 1u); }
509514

510515
fn hardbreak(printer p) { spaces(p, 0xffffu); }
516+
517+
fn hardbreak_tok() -> token {
518+
ret BREAK(rec(offset=0, blank_space=0xffff));
519+
}
520+
511521
//
512522
// Local Variables:
513523
// mode: rust

src/comp/pretty/ppaux.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,7 @@ fn print_comment(&ps s, lexer::cmnt cmnt) {
236236
}
237237
case (lexer::isolated) {
238238
hardbreak(s.s);
239-
ibox(s, 0u);
240239
for (str line in cmnt.lines) { word(s.s, line); hardbreak(s.s); }
241-
end(s);
242240
}
243241
case (lexer::trailing) {
244242
word(s.s, " ");

src/comp/pretty/pprust.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,10 @@ fn pclose(&ps s) { word(s.s, ")"); }
9999

100100
fn head(&ps s, str w) {
101101
// outer-box is consistent
102-
103102
cbox(s, indent_unit);
104103
// head-box is inconsistent
105-
106104
ibox(s, str::char_len(w) + 1u);
107105
// keyword that starts the head
108-
109106
word_nbsp(s, w);
110107
}
111108

@@ -123,6 +120,11 @@ fn bclose(&ps s, common::span span) {
123120

124121
}
125122

123+
fn space_if_not_hardbreak(&ps s) {
124+
if (s.s.last_token() != pp::hardbreak_tok()) {
125+
space(s.s);
126+
}
127+
}
126128

127129
// Synthesizes a comment that was not textually present in the original source
128130
// file.
@@ -157,7 +159,7 @@ fn commasep_cmnt[IN](&ps s, breaks b, vec[IN] elts, fn(&ps, &IN) op,
157159
word(s.s, ",");
158160
maybe_print_trailing_comment(s, get_span(elt),
159161
some(get_span(elts.(i)).hi));
160-
space(s.s);
162+
space_if_not_hardbreak(s);
161163
}
162164
}
163165
end(s);
@@ -461,7 +463,10 @@ fn print_stmt(&ps s, &ast::stmt st) {
461463
maybe_print_comment(s, st.span.lo);
462464
alt (st.node) {
463465
case (ast::stmt_decl(?decl, _)) { print_decl(s, decl); }
464-
case (ast::stmt_expr(?expr, _)) { space(s.s); print_expr(s, expr); }
466+
case (ast::stmt_expr(?expr, _)) {
467+
space_if_not_hardbreak(s);
468+
print_expr(s, expr);
469+
}
465470
}
466471
if (front::parser::stmt_ends_with_semi(st)) { word(s.s, ";"); }
467472
maybe_print_trailing_comment(s, st.span, none[uint]);
@@ -473,7 +478,7 @@ fn print_block(&ps s, ast::block blk) {
473478
for (@ast::stmt st in blk.node.stmts) { print_stmt(s, *st) }
474479
alt (blk.node.expr) {
475480
case (some(?expr)) {
476-
space(s.s);
481+
space_if_not_hardbreak(s);
477482
print_expr(s, expr);
478483
maybe_print_trailing_comment(s, expr.span, some(blk.span.hi));
479484
}

0 commit comments

Comments
 (0)