Skip to content

Commit

Permalink
fix(fmt): properly format enums (#6637)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Dec 20, 2023
1 parent 8be2649 commit 70195e7
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 22 deletions.
40 changes: 18 additions & 22 deletions crates/fmt/src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2038,31 +2038,27 @@ impl<'a, W: Write> Visitor for Formatter<'a, W> {
let enum_name = enumeration.name.safe_unwrap_mut();
let mut name =
self.visit_to_chunk(enum_name.loc.start(), Some(enum_name.loc.end()), enum_name)?;
name.content = format!("enum {}", name.content);
self.write_chunk(&name)?;

name.content = format!("enum {} ", name.content);
if enumeration.values.is_empty() {
self.write_chunk(&name)?;
self.write_empty_brackets()?;
} else {
self.surrounded(
SurroundingChunk::new(
"{",
Some(enumeration.values.first_mut().unwrap().safe_unwrap().loc.start()),
None,
),
SurroundingChunk::new("}", None, Some(enumeration.loc.end())),
|fmt, _multiline| {
let values = fmt.items_to_chunks(
Some(enumeration.loc.end()),
enumeration.values.iter_mut().map(|ident| {
let ident = ident.safe_unwrap_mut();
(ident.loc, ident)
}),
)?;
fmt.write_chunks_separated(&values, ",", true)?;
Ok(())
},
)?;
name.content.push('{');
self.write_chunk(&name)?;

self.indented(1, |fmt| {
let values = fmt.items_to_chunks(
Some(enumeration.loc.end()),
enumeration.values.iter_mut().map(|ident| {
let ident = ident.safe_unwrap_mut();
(ident.loc, ident)
}),
)?;
fmt.write_chunks_separated(&values, ",", true)?;
writeln!(fmt.buf())?;
Ok(())
})?;
write_chunk!(self, "}}")?;
}

Ok(())
Expand Down
19 changes: 19 additions & 0 deletions crates/fmt/testdata/EnumVariants/fmt.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
interface I {
enum Empty {}

/// A modification applied to either `msg.sender` or `tx.origin`. Returned by `readCallers`.
enum CallerMode {
/// No caller modification is currently active.
None
}

/// A modification applied to either `msg.sender` or `tx.origin`. Returned by `readCallers`.
enum CallerMode2 {
/// No caller modification is currently active.
None,
/// No caller modification is currently active2.
Some
}

function bar() public {}
}
23 changes: 23 additions & 0 deletions crates/fmt/testdata/EnumVariants/original.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
interface I {
enum Empty {

}

/// A modification applied to either `msg.sender` or `tx.origin`. Returned by `readCallers`.
enum CallerMode
{/// No caller modification is currently active.
None
}

/// A modification applied to either `msg.sender` or `tx.origin`. Returned by `readCallers`.
enum CallerMode2
{/// No caller modification is currently active.
None,/// No caller modification is currently active2.

Some
}

function bar() public {

}
}
1 change: 1 addition & 0 deletions crates/fmt/tests/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ test_directories! {
EmitStatement,
Repros,
BlockComments,
EnumVariants,
}

test_dir!(SortedImports, TestConfig::skip_compare_ast_eq());

0 comments on commit 70195e7

Please sign in to comment.