]> git.lizzy.rs Git - rust.git/commitdiff
Fix indentation in multi lined pattern
authorSeiichi Uchida <seuchida@gmail.com>
Sun, 10 Dec 2017 14:54:34 +0000 (23:54 +0900)
committerSeiichi Uchida <seuchida@gmail.com>
Sun, 10 Dec 2017 14:54:34 +0000 (23:54 +0900)
src/expr.rs

index e406792332d89b934d095cd5187ce138a6fdd10b..bb4b71f2963fe2baee82ba220eca6dfdb10a4b4e 100644 (file)
@@ -937,21 +937,16 @@ fn rewrite_cond(
         let offset = self.keyword.len() + label_string.len() + 1;
 
         let pat_expr_string = match self.cond {
-            Some(cond) => {
-                let cond_shape = match context.config.indent_style() {
-                    IndentStyle::Visual => constr_shape.shrink_left(offset)?,
-                    IndentStyle::Block => constr_shape.offset_left(offset)?,
-                };
-                rewrite_pat_expr(
-                    context,
-                    self.pat,
-                    cond,
-                    self.matcher,
-                    self.connector,
-                    self.keyword,
-                    cond_shape,
-                )?
-            }
+            Some(cond) => rewrite_pat_expr(
+                context,
+                self.pat,
+                cond,
+                self.matcher,
+                self.connector,
+                self.keyword,
+                constr_shape,
+                offset,
+            )?,
             None => String::new(),
         };
 
@@ -967,8 +962,8 @@ fn rewrite_cond(
             .max_width()
             .checked_sub(constr_shape.used_width() + offset + brace_overhead)
             .unwrap_or(0);
-        let force_newline_brace = context.config.indent_style() == IndentStyle::Block
-            && (pat_expr_string.contains('\n') || pat_expr_string.len() > one_line_budget)
+        let force_newline_brace = (pat_expr_string.contains('\n')
+            || pat_expr_string.len() > one_line_budget)
             && !last_line_extendable(&pat_expr_string);
 
         // Try to format if-else on single line.
@@ -1704,23 +1699,25 @@ fn rewrite_pat_expr(
     connector: &str,
     keyword: &str,
     shape: Shape,
+    offset: usize,
 ) -> Option<String> {
     debug!("rewrite_pat_expr {:?} {:?} {:?}", shape, pat, expr);
+    let cond_shape = shape.offset_left(offset)?;
     if let Some(pat) = pat {
         let matcher = if matcher.is_empty() {
             matcher.to_owned()
         } else {
             format!("{} ", matcher)
         };
-        let pat_shape = shape
+        let pat_shape = cond_shape
             .offset_left(matcher.len())?
             .sub_width(connector.len())?;
         let pat_string = pat.rewrite(context, pat_shape)?;
         let result = format!("{}{}{}", matcher, pat_string, connector);
-        return rewrite_assign_rhs(context, result, expr, shape);
+        return rewrite_assign_rhs(context, result, expr, cond_shape);
     }
 
-    let expr_rw = expr.rewrite(context, shape);
+    let expr_rw = expr.rewrite(context, cond_shape);
     // The expression may (partially) fit on the current line.
     // We do not allow splitting between `if` and condition.
     if keyword == "if" || expr_rw.is_some() {