Skip to content

Commit 97e92b3

Browse files
committed
Preserve some whitespace between struct fields etc.
1 parent 000ea50 commit 97e92b3

File tree

4 files changed

+92
-20
lines changed

4 files changed

+92
-20
lines changed

src/items.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -632,13 +632,11 @@ impl<'a> FmtVisitor<'a> {
632632
let break_line = !is_tuple || generics_str.contains('\n') ||
633633
single_line_cost as usize + used_budget > self.config.max_width;
634634

635-
if break_line {
635+
let tactic = if break_line {
636636
let indentation = make_indent(offset + self.config.tab_spaces);
637637
result.push('\n');
638638
result.push_str(&indentation);
639-
}
640639

641-
let tactic = if break_line {
642640
ListTactic::Vertical
643641
} else {
644642
ListTactic::Horizontal

src/lists.rs

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,11 @@ impl<'a> ListFormatting<'a> {
7070

7171
pub struct ListItem {
7272
pub pre_comment: Option<String>,
73-
// Item should include attributes and doc comments
73+
// Item should include attributes and doc comments.
7474
pub item: String,
7575
pub post_comment: Option<String>,
76+
// Whether there is extra whitespace before this item.
77+
pub new_lines: bool,
7678
}
7779

7880
impl ListItem {
@@ -86,7 +88,7 @@ impl ListItem {
8688
}
8789

8890
pub fn from_str<S: Into<String>>(s: S) -> ListItem {
89-
ListItem { pre_comment: None, item: s.into(), post_comment: None }
91+
ListItem { pre_comment: None, item: s.into(), post_comment: None, new_lines: false }
9092
}
9193
}
9294

@@ -206,10 +208,8 @@ pub fn write_list<'b>(items: &[ListItem], formatting: &ListFormatting<'b>) -> St
206208

207209
// Post-comments
208210
if tactic != ListTactic::Vertical && item.post_comment.is_some() {
209-
let formatted_comment = rewrite_comment(item.post_comment.as_ref().unwrap(),
210-
true,
211-
formatting.v_width,
212-
0);
211+
let comment = item.post_comment.as_ref().unwrap();
212+
let formatted_comment = rewrite_comment(comment, true, formatting.v_width, 0);
213213

214214
result.push(' ');
215215
result.push_str(&formatted_comment);
@@ -234,6 +234,10 @@ pub fn write_list<'b>(items: &[ListItem], formatting: &ListFormatting<'b>) -> St
234234
result.push(' ');
235235
result.push_str(&formatted_comment);
236236
}
237+
238+
if !last && tactic == ListTactic::Vertical && item.new_lines {
239+
result.push('\n');
240+
}
237241
}
238242

239243
result
@@ -264,13 +268,14 @@ impl<'a, T, I, F1, F2, F3> Iterator for ListItems<'a, I, F1, F2, F3>
264268
let white_space: &[_] = &[' ', '\t'];
265269

266270
self.inner.next().map(|item| {
271+
let mut new_lines = false;
267272
// Pre-comment
268273
let pre_snippet = self.codemap.span_to_snippet(codemap::mk_sp(self.prev_span_end,
269274
(self.get_lo)(&item)))
270275
.unwrap();
271-
let pre_snippet = pre_snippet.trim();
272-
let pre_comment = if !pre_snippet.is_empty() {
273-
Some(pre_snippet.to_owned())
276+
let trimmed_pre_snippet = pre_snippet.trim();
277+
let pre_comment = if !trimmed_pre_snippet.is_empty() {
278+
Some(trimmed_pre_snippet.to_owned())
274279
} else {
275280
None
276281
};
@@ -307,7 +312,7 @@ impl<'a, T, I, F1, F2, F3> Iterator for ListItems<'a, I, F1, F2, F3>
307312
separator_index + 1)
308313
}
309314
// Potential *single* line comment.
310-
(_, Some(j)) => { j + 1 }
315+
(_, Some(j)) => j + 1,
311316
_ => post_snippet.len()
312317
}
313318
},
@@ -317,18 +322,40 @@ impl<'a, T, I, F1, F2, F3> Iterator for ListItems<'a, I, F1, F2, F3>
317322
}
318323
};
319324

325+
if !post_snippet.is_empty() && comment_end > 0 {
326+
// Account for extra whitespace between items. This is fiddly
327+
// because of the way we divide pre- and post- comments.
328+
329+
// Everything from the separator to the next item.
330+
let test_snippet = &post_snippet[comment_end-1..];
331+
let first_newline = test_snippet.find('\n').unwrap_or(test_snippet.len());
332+
// From the end of the first line of comments.
333+
let test_snippet = &test_snippet[first_newline..];
334+
let first = test_snippet.find(|c: char| !c.is_whitespace())
335+
.unwrap_or(test_snippet.len());
336+
// From the end of the first line of comments to the next non-whitespace char.
337+
let test_snippet = &test_snippet[..first];
338+
339+
if test_snippet.chars().filter(|c| c == &'\n').count() > 1 {
340+
// There were multiple line breaks which got trimmed to nothing.
341+
new_lines = true;
342+
}
343+
}
344+
320345
// Cleanup post-comment: strip separators and whitespace.
321346
self.prev_span_end = (self.get_hi)(&item) + BytePos(comment_end as u32);
322-
let mut post_snippet = post_snippet[..comment_end].trim();
347+
let post_snippet = post_snippet[..comment_end].trim();
323348

324-
if post_snippet.starts_with(',') {
325-
post_snippet = post_snippet[1..].trim_matches(white_space);
349+
let post_snippet_trimmed = if post_snippet.starts_with(',') {
350+
post_snippet[1..].trim_matches(white_space)
326351
} else if post_snippet.ends_with(",") {
327-
post_snippet = post_snippet[..(post_snippet.len() - 1)].trim_matches(white_space);
328-
}
352+
post_snippet[..(post_snippet.len() - 1)].trim_matches(white_space)
353+
} else {
354+
post_snippet
355+
};
329356

330-
let post_comment = if !post_snippet.is_empty() {
331-
Some(post_snippet.to_owned())
357+
let post_comment = if !post_snippet_trimmed.is_empty() {
358+
Some(post_snippet_trimmed.to_owned())
332359
} else {
333360
None
334361
};
@@ -337,6 +364,7 @@ impl<'a, T, I, F1, F2, F3> Iterator for ListItems<'a, I, F1, F2, F3>
337364
pre_comment: pre_comment,
338365
item: (self.get_item_string)(&item),
339366
post_comment: post_comment,
367+
new_lines: new_lines,
340368
}
341369
})
342370
}

tests/source/structs.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,43 @@ pub struct Foo<'a, Y: Baz>
4949
}
5050

5151
struct Baz {
52+
5253
a: A, // Comment A
5354
b: B, // Comment B
5455
c: C, // Comment C
56+
57+
}
58+
59+
struct Baz {
60+
a: A, // Comment A
61+
62+
b: B, // Comment B
63+
64+
65+
66+
67+
c: C, // Comment C
68+
}
69+
70+
struct Baz {
71+
72+
a: A,
73+
74+
b: B,
75+
c: C,
76+
77+
78+
79+
80+
d: D
81+
5582
}
5683

5784
struct Baz
5885
{
5986
// Comment A
6087
a: A,
88+
6189
// Comment B
6290
b: B,
6391
// Comment C

tests/target/structs.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,27 @@ struct Baz {
5454
c: C, // Comment C
5555
}
5656

57+
struct Baz {
58+
a: A, // Comment A
59+
60+
b: B, // Comment B
61+
62+
c: C, // Comment C
63+
}
64+
65+
struct Baz {
66+
a: A,
67+
68+
b: B,
69+
c: C,
70+
71+
d: D,
72+
}
73+
5774
struct Baz {
5875
// Comment A
5976
a: A,
77+
6078
// Comment B
6179
b: B,
6280
// Comment C

0 commit comments

Comments
 (0)