Preserve hard \n\n pairs from source when pretty-printing, as explicit formatting control from users.

This commit is contained in:
Graydon Hoare 2011-06-30 17:25:13 -07:00
parent dc9f285b06
commit 6d441d3238
3 changed files with 32 additions and 13 deletions

View File

@ -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);

View File

@ -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);
}
}
}

View File

@ -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);