1
Fork 0

Refactor closure formatting routine

This commit is contained in:
Marcus Klaas 2015-08-20 23:05:41 +02:00
parent a75017e50e
commit c8fd23ca68
4 changed files with 34 additions and 25 deletions

View file

@ -68,9 +68,18 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
self.codemap.lookup_char_pos(b.span.lo),
self.codemap.lookup_char_pos(b.span.hi));
self.buffer.push_str("{");
self.last_pos = self.last_pos + BytePos(1);
// Check if this block has braces.
let snippet = self.snippet(b.span);
let has_braces = snippet.chars().next().unwrap() == '{' || &snippet[..6] == "unsafe";
let brace_compensation = if has_braces {
BytePos(1)
} else {
BytePos(0)
};
self.last_pos = self.last_pos + brace_compensation;
self.block_indent += self.config.tab_spaces;
self.buffer.push_str("{");
for stmt in &b.stmts {
self.visit_stmt(&stmt)
@ -86,7 +95,7 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
self.block_indent -= self.config.tab_spaces;
// TODO we should compress any newlines here to just one
self.format_missing_with_indent(b.span.hi - BytePos(1));
self.format_missing_with_indent(b.span.hi - brace_compensation);
self.buffer.push_str("}");
self.last_pos = b.span.hi;
}