]> git.lizzy.rs Git - rust.git/commitdiff
chains: prefer to use the next line for an expression, if using the same line would...
authorNick Cameron <ncameron@mozilla.com>
Wed, 11 Jul 2018 09:01:40 +0000 (21:01 +1200)
committerNick Cameron <ncameron@mozilla.com>
Tue, 24 Jul 2018 03:43:29 +0000 (15:43 +1200)
This problem came to light due to the chains changes, but effects other code too. It only happens rarely, e.g.,

before this fix:
```
    match foo {
        MacroArgKind::Delimited(ref delim_tok, ref args) => rewrite_delimited_inner(
            delim_tok,
            args,
        ).map(|(lhs, inner, rhs)| format!("{}{}{}", lhs, inner, rhs)),
    };

```

after:
```
    match foo {
        MacroArgKind::Delimited(ref delim_tok, ref args) => {
            rewrite_delimited_inner(delim_tok, args)
                .map(|(lhs, inner, rhs)| format!("{}{}{}", lhs, inner, rhs))
        }
    }

```

src/chains.rs
src/expr.rs
src/utils.rs

index 81be8d54128705281b2cf35dca8c46f75a4864f0..d7126b2f5e798c5035ef4b6fb1678eb33f90e8e9 100644 (file)
@@ -398,8 +398,8 @@ fn format_last_child(&mut self, may_extend: bool, context: &RewriteContext, shap
         if all_in_one_line || extendable {
             // First we try to 'overflow' the last child and see if it looks better than using
             // vertical layout.
-            if let Some(shape) = last_shape.offset_left(almost_total) {
-                if let Some(rw) = last.rewrite_postfix(context, shape) {
+            if let Some(one_line_shape) = last_shape.offset_left(almost_total) {
+                if let Some(rw) = last.rewrite_postfix(context, one_line_shape) {
                     // We allow overflowing here only if both of the following conditions match:
                     // 1. The entire chain fits in a single line except the last child.
                     // 2. `last_child_str.lines().count() >= 5`.
@@ -413,6 +413,7 @@ fn format_last_child(&mut self, may_extend: bool, context: &RewriteContext, shap
                         // layout, just by looking at the overflowed rewrite. Now we rewrite the
                         // last child on its own line, and compare two rewrites to choose which is
                         // better.
+                        let last_shape = child_shape.sub_width(shape.rhs_overhead(context.config) + last.tries)?;
                         match last.rewrite_postfix(context, last_shape) {
                             Some(ref new_rw) if !could_fit_single_line => {
                                 last_subexpr_str = Some(new_rw.clone());
index 6e1af266e9bfe8e9dfb95470a6fc1e3edef01c5a..8aa5d9bd286448ed8fc9f0ba3ba3c6799bf5fab4 100644 (file)
@@ -39,9 +39,9 @@
 use string::{rewrite_string, StringFormat};
 use types::{can_be_overflowed_type, rewrite_path, PathContext};
 use utils::{
-    colon_spaces, contains_skip, count_newlines, first_line_width, inner_attributes,
-    last_line_extendable, last_line_width, mk_sp, outer_attributes, ptr_vec_to_ref_vec,
-    semicolon_for_stmt, wrap_str,
+    colon_spaces, contains_skip, count_newlines, first_line_ends_with, first_line_width,
+    inner_attributes, last_line_extendable, last_line_width, mk_sp, outer_attributes,
+    ptr_vec_to_ref_vec, semicolon_for_stmt, wrap_str,
 };
 use vertical::rewrite_with_alignment;
 use visitor::FmtVisitor;
@@ -1953,6 +1953,9 @@ pub fn prefer_next_line(orig_rhs: &str, next_line_rhs: &str, rhs_tactics: RhsTac
     rhs_tactics == RhsTactics::ForceNextLineWithoutIndent
         || !next_line_rhs.contains('\n')
         || count_newlines(orig_rhs) > count_newlines(next_line_rhs) + 1
+        || first_line_ends_with(orig_rhs, '(') && !first_line_ends_with(next_line_rhs, '(')
+        || first_line_ends_with(orig_rhs, '{') && !first_line_ends_with(next_line_rhs, '{')
+        || first_line_ends_with(orig_rhs, '[') && !first_line_ends_with(next_line_rhs, '[')
 }
 
 fn rewrite_expr_addrof(
index f1b0582b1200f539cc433c6751ee8ab0bea2bc95..b8474792cd3040117398a402512288736d5e97be 100644 (file)
@@ -382,6 +382,7 @@ pub fn colon_spaces(before: bool, after: bool) -> &'static str {
     }
 }
 
+#[inline]
 pub fn left_most_sub_expr(e: &ast::Expr) -> &ast::Expr {
     match e.node {
         ast::ExprKind::Call(ref e, _)
@@ -398,6 +399,12 @@ pub fn left_most_sub_expr(e: &ast::Expr) -> &ast::Expr {
     }
 }
 
+#[inline]
 pub fn starts_with_newline(s: &str) -> bool {
     s.starts_with('\n') || s.starts_with("\r\n")
 }
+
+#[inline]
+pub fn first_line_ends_with(s: &str, c: char) -> bool {
+    s.lines().next().map_or(false, |l| l.ends_with(c))
+}