about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorNick Cameron <ncameron@mozilla.com>2017-05-05 14:29:18 +1200
committerNick Cameron <ncameron@mozilla.com>2017-05-05 14:37:13 +1200
commit75a13868dab681dfc31fbe31d76fff413bfe7c9e (patch)
treedf52949000a27dfb3cce0362d26ccb5a8d3809a8 /src
parent21cbb8d6e4f0442568968f4c8b83288e1be9b23d (diff)
Block format control flow discriminant expressions and binops
Fixes #1450

Adds control_style option
Diffstat (limited to 'src')
-rw-r--r--src/config.rs1
-rw-r--r--src/expr.rs24
-rw-r--r--src/lib.rs8
3 files changed, 26 insertions, 7 deletions
diff --git a/src/config.rs b/src/config.rs
index f0f2d529308..805581abb4e 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -338,6 +338,7 @@ create_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";
diff --git a/src/expr.rs b/src/expr.rs
index 5876409a9fa..bb4caaa7b93 100644
--- a/src/expr.rs
+++ b/src/expr.rs
@@ -26,7 +26,7 @@ use utils::{extra_offset, last_line_width, wrap_str, binary_search, first_line_w
             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 @@ impl<'a> Rewrite for ControlFlow<'a> {
 
         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 @@ impl<'a> Rewrite for ControlFlow<'a> {
             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 @@ impl<'a> Rewrite for ControlFlow<'a> {
                             &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() {
diff --git a/src/lib.rs b/src/lib.rs
index 6935f0f7acd..a3f1a93ca48 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -287,6 +287,14 @@ impl 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,