diff --git a/src/expr.rs b/src/expr.rs index 56c8772dd9e..d66630e5179 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -124,7 +124,7 @@ fn rewrite_call(context: &RewriteContext, indent: offset, h_width: remaining_width, v_width: remaining_width, - is_expression: true, + ends_with_newline: true, }; Some(format!("{}({})", callee_str, write_list(&items, &fmt))) @@ -212,7 +212,7 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext, indent: indent, h_width: budget, v_width: budget, - is_expression: true, + ends_with_newline: true, }; let fields_str = write_list(&items, &fmt); Some(format!("{} {{ {} }}", path_str, fields_str)) @@ -267,7 +267,7 @@ fn rewrite_tuple_lit(context: &RewriteContext, indent: indent, h_width: width - 2, v_width: width - 2, - is_expression: true, + ends_with_newline: true, }; Some(format!("({})", write_list(&items, &fmt))) diff --git a/src/imports.rs b/src/imports.rs index 552e86f5261..de33be849ba 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -75,7 +75,7 @@ impl<'a> FmtVisitor<'a> { indent: block_indent + indent, h_width: remaining_line_budget, v_width: remaining_multi_budget, - is_expression: true, + ends_with_newline: true, }; // TODO handle any comments inbetween items. diff --git a/src/items.rs b/src/items.rs index 10a93c23033..17941dcd242 100644 --- a/src/items.rs +++ b/src/items.rs @@ -308,7 +308,7 @@ impl<'a> FmtVisitor<'a> { indent: arg_indent, h_width: one_line_budget, v_width: multi_line_budget, - is_expression: true, + ends_with_newline: true, }; write_list(&arg_items, &fmt) @@ -458,7 +458,7 @@ impl<'a> FmtVisitor<'a> { indent: indent, h_width: budget, v_width: budget, - is_expression: false, + ends_with_newline: false, }; result.push_str(&write_list(&items, &fmt)); result.push(')'); @@ -578,7 +578,7 @@ impl<'a> FmtVisitor<'a> { indent: offset + self.config.tab_spaces, h_width: self.config.max_width, v_width: budget, - is_expression: false, + ends_with_newline: false, }; result.push_str(&write_list(&items, &fmt)); @@ -728,7 +728,7 @@ impl<'a> FmtVisitor<'a> { indent: offset + 1, h_width: budget, v_width: budget, - is_expression: true, + ends_with_newline: true, }; result.push_str(&write_list(&items, &fmt)); @@ -772,7 +772,7 @@ impl<'a> FmtVisitor<'a> { indent: indent + 10, h_width: budget, v_width: budget, - is_expression: true, + ends_with_newline: true, }; result.push_str(&write_list(&items, &fmt)); diff --git a/src/lists.rs b/src/lists.rs index aefc93d7c86..adabb00566c 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -49,7 +49,7 @@ pub struct ListFormatting<'a> { pub v_width: usize, // Non-expressions, e.g. items, will have a new line at the end of the list. // Important for comment styles. - pub is_expression: bool + pub ends_with_newline: bool } pub struct ListItem { @@ -173,7 +173,9 @@ pub fn write_list<'b>(items: &[ListItem], formatting: &ListFormatting<'b>) -> St result.push_str(&rewrite_comment(comment, // Block style in non-vertical mode tactic != ListTactic::Vertical, - 1000, + // Width restriction is only + // relevant in vertical mode. + formatting.v_width, formatting.indent)); if tactic == ListTactic::Vertical { @@ -188,10 +190,9 @@ pub fn write_list<'b>(items: &[ListItem], formatting: &ListFormatting<'b>) -> St // Post-comments if tactic != ListTactic::Vertical && item.post_comment.is_some() { - // We'll assume it'll fit on one line at this point let formatted_comment = rewrite_comment(item.post_comment.as_ref().unwrap(), true, - 1000, + formatting.v_width, 0); result.push(' '); @@ -208,14 +209,11 @@ pub fn write_list<'b>(items: &[ListItem], formatting: &ListFormatting<'b>) -> St let offset = formatting.indent + item_width + 1; let comment = item.post_comment.as_ref().unwrap(); // Use block-style only for the last item or multiline comments. - let block_style = formatting.is_expression && last || + let block_style = formatting.ends_with_newline && last || comment.trim().contains('\n') || comment.trim().len() > width; - let formatted_comment = rewrite_comment(comment, - block_style, - width, - offset); + let formatted_comment = rewrite_comment(comment, block_style, width, offset); result.push(' '); result.push_str(&formatted_comment); diff --git a/tests/source/multiple.rs b/tests/source/multiple.rs index 2ba2b0f9930..04ecb2bc734 100644 --- a/tests/source/multiple.rs +++ b/tests/source/multiple.rs @@ -107,21 +107,3 @@ fn main() { let x = "Hello!!!!!!!!! abcd abcd abcd abcd abcd abcd\n abcd abcd abcd abcd abcd abcd abcd abcd abcd \ abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd \ abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd"; } - - -fn struct_lits() { - let x = Bar; - // Comment - let y = Foo { a: x }; - Foo { a: foo() /* comment*/, /* comment*/ b: bar(), ..something }; - Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { a: foo(), b: bar(), }; - Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { - // Comment - a: foo(), // Comment - // Comment - b: bar(), // Comment - }; - - Foo { a: Bar, - b: foo() }; -} diff --git a/tests/source/struct_lits.rs b/tests/source/struct_lits.rs new file mode 100644 index 00000000000..b7e1a854b0b --- /dev/null +++ b/tests/source/struct_lits.rs @@ -0,0 +1,37 @@ +// Struct literal expressions. + +fn main() { + let x = Bar; + + // Comment + let y = Foo {a: x }; + + Foo { a: foo() /* comment*/, /* comment*/ b: bar(), ..something }; + + Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { a: foo(), b: bar(), }; + + Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { + // Comment + a: foo(), // Comment + // Comment + b: bar(), // Comment + }; + + Foo { a:Bar, + b:foo() }; + + A { + // Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor. + first: item(), + // Praesent et diam eget libero egestas mattis sit amet vitae augue. + // Nam tincidunt congue enim, ut porta lorem lacinia consectetur. + second: Item + }; + + Diagram { /* o This graph demonstrates how + * / \ significant whitespace is + * o o preserved. + * /|\ \ + * o o o o */ + graph: G, } +} diff --git a/tests/target/multiple.rs b/tests/target/multiple.rs index ad6e1c085b3..0fb0c94e3a8 100644 --- a/tests/target/multiple.rs +++ b/tests/target/multiple.rs @@ -139,34 +139,3 @@ fn main() { abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd \ abcd"; } - - -fn struct_lits() { - let x = Bar; - // Comment - let y = Foo { a: x }; - Foo { a: foo(), // comment - // comment - b: bar(), - ..something }; - Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { a: foo(), - b: bar(), }; - Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { // Comment - a: foo(), /* C - * o - * m - * m - * e - * n - * t */ - // Comment - b: bar(), /* C - * o - * m - * m - * e - * n - * t */ }; - - Foo { a: Bar, b: foo() }; -} diff --git a/tests/target/struct_lits.rs b/tests/target/struct_lits.rs index 1d32171d7d0..c46909825a1 100644 --- a/tests/target/struct_lits.rs +++ b/tests/target/struct_lits.rs @@ -6,19 +6,45 @@ fn main() { // Comment let y = Foo { a: x }; - Foo { a: Bar, b: foo() }; + Foo { a: foo(), // comment + // comment + b: bar(), + ..something }; - Foo { a: foo(), b: bar(), ..something }; - - Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { a: foo(), b: bar() }; Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { a: foo(), b: bar(), }; - Fooooooooooooooooooooooooooooooooooooooooooooooooooooo { a: foo(), - b: bar(), - c: bar(), - d: bar(), - e: bar(), - f: bar(), - ..baz() }; + Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { // Comment + a: foo(), /* C + * o + * m + * m + * e + * n + * t */ + // Comment + b: bar(), /* C + * o + * m + * m + * e + * n + * t */ }; + + Foo { a: Bar, b: foo() }; + + A { // Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit + // amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante + // hendrerit. Donec et mollis dolor. + first: item(), + // Praesent et diam eget libero egestas mattis sit amet vitae augue. + // Nam tincidunt congue enim, ut porta lorem lacinia consectetur. + second: Item, }; + + Diagram { // o This graph demonstrates how + // / \ significant whitespace is + // o o preserved. + // /|\ \ + // o o o o + graph: G, } }