From a5f8b37eeb3e388500e736579ce11533c254fd16 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Wed, 2 Sep 2015 14:29:47 +1200 Subject: [PATCH] Format match expressions properly when they appear on an overflowing line. --- src/expr.rs | 14 +++++++------- src/rewrite.rs | 17 +++++++++++++++++ src/visitor.rs | 2 ++ tests/target/match.rs | 9 +++++++++ 4 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index 19b3693d94b..f22dd04358f 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -250,7 +250,7 @@ impl Rewrite for ast::Block { } let mut visitor = FmtVisitor::from_codemap(context.codemap, context.config); - visitor.block_indent = context.block_indent; + visitor.block_indent = context.block_indent + context.overflow_indent; let prefix = match self.rules { ast::BlockCheckMode::PushUnsafeBlock(..) | @@ -541,9 +541,9 @@ fn rewrite_match(context: &RewriteContext, let cond_str = try_opt!(cond.rewrite(context, cond_budget, offset + 6)); let mut result = format!("match {} {{", cond_str); - let block_indent = context.block_indent; let nested_context = context.nested_context(); - let arm_indent_str = make_indent(nested_context.block_indent); + let arm_indent = nested_context.block_indent + context.overflow_indent; + let arm_indent_str = make_indent(arm_indent); let open_brace_pos = span_after(mk_sp(cond.span.hi, arm_start_pos(&arms[0])), "{", @@ -579,8 +579,8 @@ fn rewrite_match(context: &RewriteContext, let arm_str = arm.rewrite(&nested_context, context.config.max_width - - nested_context.block_indent, - nested_context.block_indent); + arm_indent, + arm_indent); if let Some(ref arm_str) = arm_str { result.push_str(arm_str); } else { @@ -594,7 +594,7 @@ fn rewrite_match(context: &RewriteContext, // match expression, but meh. result.push('\n'); - result.push_str(&make_indent(block_indent)); + result.push_str(&make_indent(context.block_indent + context.overflow_indent)); result.push('}'); Some(result) } @@ -1192,7 +1192,7 @@ pub fn rewrite_assign_rhs>(context: &RewriteContext, result.push_str(&format!("\n{}", make_indent(new_offset))); let max_width = try_opt!(context.config.max_width.checked_sub(new_offset + 1)); - let rhs = try_opt!(ex.rewrite(&context, max_width, new_offset)); + let rhs = try_opt!(ex.rewrite(&context.overflow_context(), max_width, new_offset)); result.push_str(&rhs); } diff --git a/src/rewrite.rs b/src/rewrite.rs index 5f1549846b5..e3a239430a5 100644 --- a/src/rewrite.rs +++ b/src/rewrite.rs @@ -28,7 +28,14 @@ pub trait Rewrite { pub struct RewriteContext<'a> { pub codemap: &'a CodeMap, pub config: &'a Config, + + // Indentation due to nesting of blocks. pub block_indent: usize, + // *Extra* indentation due to overflowing to the next line, e.g., + // let foo = + // bar(); + // The extra 4 spaces when formatting `bar()` is overflow_indent. + pub overflow_indent: usize, } impl<'a> RewriteContext<'a> { @@ -37,6 +44,16 @@ impl<'a> RewriteContext<'a> { codemap: self.codemap, config: self.config, block_indent: self.block_indent + self.config.tab_spaces, + overflow_indent: self.overflow_indent, + } + } + + pub fn overflow_context(&self) -> RewriteContext<'a> { + RewriteContext { + codemap: self.codemap, + config: self.config, + block_indent: self.block_indent, + overflow_indent: self.overflow_indent + self.config.tab_spaces, } } diff --git a/src/visitor.rs b/src/visitor.rs index c9c84d7c2a2..0b29086daf6 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -338,6 +338,7 @@ impl<'a> FmtVisitor<'a> { codemap: self.codemap, config: self.config, block_indent: self.block_indent, + overflow_indent: 0, }; // 1 = ";" match vp.rewrite(&context, self.config.max_width - offset - 1, offset) { @@ -369,6 +370,7 @@ impl<'a> FmtVisitor<'a> { codemap: self.codemap, config: self.config, block_indent: self.block_indent, + overflow_indent: 0, } } } diff --git a/tests/target/match.rs b/tests/target/match.rs index e5f1fad967a..88d63112e37 100644 --- a/tests/target/match.rs +++ b/tests/target/match.rs @@ -53,3 +53,12 @@ fn foo() { Blurb => { } }; } + +// Test that a match on an overflow line is laid out properly. +fn main() { + let sub_span = + match self.span.sub_span_after_keywooooooooooooooooooooord(use_item.span, keywords::As) { + Some(sub_span) => Some(sub_span), + None => sub_span, + }; +}