Skip to content

Commit 28f7bd4

Browse files
committed
Fixup comment wrapping in lists
1 parent 9f3ab0b commit 28f7bd4

File tree

8 files changed

+90
-78
lines changed

8 files changed

+90
-78
lines changed

src/expr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ fn rewrite_call(context: &RewriteContext,
124124
indent: offset,
125125
h_width: remaining_width,
126126
v_width: remaining_width,
127-
is_expression: true,
127+
ends_with_newline: true,
128128
};
129129

130130
Some(format!("{}({})", callee_str, write_list(&items, &fmt)))
@@ -212,7 +212,7 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext,
212212
indent: indent,
213213
h_width: budget,
214214
v_width: budget,
215-
is_expression: true,
215+
ends_with_newline: true,
216216
};
217217
let fields_str = write_list(&items, &fmt);
218218
Some(format!("{} {{ {} }}", path_str, fields_str))
@@ -267,7 +267,7 @@ fn rewrite_tuple_lit(context: &RewriteContext,
267267
indent: indent,
268268
h_width: width - 2,
269269
v_width: width - 2,
270-
is_expression: true,
270+
ends_with_newline: true,
271271
};
272272

273273
Some(format!("({})", write_list(&items, &fmt)))

src/imports.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl<'a> FmtVisitor<'a> {
7575
indent: block_indent + indent,
7676
h_width: remaining_line_budget,
7777
v_width: remaining_multi_budget,
78-
is_expression: true,
78+
ends_with_newline: true,
7979
};
8080

8181
// TODO handle any comments inbetween items.

src/items.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ impl<'a> FmtVisitor<'a> {
308308
indent: arg_indent,
309309
h_width: one_line_budget,
310310
v_width: multi_line_budget,
311-
is_expression: true,
311+
ends_with_newline: true,
312312
};
313313

314314
write_list(&arg_items, &fmt)
@@ -458,7 +458,7 @@ impl<'a> FmtVisitor<'a> {
458458
indent: indent,
459459
h_width: budget,
460460
v_width: budget,
461-
is_expression: false,
461+
ends_with_newline: false,
462462
};
463463
result.push_str(&write_list(&items, &fmt));
464464
result.push(')');
@@ -578,7 +578,7 @@ impl<'a> FmtVisitor<'a> {
578578
indent: offset + self.config.tab_spaces,
579579
h_width: self.config.max_width,
580580
v_width: budget,
581-
is_expression: false,
581+
ends_with_newline: false,
582582
};
583583

584584
result.push_str(&write_list(&items, &fmt));
@@ -728,7 +728,7 @@ impl<'a> FmtVisitor<'a> {
728728
indent: offset + 1,
729729
h_width: budget,
730730
v_width: budget,
731-
is_expression: true,
731+
ends_with_newline: true,
732732
};
733733
result.push_str(&write_list(&items, &fmt));
734734

@@ -772,7 +772,7 @@ impl<'a> FmtVisitor<'a> {
772772
indent: indent + 10,
773773
h_width: budget,
774774
v_width: budget,
775-
is_expression: true,
775+
ends_with_newline: true,
776776
};
777777
result.push_str(&write_list(&items, &fmt));
778778

src/lists.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub struct ListFormatting<'a> {
4949
pub v_width: usize,
5050
// Non-expressions, e.g. items, will have a new line at the end of the list.
5151
// Important for comment styles.
52-
pub is_expression: bool
52+
pub ends_with_newline: bool
5353
}
5454

5555
pub struct ListItem {
@@ -173,7 +173,9 @@ pub fn write_list<'b>(items: &[ListItem], formatting: &ListFormatting<'b>) -> St
173173
result.push_str(&rewrite_comment(comment,
174174
// Block style in non-vertical mode
175175
tactic != ListTactic::Vertical,
176-
1000,
176+
// Width restriction is only
177+
// relevant in vertical mode.
178+
formatting.v_width,
177179
formatting.indent));
178180

179181
if tactic == ListTactic::Vertical {
@@ -188,10 +190,9 @@ pub fn write_list<'b>(items: &[ListItem], formatting: &ListFormatting<'b>) -> St
188190

189191
// Post-comments
190192
if tactic != ListTactic::Vertical && item.post_comment.is_some() {
191-
// We'll assume it'll fit on one line at this point
192193
let formatted_comment = rewrite_comment(item.post_comment.as_ref().unwrap(),
193194
true,
194-
1000,
195+
formatting.v_width,
195196
0);
196197

197198
result.push(' ');
@@ -208,14 +209,11 @@ pub fn write_list<'b>(items: &[ListItem], formatting: &ListFormatting<'b>) -> St
208209
let offset = formatting.indent + item_width + 1;
209210
let comment = item.post_comment.as_ref().unwrap();
210211
// Use block-style only for the last item or multiline comments.
211-
let block_style = formatting.is_expression && last ||
212+
let block_style = formatting.ends_with_newline && last ||
212213
comment.trim().contains('\n') ||
213214
comment.trim().len() > width;
214215

215-
let formatted_comment = rewrite_comment(comment,
216-
block_style,
217-
width,
218-
offset);
216+
let formatted_comment = rewrite_comment(comment, block_style, width, offset);
219217

220218
result.push(' ');
221219
result.push_str(&formatted_comment);

tests/source/multiple.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -107,21 +107,3 @@ fn main() {
107107
let x = "Hello!!!!!!!!! abcd abcd abcd abcd abcd abcd\n abcd abcd abcd abcd abcd abcd abcd abcd abcd \
108108
abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd \
109109
abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd"; }
110-
111-
112-
fn struct_lits() {
113-
let x = Bar;
114-
// Comment
115-
let y = Foo { a: x };
116-
Foo { a: foo() /* comment*/, /* comment*/ b: bar(), ..something };
117-
Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { a: foo(), b: bar(), };
118-
Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo {
119-
// Comment
120-
a: foo(), // Comment
121-
// Comment
122-
b: bar(), // Comment
123-
};
124-
125-
Foo { a: Bar,
126-
b: foo() };
127-
}

tests/source/struct_lits.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Struct literal expressions.
2+
3+
fn main() {
4+
let x = Bar;
5+
6+
// Comment
7+
let y = Foo {a: x };
8+
9+
Foo { a: foo() /* comment*/, /* comment*/ b: bar(), ..something };
10+
11+
Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { a: foo(), b: bar(), };
12+
13+
Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo {
14+
// Comment
15+
a: foo(), // Comment
16+
// Comment
17+
b: bar(), // Comment
18+
};
19+
20+
Foo { a:Bar,
21+
b:foo() };
22+
23+
A {
24+
// 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.
25+
first: item(),
26+
// Praesent et diam eget libero egestas mattis sit amet vitae augue.
27+
// Nam tincidunt congue enim, ut porta lorem lacinia consectetur.
28+
second: Item
29+
};
30+
31+
Diagram { /* o This graph demonstrates how
32+
* / \ significant whitespace is
33+
* o o preserved.
34+
* /|\ \
35+
* o o o o */
36+
graph: G, }
37+
}

tests/target/multiple.rs

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -139,34 +139,3 @@ fn main() {
139139
abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd \
140140
abcd";
141141
}
142-
143-
144-
fn struct_lits() {
145-
let x = Bar;
146-
// Comment
147-
let y = Foo { a: x };
148-
Foo { a: foo(), // comment
149-
// comment
150-
b: bar(),
151-
..something };
152-
Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { a: foo(),
153-
b: bar(), };
154-
Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { // Comment
155-
a: foo(), /* C
156-
* o
157-
* m
158-
* m
159-
* e
160-
* n
161-
* t */
162-
// Comment
163-
b: bar(), /* C
164-
* o
165-
* m
166-
* m
167-
* e
168-
* n
169-
* t */ };
170-
171-
Foo { a: Bar, b: foo() };
172-
}

tests/target/struct_lits.rs

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,45 @@ fn main() {
66
// Comment
77
let y = Foo { a: x };
88

9-
Foo { a: Bar, b: foo() };
10-
11-
Foo { a: foo(), b: bar(), ..something };
9+
Foo { a: foo(), // comment
10+
// comment
11+
b: bar(),
12+
..something };
1213

13-
Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { a: foo(), b: bar() };
1414
Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { a: foo(),
1515
b: bar(), };
1616

17-
Fooooooooooooooooooooooooooooooooooooooooooooooooooooo { a: foo(),
18-
b: bar(),
19-
c: bar(),
20-
d: bar(),
21-
e: bar(),
22-
f: bar(),
23-
..baz() };
17+
Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { // Comment
18+
a: foo(), /* C
19+
* o
20+
* m
21+
* m
22+
* e
23+
* n
24+
* t */
25+
// Comment
26+
b: bar(), /* C
27+
* o
28+
* m
29+
* m
30+
* e
31+
* n
32+
* t */ };
33+
34+
Foo { a: Bar, b: foo() };
35+
36+
A { // Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit
37+
// amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante
38+
// hendrerit. Donec et mollis dolor.
39+
first: item(),
40+
// Praesent et diam eget libero egestas mattis sit amet vitae augue.
41+
// Nam tincidunt congue enim, ut porta lorem lacinia consectetur.
42+
second: Item, };
43+
44+
Diagram { // o This graph demonstrates how
45+
// / \ significant whitespace is
46+
// o o preserved.
47+
// /|\ \
48+
// o o o o
49+
graph: G, }
2450
}

0 commit comments

Comments
 (0)