From 09d54512da002e9695402b88fda63bcc49f6b762 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Thu, 16 Nov 2017 16:41:09 +0900 Subject: [PATCH 1/8] Break before '|' for multi-lined match arm pattern --- src/config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config.rs b/src/config.rs index c76933ca509..9fbdfc2754b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -611,7 +611,7 @@ create_config! { "Force match arm bodies to be in a new lines"; indent_match_arms: bool, true, false, "Indent match arms instead of keeping them at the same \ indentation level as the match keyword"; - match_pattern_separator_break_point: SeparatorPlace, SeparatorPlace::Back, false, + match_pattern_separator_break_point: SeparatorPlace, SeparatorPlace::Front, false, "Put a match sub-patterns' separator in front or back."; space_before_colon: bool, false, false, "Leave a space before the colon"; space_after_colon: bool, true, false, "Leave a space after the colon"; From 34c24990296ff473511dbd471f5ff288f956add5 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Thu, 16 Nov 2017 16:42:07 +0900 Subject: [PATCH 2/8] Cargo fmt --- src/bin/cargo-fmt.rs | 14 ++++++------ src/chains.rs | 32 +++++++++++++------------- src/closures.rs | 22 +++++++++--------- src/comment.rs | 14 ++++++------ src/expr.rs | 54 ++++++++++++++++++++++---------------------- src/imports.rs | 14 ++++++------ src/patterns.rs | 8 +++---- src/utils.rs | 32 +++++++++++++------------- 8 files changed, 95 insertions(+), 95 deletions(-) diff --git a/src/bin/cargo-fmt.rs b/src/bin/cargo-fmt.rs index 60cc59bc1a0..fdeb3cf4286 100644 --- a/src/bin/cargo-fmt.rs +++ b/src/bin/cargo-fmt.rs @@ -164,13 +164,13 @@ enum TargetKind { impl TargetKind { fn should_format(&self) -> bool { match *self { - TargetKind::Lib | - TargetKind::Bin | - TargetKind::Example | - TargetKind::Test | - TargetKind::Bench | - TargetKind::CustomBuild | - TargetKind::ProcMacro => true, + TargetKind::Lib + | TargetKind::Bin + | TargetKind::Example + | TargetKind::Test + | TargetKind::Bench + | TargetKind::CustomBuild + | TargetKind::ProcMacro => true, _ => false, } } diff --git a/src/chains.rs b/src/chains.rs index c7b91625c98..8a4b4257891 100644 --- a/src/chains.rs +++ b/src/chains.rs @@ -350,19 +350,19 @@ fn is_block_expr(context: &RewriteContext, expr: &ast::Expr, repr: &str) -> bool ast::ExprKind::Mac(..) | ast::ExprKind::Call(..) => { context.use_block_indent() && repr.contains('\n') } - ast::ExprKind::Struct(..) | - ast::ExprKind::While(..) | - ast::ExprKind::WhileLet(..) | - ast::ExprKind::If(..) | - ast::ExprKind::IfLet(..) | - ast::ExprKind::Block(..) | - ast::ExprKind::Loop(..) | - ast::ExprKind::ForLoop(..) | - ast::ExprKind::Match(..) => repr.contains('\n'), - ast::ExprKind::Paren(ref expr) | - ast::ExprKind::Binary(_, _, ref expr) | - ast::ExprKind::Index(_, ref expr) | - ast::ExprKind::Unary(_, ref expr) => is_block_expr(context, expr, repr), + ast::ExprKind::Struct(..) + | ast::ExprKind::While(..) + | ast::ExprKind::WhileLet(..) + | ast::ExprKind::If(..) + | ast::ExprKind::IfLet(..) + | ast::ExprKind::Block(..) + | ast::ExprKind::Loop(..) + | ast::ExprKind::ForLoop(..) + | ast::ExprKind::Match(..) => repr.contains('\n'), + ast::ExprKind::Paren(ref expr) + | ast::ExprKind::Binary(_, _, ref expr) + | ast::ExprKind::Index(_, ref expr) + | ast::ExprKind::Unary(_, ref expr) => is_block_expr(context, expr, repr), _ => false, } } @@ -396,9 +396,9 @@ fn pop_expr_chain(expr: &ast::Expr, context: &RewriteContext) -> Option { Some(convert_try(&expressions[0], context)) } - ast::ExprKind::TupField(ref subexpr, _) | - ast::ExprKind::Field(ref subexpr, _) | - ast::ExprKind::Try(ref subexpr) => Some(convert_try(subexpr, context)), + ast::ExprKind::TupField(ref subexpr, _) + | ast::ExprKind::Field(ref subexpr, _) + | ast::ExprKind::Try(ref subexpr) => Some(convert_try(subexpr, context)), _ => None, } } diff --git a/src/closures.rs b/src/closures.rs index a180e116a8b..5f5b3d8fe26 100644 --- a/src/closures.rs +++ b/src/closures.rs @@ -328,17 +328,17 @@ where fn is_block_closure_forced(expr: &ast::Expr) -> bool { match expr.node { - ast::ExprKind::If(..) | - ast::ExprKind::IfLet(..) | - ast::ExprKind::Loop(..) | - ast::ExprKind::While(..) | - ast::ExprKind::WhileLet(..) | - ast::ExprKind::ForLoop(..) => true, - ast::ExprKind::AddrOf(_, ref expr) | - ast::ExprKind::Box(ref expr) | - ast::ExprKind::Try(ref expr) | - ast::ExprKind::Unary(_, ref expr) | - ast::ExprKind::Cast(ref expr, _) => is_block_closure_forced(expr), + ast::ExprKind::If(..) + | ast::ExprKind::IfLet(..) + | ast::ExprKind::Loop(..) + | ast::ExprKind::While(..) + | ast::ExprKind::WhileLet(..) + | ast::ExprKind::ForLoop(..) => true, + ast::ExprKind::AddrOf(_, ref expr) + | ast::ExprKind::Box(ref expr) + | ast::ExprKind::Try(ref expr) + | ast::ExprKind::Unary(_, ref expr) + | ast::ExprKind::Cast(ref expr, _) => is_block_closure_forced(expr), _ => false, } } diff --git a/src/comment.rs b/src/comment.rs index 6d5f48fbb26..2511e5df216 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -64,10 +64,10 @@ impl<'a> CommentStyle<'a> { pub fn closer(&self) -> &'a str { match *self { - CommentStyle::DoubleSlash | - CommentStyle::TripleSlash | - CommentStyle::Custom(..) | - CommentStyle::Doc => "", + CommentStyle::DoubleSlash + | CommentStyle::TripleSlash + | CommentStyle::Custom(..) + | CommentStyle::Doc => "", CommentStyle::DoubleBullet => " **/", CommentStyle::SingleBullet | CommentStyle::Exclamation => " */", } @@ -648,9 +648,9 @@ enum FullCodeCharKind { impl FullCodeCharKind { fn is_comment(&self) -> bool { match *self { - FullCodeCharKind::StartComment | - FullCodeCharKind::InComment | - FullCodeCharKind::EndComment => true, + FullCodeCharKind::StartComment + | FullCodeCharKind::InComment + | FullCodeCharKind::EndComment => true, _ => false, } } diff --git a/src/expr.rs b/src/expr.rs index 46e7e500f22..1086a7030ba 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -99,12 +99,12 @@ pub fn format_expr( ast::ExprKind::Tup(ref items) => { rewrite_tuple(context, &ptr_vec_to_ref_vec(items), expr.span, shape) } - ast::ExprKind::If(..) | - ast::ExprKind::IfLet(..) | - ast::ExprKind::ForLoop(..) | - ast::ExprKind::Loop(..) | - ast::ExprKind::While(..) | - ast::ExprKind::WhileLet(..) => to_control_flow(expr, expr_type) + ast::ExprKind::If(..) + | ast::ExprKind::IfLet(..) + | ast::ExprKind::ForLoop(..) + | ast::ExprKind::Loop(..) + | ast::ExprKind::While(..) + | ast::ExprKind::WhileLet(..) => to_control_flow(expr, expr_type) .and_then(|control_flow| control_flow.rewrite(context, shape)), ast::ExprKind::Block(ref block) => { match expr_type { @@ -161,10 +161,10 @@ pub fn format_expr( ast::ExprKind::Closure(capture, ref fn_decl, ref body, _) => { closures::rewrite_closure(capture, fn_decl, body, expr.span, context, shape) } - ast::ExprKind::Try(..) | - ast::ExprKind::Field(..) | - ast::ExprKind::TupField(..) | - ast::ExprKind::MethodCall(..) => rewrite_chain(expr, context, shape), + ast::ExprKind::Try(..) + | ast::ExprKind::Field(..) + | ast::ExprKind::TupField(..) + | ast::ExprKind::MethodCall(..) => rewrite_chain(expr, context, shape), ast::ExprKind::Mac(ref mac) => { rewrite_macro(mac, None, context, shape, MacroPosition::Expression).or_else(|| { wrap_str( @@ -2060,29 +2060,29 @@ pub fn can_be_overflowed_expr(context: &RewriteContext, expr: &ast::Expr, args_l (context.use_block_indent() && args_len == 1) || (context.config.indent_style() == IndentStyle::Visual && args_len > 1) } - ast::ExprKind::If(..) | - ast::ExprKind::IfLet(..) | - ast::ExprKind::ForLoop(..) | - ast::ExprKind::Loop(..) | - ast::ExprKind::While(..) | - ast::ExprKind::WhileLet(..) => { + ast::ExprKind::If(..) + | ast::ExprKind::IfLet(..) + | ast::ExprKind::ForLoop(..) + | ast::ExprKind::Loop(..) + | ast::ExprKind::While(..) + | ast::ExprKind::WhileLet(..) => { context.config.combine_control_expr() && context.use_block_indent() && args_len == 1 } ast::ExprKind::Block(..) | ast::ExprKind::Closure(..) => { context.use_block_indent() || context.config.indent_style() == IndentStyle::Visual && args_len > 1 } - ast::ExprKind::Array(..) | - ast::ExprKind::Call(..) | - ast::ExprKind::Mac(..) | - ast::ExprKind::MethodCall(..) | - ast::ExprKind::Struct(..) | - ast::ExprKind::Tup(..) => context.use_block_indent() && args_len == 1, - ast::ExprKind::AddrOf(_, ref expr) | - ast::ExprKind::Box(ref expr) | - ast::ExprKind::Try(ref expr) | - ast::ExprKind::Unary(_, ref expr) | - ast::ExprKind::Cast(ref expr, _) => can_be_overflowed_expr(context, expr, args_len), + ast::ExprKind::Array(..) + | ast::ExprKind::Call(..) + | ast::ExprKind::Mac(..) + | ast::ExprKind::MethodCall(..) + | ast::ExprKind::Struct(..) + | ast::ExprKind::Tup(..) => context.use_block_indent() && args_len == 1, + ast::ExprKind::AddrOf(_, ref expr) + | ast::ExprKind::Box(ref expr) + | ast::ExprKind::Try(ref expr) + | ast::ExprKind::Unary(_, ref expr) + | ast::ExprKind::Cast(ref expr, _) => can_be_overflowed_expr(context, expr, args_len), _ => false, } } diff --git a/src/imports.rs b/src/imports.rs index faf0638f7d1..300aa046cf1 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -27,9 +27,9 @@ use visitor::{rewrite_extern_crate, FmtVisitor}; fn path_of(a: &ast::ViewPath_) -> &ast::Path { match *a { - ast::ViewPath_::ViewPathSimple(_, ref p) | - ast::ViewPath_::ViewPathGlob(ref p) | - ast::ViewPath_::ViewPathList(ref p, _) => p, + ast::ViewPath_::ViewPathSimple(_, ref p) + | ast::ViewPath_::ViewPathGlob(ref p) + | ast::ViewPath_::ViewPathList(ref p, _) => p, } } @@ -366,10 +366,10 @@ impl<'a> ImportItem<'a> { fn to_str(&self) -> Option<&str> { match *self { - ImportItem::SelfImport(s) | - ImportItem::SnakeCase(s) | - ImportItem::CamelCase(s) | - ImportItem::AllCaps(s) => Some(s), + ImportItem::SelfImport(s) + | ImportItem::SnakeCase(s) + | ImportItem::CamelCase(s) + | ImportItem::AllCaps(s) => Some(s), ImportItem::Invalid => None, } } diff --git a/src/patterns.rs b/src/patterns.rs index 20ea14a5dfe..1b8840392ca 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -245,10 +245,10 @@ impl<'a> Spanned for TuplePatField<'a> { pub fn can_be_overflowed_pat(context: &RewriteContext, pat: &TuplePatField, len: usize) -> bool { match *pat { TuplePatField::Pat(pat) => match pat.node { - ast::PatKind::Path(..) | - ast::PatKind::Tuple(..) | - ast::PatKind::Struct(..) | - ast::PatKind::TupleStruct(..) => context.use_block_indent() && len == 1, + ast::PatKind::Path(..) + | ast::PatKind::Tuple(..) + | ast::PatKind::Struct(..) + | ast::PatKind::TupleStruct(..) => context.use_block_indent() && len == 1, ast::PatKind::Ref(ref p, _) | ast::PatKind::Box(ref p) => { can_be_overflowed_pat(context, &TuplePatField::Pat(p), len) } diff --git a/src/utils.rs b/src/utils.rs index 88272ff0d38..d851dfda79d 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -240,10 +240,10 @@ pub fn semicolon_for_expr(context: &RewriteContext, expr: &ast::Expr) -> bool { pub fn semicolon_for_stmt(context: &RewriteContext, stmt: &ast::Stmt) -> bool { match stmt.node { ast::StmtKind::Semi(ref expr) => match expr.node { - ast::ExprKind::While(..) | - ast::ExprKind::WhileLet(..) | - ast::ExprKind::Loop(..) | - ast::ExprKind::ForLoop(..) => false, + ast::ExprKind::While(..) + | ast::ExprKind::WhileLet(..) + | ast::ExprKind::Loop(..) + | ast::ExprKind::ForLoop(..) => false, ast::ExprKind::Break(..) | ast::ExprKind::Continue(..) | ast::ExprKind::Ret(..) => { context.config.trailing_semicolon() } @@ -450,18 +450,18 @@ pub fn paren_overhead(context: &RewriteContext) -> usize { pub fn left_most_sub_expr(e: &ast::Expr) -> &ast::Expr { match e.node { - ast::ExprKind::InPlace(ref e, _) | - ast::ExprKind::Call(ref e, _) | - ast::ExprKind::Binary(_, ref e, _) | - ast::ExprKind::Cast(ref e, _) | - ast::ExprKind::Type(ref e, _) | - ast::ExprKind::Assign(ref e, _) | - ast::ExprKind::AssignOp(_, ref e, _) | - ast::ExprKind::Field(ref e, _) | - ast::ExprKind::TupField(ref e, _) | - ast::ExprKind::Index(ref e, _) | - ast::ExprKind::Range(Some(ref e), _, _) | - ast::ExprKind::Try(ref e) => left_most_sub_expr(e), + ast::ExprKind::InPlace(ref e, _) + | ast::ExprKind::Call(ref e, _) + | ast::ExprKind::Binary(_, ref e, _) + | ast::ExprKind::Cast(ref e, _) + | ast::ExprKind::Type(ref e, _) + | ast::ExprKind::Assign(ref e, _) + | ast::ExprKind::AssignOp(_, ref e, _) + | ast::ExprKind::Field(ref e, _) + | ast::ExprKind::TupField(ref e, _) + | ast::ExprKind::Index(ref e, _) + | ast::ExprKind::Range(Some(ref e), _, _) + | ast::ExprKind::Try(ref e) => left_most_sub_expr(e), _ => e, } } From 1c702aa252dc048dd26bf0eea828aafbd52795d5 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Thu, 16 Nov 2017 16:42:11 +0900 Subject: [PATCH 3/8] Update tests --- tests/target/expr.rs | 6 ++--- tests/target/issue-855.rs | 8 +++--- tests/target/match.rs | 57 ++++++++++++++++++++------------------- 3 files changed, 36 insertions(+), 35 deletions(-) diff --git a/tests/target/expr.rs b/tests/target/expr.rs index 4842dcb9366..08803e5db4e 100644 --- a/tests/target/expr.rs +++ b/tests/target/expr.rs @@ -402,9 +402,9 @@ fn newlines_between_list_like_expr() { ]; match x { - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | - yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy | - zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz => foo(a, b, c), + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + | yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy + | zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz => foo(a, b, c), _ => bar(), }; } diff --git a/tests/target/issue-855.rs b/tests/target/issue-855.rs index dc5600c7d5d..0430cc62954 100644 --- a/tests/target/issue-855.rs +++ b/tests/target/issue-855.rs @@ -2,8 +2,8 @@ fn main() { 'running: loop { for event in event_pump.poll_iter() { match event { - Event::Quit { .. } | - Event::KeyDown { + Event::Quit { .. } + | Event::KeyDown { keycode: Some(Keycode::Escape), .. } => break 'running, @@ -16,8 +16,8 @@ fn main2() { 'running: loop { for event in event_pump.poll_iter() { match event { - Event::Quit { .. } | - Event::KeyDownXXXXXXXXXXXXX { + Event::Quit { .. } + | Event::KeyDownXXXXXXXXXXXXX { keycode: Some(Keycode::Escape), .. } => break 'running, diff --git a/tests/target/match.rs b/tests/target/match.rs index 3e04f4ca4be..fe707080b16 100644 --- a/tests/target/match.rs +++ b/tests/target/match.rs @@ -17,12 +17,12 @@ fn foo() { aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa } Pattern1 | Pattern2 | Pattern3 => false, - Paternnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn | - Paternnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn => blah, - Patternnnnnnnnnnnnnnnnnnn | - Patternnnnnnnnnnnnnnnnnnn | - Patternnnnnnnnnnnnnnnnnnn | - Patternnnnnnnnnnnnnnnnnnn => meh, + Paternnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn + | Paternnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn => blah, + Patternnnnnnnnnnnnnnnnnnn + | Patternnnnnnnnnnnnnnnnnnn + | Patternnnnnnnnnnnnnnnnnnn + | Patternnnnnnnnnnnnnnnnnnn => meh, Patternnnnnnnnnnnnnnnnnnn | Patternnnnnnnnnnnnnnnnnnn if looooooooooooooooooong_guard => { meh @@ -35,8 +35,9 @@ fn foo() { } // Test that earlier patterns can take the guard space - (aaaa, bbbbb, ccccccc, aaaaa, bbbbbbbb, cccccc, aaaa, bbbbbbbb, cccccc, dddddd) | - Patternnnnnnnnnnnnnnnnnnnnnnnnn if loooooooooooooooooooooooooooooooooooooooooong_guard => {} + (aaaa, bbbbb, ccccccc, aaaaa, bbbbbbbb, cccccc, aaaa, bbbbbbbb, cccccc, dddddd) + | Patternnnnnnnnnnnnnnnnnnnnnnnnn + if loooooooooooooooooooooooooooooooooooooooooong_guard => {} _ => {} ast::PathParameters::AngleBracketedParameters(ref data) @@ -299,18 +300,18 @@ fn issue494() { fn issue386() { match foo { BiEq | BiLt | BiLe | BiNe | BiGt | BiGe => true, - BiAnd | - BiOr | - BiAdd | - BiSub | - BiMul | - BiDiv | - BiRem | - BiBitXor | - BiBitAnd | - BiBitOr | - BiShl | - BiShr => false, + BiAnd + | BiOr + | BiAdd + | BiSub + | BiMul + | BiDiv + | BiRem + | BiBitXor + | BiBitAnd + | BiBitOr + | BiShl + | BiShr => false, } } @@ -318,8 +319,8 @@ fn guards() { match foo { aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa if foooooooooooooo && barrrrrrrrrrrr => {} - aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | - aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + | aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa if foooooooooooooo && barrrrrrrrrrrr => {} aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa if fooooooooooooooooooooo @@ -438,12 +439,12 @@ fn match_with_near_max_width() { _ => unimplemented!(), }; match m { - Variant::Tag | - Variant::Tag2 | - Variant::Tag3 | - Variant::Tag4 | - Variant::Tag5 | - Variant::Tag6 => {} + Variant::Tag + | Variant::Tag2 + | Variant::Tag3 + | Variant::Tag4 + | Variant::Tag5 + | Variant::Tag6 => {} } } From 3bd1cc4fe072ccc6bd53ee57c90af3debca377fc Mon Sep 17 00:00:00 2001 From: topecongiro Date: Thu, 16 Nov 2017 17:37:01 +0900 Subject: [PATCH 4/8] Update a test for #1807 --- tests/target/match.rs | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/tests/target/match.rs b/tests/target/match.rs index fe707080b16..a14aadf0862 100644 --- a/tests/target/match.rs +++ b/tests/target/match.rs @@ -300,18 +300,8 @@ fn issue494() { fn issue386() { match foo { BiEq | BiLt | BiLe | BiNe | BiGt | BiGe => true, - BiAnd - | BiOr - | BiAdd - | BiSub - | BiMul - | BiDiv - | BiRem - | BiBitXor - | BiBitAnd - | BiBitOr - | BiShl - | BiShr => false, + BiAnd | BiOr | BiAdd | BiSub | BiMul | BiDiv | BiRem | BiBitXor | BiBitAnd | BiBitOr + | BiShl | BiShr => false, } } From 35466adbfe90939552580c15161a1ee9b9f1c4a5 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Thu, 16 Nov 2017 17:38:12 +0900 Subject: [PATCH 5/8] Generalize ListItems to allow a separator other than comma --- src/closures.rs | 1 + src/expr.rs | 5 +++++ src/imports.rs | 2 ++ src/items.rs | 5 +++++ src/lists.rs | 5 ++++- src/patterns.rs | 2 ++ src/types.rs | 2 ++ src/vertical.rs | 1 + src/visitor.rs | 1 + 9 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/closures.rs b/src/closures.rs index 5f5b3d8fe26..8290f1b09a1 100644 --- a/src/closures.rs +++ b/src/closures.rs @@ -194,6 +194,7 @@ fn rewrite_closure_fn_decl( context.codemap, fn_decl.inputs.iter(), "|", + ",", |arg| span_lo_for_arg(arg), |arg| span_hi_for_arg(context, arg), |arg| arg.rewrite(context, arg_shape), diff --git a/src/expr.rs b/src/expr.rs index 1086a7030ba..0b47bb847ed 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -430,6 +430,7 @@ where context.codemap, expr_iter, "]", + ",", |item| item.span.lo(), |item| item.span.hi(), |item| item.rewrite(context, nested_shape), @@ -1341,6 +1342,7 @@ fn rewrite_match_arms( .zip(is_last_iter) .map(|(arm, is_last)| ArmWrapper::new(arm, is_last)), "}", + "|", |arm| arm.arm.span().lo(), |arm| arm.arm.span().hi(), |arm| arm.rewrite(context, arm_shape), @@ -1874,6 +1876,7 @@ where context.codemap, args.iter(), ")", + ",", |item| item.span().lo(), |item| item.span().hi(), |item| item.rewrite(context, shape), @@ -2301,6 +2304,7 @@ fn rewrite_struct_lit<'a>( context.codemap, field_iter, "}", + ",", span_lo, span_hi, rewrite, @@ -2451,6 +2455,7 @@ where context.codemap, items, ")", + ",", |item| item.span().lo(), |item| item.span().hi(), |item| item.rewrite(context, nested_shape), diff --git a/src/imports.rs b/src/imports.rs index 300aa046cf1..56d23fa9827 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -212,6 +212,7 @@ fn rewrite_imports( context.codemap, use_items.iter(), "", + ";", |item| item.span().lo(), |item| item.span().hi(), |item| { @@ -442,6 +443,7 @@ fn rewrite_use_list( context.codemap, path_list.iter(), "}", + ",", |vpi| vpi.span.lo(), |vpi| vpi.span.hi(), rewrite_path_item, diff --git a/src/items.rs b/src/items.rs index 0c1895c2391..2dda5bc16bd 100644 --- a/src/items.rs +++ b/src/items.rs @@ -489,6 +489,7 @@ impl<'a> FmtVisitor<'a> { self.codemap, enum_def.variants.iter(), "}", + ",", |f| if !f.node.attrs.is_empty() { f.node.attrs[0].span.lo() } else { @@ -2226,6 +2227,7 @@ fn rewrite_args( .map(ArgumentKind::Regular) .chain(variadic_arg), ")", + ",", |arg| match *arg { ArgumentKind::Regular(arg) => span_lo_for_arg(arg), ArgumentKind::Variadic(start) => start, @@ -2447,6 +2449,7 @@ fn rewrite_generics_inner( context.codemap, generics_args, ">", + ",", |arg| arg.span().lo(), |arg| arg.span().hi(), |arg| arg.rewrite(context, shape), @@ -2589,6 +2592,7 @@ fn rewrite_where_clause_rfc_style( context.codemap, where_clause.predicates.iter(), terminator, + ",", |pred| pred.span().lo(), |pred| pred.span().hi(), |pred| pred.rewrite(context, clause_shape), @@ -2702,6 +2706,7 @@ fn rewrite_where_clause( context.codemap, where_clause.predicates.iter(), terminator, + ",", |pred| pred.span().lo(), |pred| pred.span().hi(), |pred| pred.rewrite(context, Shape::legacy(budget, offset)), diff --git a/src/lists.rs b/src/lists.rs index dc55a8261fa..89382ab76ec 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -509,6 +509,7 @@ where prev_span_end: BytePos, next_span_start: BytePos, terminator: &'a str, + separator: &'a str, leave_last: bool, } @@ -581,7 +582,7 @@ where } } let newline_index = post_snippet.find('\n'); - if let Some(separator_index) = post_snippet.find_uncommented(",") { + if let Some(separator_index) = post_snippet.find_uncommented(self.separator) { match (block_open_index, newline_index) { // Separator before comment, with the next item on same line. // Comment belongs to next item. @@ -677,6 +678,7 @@ pub fn itemize_list<'a, T, I, F1, F2, F3>( codemap: &'a CodeMap, inner: I, terminator: &'a str, + separator: &'a str, get_lo: F1, get_hi: F2, get_item_string: F3, @@ -699,6 +701,7 @@ where prev_span_end: prev_span_end, next_span_start: next_span_start, terminator: terminator, + separator: separator, leave_last: leave_last, } } diff --git a/src/patterns.rs b/src/patterns.rs index 1b8840392ca..f4bb59e5127 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -151,6 +151,7 @@ fn rewrite_struct_pat( context.codemap, fields.iter(), terminator, + ",", |f| f.span.lo(), |f| f.span.hi(), |f| f.node.rewrite(context, v_shape), @@ -347,6 +348,7 @@ fn count_wildcard_suffix_len( context.codemap, patterns.iter(), ")", + ",", |item| item.span().lo(), |item| item.span().hi(), |item| item.rewrite(context, shape), diff --git a/src/types.rs b/src/types.rs index 180a38b2f23..ab4de7349b1 100644 --- a/src/types.rs +++ b/src/types.rs @@ -230,6 +230,7 @@ fn rewrite_segment( context.codemap, param_list.into_iter(), ">", + ",", |param| param.get_span().lo(), |param| param.get_span().hi(), |seg| seg.rewrite(context, generics_shape), @@ -321,6 +322,7 @@ where .map(|i| ArgumentKind::Regular(Box::new(i))) .chain(variadic_arg), ")", + ",", |arg| match *arg { ArgumentKind::Regular(ref ty) => ty.span().lo(), ArgumentKind::Variadic(start) => start, diff --git a/src/vertical.rs b/src/vertical.rs index 32072acfc9e..bb4bad7852e 100644 --- a/src/vertical.rs +++ b/src/vertical.rs @@ -231,6 +231,7 @@ fn rewrite_aligned_items_inner( context.codemap, fields.iter(), "}", + ",", |field| field.get_span().lo(), |field| field.get_span().hi(), |field| field.rewrite_aligned_item(context, item_shape, field_prefix_max_width), diff --git a/src/visitor.rs b/src/visitor.rs index 159f73452bd..79bade0cc20 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -750,6 +750,7 @@ impl Rewrite for ast::MetaItem { context.codemap, list.iter(), ")", + ",", |nested_meta_item| nested_meta_item.span.lo(), |nested_meta_item| nested_meta_item.span.hi(), |nested_meta_item| nested_meta_item.rewrite(context, item_shape), From 311a3c526c50cf12fd773c8a847a22a5db3022ee Mon Sep 17 00:00:00 2001 From: topecongiro Date: Thu, 16 Nov 2017 17:39:38 +0900 Subject: [PATCH 6/8] Fix up write_list() to handle Mixed tactic --- src/lists.rs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/lists.rs b/src/lists.rs index 89382ab76ec..b80d8d6541e 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -207,10 +207,18 @@ impl SeparatorPlace { *self == SeparatorPlace::Back } - pub fn from_tactic(default: SeparatorPlace, tactic: DefinitiveListTactic) -> SeparatorPlace { + pub fn from_tactic( + default: SeparatorPlace, + tactic: DefinitiveListTactic, + sep: &str, + ) -> SeparatorPlace { match tactic { DefinitiveListTactic::Vertical => default, - _ => SeparatorPlace::Back, + _ => if sep == "," { + SeparatorPlace::Back + } else { + default + }, } } } @@ -269,7 +277,8 @@ where let cloned_items = items.clone(); let mut iter = items.into_iter().enumerate().peekable(); let mut item_max_width: Option = None; - let mut sep_place = SeparatorPlace::from_tactic(formatting.separator_place, tactic); + let sep_place = + SeparatorPlace::from_tactic(formatting.separator_place, tactic, formatting.separator); let mut line_len = 0; let indent_str = &formatting.shape.indent.to_string(formatting.config); @@ -278,7 +287,10 @@ where let inner_item = item.item.as_ref()?; let first = i == 0; let last = iter.peek().is_none(); - let mut separate = !last || trailing_separator; + let mut separate = match sep_place { + SeparatorPlace::Front => !first, + SeparatorPlace::Back => !last || trailing_separator, + }; let item_sep_len = if separate { sep_len } else { 0 }; // Item string may be multi-line. Its length (used for block comment alignment) @@ -316,9 +328,6 @@ where trailing_separator = true; } } - sep_place = formatting.separator_place; - } else { - sep_place = SeparatorPlace::Back; } if line_len > 0 { From e09a0cc83622e57cb157d4fed13b9740f1a40bfe Mon Sep 17 00:00:00 2001 From: topecongiro Date: Thu, 16 Nov 2017 17:40:24 +0900 Subject: [PATCH 7/8] Add is_short_pattern() --- src/expr.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/expr.rs b/src/expr.rs index 0b47bb847ed..ab9bc2fa240 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1411,6 +1411,38 @@ fn rewrite_match_arm( ) } +/// Returns true if the given pattern is short. A short pattern is defined by the following grammer: +/// +/// [small, ntp]: +/// - single token +/// - `&[single-line, ntp]` +/// +/// [small]: +/// - `[small, ntp]` +/// - unary tuple constructor `([small, ntp])` +/// - `&[small]` +fn is_short_pattern(pat: &ast::Pat, pat_str: &str) -> bool { + // We also require that the pattern is reasonably 'small' with its literal width. + pat_str.len() <= 20 && !pat_str.contains("\n") && is_short_pattern_inner(pat) +} + +fn is_short_pattern_inner(pat: &ast::Pat) -> bool { + match pat.node { + ast::PatKind::Wild | ast::PatKind::Lit(_) => true, + ast::PatKind::Ident(_, _, ref pat) => pat.is_none(), + ast::PatKind::Struct(..) + | ast::PatKind::Mac(..) + | ast::PatKind::Slice(..) + | ast::PatKind::Path(..) + | ast::PatKind::Range(..) => false, + ast::PatKind::Tuple(ref subpats, _) => subpats.len() <= 1, + ast::PatKind::TupleStruct(ref path, ref subpats, _) => { + path.segments.len() <= 1 && subpats.len() <= 1 + } + ast::PatKind::Box(ref p) | ast::PatKind::Ref(ref p, _) => is_short_pattern_inner(&*p), + } +} + fn rewrite_match_pattern( context: &RewriteContext, pats: &[ptr::P], From 95d7619e345ac6fa997297af9565bfd44ef9e855 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Thu, 16 Nov 2017 17:40:33 +0900 Subject: [PATCH 8/8] Use mixed layout for patterns that are all short --- src/expr.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index ab9bc2fa240..4716974153b 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1457,13 +1457,20 @@ fn rewrite_match_pattern( .map(|p| p.rewrite(context, pat_shape)) .collect::>>()?; + let use_mixed_layout = pats.iter() + .zip(pat_strs.iter()) + .all(|(pat, pat_str)| is_short_pattern(pat, pat_str)); let items: Vec<_> = pat_strs.into_iter().map(ListItem::from_str).collect(); - let tactic = definitive_tactic( - &items, - ListTactic::HorizontalVertical, - Separator::VerticalBar, - pat_shape.width, - ); + let tactic = if use_mixed_layout { + DefinitiveListTactic::Mixed + } else { + definitive_tactic( + &items, + ListTactic::HorizontalVertical, + Separator::VerticalBar, + pat_shape.width, + ) + }; let fmt = ListFormatting { tactic: tactic, separator: " |",