Fixup comment wrapping in lists

This commit is contained in:
Marcus Klaas 2015-06-24 21:14:08 +02:00
parent 9f3ab0b5fe
commit 28f7bd4205
8 changed files with 90 additions and 78 deletions

View File

@ -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)))

View File

@ -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.

View File

@ -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));

View File

@ -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);

View File

@ -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() };
}

View File

@ -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, }
}

View File

@ -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() };
}

View File

@ -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, }
}