Skip to content

Commit a5f8b37

Browse files
committed
Format match expressions properly when they appear on an overflowing line.
1 parent 97e92b3 commit a5f8b37

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

src/expr.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ impl Rewrite for ast::Block {
250250
}
251251

252252
let mut visitor = FmtVisitor::from_codemap(context.codemap, context.config);
253-
visitor.block_indent = context.block_indent;
253+
visitor.block_indent = context.block_indent + context.overflow_indent;
254254

255255
let prefix = match self.rules {
256256
ast::BlockCheckMode::PushUnsafeBlock(..) |
@@ -541,9 +541,9 @@ fn rewrite_match(context: &RewriteContext,
541541
let cond_str = try_opt!(cond.rewrite(context, cond_budget, offset + 6));
542542
let mut result = format!("match {} {{", cond_str);
543543

544-
let block_indent = context.block_indent;
545544
let nested_context = context.nested_context();
546-
let arm_indent_str = make_indent(nested_context.block_indent);
545+
let arm_indent = nested_context.block_indent + context.overflow_indent;
546+
let arm_indent_str = make_indent(arm_indent);
547547

548548
let open_brace_pos = span_after(mk_sp(cond.span.hi, arm_start_pos(&arms[0])),
549549
"{",
@@ -579,8 +579,8 @@ fn rewrite_match(context: &RewriteContext,
579579

580580
let arm_str = arm.rewrite(&nested_context,
581581
context.config.max_width -
582-
nested_context.block_indent,
583-
nested_context.block_indent);
582+
arm_indent,
583+
arm_indent);
584584
if let Some(ref arm_str) = arm_str {
585585
result.push_str(arm_str);
586586
} else {
@@ -594,7 +594,7 @@ fn rewrite_match(context: &RewriteContext,
594594
// match expression, but meh.
595595

596596
result.push('\n');
597-
result.push_str(&make_indent(block_indent));
597+
result.push_str(&make_indent(context.block_indent + context.overflow_indent));
598598
result.push('}');
599599
Some(result)
600600
}
@@ -1192,7 +1192,7 @@ pub fn rewrite_assign_rhs<S: Into<String>>(context: &RewriteContext,
11921192
result.push_str(&format!("\n{}", make_indent(new_offset)));
11931193

11941194
let max_width = try_opt!(context.config.max_width.checked_sub(new_offset + 1));
1195-
let rhs = try_opt!(ex.rewrite(&context, max_width, new_offset));
1195+
let rhs = try_opt!(ex.rewrite(&context.overflow_context(), max_width, new_offset));
11961196

11971197
result.push_str(&rhs);
11981198
}

src/rewrite.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,14 @@ pub trait Rewrite {
2828
pub struct RewriteContext<'a> {
2929
pub codemap: &'a CodeMap,
3030
pub config: &'a Config,
31+
32+
// Indentation due to nesting of blocks.
3133
pub block_indent: usize,
34+
// *Extra* indentation due to overflowing to the next line, e.g.,
35+
// let foo =
36+
// bar();
37+
// The extra 4 spaces when formatting `bar()` is overflow_indent.
38+
pub overflow_indent: usize,
3239
}
3340

3441
impl<'a> RewriteContext<'a> {
@@ -37,6 +44,16 @@ impl<'a> RewriteContext<'a> {
3744
codemap: self.codemap,
3845
config: self.config,
3946
block_indent: self.block_indent + self.config.tab_spaces,
47+
overflow_indent: self.overflow_indent,
48+
}
49+
}
50+
51+
pub fn overflow_context(&self) -> RewriteContext<'a> {
52+
RewriteContext {
53+
codemap: self.codemap,
54+
config: self.config,
55+
block_indent: self.block_indent,
56+
overflow_indent: self.overflow_indent + self.config.tab_spaces,
4057
}
4158
}
4259

src/visitor.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ impl<'a> FmtVisitor<'a> {
338338
codemap: self.codemap,
339339
config: self.config,
340340
block_indent: self.block_indent,
341+
overflow_indent: 0,
341342
};
342343
// 1 = ";"
343344
match vp.rewrite(&context, self.config.max_width - offset - 1, offset) {
@@ -369,6 +370,7 @@ impl<'a> FmtVisitor<'a> {
369370
codemap: self.codemap,
370371
config: self.config,
371372
block_indent: self.block_indent,
373+
overflow_indent: 0,
372374
}
373375
}
374376
}

tests/target/match.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,12 @@ fn foo() {
5353
Blurb => { }
5454
};
5555
}
56+
57+
// Test that a match on an overflow line is laid out properly.
58+
fn main() {
59+
let sub_span =
60+
match self.span.sub_span_after_keywooooooooooooooooooooord(use_item.span, keywords::As) {
61+
Some(sub_span) => Some(sub_span),
62+
None => sub_span,
63+
};
64+
}

0 commit comments

Comments
 (0)