Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(fmt): properly format enums #6637

Merged
merged 1 commit into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());