Skip to content

Commit 9abe863

Browse files
authored
Merge pull request #1455 from topecongiro/unary-op
Add heuristic choosing block or visual indent for unary op based on span
2 parents b4833a8 + 0614e94 commit 9abe863

File tree

5 files changed

+52
-31
lines changed

5 files changed

+52
-31
lines changed

src/expr.rs

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,11 @@ fn format_expr(expr: &ast::Expr,
157157
};
158158

159159
if let Some(ref expr) = *opt_expr {
160-
rewrite_unary_prefix(context, &format!("break{} ", id_str), &**expr, shape)
160+
rewrite_unary_prefix(context,
161+
&format!("break{} ", id_str),
162+
&**expr,
163+
shape,
164+
expr.span)
161165
} else {
162166
wrap_str(format!("break{}", id_str), context.config.max_width, shape)
163167
}
@@ -178,9 +182,11 @@ fn format_expr(expr: &ast::Expr,
178182
}
179183
ast::ExprKind::Ret(None) => wrap_str("return".to_owned(), context.config.max_width, shape),
180184
ast::ExprKind::Ret(Some(ref expr)) => {
181-
rewrite_unary_prefix(context, "return ", &**expr, shape)
185+
rewrite_unary_prefix(context, "return ", &**expr, shape, expr.span)
186+
}
187+
ast::ExprKind::Box(ref expr) => {
188+
rewrite_unary_prefix(context, "box ", &**expr, shape, expr.span)
182189
}
183-
ast::ExprKind::Box(ref expr) => rewrite_unary_prefix(context, "box ", &**expr, shape),
184190
ast::ExprKind::AddrOf(mutability, ref expr) => {
185191
rewrite_expr_addrof(context, mutability, expr, shape)
186192
}
@@ -222,7 +228,7 @@ fn format_expr(expr: &ast::Expr,
222228
} else {
223229
delim.into()
224230
};
225-
rewrite_unary_prefix(context, &sp_delim, &**rhs, shape)
231+
rewrite_unary_prefix(context, &sp_delim, &**rhs, shape, expr.span)
226232
}
227233
(Some(ref lhs), None) => {
228234
let sp_delim = if context.config.spaces_around_ranges {
@@ -1224,10 +1230,10 @@ fn rewrite_match(context: &RewriteContext,
12241230

12251231
fn arm_start_pos(arm: &ast::Arm) -> BytePos {
12261232
let &ast::Arm {
1227-
ref attrs,
1228-
ref pats,
1229-
..
1230-
} = arm;
1233+
ref attrs,
1234+
ref pats,
1235+
..
1236+
} = arm;
12311237
if !attrs.is_empty() {
12321238
return attrs[0].span.lo;
12331239
}
@@ -1258,11 +1264,11 @@ impl Rewrite for ast::Arm {
12581264
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
12591265
debug!("Arm::rewrite {:?} {:?}", self, shape);
12601266
let &ast::Arm {
1261-
ref attrs,
1262-
ref pats,
1263-
ref guard,
1264-
ref body,
1265-
} = self;
1267+
ref attrs,
1268+
ref pats,
1269+
ref guard,
1270+
ref body,
1271+
} = self;
12661272

12671273
// FIXME this is all a bit grotty, would be nice to abstract out the
12681274
// treatment of attributes.
@@ -1997,9 +2003,22 @@ pub fn rewrite_tuple<'a, I>(context: &RewriteContext,
19972003
pub fn rewrite_unary_prefix<R: Rewrite>(context: &RewriteContext,
19982004
prefix: &str,
19992005
rewrite: &R,
2000-
shape: Shape)
2006+
mut shape: Shape,
2007+
span: Span)
20012008
-> Option<String> {
2002-
let shape = try_opt!(shape.shrink_left(prefix.len())).visual_indent(0);
2009+
// Heuristic: if unary is `&` and `rewrite` contains `{`,
2010+
// it is likely that block indent is preferred to visual indent.
2011+
if prefix == "&" {
2012+
let snippet = String::from(context.snippet(span).trim_left_matches('&'));
2013+
let first_line = try_opt!(snippet.lines().nth(0));
2014+
if first_line.contains("{") {
2015+
shape = try_opt!(shape.sub_width(prefix.len())).block_indent(0);
2016+
} else {
2017+
shape = try_opt!(shape.shrink_left(prefix.len())).visual_indent(0);
2018+
}
2019+
} else {
2020+
shape = try_opt!(shape.shrink_left(prefix.len())).visual_indent(0);
2021+
}
20032022
rewrite
20042023
.rewrite(context, shape)
20052024
.map(|r| format!("{}{}", prefix, r))
@@ -2031,7 +2050,7 @@ fn rewrite_unary_op(context: &RewriteContext,
20312050
ast::UnOp::Not => "!",
20322051
ast::UnOp::Neg => "-",
20332052
};
2034-
rewrite_unary_prefix(context, operator_str, expr, shape)
2053+
rewrite_unary_prefix(context, operator_str, expr, shape, expr.span)
20352054
}
20362055

20372056
fn rewrite_assignment(context: &RewriteContext,
@@ -2126,5 +2145,5 @@ fn rewrite_expr_addrof(context: &RewriteContext,
21262145
ast::Mutability::Immutable => "&",
21272146
ast::Mutability::Mutable => "&mut ",
21282147
};
2129-
rewrite_unary_prefix(context, operator_str, expr, shape)
2148+
rewrite_unary_prefix(context, operator_str, expr, shape, expr.span)
21302149
}

src/imports.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ fn rewrite_view_path_prefix(path: &ast::Path,
133133
let path_str = if path.segments.last().unwrap().identifier.to_string() == "self" &&
134134
path.segments.len() > 1 {
135135
let path = &ast::Path {
136-
span: path.span.clone(),
137-
segments: path.segments[..path.segments.len() - 1].to_owned(),
138-
};
136+
span: path.span.clone(),
137+
segments: path.segments[..path.segments.len() - 1].to_owned(),
138+
};
139139
try_opt!(rewrite_path(context, PathContext::Import, None, path, shape))
140140
} else {
141141
try_opt!(rewrite_path(context, PathContext::Import, None, path, shape))

src/patterns.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ use syntax::codemap::{self, BytePos, Span};
2727
impl Rewrite for Pat {
2828
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
2929
match self.node {
30-
PatKind::Box(ref pat) => rewrite_unary_prefix(context, "box ", &**pat, shape),
30+
PatKind::Box(ref pat) => {
31+
rewrite_unary_prefix(context, "box ", &**pat, shape, self.span)
32+
}
3133
PatKind::Ident(binding_mode, ident, ref sub_pat) => {
3234
let (prefix, mutability) = match binding_mode {
3335
BindingMode::ByRef(mutability) => ("ref ", mutability),
@@ -71,7 +73,7 @@ impl Rewrite for Pat {
7173
}
7274
PatKind::Ref(ref pat, mutability) => {
7375
let prefix = format!("&{}", format_mutability(mutability));
74-
rewrite_unary_prefix(context, &prefix, &**pat, shape)
76+
rewrite_unary_prefix(context, &prefix, &**pat, shape, self.span)
7577
}
7678
PatKind::Tuple(ref items, dotdot_pos) => {
7779
rewrite_tuple_pat(items, dotdot_pos, None, self.span, context, shape)

src/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ impl Rewrite for ast::Ty {
580580
Mutability::Immutable => "*const ",
581581
};
582582

583-
rewrite_unary_prefix(context, prefix, &*mt.ty, shape)
583+
rewrite_unary_prefix(context, prefix, &*mt.ty, shape, self.span)
584584
}
585585
ast::TyKind::Rptr(ref lifetime, ref mt) => {
586586
let mut_str = format_mutability(mt.mutbl);

tests/target/closure.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,16 @@ fn issue863() {
7474

7575
fn issue934() {
7676
let hash: &Fn(&&Block) -> u64 = &|block| -> u64 {
77-
let mut h = SpanlessHash::new(cx);
78-
h.hash_block(block);
79-
h.finish()
80-
};
77+
let mut h = SpanlessHash::new(cx);
78+
h.hash_block(block);
79+
h.finish()
80+
};
8181

8282
let hash: &Fn(&&Block) -> u64 = &|block| -> u64 {
83-
let mut h = SpanlessHash::new(cx);
84-
h.hash_block(block);
85-
h.finish();
86-
};
83+
let mut h = SpanlessHash::new(cx);
84+
h.hash_block(block);
85+
h.finish();
86+
};
8787
}
8888

8989
impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {

0 commit comments

Comments
 (0)