Skip to content

Commit ecbb064

Browse files
refactor: modify StructParts based formatting for greater reuse
1 parent 0146929 commit ecbb064

File tree

2 files changed

+68
-75
lines changed

2 files changed

+68
-75
lines changed

src/items.rs

Lines changed: 60 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::cmp::{max, min, Ordering};
66
use regex::Regex;
77
use rustc_ast::visit;
88
use rustc_ast::{ast, ptr};
9-
use rustc_span::{symbol, BytePos, Span};
9+
use rustc_span::{symbol::kw, symbol::Ident, BytePos, Span};
1010

1111
use crate::attr::filter_inline_attrs;
1212
use crate::comment::{
@@ -291,7 +291,7 @@ impl<'a> FmtVisitor<'a> {
291291
pub(crate) fn rewrite_fn_before_block(
292292
&mut self,
293293
indent: Indent,
294-
ident: symbol::Ident,
294+
ident: Ident,
295295
fn_sig: &FnSig<'_>,
296296
span: Span,
297297
) -> Option<(String, FnBraceStyle)> {
@@ -315,7 +315,7 @@ impl<'a> FmtVisitor<'a> {
315315
pub(crate) fn rewrite_required_fn(
316316
&mut self,
317317
indent: Indent,
318-
ident: symbol::Ident,
318+
ident: Ident,
319319
sig: &ast::FnSig,
320320
generics: &ast::Generics,
321321
span: Span,
@@ -385,18 +385,14 @@ impl<'a> FmtVisitor<'a> {
385385
}
386386

387387
pub(crate) fn visit_struct(&mut self, struct_parts: &StructParts<'_>) {
388-
let is_tuple = match struct_parts.def {
389-
ast::VariantData::Tuple(..) => true,
390-
_ => false,
391-
};
392388
let rewrite = format_struct(&self.get_context(), struct_parts, self.block_indent, None)
393-
.map(|s| if is_tuple { s + ";" } else { s });
389+
.map(|s| if struct_parts.is_tuple() { s + ";" } else { s });
394390
self.push_rewrite(struct_parts.span, rewrite);
395391
}
396392

397393
pub(crate) fn visit_enum(
398394
&mut self,
399-
ident: symbol::Ident,
395+
ident: Ident,
400396
vis: &ast::Visibility,
401397
enum_def: &ast::EnumDef,
402398
generics: &ast::Generics,
@@ -957,11 +953,27 @@ fn rewrite_trait_ref(
957953
))
958954
}
959955

956+
enum StructPartsVariantData<'a> {
957+
Unit,
958+
Tuple(&'a [ast::FieldDef]),
959+
Struct(&'a [ast::FieldDef]),
960+
}
961+
962+
impl<'a> From<&'a ast::VariantData> for StructPartsVariantData<'a> {
963+
fn from(variant_data: &'a ast::VariantData) -> Self {
964+
match variant_data {
965+
ast::VariantData::Unit(..) => StructPartsVariantData::Unit,
966+
ast::VariantData::Tuple(ref fields, _) => StructPartsVariantData::Tuple(fields),
967+
ast::VariantData::Struct(ref fields, _) => StructPartsVariantData::Struct(fields),
968+
}
969+
}
970+
}
971+
960972
pub(crate) struct StructParts<'a> {
961973
prefix: &'a str,
962-
ident: symbol::Ident,
974+
ident: Ident,
963975
vis: &'a ast::Visibility,
964-
def: &'a ast::VariantData,
976+
def: StructPartsVariantData<'a>,
965977
generics: Option<&'a ast::Generics>,
966978
span: Span,
967979
}
@@ -971,12 +983,16 @@ impl<'a> StructParts<'a> {
971983
format_header(context, self.prefix, self.ident, self.vis, offset)
972984
}
973985

986+
pub(crate) fn is_tuple(&self) -> bool {
987+
matches!(self.def, StructPartsVariantData::Tuple(..))
988+
}
989+
974990
pub(crate) fn from_variant(variant: &'a ast::Variant) -> Self {
975991
StructParts {
976992
prefix: "",
977993
ident: variant.ident,
978994
vis: &DEFAULT_VISIBILITY,
979-
def: &variant.data,
995+
def: StructPartsVariantData::from(&variant.data),
980996
generics: None,
981997
span: variant.span,
982998
}
@@ -992,11 +1008,28 @@ impl<'a> StructParts<'a> {
9921008
prefix,
9931009
ident: item.ident,
9941010
vis: &item.vis,
995-
def,
1011+
def: StructPartsVariantData::from(def),
9961012
generics: Some(generics),
9971013
span: item.span,
9981014
}
9991015
}
1016+
1017+
pub(crate) fn from_anonymous_type(ty: &'a ast::Ty) -> Self {
1018+
let (fields, kw, kw_length) = match ty.kind {
1019+
ast::TyKind::AnonymousStruct(ref fields, _) => (fields, kw::Struct, 6),
1020+
ast::TyKind::AnonymousUnion(ref fields, _) => (fields, kw::Union, 5),
1021+
_ => unreachable!(),
1022+
};
1023+
1024+
StructParts {
1025+
prefix: "",
1026+
ident: Ident::new(kw, mk_sp(ty.span.lo(), ty.span.lo() + BytePos(kw_length))),
1027+
vis: &DEFAULT_VISIBILITY,
1028+
def: StructPartsVariantData::Struct(fields),
1029+
generics: None,
1030+
span: ty.span,
1031+
}
1032+
}
10001033
}
10011034

10021035
fn format_struct(
@@ -1005,12 +1038,12 @@ fn format_struct(
10051038
offset: Indent,
10061039
one_line_width: Option<usize>,
10071040
) -> Option<String> {
1008-
match *struct_parts.def {
1009-
ast::VariantData::Unit(..) => format_unit_struct(context, struct_parts, offset),
1010-
ast::VariantData::Tuple(ref fields, _) => {
1041+
match struct_parts.def {
1042+
StructPartsVariantData::Unit => format_unit_struct(context, struct_parts, offset),
1043+
StructPartsVariantData::Tuple(fields) => {
10111044
format_tuple_struct(context, struct_parts, fields, offset)
10121045
}
1013-
ast::VariantData::Struct(ref fields, _) => {
1046+
StructPartsVariantData::Struct(fields) => {
10141047
format_struct_struct(context, struct_parts, fields, offset, one_line_width)
10151048
}
10161049
}
@@ -1222,7 +1255,7 @@ impl<'a> Rewrite for TraitAliasBounds<'a> {
12221255

12231256
pub(crate) fn format_trait_alias(
12241257
context: &RewriteContext<'_>,
1225-
ident: symbol::Ident,
1258+
ident: Ident,
12261259
vis: &ast::Visibility,
12271260
generics: &ast::Generics,
12281261
generic_bounds: &ast::GenericBounds,
@@ -1500,7 +1533,7 @@ fn format_tuple_struct(
15001533
fn rewrite_type<R: Rewrite>(
15011534
context: &RewriteContext<'_>,
15021535
indent: Indent,
1503-
ident: symbol::Ident,
1536+
ident: Ident,
15041537
vis: &ast::Visibility,
15051538
generics: &ast::Generics,
15061539
generic_bounds_opt: Option<&ast::GenericBounds>,
@@ -1598,7 +1631,7 @@ fn rewrite_type<R: Rewrite>(
15981631
pub(crate) fn rewrite_opaque_type(
15991632
context: &RewriteContext<'_>,
16001633
indent: Indent,
1601-
ident: symbol::Ident,
1634+
ident: Ident,
16021635
generic_bounds: &ast::GenericBounds,
16031636
generics: &ast::Generics,
16041637
vis: &ast::Visibility,
@@ -1714,7 +1747,7 @@ pub(crate) fn rewrite_struct_field(
17141747
pub(crate) struct StaticParts<'a> {
17151748
prefix: &'a str,
17161749
vis: &'a ast::Visibility,
1717-
ident: symbol::Ident,
1750+
ident: Ident,
17181751
ty: &'a ast::Ty,
17191752
mutability: ast::Mutability,
17201753
expr_opt: Option<&'a ptr::P<ast::Expr>>,
@@ -1843,7 +1876,7 @@ fn rewrite_static(
18431876
}
18441877

18451878
pub(crate) fn rewrite_type_alias(
1846-
ident: symbol::Ident,
1879+
ident: Ident,
18471880
ty_opt: Option<&ptr::P<ast::Ty>>,
18481881
generics: &ast::Generics,
18491882
generic_bounds_opt: Option<&ast::GenericBounds>,
@@ -1879,7 +1912,7 @@ impl<'a> Rewrite for OpaqueType<'a> {
18791912

18801913
pub(crate) fn rewrite_opaque_impl_type(
18811914
context: &RewriteContext<'_>,
1882-
ident: symbol::Ident,
1915+
ident: Ident,
18831916
generics: &ast::Generics,
18841917
generic_bounds: &ast::GenericBounds,
18851918
indent: Indent,
@@ -1903,7 +1936,7 @@ pub(crate) fn rewrite_opaque_impl_type(
19031936
}
19041937

19051938
pub(crate) fn rewrite_associated_impl_type(
1906-
ident: symbol::Ident,
1939+
ident: Ident,
19071940
vis: &ast::Visibility,
19081941
defaultness: ast::Defaultness,
19091942
ty_opt: Option<&ptr::P<ast::Ty>>,
@@ -2125,7 +2158,7 @@ pub(crate) fn span_hi_for_param(context: &RewriteContext<'_>, param: &ast::Param
21252158

21262159
pub(crate) fn is_named_param(param: &ast::Param) -> bool {
21272160
if let ast::PatKind::Ident(_, ident, _) = param.pat.kind {
2128-
ident.name != symbol::kw::Empty
2161+
ident.name != kw::Empty
21292162
} else {
21302163
true
21312164
}
@@ -2142,7 +2175,7 @@ pub(crate) enum FnBraceStyle {
21422175
fn rewrite_fn_base(
21432176
context: &RewriteContext<'_>,
21442177
indent: Indent,
2145-
ident: symbol::Ident,
2178+
ident: Ident,
21462179
fn_sig: &FnSig<'_>,
21472180
span: Span,
21482181
fn_brace_style: FnBraceStyle,
@@ -2971,7 +3004,7 @@ fn rewrite_comments_before_after_where(
29713004
fn format_header(
29723005
context: &RewriteContext<'_>,
29733006
item_name: &str,
2974-
ident: symbol::Ident,
3007+
ident: Ident,
29753008
vis: &ast::Visibility,
29763009
offset: Indent,
29773010
) -> String {

src/types.rs

Lines changed: 8 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -769,54 +769,14 @@ impl Rewrite for ast::Ty {
769769
ast::TyKind::Tup(ref items) => {
770770
rewrite_tuple(context, items.iter(), self.span, shape, items.len() == 1)
771771
}
772-
ast::TyKind::AnonymousStruct(ref fields, recovered) => {
773-
let ident = Ident::new(
774-
kw::Struct,
775-
mk_sp(self.span.lo(), self.span.lo() + BytePos(6)),
776-
);
777-
let data = ast::VariantData::Struct(fields.clone(), recovered);
778-
let variant = ast::Variant {
779-
attrs: vec![],
780-
id: self.id,
781-
span: self.span,
782-
vis: DEFAULT_VISIBILITY,
783-
ident,
784-
data,
785-
disr_expr: None,
786-
is_placeholder: false,
787-
};
788-
format_struct_struct(
789-
&context,
790-
&StructParts::from_variant(&variant),
791-
fields,
792-
shape.indent,
793-
None,
794-
)
795-
}
796-
ast::TyKind::AnonymousUnion(ref fields, recovered) => {
797-
let ident = Ident::new(
798-
kw::Union,
799-
mk_sp(self.span.lo(), self.span.lo() + BytePos(5)),
800-
);
801-
let data = ast::VariantData::Struct(fields.clone(), recovered);
802-
let variant = ast::Variant {
803-
attrs: vec![],
804-
id: self.id,
805-
span: self.span,
806-
vis: DEFAULT_VISIBILITY,
807-
ident,
808-
data,
809-
disr_expr: None,
810-
is_placeholder: false,
811-
};
812-
format_struct_struct(
813-
&context,
814-
&StructParts::from_variant(&variant),
815-
fields,
816-
shape.indent,
817-
None,
818-
)
819-
}
772+
ast::TyKind::AnonymousStruct(ref fields, _)
773+
| ast::TyKind::AnonymousUnion(ref fields, _) => format_struct_struct(
774+
&context,
775+
&StructParts::from_anonymous_type(&self),
776+
fields,
777+
shape.indent,
778+
None,
779+
),
820780
ast::TyKind::Path(ref q_self, ref path) => {
821781
rewrite_path(context, PathContext::Type, q_self.as_ref(), path, shape)
822782
}

0 commit comments

Comments
 (0)