Skip to content

Commit

Permalink
refactor visit_enum
Browse files Browse the repository at this point in the history
- introduce format_enum that returns Rewrite
- early return when it fails to format the generics in enum
  • Loading branch information
ding-young committed Nov 29, 2024
1 parent ff6ebd3 commit af03169
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 20 deletions.
42 changes: 27 additions & 15 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,21 +522,22 @@ impl<'a> FmtVisitor<'a> {
self.push_rewrite(struct_parts.span, rewrite);
}

pub(crate) fn visit_enum(
// TODO(ding-young) do I need to make it a separate function instead of a method of FmtVisitor?
fn format_enum(
&mut self,
ident: symbol::Ident,
vis: &ast::Visibility,
enum_def: &ast::EnumDef,
generics: &ast::Generics,
span: Span,
) {
) -> Option<String> {
let enum_header =
format_header(&self.get_context(), "enum ", ident, vis, self.block_indent);
self.push_str(&enum_header);

let enum_snippet = self.snippet(span);
let brace_pos = enum_snippet.find_uncommented("{").unwrap();
let body_start = span.lo() + BytePos(brace_pos as u32 + 1);
// TODO(ding-young) what if there is no generic?
let generics_str = format_generics(
&self.get_context(),
generics,
Expand All @@ -550,25 +551,36 @@ impl<'a> FmtVisitor<'a> {
// make a span that starts right after `enum Foo`
mk_sp(ident.span.hi(), body_start),
last_line_width(&enum_header),
);

if let Some(generics_str) = generics_str {
self.push_str(&generics_str);
} else {
self.push_str(self.snippet(mk_sp(ident.span.hi(), body_start)));
}

self.last_pos = body_start;
)?;

match self.format_variant_list(enum_def, body_start, span.hi()) {
Some(ref s) if enum_def.variants.is_empty() => self.push_str(s),
rw => {
self.push_rewrite(mk_sp(body_start, span.hi()), rw);
Some(ref s) if enum_def.variants.is_empty() => {
Some(format!("{enum_header}{generics_str}{s}"))
}
Some(rw) => {
let indent = self.block_indent.to_string(self.config);
self.block_indent = self.block_indent.block_unindent(self.config);
Some(format!("{enum_header}{generics_str}\n{indent}{rw}"))
}
None => {
self.block_indent = self.block_indent.block_unindent(self.config);
None
}
}
}

pub(crate) fn visit_enum(
&mut self,
ident: symbol::Ident,
vis: &ast::Visibility,
enum_def: &ast::EnumDef,
generics: &ast::Generics,
span: Span,
) {
let rewrite = self.format_enum(ident, vis, enum_def, generics, span);
self.push_rewrite(span, rewrite);
}

// Format the body of an enum definition
fn format_variant_list(
&mut self,
Expand Down
2 changes: 0 additions & 2 deletions src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,9 +515,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
self.visit_struct(&StructParts::from_item(item));
}
ast::ItemKind::Enum(ref def, ref generics) => {
self.format_missing_with_indent(source!(self, item.span).lo());
self.visit_enum(item.ident, &item.vis, def, generics, item.span);
self.last_pos = source!(self, item.span).hi();
}
ast::ItemKind::Mod(safety, ref mod_kind) => {
self.format_missing_with_indent(source!(self, item.span).lo());
Expand Down
2 changes: 1 addition & 1 deletion tests/source/crash-on-enum-generics/issue_5738.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ where
{
V0,
V1,
V2,
V2, // left unformatted since formatting where clause fails
V3,
}
1 change: 1 addition & 0 deletions tests/source/crash-on-enum-generics/issue_6318.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ fn my_fn() {
Option<[u8; 4]>,
>>::Archived,
>,
// left unformatted since formatting where clause fails
{
}
}
2 changes: 1 addition & 1 deletion tests/target/crash-on-enum-generics/issue_5738.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ where
{
V0,
V1,
V2,
V2, // left unformatted since formatting where clause fails
V3,
}
4 changes: 3 additions & 1 deletion tests/target/crash-on-enum-generics/issue_6318.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ fn my_fn() {
Option<[u8; 4]>,
>>::Archived,
>,
{}
// left unformatted since formatting where clause fails
{
}
}

0 comments on commit af03169

Please sign in to comment.