From 6d441d32381037401642a204ddb8eccc71f4e4c5 Mon Sep 17 00:00:00 2001 From: Graydon Hoare Date: Thu, 30 Jun 2011 17:25:13 -0700 Subject: [PATCH] Preserve hard \n\n pairs from source when pretty-printing, as explicit formatting control from users. --- src/comp/front/lexer.rs | 24 ++++++++++++++++++++---- src/comp/pretty/ppaux.rs | 5 +++++ src/comp/pretty/pprust.rs | 16 +++++++--------- 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/comp/front/lexer.rs b/src/comp/front/lexer.rs index c688f79d7ca..d6bc3723bf3 100644 --- a/src/comp/front/lexer.rs +++ b/src/comp/front/lexer.rs @@ -536,6 +536,8 @@ fn binop(&reader rdr, token::binop op) -> token::token { mixed; // Code before /* foo */ and after the comment + blank_line; // Just a manual blank linke "\n\n", for layout + } type cmnt = rec(cmnt_style style, vec[str] lines, uint pos); @@ -546,7 +548,6 @@ fn read_to_eol(&reader rdr) -> str { str::push_char(val, rdr.curr()); rdr.bump(); } - if (rdr.curr() == '\n') { rdr.bump(); } else { assert (rdr.is_eof()); } ret val; } @@ -566,6 +567,19 @@ fn consume_non_eol_whitespace(&reader rdr) { } } +fn consume_whitespace_counting_blank_lines(&reader rdr, + &mutable vec[cmnt] comments) { + while (is_whitespace(rdr.curr()) && !rdr.is_eof()) { + if (rdr.curr() == '\n' && rdr.next() == '\n') { + log ">>> blank-line comment"; + let vec[str] v = []; + comments += [rec(style=blank_line, lines=v, + pos=rdr.get_chpos())]; + } + rdr.bump(); + } +} + fn read_line_comments(&reader rdr, bool code_to_the_left) -> cmnt { log ">>> line comments"; auto p = rdr.get_chpos(); @@ -694,19 +708,21 @@ fn gather_comments_and_literals(session sess, str path) -> consume_non_eol_whitespace(rdr); if (rdr.curr() == '\n') { code_to_the_left = false; - consume_whitespace(rdr); + consume_whitespace_counting_blank_lines(rdr, comments); } while (peeking_at_comment(rdr)) { consume_comment(rdr, code_to_the_left, comments); - consume_whitespace(rdr); + consume_whitespace_counting_blank_lines(rdr, comments); } break; } - if (is_lit(next_token(rdr))) { + auto tok = next_token(rdr); + if (is_lit(tok)) { vec::push[lit](literals, rec(lit=rdr.get_mark_str(), pos=rdr.get_mark_chpos())); } + log "tok: " + token::to_str(rdr, tok); first_read = false; } ret rec(cmnts=comments, lits=literals); diff --git a/src/comp/pretty/ppaux.rs b/src/comp/pretty/ppaux.rs index f0ebac66dbd..05cd539e00c 100644 --- a/src/comp/pretty/ppaux.rs +++ b/src/comp/pretty/ppaux.rs @@ -279,6 +279,11 @@ fn print_comment(&ps s, lexer::cmnt cmnt) { end(s); } } + case (lexer::blank_line) { + // We need to do at least one, possibly two hardbreaks. + pprust::hardbreak_if_not_bol(s); + hardbreak(s.s); + } } } diff --git a/src/comp/pretty/pprust.rs b/src/comp/pretty/pprust.rs index 9cc017a221f..ca5b283c38d 100644 --- a/src/comp/pretty/pprust.rs +++ b/src/comp/pretty/pprust.rs @@ -191,9 +191,7 @@ fn print_mod(&ps s, ast::_mod _mod, &vec[ast::attribute] attrs) { print_view_item(s, vitem); } for (@ast::item item in _mod.items) { - // Mod-level item printing we're a little more space-y about. - hardbreak(s.s); - hardbreak(s.s); + hardbreak_if_not_bol(s); print_item(s, item); } print_remaining_comments(s); @@ -271,7 +269,7 @@ fn print_field(&ps s, &ast::ty_field f) { head(s, "obj"); bopen(s); for (ast::ty_method m in methods) { - hardbreak(s.s); + hardbreak_if_not_bol(s); cbox(s, indent_unit); maybe_print_comment(s, m.span.lo); print_ty_fn(s, m.node.proto, some(m.node.ident), @@ -338,7 +336,7 @@ fn print_item(&ps s, &@ast::item item) { word_nbsp(s, item.ident); bopen(s); for (@ast::native_item item in nmod.items) { - hardbreak(s.s); + hardbreak_if_not_bol(s); ibox(s, indent_unit); maybe_print_comment(s, item.span.lo); alt (item.node) { @@ -427,7 +425,7 @@ fn print_field(&ps s, &ast::obj_field field) { bopen(s); for (@ast::method meth in _obj.methods) { let vec[ast::ty_param] typarams = []; - hardbreak(s.s); + hardbreak_if_not_bol(s); maybe_print_comment(s, meth.span.lo); print_fn(s, meth.node.meth.decl, meth.node.meth.proto, meth.node.ident, typarams); @@ -475,7 +473,7 @@ fn print_outer_attributes(&ps s, vec[ast::attribute] attrs) { case (_) {/* fallthrough */ } } } - if (count > 0) { hardbreak(s.s); } + if (count > 0) { hardbreak_if_not_bol(s); } } fn print_inner_attributes(&ps s, vec[ast::attribute] attrs) { @@ -490,11 +488,11 @@ fn print_inner_attributes(&ps s, vec[ast::attribute] attrs) { case (_) { /* fallthrough */ } } } - if (count > 0) { hardbreak(s.s); } + if (count > 0) { hardbreak_if_not_bol(s); } } fn print_attribute(&ps s, &ast::attribute attr) { - hardbreak(s.s); + hardbreak_if_not_bol(s); maybe_print_comment(s, attr.span.lo); word(s.s, "#["); print_meta_item(s, @attr.node.value);