From 9b6ae59b22ae7bae077b1e0606fed2ad239a65e8 Mon Sep 17 00:00:00 2001 From: Graydon Hoare Date: Sun, 19 Jun 2011 22:55:28 -0400 Subject: [PATCH] Fix pp blank-line insertion after isolated comments in cboxes. --- src/comp/pretty/pp.rs | 10 ++++++++++ src/comp/pretty/ppaux.rs | 2 -- src/comp/pretty/pprust.rs | 17 +++++++++++------ 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/comp/pretty/pp.rs b/src/comp/pretty/pp.rs index 61f502ac238..3ae3eafa5b0 100644 --- a/src/comp/pretty/pp.rs +++ b/src/comp/pretty/pp.rs @@ -234,6 +234,11 @@ obj printer(io::writer out, // buffered indentation to avoid writing trailing whitespace mutable int pending_indentation) { + + fn last_token() -> token { + ret token.(right); + } + fn pretty_print(token t) { log #fmt("pp [%u,%u]", left, right); alt (t) { @@ -508,6 +513,11 @@ fn zerobreak(printer p) { spaces(p, 0u); } fn space(printer p) { spaces(p, 1u); } fn hardbreak(printer p) { spaces(p, 0xffffu); } + +fn hardbreak_tok() -> token { + ret BREAK(rec(offset=0, blank_space=0xffff)); +} + // // Local Variables: // mode: rust diff --git a/src/comp/pretty/ppaux.rs b/src/comp/pretty/ppaux.rs index d00b10596a1..37687bc3c2f 100644 --- a/src/comp/pretty/ppaux.rs +++ b/src/comp/pretty/ppaux.rs @@ -236,9 +236,7 @@ fn print_comment(&ps s, lexer::cmnt cmnt) { } case (lexer::isolated) { hardbreak(s.s); - ibox(s, 0u); for (str line in cmnt.lines) { word(s.s, line); hardbreak(s.s); } - end(s); } case (lexer::trailing) { word(s.s, " "); diff --git a/src/comp/pretty/pprust.rs b/src/comp/pretty/pprust.rs index 7117bb7a18c..1f370a73565 100644 --- a/src/comp/pretty/pprust.rs +++ b/src/comp/pretty/pprust.rs @@ -99,13 +99,10 @@ fn pclose(&ps s) { word(s.s, ")"); } fn head(&ps s, str w) { // outer-box is consistent - cbox(s, indent_unit); // head-box is inconsistent - ibox(s, str::char_len(w) + 1u); // keyword that starts the head - word_nbsp(s, w); } @@ -123,6 +120,11 @@ fn bclose(&ps s, common::span span) { } +fn space_if_not_hardbreak(&ps s) { + if (s.s.last_token() != pp::hardbreak_tok()) { + space(s.s); + } +} // Synthesizes a comment that was not textually present in the original source // file. @@ -157,7 +159,7 @@ fn commasep_cmnt[IN](&ps s, breaks b, vec[IN] elts, fn(&ps, &IN) op, word(s.s, ","); maybe_print_trailing_comment(s, get_span(elt), some(get_span(elts.(i)).hi)); - space(s.s); + space_if_not_hardbreak(s); } } end(s); @@ -461,7 +463,10 @@ fn print_stmt(&ps s, &ast::stmt st) { maybe_print_comment(s, st.span.lo); alt (st.node) { case (ast::stmt_decl(?decl, _)) { print_decl(s, decl); } - case (ast::stmt_expr(?expr, _)) { space(s.s); print_expr(s, expr); } + case (ast::stmt_expr(?expr, _)) { + space_if_not_hardbreak(s); + print_expr(s, expr); + } } if (front::parser::stmt_ends_with_semi(st)) { word(s.s, ";"); } maybe_print_trailing_comment(s, st.span, none[uint]); @@ -473,7 +478,7 @@ fn print_block(&ps s, ast::block blk) { for (@ast::stmt st in blk.node.stmts) { print_stmt(s, *st) } alt (blk.node.expr) { case (some(?expr)) { - space(s.s); + space_if_not_hardbreak(s); print_expr(s, expr); maybe_print_trailing_comment(s, expr.span, some(blk.span.hi)); }