Skip to content

Format loop statements #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use visitor::FmtVisitor;
use utils::*;
use lists::{write_list, ListFormatting, SeparatorTactic, ListTactic};

use syntax::{ast, ptr};
use syntax::codemap::{Pos, Span};
use syntax::{ast, ptr, visit};
use syntax::codemap::{Pos, Span, BytePos};
use syntax::parse::token;
use syntax::print::pprust;

Expand Down Expand Up @@ -250,9 +250,33 @@ impl<'a> FmtVisitor<'a> {
ast::Expr_::ExprTup(ref items) => {
return self.rewrite_tuple_lit(items, width, offset);
}
ast::Expr_::ExprLoop(ref block, _) => {
self.rewrite_loop(block);
return "".to_string();
}
_ => {}
}

self.snippet(expr.span)
}

fn rewrite_loop(&mut self, block: &ast::Block) {
self.changes.push_str_span(block.span, "loop {");

self.last_pos = block.span.lo + BytePos(1);
self.block_indent += config!(tab_spaces);

visit::walk_block(self, block);

self.block_indent -= config!(tab_spaces);

if let Some(stmt) = block.stmts.last() {
self.changes.push_str_span(stmt.span, ";");
self.last_pos = stmt.span.hi;
}

self.format_missing_with_indent(block.span.hi - BytePos(1));

self.changes.push_str_span(block.span, "}");
}
}
15 changes: 15 additions & 0 deletions tests/source/loop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

fn looper() {
loop {}
loop{}
loop {
}
loop
{let x=1;let y=2;}

loop /*comment1*/ {// comment2
/*comment3*/ let x=1;//comment4
let y=2;//comment5
/*comment6*/}//comment7

}
2 changes: 1 addition & 1 deletion tests/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ fn handle_result(result: HashMap<String, String>) {
for (file_name, fmt_text) in result {
// If file is in tests/source, compare to file with same name in tests/target
let target_file_name = get_target(&file_name);
let mut f = fs::File::open(&target_file_name).ok().expect("Couldn't open target.");
let mut f = fs::File::open(&target_file_name).ok().expect(&format!("Couldn't open target: {}", target_file_name));

let mut text = String::new();
// TODO: speedup by running through bytes iterator
Expand Down
19 changes: 19 additions & 0 deletions tests/target/loop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

fn looper() {
loop {
}
loop {
}
loop {
}
loop {
let x=1;
let y=2;
}

loop {// comment2
/*comment3*/ let x=1;//comment4
let y=2;//comment5
/*comment6*/ }//comment7

}