Skip to content

Commit 1f991d0

Browse files
authored
Merge pull request #1822 from topecongiro/issue-1172
Remove newlines between list elements for expressions
2 parents bca0dd9 + 7c9aee7 commit 1f991d0

File tree

9 files changed

+70
-1
lines changed

9 files changed

+70
-1
lines changed

src/expr.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ where
527527
},
528528
shape: nested_shape,
529529
ends_with_newline: ends_with_newline,
530+
preserve_newline: false,
530531
config: context.config,
531532
};
532533
let list_str = try_opt!(write_list(&items, &fmt));
@@ -602,6 +603,7 @@ fn rewrite_closure_fn_decl(
602603
trailing_separator: SeparatorTactic::Never,
603604
shape: arg_shape,
604605
ends_with_newline: false,
606+
preserve_newline: true,
605607
config: context.config,
606608
};
607609
let list_str = try_opt!(write_list(&item_vec, &fmt));
@@ -1673,6 +1675,7 @@ fn rewrite_match_pattern(
16731675
trailing_separator: SeparatorTactic::Never,
16741676
shape: pat_shape,
16751677
ends_with_newline: false,
1678+
preserve_newline: false,
16761679
config: context.config,
16771680
};
16781681
let pats_str = try_opt!(write_list(&items, &fmt));
@@ -2165,6 +2168,7 @@ where
21652168
},
21662169
shape: shape,
21672170
ends_with_newline: context.use_block_indent() && tactic == DefinitiveListTactic::Vertical,
2171+
preserve_newline: false,
21682172
config: context.config,
21692173
};
21702174

@@ -2760,6 +2764,7 @@ where
27602764
trailing_separator: SeparatorTactic::Never,
27612765
shape: shape,
27622766
ends_with_newline: false,
2767+
preserve_newline: false,
27632768
config: context.config,
27642769
};
27652770
let list_str = try_opt!(write_list(&item_vec, &fmt));

src/imports.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ fn rewrite_use_list(
478478
},
479479
shape: nested_shape,
480480
ends_with_newline: ends_with_newline,
481+
preserve_newline: true,
481482
config: context.config,
482483
};
483484
let list_str = try_opt!(write_list(&items[first_index..], &fmt));

src/items.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ impl<'a> FmtVisitor<'a> {
477477
trailing_separator: self.config.trailing_comma(),
478478
shape: shape,
479479
ends_with_newline: true,
480+
preserve_newline: true,
480481
config: self.config,
481482
};
482483

@@ -2252,6 +2253,7 @@ fn rewrite_args(
22522253
},
22532254
shape: Shape::legacy(budget, indent),
22542255
ends_with_newline: tactic.ends_with_newline(context.config.fn_args_layout()),
2256+
preserve_newline: true,
22552257
config: context.config,
22562258
};
22572259

@@ -2425,6 +2427,7 @@ where
24252427
},
24262428
shape: shape,
24272429
ends_with_newline: tactic.ends_with_newline(context.config.generics_indent()),
2430+
preserve_newline: true,
24282431
config: context.config,
24292432
};
24302433

@@ -2538,6 +2541,7 @@ fn rewrite_where_clause_rfc_style(
25382541
trailing_separator: comma_tactic,
25392542
shape: clause_shape,
25402543
ends_with_newline: true,
2544+
preserve_newline: true,
25412545
config: context.config,
25422546
};
25432547
let preds_str = try_opt!(write_list(&items.collect::<Vec<_>>(), &fmt));
@@ -2639,6 +2643,7 @@ fn rewrite_where_clause(
26392643
trailing_separator: comma_tactic,
26402644
shape: Shape::legacy(budget, offset),
26412645
ends_with_newline: tactic.ends_with_newline(context.config.where_pred_indent()),
2646+
preserve_newline: true,
26422647
config: context.config,
26432648
};
26442649
let preds_str = try_opt!(write_list(&item_vec, &fmt));

src/lists.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ pub struct ListFormatting<'a> {
6565
// Non-expressions, e.g. items, will have a new line at the end of the list.
6666
// Important for comment styles.
6767
pub ends_with_newline: bool,
68+
// Remove newlines between list elements for expressions.
69+
pub preserve_newline: bool,
6870
pub config: &'a Config,
6971
}
7072

@@ -342,7 +344,9 @@ where
342344
item_max_width = None;
343345
}
344346

345-
if !last && tactic == DefinitiveListTactic::Vertical && item.new_lines {
347+
if formatting.preserve_newline && !last && tactic == DefinitiveListTactic::Vertical &&
348+
item.new_lines
349+
{
346350
item_max_width = None;
347351
result.push('\n');
348352
}
@@ -675,6 +679,7 @@ pub fn struct_lit_formatting<'a>(
675679
},
676680
shape: shape,
677681
ends_with_newline: ends_with_newline,
682+
preserve_newline: true,
678683
config: context.config,
679684
}
680685
}

src/types.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ where
360360
},
361361
shape: list_shape,
362362
ends_with_newline: tactic.ends_with_newline(context.config.fn_call_style()),
363+
preserve_newline: true,
363364
config: context.config,
364365
};
365366

src/vertical.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ fn rewrite_aligned_items_inner<T: AlignedItem>(
229229
trailing_separator: context.config.trailing_comma(),
230230
shape: item_shape,
231231
ends_with_newline: true,
232+
preserve_newline: true,
232233
config: context.config,
233234
};
234235
write_list(&items, &fmt)

src/visitor.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,7 @@ impl Rewrite for ast::MetaItem {
819819
trailing_separator: SeparatorTactic::Never,
820820
shape: item_shape,
821821
ends_with_newline: false,
822+
preserve_newline: false,
822823
config: context.config,
823824
};
824825
format!("{}({})", name, try_opt!(write_list(&item_vec, &fmt)))

tests/source/expr.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,3 +327,31 @@ fn issue1749() {
327327
}
328328
}
329329
}
330+
331+
// #1172
332+
fn newlines_between_list_like_expr() {
333+
foo(
334+
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
335+
336+
yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy,
337+
338+
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz,
339+
);
340+
341+
vec![
342+
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
343+
344+
yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy,
345+
346+
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz,
347+
];
348+
349+
match x {
350+
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
351+
352+
yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy |
353+
354+
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz => foo(a, b, c),
355+
_ => bar(),
356+
};
357+
}

tests/target/expr.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,3 +391,25 @@ fn issue1749() {
391391
}
392392
}
393393
}
394+
395+
// #1172
396+
fn newlines_between_list_like_expr() {
397+
foo(
398+
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
399+
yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy,
400+
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz,
401+
);
402+
403+
vec![
404+
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
405+
yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy,
406+
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz,
407+
];
408+
409+
match x {
410+
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
411+
yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy |
412+
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz => foo(a, b, c),
413+
_ => bar(),
414+
};
415+
}

0 commit comments

Comments
 (0)