Skip to content

Commit 5ac3be2

Browse files
committed
Merge pull request #469 from marcusklaas/rustup300
Adjust for unified enum variants and structs
2 parents f04358e + 5ca9381 commit 5ac3be2

File tree

1 file changed

+66
-67
lines changed

1 file changed

+66
-67
lines changed

src/items.rs

Lines changed: 66 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -746,73 +746,74 @@ impl<'a> FmtVisitor<'a> {
746746
result.push_str(&indent.to_string(self.config));
747747
}
748748

749-
let variant_body = match field.node.kind {
750-
ast::VariantKind::TupleVariantKind(ref types) => {
749+
let variant_body = match *field.node.data {
750+
ast::VariantData::Tuple(ref types, _) => {
751751
let mut result = field.node.name.to_string();
752+
let items = itemize_list(self.codemap,
753+
types.iter(),
754+
")",
755+
|arg| arg.node.ty.span.lo,
756+
|arg| arg.node.ty.span.hi,
757+
|arg| {
758+
// FIXME silly width, indent
759+
arg.node
760+
.ty
761+
.rewrite(&self.get_context(), 1000, Indent::empty())
762+
},
763+
span_after(field.span, "(", self.codemap),
764+
field.span.hi);
765+
let item_vec = items.collect::<Vec<_>>();
752766

753-
if !types.is_empty() {
754-
let items = itemize_list(self.codemap,
755-
types.iter(),
756-
")",
757-
|arg| arg.ty.span.lo,
758-
|arg| arg.ty.span.hi,
759-
|arg| {
760-
// FIXME silly width, indent
761-
arg.ty.rewrite(&self.get_context(),
762-
1000,
763-
Indent::empty())
764-
},
765-
span_after(field.span, "(", self.codemap),
766-
field.span.hi);
767-
let item_vec = items.collect::<Vec<_>>();
768-
769-
result.push('(');
770-
771-
let indent = indent + field.node.name.to_string().len() + "(".len();
772-
773-
let comma_cost = if self.config.enum_trailing_comma {
774-
1
775-
} else {
776-
0
777-
};
778-
let budget = self.config.max_width - indent.width() - comma_cost - 1; // 1 = )
779-
let tactic = definitive_tactic(&item_vec,
780-
ListTactic::HorizontalVertical,
781-
budget);
782-
783-
let fmt = ListFormatting {
784-
tactic: tactic,
785-
separator: ",",
786-
trailing_separator: SeparatorTactic::Never,
787-
indent: indent,
788-
width: budget,
789-
ends_with_newline: true,
790-
config: self.config,
791-
};
792-
let list_str = try_opt!(write_list(&item_vec, &fmt));
793-
794-
result.push_str(&list_str);
795-
result.push(')');
796-
}
767+
result.push('(');
797768

798-
if let Some(ref expr) = field.node.disr_expr {
799-
result.push_str(" = ");
800-
let expr_snippet = self.snippet(expr.span);
801-
result.push_str(&expr_snippet);
802-
}
769+
let indent = indent + field.node.name.to_string().len() + "(".len();
770+
771+
let comma_cost = if self.config.enum_trailing_comma {
772+
1
773+
} else {
774+
0
775+
};
776+
let budget = self.config.max_width - indent.width() - comma_cost - 1; // 1 = )
777+
let tactic = definitive_tactic(&item_vec, ListTactic::HorizontalVertical, budget);
778+
779+
let fmt = ListFormatting {
780+
tactic: tactic,
781+
separator: ",",
782+
trailing_separator: SeparatorTactic::Never,
783+
indent: indent,
784+
width: budget,
785+
ends_with_newline: true,
786+
config: self.config,
787+
};
788+
let list_str = try_opt!(write_list(&item_vec, &fmt));
789+
790+
result.push_str(&list_str);
791+
result.push(')');
803792

804793
Some(result)
805794
}
806-
ast::VariantKind::StructVariantKind(ref struct_def) => {
795+
ast::VariantData::Struct(..) => {
807796
// FIXME: Should limit the width, as we have a trailing comma
808797
self.format_struct("",
809798
field.node.name,
810799
ast::Visibility::Inherited,
811-
struct_def,
800+
&*field.node.data,
812801
None,
813802
field.span,
814803
indent)
815804
}
805+
ast::VariantData::Unit(..) => {
806+
let tag = if let Some(ref expr) = field.node.disr_expr {
807+
format!("{} = {}", field.node.name, self.snippet(expr.span))
808+
} else {
809+
field.node.name.to_string()
810+
};
811+
812+
wrap_str(tag,
813+
self.config.max_width,
814+
self.config.max_width - indent.width(),
815+
indent)
816+
}
816817
};
817818

818819
if let Some(variant_str) = variant_body {
@@ -827,7 +828,7 @@ impl<'a> FmtVisitor<'a> {
827828
item_name: &str,
828829
ident: ast::Ident,
829830
vis: ast::Visibility,
830-
struct_def: &ast::StructDef,
831+
struct_def: &ast::VariantData,
831832
generics: Option<&ast::Generics>,
832833
span: Span,
833834
offset: Indent)
@@ -837,14 +838,13 @@ impl<'a> FmtVisitor<'a> {
837838
let header_str = self.format_header(item_name, ident, vis);
838839
result.push_str(&header_str);
839840

840-
if struct_def.fields.is_empty() {
841-
result.push(';');
842-
return Some(result);
843-
}
844-
845-
let is_tuple = match struct_def.fields[0].node.kind {
846-
ast::StructFieldKind::NamedField(..) => false,
847-
ast::StructFieldKind::UnnamedField(..) => true,
841+
let (is_tuple, fields) = match *struct_def {
842+
ast::VariantData::Unit(..) => {
843+
result.push(';');
844+
return Some(result);
845+
}
846+
ast::VariantData::Tuple(ref vec, _) => (true, vec),
847+
ast::VariantData::Struct(ref vec, _) => (false, vec),
848848
};
849849

850850
let (opener, terminator) = if is_tuple {
@@ -859,15 +859,14 @@ impl<'a> FmtVisitor<'a> {
859859
opener,
860860
offset,
861861
offset + header_str.len(),
862-
codemap::mk_sp(span.lo,
863-
struct_def.fields[0].span.lo)))
862+
codemap::mk_sp(span.lo, fields[0].span.lo)))
864863
}
865864
None => opener.to_owned(),
866865
};
867866
result.push_str(&generics_str);
868867

869868
let items = itemize_list(self.codemap,
870-
struct_def.fields.iter(),
869+
fields.iter(),
871870
terminator,
872871
|field| {
873872
// Include attributes and doc comments, if present
@@ -886,7 +885,7 @@ impl<'a> FmtVisitor<'a> {
886885
let used_budget = offset.width() + header_str.len() + generics_str.len() + 3;
887886

888887
// Conservative approximation
889-
let single_line_cost = (span.hi - struct_def.fields[0].span.lo).0;
888+
let single_line_cost = (span.hi - fields[0].span.lo).0;
890889
let break_line = !is_tuple || generics_str.contains('\n') ||
891890
single_line_cost as usize + used_budget > self.config.max_width;
892891

@@ -932,7 +931,7 @@ impl<'a> FmtVisitor<'a> {
932931
pub fn visit_struct(&mut self,
933932
ident: ast::Ident,
934933
vis: ast::Visibility,
935-
struct_def: &ast::StructDef,
934+
struct_def: &ast::VariantData,
936935
generics: &ast::Generics,
937936
span: Span) {
938937
let indent = self.block_indent;

0 commit comments

Comments
 (0)