]> git.lizzy.rs Git - rust.git/commitdiff
Block format control flow discriminant expressions and binops
authorNick Cameron <ncameron@mozilla.com>
Fri, 5 May 2017 02:29:18 +0000 (14:29 +1200)
committerNick Cameron <ncameron@mozilla.com>
Fri, 5 May 2017 02:37:13 +0000 (14:37 +1200)
Fixes #1450

Adds control_style option

rfc-rustfmt.toml
src/config.rs
src/expr.rs
src/lib.rs
tests/source/expr-block.rs
tests/target/expr-block.rs

index 914d19d6cb233915c04a7838c79e74d16003bf97..0e622bdd9434fd40a1a3d7a1f3e283a99102639b 100644 (file)
@@ -1,5 +1,6 @@
 fn_args_layout = "Block"
 array_layout = "Block"
+control_style = "Rfc"
 where_style = "Rfc"
 generics_indent = "Block"
 fn_call_style = "Block"
index f0f2d52930825f72e285002a881cca09731787fb..805581abb4ef34087544142fb6ca03a730883715 100644 (file)
@@ -338,6 +338,7 @@ fn default() -> Config {
     newline_style: NewlineStyle, NewlineStyle::Unix, "Unix or Windows line endings";
     fn_brace_style: BraceStyle, BraceStyle::SameLineWhere, "Brace style for functions";
     item_brace_style: BraceStyle, BraceStyle::SameLineWhere, "Brace style for structs and enums";
+    control_style: Style, Style::Default, "Indent style for control flow statements";
     control_brace_style: ControlBraceStyle, ControlBraceStyle::AlwaysSameLine,
         "Brace style for control flow constructs";
     impl_empty_single_line: bool, true, "Put empty-body implementations on a single line";
index 5876409a9fa22e95c488dae8a1cb399cd4e746e8..bb4caaa7b935485b8c1b636e6997abde5b30c282 100644 (file)
@@ -26,7 +26,7 @@
             semicolon_for_stmt, trimmed_last_line_width, left_most_sub_expr, stmt_expr,
             colon_spaces};
 use visitor::FmtVisitor;
-use config::{Config, IndentStyle, MultilineStyle, ControlBraceStyle};
+use config::{Config, IndentStyle, MultilineStyle, ControlBraceStyle, Style};
 use comment::{FindUncommented, rewrite_comment, contains_comment, recover_comment_removed};
 use types::{rewrite_path, PathContext};
 use items::{span_lo_for_arg, span_hi_for_arg};
@@ -314,8 +314,12 @@ pub fn rewrite_pair<LHS, RHS>(lhs: &LHS,
                                   .max_width
                                   .checked_sub(shape.used_width() + prefix.len() +
                                                infix.len()));
-    let rhs_shape = try_opt!(shape.sub_width(suffix.len() + prefix.len()))
-        .visual_indent(prefix.len());
+    let rhs_shape = match context.config.control_style {
+        Style::Default => {
+            try_opt!(shape.sub_width(suffix.len() + prefix.len())).visual_indent(prefix.len())
+        }
+        Style::Rfc => try_opt!(shape.block_left(context.config.tab_spaces)),
+    };
 
     let rhs_result = try_opt!(rhs.rewrite(context, rhs_shape));
     let lhs_result = try_opt!(lhs.rewrite(context,
@@ -884,7 +888,10 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
 
         let pat_expr_string = match self.cond {
             Some(cond) => {
-                let mut cond_shape = try_opt!(constr_shape.shrink_left(add_offset));
+                let mut cond_shape = match context.config.control_style {
+                    Style::Default => try_opt!(constr_shape.shrink_left(add_offset)),
+                    Style::Rfc => constr_shape,
+                };
                 if context.config.control_brace_style != ControlBraceStyle::AlwaysNextLine {
                     // 2 = " {".len()
                     cond_shape = try_opt!(cond_shape.sub_width(2));
@@ -900,6 +907,9 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
             None => String::new(),
         };
 
+        let force_newline_brace = context.config.control_style == Style::Rfc &&
+                                  pat_expr_string.contains('\n');
+
         // Try to format if-else on single line.
         if self.allow_single_line && context.config.single_line_if_else_max_width > 0 {
             let trial = self.rewrite_single_line(&pat_expr_string, context, shape.width);
@@ -957,8 +967,8 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
                             &shape.indent.block_only().to_string(context.config);
         let block_sep = if self.cond.is_none() && between_kwd_cond_comment.is_some() {
             ""
-        } else if context.config.control_brace_style ==
-                  ControlBraceStyle::AlwaysNextLine {
+        } else if context.config.control_brace_style == ControlBraceStyle::AlwaysNextLine ||
+                  force_newline_brace {
             alt_block_sep.as_str()
         } else {
             " "
@@ -1494,7 +1504,7 @@ fn rewrite_pat_expr(context: &RewriteContext,
                     connector: &str,
                     shape: Shape)
                     -> Option<String> {
-    debug!("rewrite_pat_expr {:?} {:?}", shape, pat);
+    debug!("rewrite_pat_expr {:?} {:?} {:?}", shape, pat, expr);
     let mut result = match pat {
         Some(pat) => {
             let matcher = if matcher.is_empty() {
index 6935f0f7acdf1fb582afed4ef835c2396235e939..a3f1a93ca48886a02c1f682d4c74cae78f716f04 100644 (file)
@@ -287,6 +287,14 @@ pub fn block_indent(&self, extra_width: usize) -> Shape {
         }
     }
 
+    pub fn block_left(&self, width: usize) -> Option<Shape> {
+        let block_shape = self.block_indent(width);
+        Some(Shape {
+                 width: try_opt!(block_shape.width.checked_sub(width)),
+                 ..block_shape
+             })
+    }
+
     pub fn add_offset(&self, extra_width: usize) -> Shape {
         Shape {
             width: self.width,
index d7f7c610eb34f3a8bfc0edcd37bc3471b7908442..ad959f8ee0172561b3cf078fbddf6cccb5e6fbcf 100644 (file)
@@ -1,5 +1,6 @@
 // rustfmt-array_layout: Block
 // rustfmt-fn_call_style: Block
+// rustfmt-control_style: Rfc
 // Test expressions with block formatting.
 
 fn arrays() {
@@ -120,3 +121,25 @@ fn macros() {
         Some(p) => baz!(one_item_macro_as_expression_which_is_also_loooooooooooooooong),
     };
 }
+
+fn issue_1450() {
+    if selfstate
+        .compare_exchandsfasdsdfgsdgsdfgsdfgsdfgsdfgsdfgfsfdsage_weak(
+            STATE_PARKED,
+            STATE_UNPARKED,
+            Release,
+            Relaxed,
+            Release,
+            Relaxed,
+        )
+        .is_ok() {
+        return;
+    }
+}
+
+fn foo() {
+    if real_total <= limit && !pre_line_comments &&
+            !items.into_iter().any(|item| item.as_ref().is_multiline()) {
+         DefinitiveListTactic::Horizontal
+    }
+}
index f7a157cf3df509246097d31a4853ab7e42403f2e..349f2a00c8484bf51db4bee4f95aa6e1bab93226 100644 (file)
@@ -1,5 +1,6 @@
 // rustfmt-array_layout: Block
 // rustfmt-fn_call_style: Block
+// rustfmt-control_style: Rfc
 // Test expressions with block formatting.
 
 fn arrays() {
@@ -188,3 +189,27 @@ fn macros() {
         Some(p) => baz!(one_item_macro_as_expression_which_is_also_loooooooooooooooong),
     };
 }
+
+fn issue_1450() {
+    if selfstate
+        .compare_exchandsfasdsdfgsdgsdfgsdfgsdfgsdfgsdfgfsfdsage_weak(
+            STATE_PARKED,
+            STATE_UNPARKED,
+            Release,
+            Relaxed,
+            Release,
+            Relaxed,
+        )
+        .is_ok()
+    {
+        return;
+    }
+}
+
+fn foo() {
+    if real_total <= limit && !pre_line_comments &&
+        !items.into_iter().any(|item| item.as_ref().is_multiline())
+    {
+        DefinitiveListTactic::Horizontal
+    }
+}