Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 2f8c1fe

Browse files
authored
Merge pull request rust-lang#3093 from scampi/issue2633
do not vertically align list items in case the tactic is Horizontal
2 parents 945fb50 + 8f7a047 commit 2f8c1fe

File tree

5 files changed

+61
-4
lines changed

5 files changed

+61
-4
lines changed

src/comment.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -752,9 +752,16 @@ fn trim_custom_comment_prefix(s: &str) -> String {
752752
.map(|line| {
753753
let left_trimmed = line.trim_left();
754754
if left_trimmed.starts_with(RUSTFMT_CUSTOM_COMMENT_PREFIX) {
755-
left_trimmed.trim_left_matches(RUSTFMT_CUSTOM_COMMENT_PREFIX)
755+
let orig = left_trimmed.trim_left_matches(RUSTFMT_CUSTOM_COMMENT_PREFIX);
756+
// due to comment wrapping, a line that was originaly behind `#` is split over
757+
// multiple lines, which needs then to be prefixed with a `#`
758+
if !orig.trim_left().starts_with("# ") {
759+
format!("# {}", orig)
760+
} else {
761+
orig.to_string()
762+
}
756763
} else {
757-
line
764+
line.to_string()
758765
}
759766
})
760767
.collect::<Vec<_>>()

src/vertical.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use syntax::source_map::{BytePos, Span};
1919
use comment::{combine_strs_with_missing_comments, contains_comment};
2020
use expr::rewrite_field;
2121
use items::{rewrite_struct_field, rewrite_struct_field_prefix};
22-
use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
22+
use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator};
2323
use rewrite::{Rewrite, RewriteContext};
2424
use shape::{Indent, Shape};
2525
use source_map::SpanUtils;
@@ -227,7 +227,7 @@ fn rewrite_aligned_items_inner<T: AlignedItem>(
227227
field_prefix_max_width = 0;
228228
}
229229

230-
let items = itemize_list(
230+
let mut items = itemize_list(
231231
context.snippet_provider,
232232
fields.iter(),
233233
"}",
@@ -248,6 +248,20 @@ fn rewrite_aligned_items_inner<T: AlignedItem>(
248248
one_line_width,
249249
);
250250

251+
if tactic == DefinitiveListTactic::Horizontal {
252+
// since the items fits on a line, there is no need to align them
253+
let do_rewrite =
254+
|field: &T| -> Option<String> { field.rewrite_aligned_item(context, item_shape, 0) };
255+
fields
256+
.iter()
257+
.zip(items.iter_mut())
258+
.for_each(|(field, list_item): (&T, &mut ListItem)| {
259+
if list_item.item.is_some() {
260+
list_item.item = do_rewrite(field);
261+
}
262+
});
263+
}
264+
251265
let fmt = ListFormatting::new(item_shape, context.config)
252266
.tactic(tactic)
253267
.trailing_separator(context.config.trailing_comma())
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// rustfmt-max_width: 79
2+
// rustfmt-wrap_comments: true
3+
4+
/// ```rust
5+
/// # #![cfg_attr(not(dox), feature(cfg_target_feature, target_feature, stdsimd)not(dox), feature(cfg_target_feature, target_feature, stdsimd))]
6+
///
7+
/// // Est lectus hendrerit lorem, eget dignissim orci nisl sit amet massa. Etiam volutpat lobortis eros.
8+
/// let x = 42;
9+
/// ```
10+
fn func() {}

tests/target/issue-2633.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// rustfmt-struct_field_align_threshold: 5
2+
3+
#[derive(Fail, Debug, Clone)]
4+
pub enum BuildError {
5+
LineTooLong { length: usize, limit: usize },
6+
DisallowedByte { b: u8, pos: usize },
7+
ContainsNewLine { pos: usize },
8+
}
9+
10+
enum Foo {
11+
A { a: usize, bbbbb: () },
12+
B { a: (), bbbbb: () },
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// rustfmt-max_width: 79
2+
// rustfmt-wrap_comments: true
3+
4+
/// ```rust
5+
/// # #![cfg_attr(not(dox), feature(cfg_target_feature, target_feature,
6+
/// # stdsimd)not(dox), feature(cfg_target_feature, target_feature,
7+
/// # stdsimd))]
8+
///
9+
/// // Est lectus hendrerit lorem, eget dignissim orci nisl sit amet massa.
10+
/// // Etiam volutpat lobortis eros.
11+
/// let x = 42;
12+
/// ```
13+
fn func() {}

0 commit comments

Comments
 (0)