Skip to content

Commit 372d451

Browse files
committed
blank lines separate alignment groups in structs
1 parent 685e0e5 commit 372d451

File tree

4 files changed

+20
-13
lines changed

4 files changed

+20
-13
lines changed

src/vertical.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use itertools::Itertools;
66
use syntax::ast;
77
use syntax::source_map::{BytePos, Span};
88

9-
use crate::comment::{combine_strs_with_missing_comments, contains_comment};
9+
use crate::comment::combine_strs_with_missing_comments;
1010
use crate::config::lists::*;
1111
use crate::expr::rewrite_field;
1212
use crate::items::{rewrite_struct_field, rewrite_struct_field_prefix};
@@ -253,6 +253,9 @@ fn rewrite_aligned_items_inner<T: AlignedItem>(
253253
write_list(&items, &fmt)
254254
}
255255

256+
/// Returns the index in `fields` up to which a field belongs to the current group.
257+
/// The returned string is the group separator to use when rewriting the fields.
258+
/// Groups are defined by blank lines.
256259
fn group_aligned_items<T: AlignedItem>(
257260
context: &RewriteContext<'_>,
258261
fields: &[T],
@@ -262,25 +265,19 @@ fn group_aligned_items<T: AlignedItem>(
262265
if fields[i].skip() {
263266
return ("", index);
264267
}
265-
// See if there are comments or empty lines between fields.
266268
let span = mk_sp(fields[i].get_span().hi(), fields[i + 1].get_span().lo());
267269
let snippet = context
268270
.snippet(span)
269271
.lines()
270272
.skip(1)
271273
.collect::<Vec<_>>()
272274
.join("\n");
273-
let spacings = if snippet
275+
let has_blank_line = snippet
274276
.lines()
275277
.dropping_back(1)
276-
.any(|l| l.trim().is_empty())
277-
{
278-
"\n"
279-
} else {
280-
""
281-
};
282-
if contains_comment(&snippet) || snippet.lines().count() > 1 {
283-
return (spacings, index);
278+
.any(|l| l.trim().is_empty());
279+
if has_blank_line {
280+
return ("\n", index);
284281
}
285282
index += 1;
286283
}

tests/source/issue-2869.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ struct AuditLog1 {
1616
actor_context_id: Option<String>,
1717
actor_ip_address: Option<IpAddr>,
1818
azure_active_directory_event_type: Option<u8>,
19+
20+
#[serde(rename = "very")]
21+
aaaaa: String,
22+
#[serde(rename = "cool")]
23+
bb: i32,
1924
}
2025

2126
#[derive(Serialize, Deserialize, Debug)]

tests/target/configs/struct_field_align_threshold/20.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ fn main() {
3838
pub struct Foo {
3939
#[rustfmt::skip]
4040
f : SomeType, // Comment beside a field
41-
f: SomeType, // Comment beside a field
41+
f: SomeType, // Comment beside a field
4242
// Comment on a field
4343
#[AnAttribute]
4444
g: SomeOtherType,
@@ -323,7 +323,7 @@ fn main() {
323323
// Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit
324324
// amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante
325325
// hendrerit. Donec et mollis dolor.
326-
first: item(),
326+
first: item(),
327327
// Praesent et diam eget libero egestas mattis sit amet vitae augue.
328328
// Nam tincidunt congue enim, ut porta lorem lacinia consectetur.
329329
second: Item,

tests/target/issue-2869.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ struct AuditLog1 {
1616
actor_context_id: Option<String>,
1717
actor_ip_address: Option<IpAddr>,
1818
azure_active_directory_event_type: Option<u8>,
19+
20+
#[serde(rename = "very")]
21+
aaaaa: String,
22+
#[serde(rename = "cool")]
23+
bb: i32,
1924
}
2025

2126
#[derive(Serialize, Deserialize, Debug)]

0 commit comments

Comments
 (0)