Use an explicit flag to decide on whether to add brace compensation

This commit is contained in:
Seiichi Uchida 2017-11-30 06:12:32 +09:00
parent 9ea3e65df6
commit b5e4c99ca7
3 changed files with 17 additions and 11 deletions

View File

@ -131,7 +131,8 @@ fn rewrite_closure_with_block(
rules: ast::BlockCheckMode::Default,
span: body.span,
};
rewrite_closure_block(&block, prefix, context, shape)
let block = ::expr::rewrite_block_with_visitor(context, "", &block, shape, true)?;
Some(format!("{} {}", prefix, block))
}
// Rewrite closure with a single expression without wrapping its body with block.

View File

@ -116,7 +116,7 @@ pub fn format_expr(
rw
} else {
let prefix = block_prefix(context, block, shape)?;
rewrite_block_with_visitor(context, &prefix, block, shape)
rewrite_block_with_visitor(context, &prefix, block, shape, false)
}
}
ExprType::SubExpression => block.rewrite(context, shape),
@ -598,11 +598,12 @@ fn rewrite_single_line_block(
None
}
fn rewrite_block_with_visitor(
pub fn rewrite_block_with_visitor(
context: &RewriteContext,
prefix: &str,
block: &ast::Block,
shape: Shape,
is_dummy: bool,
) -> Option<String> {
if let rw @ Some(_) = rewrite_empty_block(context, block, shape) {
return rw;
@ -620,7 +621,7 @@ fn rewrite_block_with_visitor(
ast::BlockCheckMode::Default => visitor.last_pos = block.span.lo(),
}
visitor.visit_block(block, None);
visitor.visit_block(block, None, is_dummy);
Some(format!("{}{}", prefix, visitor.buffer))
}
@ -634,7 +635,7 @@ impl Rewrite for ast::Block {
let prefix = block_prefix(context, self, shape)?;
let result = rewrite_block_with_visitor(context, &prefix, self, shape);
let result = rewrite_block_with_visitor(context, &prefix, self, shape, false);
if let Some(ref result_str) = result {
if result_str.lines().count() <= 3 {
if let rw @ Some(_) = rewrite_single_line_block(context, &prefix, self, shape) {
@ -1064,7 +1065,8 @@ impl<'a> Rewrite for ControlFlow<'a> {
};
let mut block_context = context.clone();
block_context.is_if_else_block = self.else_block.is_some();
let block_str = rewrite_block_with_visitor(&block_context, "", self.block, block_shape)?;
let block_str =
rewrite_block_with_visitor(&block_context, "", self.block, block_shape, false)?;
let mut result = format!("{}{}", cond_str, block_str);

View File

@ -90,7 +90,12 @@ impl<'a> FmtVisitor<'a> {
}
}
pub fn visit_block(&mut self, b: &ast::Block, inner_attrs: Option<&[ast::Attribute]>) {
pub fn visit_block(
&mut self,
b: &ast::Block,
inner_attrs: Option<&[ast::Attribute]>,
is_dummy: bool,
) {
debug!(
"visit_block: {:?} {:?}",
self.codemap.lookup_char_pos(b.span.lo()),
@ -98,9 +103,7 @@ impl<'a> FmtVisitor<'a> {
);
// Check if this block has braces.
let snippet = self.snippet(b.span);
let has_braces = snippet.starts_with('{') || snippet.starts_with("unsafe");
let brace_compensation = if has_braces { BytePos(1) } else { BytePos(0) };
let brace_compensation = BytePos(if is_dummy { 0 } else { 1 });
self.last_pos = self.last_pos + brace_compensation;
self.block_indent = self.block_indent.block_indent(self.config);
@ -272,7 +275,7 @@ impl<'a> FmtVisitor<'a> {
}
self.last_pos = source!(self, block.span).lo();
self.visit_block(block, inner_attrs)
self.visit_block(block, inner_attrs, false)
}
pub fn visit_item(&mut self, item: &ast::Item) {