Add support for formatting AddrOf (unary "&").

This commit is contained in:
Eli Friedman 2015-09-24 01:13:57 -07:00
parent d941fe20b4
commit 19e887c309
5 changed files with 36 additions and 12 deletions

View File

@ -150,10 +150,7 @@ fn rewrite_chain_expr(expr: &ast::Expr,
-> Option<String> { -> Option<String> {
match expr.node { match expr.node {
ast::Expr_::ExprMethodCall(ref method_name, ref types, ref expressions) => { ast::Expr_::ExprMethodCall(ref method_name, ref types, ref expressions) => {
let inner = &RewriteContext { let inner = &RewriteContext { block_indent: offset, ..*context };
block_indent: offset,
..*context
};
rewrite_method_call(method_name.node, types, expressions, span, inner, width, offset) rewrite_method_call(method_name.node, types, expressions, span, inner, width, offset)
} }
ast::Expr_::ExprField(_, ref field) => { ast::Expr_::ExprField(_, ref field) => {

View File

@ -163,12 +163,14 @@ impl Rewrite for ast::Expr {
ast::Expr_::ExprRet(Some(ref expr)) => { ast::Expr_::ExprRet(Some(ref expr)) => {
rewrite_unary_prefix(context, "return ", &expr, width, offset) rewrite_unary_prefix(context, "return ", &expr, width, offset)
} }
ast::Expr_::ExprAddrOf(mutability, ref expr) => {
rewrite_expr_addrof(context, mutability, &expr, width, offset)
}
// We do not format these expressions yet, but they should still // We do not format these expressions yet, but they should still
// satisfy our width restrictions. // satisfy our width restrictions.
ast::Expr_::ExprBox(..) | ast::Expr_::ExprBox(..) |
ast::Expr_::ExprCast(..) | ast::Expr_::ExprCast(..) |
ast::Expr_::ExprIndex(..) | ast::Expr_::ExprIndex(..) |
ast::Expr_::ExprAddrOf(..) |
ast::Expr_::ExprInlineAsm(..) | ast::Expr_::ExprInlineAsm(..) |
ast::Expr_::ExprRepeat(..) => { ast::Expr_::ExprRepeat(..) => {
wrap_str(context.snippet(self.span), context.config.max_width, width, offset) wrap_str(context.snippet(self.span), context.config.max_width, width, offset)
@ -684,11 +686,7 @@ fn rewrite_match_arm_comment(context: &RewriteContext,
if !missed_str.is_empty() { if !missed_str.is_empty() {
result.push('\n'); result.push('\n');
result.push_str(arm_indent_str); result.push_str(arm_indent_str);
result.push_str(&rewrite_comment(&missed_str, result.push_str(&rewrite_comment(&missed_str, false, width, arm_indent, context.config));
false,
width,
arm_indent,
context.config));
} }
return result; return result;
} }
@ -746,7 +744,8 @@ fn rewrite_match(context: &RewriteContext,
let last_comment = context.snippet(mk_sp(arm_end_pos(&arms[arms.len() - 1]), span.hi)); let last_comment = context.snippet(mk_sp(arm_end_pos(&arms[arms.len() - 1]), span.hi));
result.push_str(&rewrite_match_arm_comment(context, result.push_str(&rewrite_match_arm_comment(context,
&last_comment, &last_comment,
width, arm_indent, width,
arm_indent,
&arm_indent_str)); &arm_indent_str));
result.push('\n'); result.push('\n');
result.push_str(&(context.block_indent + context.overflow_indent).to_string(context.config)); result.push_str(&(context.block_indent + context.overflow_indent).to_string(context.config));
@ -1414,3 +1413,16 @@ pub fn rewrite_assign_rhs<S: Into<String>>(context: &RewriteContext,
Some(result) Some(result)
} }
fn rewrite_expr_addrof(context: &RewriteContext,
mutability: ast::Mutability,
expr: &ast::Expr,
width: usize,
offset: Indent)
-> Option<String> {
let operator_str = match mutability {
ast::Mutability::MutImmutable => "&",
ast::Mutability::MutMutable => "&mut ",
};
rewrite_unary_prefix(context, operator_str, expr, width, offset)
}

View File

@ -200,7 +200,10 @@ pub fn write_list<'b>(items: &[ListItem], formatting: &ListFormatting<'b>) -> Op
let block_mode = tactic != ListTactic::Vertical; let block_mode = tactic != ListTactic::Vertical;
// Width restriction is only relevant in vertical mode. // Width restriction is only relevant in vertical mode.
let max_width = formatting.v_width; let max_width = formatting.v_width;
result.push_str(&rewrite_comment(comment, block_mode, max_width, formatting.indent, result.push_str(&rewrite_comment(comment,
block_mode,
max_width,
formatting.indent,
formatting.config)); formatting.config));
if tactic == ListTactic::Vertical { if tactic == ListTactic::Vertical {

View File

@ -298,3 +298,8 @@ fn returns() {
return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;
} }
fn addrof() {
& mut(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);
& (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);
}

View File

@ -306,3 +306,10 @@ fn returns() {
return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;
} }
fn addrof() {
&mut (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);
&(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);
}