Skip to content

Commit abe6eec

Browse files
authored
Merge pull request #2160 from topecongiro/issue-1809
Force vertical layout for all variants if one of then use multiple lines
2 parents d4fdaec + b1a6dd6 commit abe6eec

File tree

4 files changed

+83
-21
lines changed

4 files changed

+83
-21
lines changed

src/bin/rustfmt.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ enum Operation {
4545
/// Print detailed configuration help.
4646
ConfigHelp,
4747
/// Output default config to a file, or stdout if None
48-
ConfigOutputDefault { path: Option<String> },
48+
ConfigOutputDefault {
49+
path: Option<String>,
50+
},
4951
/// No file specified, read from stdin
5052
Stdin {
5153
input: String,

src/items.rs

+27-18
Original file line numberDiff line numberDiff line change
@@ -484,21 +484,30 @@ impl<'a> FmtVisitor<'a> {
484484
let indentation = self.block_indent.to_string(self.config);
485485
result.push_str(&indentation);
486486

487-
let items = itemize_list(
488-
self.codemap,
489-
enum_def.variants.iter(),
490-
"}",
491-
|f| if !f.node.attrs.is_empty() {
492-
f.node.attrs[0].span.lo()
493-
} else {
494-
f.span.lo()
495-
},
496-
|f| f.span.hi(),
497-
|f| self.format_variant(f),
498-
body_lo,
499-
body_hi,
500-
false,
501-
);
487+
let itemize_list_with = |one_line_width: usize| {
488+
itemize_list(
489+
self.codemap,
490+
enum_def.variants.iter(),
491+
"}",
492+
|f| if !f.node.attrs.is_empty() {
493+
f.node.attrs[0].span.lo()
494+
} else {
495+
f.span.lo()
496+
},
497+
|f| f.span.hi(),
498+
|f| self.format_variant(f, one_line_width),
499+
body_lo,
500+
body_hi,
501+
false,
502+
).collect()
503+
};
504+
let mut items: Vec<_> = itemize_list_with(self.config.struct_variant_width());
505+
// If one of the variants use multiple lines, use multi-lined formatting for all variants.
506+
let has_multiline_variant = items.iter().any(|item| item.inner_as_ref().contains("\n"));
507+
let has_single_line_variant = items.iter().any(|item| !item.inner_as_ref().contains("\n"));
508+
if has_multiline_variant && has_single_line_variant {
509+
items = itemize_list_with(0);
510+
}
502511

503512
let shape = self.shape().sub_width(2).unwrap();
504513
let fmt = ListFormatting {
@@ -512,14 +521,14 @@ impl<'a> FmtVisitor<'a> {
512521
config: self.config,
513522
};
514523

515-
let list = write_list(&items.collect::<Vec<_>>(), &fmt)?;
524+
let list = write_list(&items, &fmt)?;
516525
result.push_str(&list);
517526
result.push('\n');
518527
Some(result)
519528
}
520529

521530
// Variant of an enum.
522-
fn format_variant(&self, field: &ast::Variant) -> Option<String> {
531+
fn format_variant(&self, field: &ast::Variant, one_line_width: usize) -> Option<String> {
523532
if contains_skip(&field.node.attrs) {
524533
let lo = field.node.attrs[0].span.lo();
525534
let span = mk_sp(lo, field.span.hi());
@@ -544,7 +553,7 @@ impl<'a> FmtVisitor<'a> {
544553
&context,
545554
&StructParts::from_variant(field),
546555
indent,
547-
Some(self.config.struct_variant_width()),
556+
Some(one_line_width),
548557
)?
549558
}
550559
ast::VariantData::Unit(..) => if let Some(ref expr) = field.node.disr_expr {

tests/source/enum.rs

+19
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,22 @@ pub enum ForegroundColor {
146146
pub enum E<'a> {
147147
V ( < std::slice::Iter<'a, Xxxxxxxxxxxxxx> as Iterator> :: Item ) ,
148148
}
149+
150+
// #1809
151+
enum State {
152+
TryRecv {
153+
pos: usize,
154+
lap: u8,
155+
closed_count: usize,
156+
},
157+
Subscribe { pos: usize },
158+
IsReady { pos: usize, ready: bool },
159+
Unsubscribe {
160+
pos: usize,
161+
lap: u8,
162+
id_woken: usize,
163+
},
164+
FinalTryRecv { pos: usize, id_woken: usize },
165+
TimedOut,
166+
Disconnected,
167+
}

tests/target/enum.rs

+34-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ enum StructLikeVariants {
4444
// Pre-comment
4545
#[Attr50] y: SomeType, // Aanother Comment
4646
},
47-
SL { a: A },
47+
SL {
48+
a: A,
49+
},
4850
}
4951

5052
enum X {
@@ -65,7 +67,10 @@ pub enum EnumWithAttributes {
6567
SkippedItem(String,String,), // Post-comment
6668
#[another_attr]
6769
#[attr2]
68-
ItemStruct { x: usize, y: usize }, /* Comment AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA */
70+
ItemStruct {
71+
x: usize,
72+
y: usize,
73+
}, /* Comment AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA */
6974
// And another
7075
ForcedPreflight, /* AAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
7176
* AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA */
@@ -183,3 +188,30 @@ pub enum ForegroundColor {
183188
pub enum E<'a> {
184189
V(<std::slice::Iter<'a, Xxxxxxxxxxxxxx> as Iterator>::Item),
185190
}
191+
192+
// #1809
193+
enum State {
194+
TryRecv {
195+
pos: usize,
196+
lap: u8,
197+
closed_count: usize,
198+
},
199+
Subscribe {
200+
pos: usize,
201+
},
202+
IsReady {
203+
pos: usize,
204+
ready: bool,
205+
},
206+
Unsubscribe {
207+
pos: usize,
208+
lap: u8,
209+
id_woken: usize,
210+
},
211+
FinalTryRecv {
212+
pos: usize,
213+
id_woken: usize,
214+
},
215+
TimedOut,
216+
Disconnected,
217+
}

0 commit comments

Comments
 (0)