]> git.lizzy.rs Git - rust.git/commitdiff
Reviewer comments - mostly fix rewrite_guard
authorNick Cameron <ncameron@mozilla.com>
Mon, 11 Apr 2016 22:30:57 +0000 (10:30 +1200)
committerNick Cameron <ncameron@mozilla.com>
Mon, 11 Apr 2016 22:30:57 +0000 (10:30 +1200)
src/expr.rs
src/utils.rs
tests/source/match.rs
tests/target/match.rs

index 8befc1adbe159b201ca3ee0c45efe298276814e3..7ee69885a429da8f58ab2b037e10b7e13821871b 100644 (file)
@@ -21,7 +21,7 @@
             DefinitiveListTactic, definitive_tactic, ListItem, format_item_list};
 use string::{StringFormat, rewrite_string};
 use utils::{CodeMapSpanUtils, extra_offset, last_line_width, wrap_str, binary_search,
-            first_line_width, semicolon_for_stmt};
+            first_line_width, semicolon_for_stmt, trimmed_last_line_width};
 use visitor::FmtVisitor;
 use config::{Config, StructLitStyle, MultilineStyle};
 use comment::{FindUncommented, rewrite_comment, contains_comment, recover_comment_removed};
@@ -998,7 +998,7 @@ fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Opt
         let pats_str = try_opt!(write_list(items, &fmt));
 
         let budget = if pats_str.contains('\n') {
-            context.config.max_width
+            context.config.max_width - offset.width()
         } else {
             width
         };
@@ -1007,7 +1007,7 @@ fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Opt
                                                guard,
                                                budget,
                                                offset,
-                                               last_line_width(&pats_str)));
+                                               trimmed_last_line_width(&pats_str)));
 
         let pats_str = format!("{}{}", pats_str, guard_str);
         // Where the next text can start.
@@ -1019,7 +1019,7 @@ fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Opt
         let body = match **body {
             ast::Expr { node: ast::ExprKind::Block(ref block), .. }
                 if !is_unsafe_block(block) && is_simple_block(block, context.codemap) &&
-                context.config.wrap_match_arms => block.expr.as_ref().map(|e| &**e).unwrap(),
+                   context.config.wrap_match_arms => block.expr.as_ref().map(|e| &**e).unwrap(),
             ref x => x,
         };
 
@@ -1081,6 +1081,8 @@ fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Opt
     }
 }
 
+// A pattern is simple if it is very short or it is short-ish and just a path.
+// E.g. `Foo::Bar` is simple, but `Foo(..)` is not.
 fn pat_is_simple(pat_str: &str) -> bool {
     pat_str.len() <= 16 ||
     (pat_str.len() <= 24 && pat_str.chars().all(|c| c.is_alphabetic() || c == ':'))
@@ -1107,11 +1109,12 @@ fn rewrite_guard(context: &RewriteContext,
         }
 
         // Not enough space to put the guard after the pattern, try a newline.
-        let overhead = context.config.tab_spaces + 4 + 5;
+        let overhead = offset.block_indent(context.config).width() + 4 + 5;
         if overhead < width {
             let cond_str = guard.rewrite(context,
                                          width - overhead,
-                                         offset.block_indent(context.config));
+                                         // 3 == `if `
+                                         offset.block_indent(context.config) + 3);
             if let Some(cond_str) = cond_str {
                 return Some(format!("\n{}if {}",
                                     offset.block_indent(context.config).to_string(context.config),
index 66b9880777f94cb16a4c69942f46f1025f0b48dc..862ceb1e13eb0974c4497e3a576f7f24060b08d3 100644 (file)
@@ -113,6 +113,13 @@ pub fn last_line_width(s: &str) -> usize {
         None => s.len(),
     }
 }
+#[inline]
+pub fn trimmed_last_line_width(s: &str) -> usize {
+    match s.rfind('\n') {
+        Some(n) => s[(n + 1)..].trim().len(),
+        None => s.trim().len(),
+    }
+}
 
 #[inline]
 fn is_skip(meta_item: &MetaItem) -> bool {
index 6e8418f66d2d5acb3c128fabc9b8f7938e5587a5..a9fb540404729e6b001f7f6722a7514400a971ef 100644 (file)
@@ -284,3 +284,13 @@ fn issue386() {
             false,
     }
 }
+
+fn guards() {
+    match foo {
+        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa if foooooooooooooo && barrrrrrrrrrrr => {}
+        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa if foooooooooooooo && barrrrrrrrrrrr => {}
+        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+            if fooooooooooooooooooooo &&
+               (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb || cccccccccccccccccccccccccccccccccccccccc) => {}
+    }
+}
index 3bc675aa3d9277cfc177cf62365997ce260358cc..bc5556283c37f778f68fd9bbfccc4d3899305786 100644 (file)
@@ -295,3 +295,17 @@ fn issue386() {
         BiShl | BiShr => false,
     }
 }
+
+fn guards() {
+    match foo {
+        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa if foooooooooooooo &&
+                                                                      barrrrrrrrrrrr => {}
+        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |
+        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa if foooooooooooooo &&
+                                                                      barrrrrrrrrrrr => {}
+        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+            if fooooooooooooooooooooo &&
+               (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb ||
+                cccccccccccccccccccccccccccccccccccccccc) => {}
+    }
+}