From f135641cc873c21ea8768a78534efc9ff3f4bb82 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Sun, 11 Jun 2017 23:26:49 +0900 Subject: Use multi line when type bounds does not fit in a single line --- src/expr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/expr.rs') diff --git a/src/expr.rs b/src/expr.rs index aa5913e5f0f..04778b6ddca 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -2063,12 +2063,12 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext, // FIXME if context.config.struct_lit_style() == Visual, but we run out // of space, we should fall back to BlockIndent. } + pub fn struct_lit_field_separator(config: &Config) -> &str { colon_spaces(config.space_before_struct_lit_field_colon(), config.space_after_struct_lit_field_colon()) } - fn rewrite_field(context: &RewriteContext, field: &ast::Field, shape: Shape) -> Option { let name = &field.ident.node.to_string(); if field.is_shorthand { -- cgit 1.4.1-3-g733a5 From 9ad499786d1eb8008423d9d9810b47c4124e8d47 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Mon, 12 Jun 2017 15:58:27 +0900 Subject: Use block indent for args if single line args exceeds max width --- src/expr.rs | 18 +++++++++++++++--- tests/source/configs-fn_call_style-block.rs | 6 ++++++ .../configs-fn_call_style-block-trailing-comma.rs | 4 +++- tests/target/configs-fn_call_style-block.rs | 7 +++++++ 4 files changed, 31 insertions(+), 4 deletions(-) (limited to 'src/expr.rs') diff --git a/src/expr.rs b/src/expr.rs index aa5913e5f0f..b47bf391ac1 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1734,9 +1734,12 @@ fn rewrite_call_inner(context: &RewriteContext, force_trailing_comma); } + let args_shape = shape + .sub_width(last_line_width(&callee_str)) + .ok_or(Ordering::Less)?; Ok(format!("{}{}", callee_str, - wrap_args_with_parens(context, &list_str, extendable, shape, nested_shape))) + wrap_args_with_parens(context, &list_str, extendable, args_shape, nested_shape))) } fn need_block_indent(s: &str, shape: Shape) -> bool { @@ -1906,14 +1909,23 @@ fn can_be_overflowed_expr(context: &RewriteContext, expr: &ast::Expr, args_len: } } +fn paren_overhead(context: &RewriteContext) -> usize { + if context.config.spaces_within_parens() { + 4 + } else { + 2 + } +} + fn wrap_args_with_parens(context: &RewriteContext, args_str: &str, is_extendable: bool, shape: Shape, nested_shape: Shape) -> String { - if !context.use_block_indent() || (context.inside_macro && !args_str.contains('\n')) || - is_extendable { + if !context.use_block_indent() || + (context.inside_macro && !args_str.contains('\n') && + args_str.len() + paren_overhead(context) <= shape.width) || is_extendable { if context.config.spaces_within_parens() && args_str.len() > 0 { format!("( {} )", args_str) } else { diff --git a/tests/source/configs-fn_call_style-block.rs b/tests/source/configs-fn_call_style-block.rs index 2068216d18e..ee6178c1902 100644 --- a/tests/source/configs-fn_call_style-block.rs +++ b/tests/source/configs-fn_call_style-block.rs @@ -125,3 +125,9 @@ fn issue1581() { }, ); } + +fn issue1651() { + { + let type_list: Vec<_> = try_opt!(types.iter().map(|ty| ty.rewrite(context, shape)).collect()); + } +} diff --git a/tests/target/configs-fn_call_style-block-trailing-comma.rs b/tests/target/configs-fn_call_style-block-trailing-comma.rs index ebdf41d0e3b..b6eb94eb677 100644 --- a/tests/target/configs-fn_call_style-block-trailing-comma.rs +++ b/tests/target/configs-fn_call_style-block-trailing-comma.rs @@ -3,7 +3,9 @@ // rustfmt should not add trailing comma when rewriting macro. See #1528. fn a() { - panic!("this is a long string that goes past the maximum line length causing rustfmt to insert a comma here:"); + panic!( + "this is a long string that goes past the maximum line length causing rustfmt to insert a comma here:" + ); foo( oooptoptoptoptptooptoptoptoptptooptoptoptoptptoptoptoptoptpt(), ); diff --git a/tests/target/configs-fn_call_style-block.rs b/tests/target/configs-fn_call_style-block.rs index ddead8ce5a8..dfc8daef6d9 100644 --- a/tests/target/configs-fn_call_style-block.rs +++ b/tests/target/configs-fn_call_style-block.rs @@ -145,3 +145,10 @@ fn issue1581() { } }); } + +fn issue1651() { + { + let type_list: Vec<_> = + try_opt!(types.iter().map(|ty| ty.rewrite(context, shape)).collect()); + } +} -- cgit 1.4.1-3-g733a5 From 9df1509d1913865303844728de1b24d4495e9a4f Mon Sep 17 00:00:00 2001 From: topecongiro Date: Mon, 12 Jun 2017 17:25:07 +0900 Subject: Use rewrite instead of visitor for attributes --- src/expr.rs | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) (limited to 'src/expr.rs') diff --git a/src/expr.rs b/src/expr.rs index aa5913e5f0f..e6889991b07 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1321,20 +1321,13 @@ impl Rewrite for ast::Arm { ref body, } = self; - // FIXME this is all a bit grotty, would be nice to abstract out the - // treatment of attributes. let attr_str = if !attrs.is_empty() { - // We only use this visitor for the attributes, should we use it for - // more? - let mut attr_visitor = FmtVisitor::from_codemap(context.parse_session, context.config); - attr_visitor.block_indent = shape.indent.block_only(); - attr_visitor.last_pos = attrs[0].span.lo; - if attr_visitor.visit_attrs(attrs) { - // Attributes included a skip instruction. + if contains_skip(attrs) { return None; } - attr_visitor.format_missing(pats[0].span.lo); - attr_visitor.buffer.to_string() + format!("{}\n{}", + try_opt!(attrs.rewrite(context, shape)), + shape.indent.to_string(context.config)) } else { String::new() }; -- cgit 1.4.1-3-g733a5 From 57fc39305dc765d4454869ab6a6b9667c17b6e57 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Mon, 12 Jun 2017 17:25:38 +0900 Subject: Put guard on newline if it exceeds max width --- src/expr.rs | 28 +++++++++++----------------- tests/target/configs-control_style-rfc.rs | 17 +++++++++++++++++ tests/target/match.rs | 4 ++-- 3 files changed, 30 insertions(+), 19 deletions(-) (limited to 'src/expr.rs') diff --git a/src/expr.rs b/src/expr.rs index e6889991b07..06993c729b7 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1495,28 +1495,22 @@ fn rewrite_guard(context: &RewriteContext, if let Some(ref guard) = *guard { // First try to fit the guard string on the same line as the pattern. // 4 = ` if `, 5 = ` => {` - let overhead = pattern_width + 4 + 5; - if overhead < shape.width { - let cond_shape = shape - .shrink_left(pattern_width + 4) - .unwrap() - .sub_width(5) - .unwrap(); - let cond_str = guard.rewrite(context, cond_shape); - if let Some(cond_str) = cond_str { + if let Some(cond_shape) = shape + .shrink_left(pattern_width + 4) + .and_then(|s| s.sub_width(5)) { + if let Some(cond_str) = guard + .rewrite(context, cond_shape) + .and_then(|s| s.rewrite(context, cond_shape)) { return Some(format!(" if {}", cond_str)); } } // Not enough space to put the guard after the pattern, try a newline. - let overhead = shape.indent.block_indent(context.config).width() + 4 + 5; - if overhead < shape.width { - let cond_str = guard.rewrite(context, - Shape::legacy(shape.width - overhead, - // 3 == `if ` - shape.indent.block_indent(context.config) + - 3)); - if let Some(cond_str) = cond_str { + // 3 == `if ` + if let Some(cond_shape) = Shape::indented(shape.indent.block_indent(context.config) + 3, + context.config) + .sub_width(3) { + if let Some(cond_str) = guard.rewrite(context, cond_shape) { return Some(format!("\n{}if {}", shape .indent diff --git a/tests/target/configs-control_style-rfc.rs b/tests/target/configs-control_style-rfc.rs index a7213c34dfb..43a10e92339 100644 --- a/tests/target/configs-control_style-rfc.rs +++ b/tests/target/configs-control_style-rfc.rs @@ -21,3 +21,20 @@ fn main() { } } } + +fn issue1656() { + { + { + match rewrite { + Some(ref body_str) + if (!body_str.contains('\n') && body_str.len() <= arm_shape.width) || + !context.config.wrap_match_arms() || + (extend && first_line_width(body_str) <= arm_shape.width) || + is_block => { + return None; + } + _ => {} + } + } + } +} diff --git a/tests/target/match.rs b/tests/target/match.rs index 6acab043e67..45f62ca6845 100644 --- a/tests/target/match.rs +++ b/tests/target/match.rs @@ -306,8 +306,8 @@ fn guards() { barrrrrrrrrrrr => {} aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa if fooooooooooooooooooooo && - (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb || - cccccccccccccccccccccccccccccccccccccccc) => {} + (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb || cccccccccccccccccccccccccccccccccccccccc) => { + } } } -- cgit 1.4.1-3-g733a5 From ad628f6accd7024305c8a564fbdf177ba5068ba3 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Mon, 12 Jun 2017 18:34:38 +0900 Subject: Force multi line if the first attempt of rewriting args failed --- src/expr.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'src/expr.rs') diff --git a/src/expr.rs b/src/expr.rs index b47bf391ac1..cbe8671fd92 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1721,6 +1721,20 @@ fn rewrite_call_inner(context: &RewriteContext, nested_shape, one_line_width, force_trailing_comma) + .or_else(|| if context.use_block_indent() { + rewrite_call_args(context, + args, + args_span, + Shape::indented(shape + .block() + .indent + .block_indent(context.config), + context.config), + 0, + force_trailing_comma) + } else { + None + }) .ok_or(Ordering::Less)?; if !context.use_block_indent() && need_block_indent(&list_str, nested_shape) && !extendable { -- cgit 1.4.1-3-g733a5