diff --git a/src/bin/rustfmt.rs b/src/bin/rustfmt.rs index 4a041144789..41454c853ed 100644 --- a/src/bin/rustfmt.rs +++ b/src/bin/rustfmt.rs @@ -45,7 +45,9 @@ enum Operation { /// Print detailed configuration help. ConfigHelp, /// Output default config to a file, or stdout if None - ConfigOutputDefault { path: Option }, + ConfigOutputDefault { + path: Option, + }, /// No file specified, read from stdin Stdin { input: String, diff --git a/src/items.rs b/src/items.rs index 5f6c043bb4a..0c1895c2391 100644 --- a/src/items.rs +++ b/src/items.rs @@ -484,21 +484,30 @@ impl<'a> FmtVisitor<'a> { let indentation = self.block_indent.to_string(self.config); result.push_str(&indentation); - let items = itemize_list( - self.codemap, - enum_def.variants.iter(), - "}", - |f| if !f.node.attrs.is_empty() { - f.node.attrs[0].span.lo() - } else { - f.span.lo() - }, - |f| f.span.hi(), - |f| self.format_variant(f), - body_lo, - body_hi, - false, - ); + let itemize_list_with = |one_line_width: usize| { + itemize_list( + self.codemap, + enum_def.variants.iter(), + "}", + |f| if !f.node.attrs.is_empty() { + f.node.attrs[0].span.lo() + } else { + f.span.lo() + }, + |f| f.span.hi(), + |f| self.format_variant(f, one_line_width), + body_lo, + body_hi, + false, + ).collect() + }; + let mut items: Vec<_> = itemize_list_with(self.config.struct_variant_width()); + // If one of the variants use multiple lines, use multi-lined formatting for all variants. + let has_multiline_variant = items.iter().any(|item| item.inner_as_ref().contains("\n")); + let has_single_line_variant = items.iter().any(|item| !item.inner_as_ref().contains("\n")); + if has_multiline_variant && has_single_line_variant { + items = itemize_list_with(0); + } let shape = self.shape().sub_width(2).unwrap(); let fmt = ListFormatting { @@ -512,14 +521,14 @@ impl<'a> FmtVisitor<'a> { config: self.config, }; - let list = write_list(&items.collect::>(), &fmt)?; + let list = write_list(&items, &fmt)?; result.push_str(&list); result.push('\n'); Some(result) } // Variant of an enum. - fn format_variant(&self, field: &ast::Variant) -> Option { + fn format_variant(&self, field: &ast::Variant, one_line_width: usize) -> Option { if contains_skip(&field.node.attrs) { let lo = field.node.attrs[0].span.lo(); let span = mk_sp(lo, field.span.hi()); @@ -544,7 +553,7 @@ impl<'a> FmtVisitor<'a> { &context, &StructParts::from_variant(field), indent, - Some(self.config.struct_variant_width()), + Some(one_line_width), )? } ast::VariantData::Unit(..) => if let Some(ref expr) = field.node.disr_expr { diff --git a/tests/source/enum.rs b/tests/source/enum.rs index 17c26b26d15..821bb0efd99 100644 --- a/tests/source/enum.rs +++ b/tests/source/enum.rs @@ -146,3 +146,22 @@ pub enum ForegroundColor { pub enum E<'a> { V ( < std::slice::Iter<'a, Xxxxxxxxxxxxxx> as Iterator> :: Item ) , } + +// #1809 +enum State { + TryRecv { + pos: usize, + lap: u8, + closed_count: usize, + }, + Subscribe { pos: usize }, + IsReady { pos: usize, ready: bool }, + Unsubscribe { + pos: usize, + lap: u8, + id_woken: usize, + }, + FinalTryRecv { pos: usize, id_woken: usize }, + TimedOut, + Disconnected, +} diff --git a/tests/target/enum.rs b/tests/target/enum.rs index b66f833a932..f3e8f2d4d6b 100644 --- a/tests/target/enum.rs +++ b/tests/target/enum.rs @@ -44,7 +44,9 @@ enum StructLikeVariants { // Pre-comment #[Attr50] y: SomeType, // Aanother Comment }, - SL { a: A }, + SL { + a: A, + }, } enum X { @@ -65,7 +67,10 @@ pub enum EnumWithAttributes { SkippedItem(String,String,), // Post-comment #[another_attr] #[attr2] - ItemStruct { x: usize, y: usize }, /* Comment AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA */ + ItemStruct { + x: usize, + y: usize, + }, /* Comment AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA */ // And another ForcedPreflight, /* AAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA * AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA */ @@ -183,3 +188,30 @@ pub enum ForegroundColor { pub enum E<'a> { V( as Iterator>::Item), } + +// #1809 +enum State { + TryRecv { + pos: usize, + lap: u8, + closed_count: usize, + }, + Subscribe { + pos: usize, + }, + IsReady { + pos: usize, + ready: bool, + }, + Unsubscribe { + pos: usize, + lap: u8, + id_woken: usize, + }, + FinalTryRecv { + pos: usize, + id_woken: usize, + }, + TimedOut, + Disconnected, +}