Skip to content
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
7 changes: 4 additions & 3 deletions crates/config/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,9 @@ pub enum SingleLineBlockStyle {
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MultilineFuncHeaderStyle {
/// Write function parameters multiline first.
ParamsFirst,
/// Always write function parameters multiline.
#[serde(alias = "params_first")] // alias for backwards compatibility
ParamsAlways,
/// Write function parameters multiline first when there is more than one param.
ParamsFirstMulti,
/// Write function attributes multiline first.
Expand All @@ -162,7 +163,7 @@ impl MultilineFuncHeaderStyle {
}

pub fn params_first(&self) -> bool {
matches!(self, Self::ParamsFirst | Self::ParamsFirstMulti)
matches!(self, Self::ParamsAlways | Self::ParamsFirstMulti)
}

pub fn attrib_first(&self) -> bool {
Expand Down
3 changes: 2 additions & 1 deletion crates/fmt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ The formatter supports multiple configuration options defined in `foundry.toml`.
| `style` | `space` | The style of indentation. Options: `space`, `tab`. |
| `bracket_spacing` | `false` | Print spaces between brackets. |
| `int_types` | `long` | Style for `uint256`/`int256` types. Options: `long`, `short`, `preserve`. |
| `multiline_func_header` | `attributes_first` | The style of multiline function headers. Options: `attributes_first`, `params_first`, `params_first_multi`, `all`, `all_params`. |
| `multiline_func_header` | `attributes_first` | The style of multiline function headers. Options: `attributes_first`, `params_always`, `params_first_multi`, `all`, `all_params`. |
| `quote_style` | `double` | The style of quotation marks. Options: `double`, `single`, `preserve`. |
| `number_underscore` | `preserve` | The style of underscores in number literals. Options: `preserve`, `remove`, `thousands`. |
| `hex_underscore` | `remove` | The style of underscores in hex literals. Options: `preserve`, `remove`, `bytes`. |
Expand All @@ -127,6 +127,7 @@ The formatter supports multiple configuration options defined in `foundry.toml`.
| `sort_imports` | `false` | Sort import statements alphabetically in groups. A group is a set of imports separated by a newline. |
| `pow_no_space` | `false` | Suppress spaces around the power operator (`**`). |

> Check [`FormatterConfig`](../config/src/fmt.rs) for a more detailed explanation.

### Inline Configuration

Expand Down
2 changes: 1 addition & 1 deletion crates/fmt/src/state/sol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ impl<'ast> State<'_, 'ast> {
self.s.cbox(-self.ind);
let header_style = self.config.multiline_func_header;
let params_format = match header_style {
MultilineFuncHeaderStyle::ParamsFirst => ListFormat::always_break(),
MultilineFuncHeaderStyle::ParamsAlways => ListFormat::always_break(),
MultilineFuncHeaderStyle::AllParams
if !header.parameters.is_empty() && !self.can_header_be_inlined(header) =>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// config: line_length = 60
// config: multiline_func_header = "params_first"
// config: multiline_func_header = "params_always"
interface FunctionInterfaces {
function noParamsNoModifiersNoReturns();

Expand Down
27 changes: 25 additions & 2 deletions crates/forge/tests/cli/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ Diff in stdin:
1 1 | // SPDX-License-Identifier: MIT
2 |-pragma solidity =0.8.30 ;
2 |+pragma solidity =0.8.30;
3 3 |
...
4 |-contract Test {
5 |- uint256 public value ;
6 |- function setValue ( uint256 _value ) public {
7 |- value = _value ;
4 |+contract Test {
5 |+ uint256 public value;
6 |+
...
7 |+ function setValue(uint256 _value) public {
8 |+ value = _value;
8 9 | }
Expand All @@ -103,3 +103,26 @@ forgetest!(fmt_stdin_original, |_prj, cmd| {
cmd.stdin(FORMATTED.as_bytes());
cmd.assert_success().stdout_eq(FORMATTED.as_bytes());
});

// Test that fmt can format a simple contract file
forgetest_init!(fmt_file_config_parms_first, |prj, cmd| {
prj.create_file(
"foundry.toml",
r#"
[fmt]
multiline_func_header = 'params_first'
"#,
);
prj.add_raw_source("FmtTest.sol", FORMATTED);
cmd.forge_fuse().args(["fmt", "--check"]).arg("src/FmtTest.sol");
cmd.assert_failure().stdout_eq(str![[r#"
Diff in src/FmtTest.sol:
...
7 |- function setValue(uint256 _value) public {
7 |+ function setValue(
8 |+ uint256 _value
9 |+ ) public {
...

"#]]);
});
Loading