]> git.lizzy.rs Git - rust.git/commitdiff
Merge pull request #370 from marcusklaas/match-arm-delining
authorMarcus Klaas de Vries <mail@marcusklaas.nl>
Sun, 27 Sep 2015 10:00:57 +0000 (12:00 +0200)
committerMarcus Klaas de Vries <mail@marcusklaas.nl>
Sun, 27 Sep 2015 10:00:57 +0000 (12:00 +0200)
Improve heuristics for match arm body placement

12 files changed:
src/comment.rs
src/config.rs
src/expr.rs
src/issues.rs
src/lib.rs
src/lists.rs
src/rustfmt_diff.rs
src/visitor.rs
tests/source/struct_lits.rs
tests/source/struct_lits_visual.rs
tests/target/struct_lits.rs
tests/target/struct_lits_visual.rs

index f5ba982ecc0e0cce4ed50cfc63f878cdb033092e..b08e46681a7d027fc0f4f06ab99a54708c9f7936 100644 (file)
@@ -212,7 +212,10 @@ enum CodeCharKind {
 
 impl<T> CharClasses<T> where T: Iterator, T::Item: RichChar {
     fn new(base: T) -> CharClasses<T> {
-        CharClasses { base: base.peekable(), status: CharClassesStatus::Normal }
+        CharClasses {
+            base: base.peekable(),
+            status: CharClassesStatus::Normal,
+        }
     }
 }
 
index 4779d0e9b98c704e2ea3452046b0eed5990f319f..020761744820ec9a977bb5aa6fd81710a0ca916c 100644 (file)
@@ -218,8 +218,10 @@ pub fn get_docs() -> Vec<ConfigHelpItem> {
     ideal_width: usize, "Ideal width of each line (only used for comments)",
     leeway: usize, "Leeway of line width (deprecated)",
     tab_spaces: usize, "Number of spaces per tab",
-    list_width: usize, "Maximum width in a struct literal or function \
-                        call before faling back to vertical formatting",
+    fn_call_width: usize, "Maximum width of the args of a function call\
+                           before faling back to vertical formatting",
+    struct_lit_width: usize, "Maximum width in the body of a struct lit\
+                              before faling back to vertical formatting",
     newline_style: NewlineStyle, "Unix or Windows line endings",
     fn_brace_style: BraceStyle, "Brace style for functions",
     fn_return_indent: ReturnIndent, "Location of return type in function declaration",
@@ -258,7 +260,8 @@ fn default() -> Config {
             ideal_width: 80,
             leeway: 5,
             tab_spaces: 4,
-            list_width: 50,
+            fn_call_width: 50,
+            struct_lit_width: 12,
             newline_style: NewlineStyle::Unix,
             fn_brace_style: BraceStyle::SameLineWhere,
             fn_return_indent: ReturnIndent::WithArgs,
index 38305fc7d62e6e0a782dbb4ef7fb897d3ee15685..4076298cec8fb324650af1aef87aecdc4f4a9aab 100644 (file)
@@ -1251,11 +1251,15 @@ enum StructLitField<'a> {
                              span_after(span, "{", context.codemap),
                              span.hi);
 
-    let tactic = match (context.config.struct_lit_style, fields.len()) {
+    let mut tactic = match (context.config.struct_lit_style, fields.len()) {
         (StructLitStyle::Visual, 1) => ListTactic::HorizontalVertical,
         _ => context.config.struct_lit_multiline_style.to_list_tactic(),
     };
 
+    if tactic == ListTactic::HorizontalVertical && fields.len() > 1 {
+        tactic = ListTactic::LimitedHorizontalVertical(context.config.struct_lit_width);
+    }
+
     let fmt = ListFormatting {
         tactic: tactic,
         separator: ",",
index 90123cf63e158c115063a1044f8bada76a021849..2a5eaae287a16cec3ac8f98fb43e272c6245ee0d 100644 (file)
@@ -101,7 +101,10 @@ pub struct BadIssueSeeker {
 impl BadIssueSeeker {
     pub fn new(report_todo: ReportTactic, report_fixme: ReportTactic) -> BadIssueSeeker {
         BadIssueSeeker {
-            state: Seeking::Issue { todo_idx: 0, fixme_idx: 0 },
+            state: Seeking::Issue {
+                todo_idx: 0,
+                fixme_idx: 0,
+            },
             report_todo: report_todo,
             report_fixme: report_fixme,
         }
@@ -121,7 +124,10 @@ pub fn inspect(&mut self, c: char) -> Option<Issue> {
                     return None;
                 }
 
-                self.state = Seeking::Issue { todo_idx: 0, fixme_idx: 0 };
+                self.state = Seeking::Issue {
+                    todo_idx: 0,
+                    fixme_idx: 0,
+                };
 
                 if let IssueClassification::Bad(issue) = result {
                     return Some(issue);
@@ -173,7 +179,10 @@ fn inspect_issue(&mut self, c: char, mut todo_idx: usize, mut fixme_idx: usize)
             fixme_idx = 0;
         }
 
-        Seeking::Issue { todo_idx: todo_idx, fixme_idx: fixme_idx }
+        Seeking::Issue {
+            todo_idx: todo_idx,
+            fixme_idx: fixme_idx,
+        }
     }
 
     fn inspect_number(&mut self,
@@ -214,7 +223,10 @@ fn inspect_number(&mut self,
             NumberPart::CloseParen => {}
         }
 
-        self.state = Seeking::Number { part: part, issue: issue };
+        self.state = Seeking::Number {
+            part: part,
+            issue: issue,
+        };
 
         IssueClassification::None
     }
@@ -274,7 +286,10 @@ fn is_bad_issue(text: &str, report_todo: ReportTactic, report_fixme: ReportTacti
 #[test]
 fn issue_type() {
     let mut seeker = BadIssueSeeker::new(ReportTactic::Always, ReportTactic::Never);
-    let expected = Some(Issue { issue_type: IssueType::Todo, missing_number: false });
+    let expected = Some(Issue {
+        issue_type: IssueType::Todo,
+        missing_number: false,
+    });
 
     assert_eq!(expected,
                "TODO(#100): more awesomeness"
@@ -284,7 +299,10 @@ fn issue_type() {
                    .unwrap());
 
     let mut seeker = BadIssueSeeker::new(ReportTactic::Never, ReportTactic::Unnumbered);
-    let expected = Some(Issue { issue_type: IssueType::Fixme, missing_number: true });
+    let expected = Some(Issue {
+        issue_type: IssueType::Fixme,
+        missing_number: true,
+    });
 
     assert_eq!(expected,
                "Test. FIXME: bad, bad, not good"
index b483cae60d4bc2e68f2804345fb3eae45494dbc3..fdf08c001a3f568286abd8473a60d327bd1d244c 100644 (file)
@@ -92,7 +92,10 @@ pub struct Indent {
 
 impl Indent {
     pub fn new(block_indent: usize, alignment: usize) -> Indent {
-        Indent { block_indent: block_indent, alignment: alignment }
+        Indent {
+            block_indent: block_indent,
+            alignment: alignment,
+        }
     }
 
     pub fn empty() -> Indent {
@@ -304,7 +307,10 @@ pub fn fmt_lines(file_map: &mut FileMap, config: &Config) -> FormatReport {
 
             // Add warnings for bad todos/ fixmes
             if let Some(issue) = issue_seeker.inspect(c) {
-                errors.push(FormattingError { line: cur_line, kind: ErrorKind::BadIssue(issue) });
+                errors.push(FormattingError {
+                    line: cur_line,
+                    kind: ErrorKind::BadIssue(issue),
+                });
             }
 
             if c == '\n' {
@@ -315,7 +321,10 @@ pub fn fmt_lines(file_map: &mut FileMap, config: &Config) -> FormatReport {
                 }
                 // Check for any line width errors we couldn't correct.
                 if line_len > config.max_width {
-                    errors.push(FormattingError { line: cur_line, kind: ErrorKind::LineOverflow });
+                    errors.push(FormattingError {
+                        line: cur_line,
+                        kind: ErrorKind::LineOverflow,
+                    });
                 }
                 line_len = 0;
                 cur_line += 1;
@@ -340,7 +349,10 @@ pub fn fmt_lines(file_map: &mut FileMap, config: &Config) -> FormatReport {
         }
 
         for &(l, _, _) in &trims {
-            errors.push(FormattingError { line: l, kind: ErrorKind::TrailingWhitespace });
+            errors.push(FormattingError {
+                line: l,
+                kind: ErrorKind::TrailingWhitespace,
+            });
         }
 
         report.file_error_map.insert(f.to_owned(), errors);
@@ -395,7 +407,10 @@ pub fn format(args: Vec<String>, config: &Config) -> FileMap {
 
     {
         let config = Rc::new(config.clone());
-        let mut call_ctxt = RustFmtCalls { config: config, result: result.clone() };
+        let mut call_ctxt = RustFmtCalls {
+            config: config,
+            result: result.clone(),
+        };
         rustc_driver::run_compiler(&args, &mut call_ctxt);
     }
 
index ef3372bf0b6002f5d77d71769ceddc9da27d478d..12eebb7b6918cf73f45d59f479482fc03aff2976 100644 (file)
@@ -62,7 +62,7 @@ pub struct ListFormatting<'a> {
 impl<'a> ListFormatting<'a> {
     pub fn for_fn(width: usize, offset: Indent, config: &'a Config) -> ListFormatting<'a> {
         ListFormatting {
-            tactic: ListTactic::LimitedHorizontalVertical(config.list_width),
+            tactic: ListTactic::LimitedHorizontalVertical(config.fn_call_width),
             separator: ",",
             trailing_separator: SeparatorTactic::Never,
             indent: offset,
@@ -107,7 +107,12 @@ pub fn has_line_pre_comment(&self) -> bool {
     }
 
     pub fn from_str<S: Into<String>>(s: S) -> ListItem {
-        ListItem { pre_comment: None, item: s.into(), post_comment: None, new_lines: false }
+        ListItem {
+            pre_comment: None,
+            item: s.into(),
+            post_comment: None,
+            new_lines: false,
+        }
     }
 }
 
index 375cfd661fa37a9a3e1eaddc1a8596287f393f14..8c20b8f5a0f0a68d7dc7a9f5be65e61af8ce56bc 100644 (file)
@@ -15,7 +15,10 @@ pub struct Mismatch {
 
 impl Mismatch {
     fn new(line_number: u32) -> Mismatch {
-        Mismatch { line_number: line_number, lines: Vec::new() }
+        Mismatch {
+            line_number: line_number,
+            lines: Vec::new(),
+        }
     }
 }
 
index 5faff9e6d111ff0302c166e1d936f3d7be94b76d..883d46a22d43ff8120b09f518c929a85f95580c2 100644 (file)
@@ -275,7 +275,10 @@ pub fn from_codemap<'b>(codemap: &'b CodeMap, config: &'b Config) -> FmtVisitor<
             codemap: codemap,
             buffer: StringBuffer::new(),
             last_pos: BytePos(0),
-            block_indent: Indent { block_indent: 0, alignment: 0 },
+            block_indent: Indent {
+                block_indent: 0,
+                alignment: 0,
+            },
             config: config,
         }
     }
index 69f90879164eab1277df2a9c765286b140d47127..aecbc285fc6ed529eca0af2db6411d734e6db55e 100644 (file)
@@ -8,9 +8,9 @@ fn main() {
 
     Foo { a: foo() /* comment*/, /* comment*/ b: bar(), ..something };
 
-    Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { a: foo(), b: bar(), };
+    Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { a: f(), b: b(), };
 
-    Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { a: foo(), b: bar(), };
+    Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { a: f(), b: b(), };
 
     Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo {
         // Comment
@@ -20,7 +20,7 @@ fn main() {
     };
 
     Foo { a:Bar,
-          b:foo() };
+          b:f() };
 
     Quux { x: if cond { bar(); }, y: baz() };
 
@@ -91,3 +91,11 @@ fn struct_exprs() {
     LoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongStruct { ..base };
     IntrinsicISizesContribution { content_intrinsic_sizes: IntrinsicISizes { minimum_inline_size: 0, }, };
 }
+
+fn issue123() {
+    Foo { a: b, c: d, e: f };
+
+    Foo { a: bb, c: dd, e: ff };
+
+    Foo { a: ddddddddddddddddddddd, b: cccccccccccccccccccccccccccccccccccccc };
+}
index 180a1229faa58ec60e0b38e7512f0f4d421202b6..6b78df7e0f3da963601b70bd763d9830d900310c 100644 (file)
@@ -10,7 +10,7 @@ fn main() {
 
     Foo { a: foo() /* comment*/, /* comment*/ b: bar(), ..something };
 
-    Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { a: foo(), b: bar(), };
+    Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { a: f(), b: b(), };
 
     Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo {
         // Comment
@@ -20,7 +20,7 @@ fn main() {
     };
 
     Foo { a:Bar,
-          b:foo() };
+          b:f() };
 
     Quux { x: if cond { bar(); }, y: baz() };
 
index ac7b8aaaeab08cc46fd7b71847d23bb39531e8ea..66fb4925042dfe4366b6125e6646a52470468e6f 100644 (file)
@@ -13,11 +13,11 @@ fn main() {
         ..something
     };
 
-    Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { a: foo(), b: bar() };
+    Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { a: f(), b: b() };
 
-    Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo {
-        a: foo(),
-        b: bar(),
+    Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo {
+        a: f(),
+        b: b(),
     };
 
     Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo {
@@ -27,7 +27,7 @@ fn main() {
         b: bar(), /* Comment */
     };
 
-    Foo { a: Bar, b: foo() };
+    Foo { a: Bar, b: f() };
 
     Quux {
         x: if cond {
@@ -65,7 +65,10 @@ fn main() {
 
 fn matcher() {
     TagTerminatedByteMatcher {
-        matcher: ByteMatcher { pattern: b"<HTML", mask: b"\xFF\xDF\xDF\xDF\xDF\xFF" },
+        matcher: ByteMatcher {
+            pattern: b"<HTML",
+            mask: b"\xFF\xDF\xDF\xDF\xDF\xFF",
+        },
     };
 }
 
@@ -101,7 +104,11 @@ fn issue278() {
 
 fn struct_exprs() {
     Foo { a: 1, b: f(2) };
-    Foo { a: 1, b: f(2), ..g(3) };
+    Foo {
+        a: 1,
+        b: f(2),
+        ..g(3)
+    };
     LoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongStruct {
         ..base
     };
@@ -109,3 +116,18 @@ fn struct_exprs() {
         content_intrinsic_sizes: IntrinsicISizes { minimum_inline_size: 0 },
     };
 }
+
+fn issue123() {
+    Foo { a: b, c: d, e: f };
+
+    Foo {
+        a: bb,
+        c: dd,
+        e: ff,
+    };
+
+    Foo {
+        a: ddddddddddddddddddddd,
+        b: cccccccccccccccccccccccccccccccccccccc,
+    };
+}
index 4274c9167773c80e9271787ac059e83b2c5deb93..86812c147a15609eeca3c9f0ef45855c299b00e0 100644 (file)
@@ -13,7 +13,7 @@ fn main() {
           b: bar(),
           ..something };
 
-    Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { a: foo(), b: bar() };
+    Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { a: f(), b: b() };
 
     Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { // Commen
                                                                                         // t
@@ -34,7 +34,7 @@ fn main() {
                                                                                                    * n
                                                                                                    * t */ };
 
-    Foo { a: Bar, b: foo() };
+    Foo { a: Bar, b: f() };
 
     Quux { x: if cond {
                bar();