Skip to content

Commit 17bbb40

Browse files
chore(fmt): prefer_compact + indent bin expr w/ parenthesis (#12281)
* fix: indentation in operator exprs with parentheses * docs: new param * feat: prefer_compact * fix: test * style: typo * fix: default config test * Fix wrap comments test * fix: emit and revert --------- Co-authored-by: grandizzy <[email protected]>
1 parent 27cf095 commit 17bbb40

File tree

18 files changed

+164
-50
lines changed

18 files changed

+164
-50
lines changed

crates/config/src/fmt.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ pub struct FormatterConfig {
3939
pub sort_imports: bool,
4040
/// Whether to suppress spaces around the power operator (`**`).
4141
pub pow_no_space: bool,
42-
/// Whether to compact call args in a single line when possible
43-
pub call_compact_args: bool,
42+
/// Style that determines if a broken list, should keep its elements together on their own
43+
/// line, before breaking individually.
44+
pub prefer_compact: PreferCompact,
4445
}
4546

4647
/// Style of integer types.
@@ -186,6 +187,40 @@ impl MultilineFuncHeaderStyle {
186187
}
187188
}
188189

190+
/// Style that determines if a broken list, should keep its elements together on their own line,
191+
/// before breaking individually.
192+
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
193+
#[serde(rename_all = "snake_case")]
194+
pub enum PreferCompact {
195+
/// All elements are preferred consistent.
196+
None,
197+
/// Calls are preferred compact. Events and errors break consistently.
198+
Calls,
199+
/// Events are preferred compact. Calls and errors break consistently.
200+
Events,
201+
/// Errors are preferred compact. Calls and events break consistently.
202+
Errors,
203+
/// Events and errors are preferred compact. Calls break consistently.
204+
EventsErrors,
205+
/// All elements are preferred compact.
206+
#[default]
207+
All,
208+
}
209+
210+
impl PreferCompact {
211+
pub fn calls(&self) -> bool {
212+
matches!(self, Self::All | Self::Calls)
213+
}
214+
215+
pub fn events(&self) -> bool {
216+
matches!(self, Self::All | Self::Events | Self::EventsErrors)
217+
}
218+
219+
pub fn errors(&self) -> bool {
220+
matches!(self, Self::All | Self::Errors | Self::EventsErrors)
221+
}
222+
}
223+
189224
/// Style of indent
190225
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
191226
#[serde(rename_all = "snake_case")]
@@ -214,7 +249,7 @@ impl Default for FormatterConfig {
214249
contract_new_lines: false,
215250
sort_imports: false,
216251
pow_no_space: false,
217-
call_compact_args: true,
252+
prefer_compact: PreferCompact::default(),
218253
docs_style: DocCommentStyle::default(),
219254
}
220255
}

crates/fmt/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ The formatter supports multiple configuration options defined in `foundry.toml`.
116116
| `bracket_spacing` | `false` | Print spaces between brackets. |
117117
| `int_types` | `long` | Style for `uint256`/`int256` types. Options: `long`, `short`, `preserve`. |
118118
| `multiline_func_header` | `attributes_first` | The style of multiline function headers. Options: `attributes_first`, `params_always`, `params_first_multi`, `all`, `all_params`. |
119+
| `prefer_compact` | `calls` | Style that determines if a broken list, should keep its elements together on their own line, before breaking individually. Options: `calls`, `events`, `errors`, `events_errors`, `all`. |
119120
| `quote_style` | `double` | The style of quotation marks. Options: `double`, `single`, `preserve`. |
120121
| `number_underscore` | `preserve` | The style of underscores in number literals. Options: `preserve`, `remove`, `thousands`. |
121122
| `hex_underscore` | `remove` | The style of underscores in hex literals. Options: `preserve`, `remove`, `bytes`. |

crates/fmt/src/state/common.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,9 @@ impl<'ast> State<'_, 'ast> {
524524
for (pos, ident) in path.segments().iter().delimited() {
525525
self.print_ident(ident);
526526
if !pos.is_last {
527-
self.zerobreak();
527+
if !self.emit_or_revert {
528+
self.zerobreak();
529+
}
528530
self.word(".");
529531
}
530532
}

crates/fmt/src/state/sol.rs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -725,15 +725,31 @@ impl<'ast> State<'_, 'ast> {
725725
let ast::ItemError { name, parameters } = err;
726726
self.word("error ");
727727
self.print_ident(name);
728-
self.print_parameter_list(parameters, parameters.span, ListFormat::compact());
728+
self.print_parameter_list(
729+
parameters,
730+
parameters.span,
731+
if self.config.prefer_compact.errors() {
732+
ListFormat::compact()
733+
} else {
734+
ListFormat::consistent()
735+
},
736+
);
729737
self.word(";");
730738
}
731739

732740
fn print_event(&mut self, event: &'ast ast::ItemEvent<'ast>) {
733741
let ast::ItemEvent { name, parameters, anonymous } = event;
734742
self.word("event ");
735743
self.print_ident(name);
736-
self.print_parameter_list(parameters, parameters.span, ListFormat::compact().break_cmnts());
744+
self.print_parameter_list(
745+
parameters,
746+
parameters.span,
747+
if self.config.prefer_compact.events() {
748+
ListFormat::compact().break_cmnts()
749+
} else {
750+
ListFormat::consistent().break_cmnts()
751+
},
752+
);
737753
if *anonymous {
738754
self.word(" anonymous");
739755
}
@@ -1701,7 +1717,7 @@ impl<'ast> State<'_, 'ast> {
17011717
}
17021718

17031719
fn print_named_args(&mut self, args: &'ast [ast::NamedArg<'ast>], pos_hi: BytePos) {
1704-
let list_format = match (self.config.bracket_spacing, self.config.call_compact_args) {
1720+
let list_format = match (self.config.bracket_spacing, self.config.prefer_compact.calls()) {
17051721
(false, true) => ListFormat::compact(),
17061722
(false, false) => ListFormat::consistent(),
17071723
(true, true) => ListFormat::compact().with_space(),
@@ -1738,7 +1754,7 @@ impl<'ast> State<'_, 'ast> {
17381754
.break_cmnts()
17391755
.break_single(true)
17401756
.without_ind(self.call_stack.is_chain())
1741-
.with_delimiters(!(self.emit_or_revert || self.call_with_opts_and_args)),
1757+
.with_delimiters(!self.call_with_opts_and_args),
17421758
);
17431759
} else if self.config.bracket_spacing {
17441760
self.nbsp();
@@ -2264,17 +2280,14 @@ impl<'ast> State<'_, 'ast> {
22642280
self.nbsp();
22652281
};
22662282
self.s.cbox(0);
2267-
self.print_path(path, false);
22682283
self.emit_or_revert = path.segments().len() > 1;
2269-
self.print_call_args(
2270-
args,
2271-
if self.config.call_compact_args {
2272-
ListFormat::compact().break_cmnts().with_delimiters(args.len() == 1)
2273-
} else {
2274-
ListFormat::consistent().break_cmnts().with_delimiters(args.len() == 1)
2275-
},
2276-
path.to_string().len(),
2277-
);
2284+
self.print_path(path, false);
2285+
let format = if self.config.prefer_compact.calls() {
2286+
ListFormat::compact()
2287+
} else {
2288+
ListFormat::consistent()
2289+
};
2290+
self.print_call_args(args, format.break_cmnts(), path.to_string().len());
22782291
self.emit_or_revert = false;
22792292
self.end();
22802293
}
@@ -2797,6 +2810,7 @@ fn has_complex_successor(expr_kind: &ast::ExprKind<'_>, left: bool) -> bool {
27972810
}
27982811
ast::ExprKind::Unary(_, expr) => has_complex_successor(&expr.kind, left),
27992812
ast::ExprKind::Lit(..) | ast::ExprKind::Ident(_) => false,
2813+
ast::ExprKind::Tuple(..) => false,
28002814
_ => true,
28012815
}
28022816
}

crates/fmt/testdata/DocComments/wrap-comments.fmt.sol

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// config: line_length = 40
22
// config: wrap_comments = true
3-
// config: call_compact_args = false
43
pragma solidity ^0.8.13;
54

65
/// @title A Hello world example
@@ -24,8 +23,7 @@ contract HelloWorld {
2423
/// @param age The dude's age
2524
constructor(uint256 age) {
2625
theDude = Person({
27-
age: age,
28-
wallet: msg.sender
26+
age: age, wallet: msg.sender
2927
});
3028
}
3129

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// config: line_length = 120
2+
event NewEvent(address beneficiary, uint256 index, uint64 timestamp, uint64 endTimestamp);
3+
4+
function emitEvent() {
5+
emit NewEvent(beneficiary, _vestingBeneficiaries.length - 1, uint64(block.timestamp), endTimestamp);
6+
7+
emit NewEvent( /* beneficiary */
8+
beneficiary,
9+
/* index */
10+
_vestingBeneficiaries.length - 1,
11+
/* timestamp */
12+
uint64(block.timestamp),
13+
/* end timestamp */
14+
endTimestamp
15+
);
16+
17+
emit NewEvent(
18+
beneficiary, // beneficiary
19+
_vestingBeneficiaries.length - 1, // index
20+
uint64(block.timestamp), // timestamp
21+
endTimestamp // end timestamp
22+
);
23+
24+
// https://github.com/foundry-rs/foundry/issues/12029
25+
emit OperatorSharesDecreased(
26+
defaultOperator,
27+
address(0),
28+
strategyMock,
29+
depositAmount / 6 // 1 withdrawal not queued so decreased
30+
);
31+
32+
// https://github.com/foundry-rs/foundry/issues/12146
33+
emit ISablierComptroller.DisableCustomFeeUSD(
34+
protocol_protocol, caller_caller, user_users.sender, previousMinFeeUSD_0, newMinFeeUSD_feeUSD
35+
);
36+
emit ISablierComptroller.DisableCustomFeeUSD({
37+
protocol: protocol, caller: caller, user: users.sender, previousMinFeeUSD: 0, newMinFeeUSD: feeUSD
38+
});
39+
40+
emit ISablierLockupLinear.CreateLockupLinearStream({
41+
streamId: streamId,
42+
commonParams: Lockup.CreateEventCommon({
43+
funder: msg.sender, sender: sender, recipient: recipient, depositAmount: depositAmount
44+
}),
45+
cliffTime: cliffTime,
46+
unlockAmounts: unlockAmounts
47+
});
48+
}

crates/fmt/testdata/EmitStatement/120.fmt.sol

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// config: line_length = 120
2+
// config: prefer_compact = "none"
23
event NewEvent(address beneficiary, uint256 index, uint64 timestamp, uint64 endTimestamp);
34

45
function emitEvent() {
@@ -30,9 +31,19 @@ function emitEvent() {
3031
);
3132

3233
// https://github.com/foundry-rs/foundry/issues/12146
33-
emit ISablierComptroller.DisableCustomFeeUSD(protocol, caller, users.sender, 0, feeUSD);
34+
emit ISablierComptroller.DisableCustomFeeUSD(
35+
protocol_protocol,
36+
caller_caller,
37+
user_users.sender,
38+
previousMinFeeUSD_0,
39+
newMinFeeUSD_feeUSD
40+
);
3441
emit ISablierComptroller.DisableCustomFeeUSD({
35-
protocol: protocol, caller: caller, user: users.sender, previousMinFeeUSD: 0, newMinFeeUSD: feeUSD
42+
protocol: protocol,
43+
caller: caller,
44+
user: users.sender,
45+
previousMinFeeUSD: 0,
46+
newMinFeeUSD: feeUSD
3647
});
3748

3849
emit ISablierLockupLinear.CreateLockupLinearStream({
@@ -41,12 +52,7 @@ function emitEvent() {
4152
funder: msg.sender,
4253
sender: sender,
4354
recipient: recipient,
44-
depositAmount: depositAmount,
45-
token: token,
46-
cancelable: cancelable,
47-
transferable: transferable,
48-
timestamps: timestamps,
49-
shape: shape
55+
depositAmount: depositAmount
5056
}),
5157
cliffTime: cliffTime,
5258
unlockAmounts: unlockAmounts

crates/fmt/testdata/EmitStatement/fmt.sol

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ function emitEvent() {
3838

3939
// https://github.com/foundry-rs/foundry/issues/12146
4040
emit ISablierComptroller.DisableCustomFeeUSD(
41-
protocol, caller, users.sender, 0, feeUSD
41+
protocol_protocol,
42+
caller_caller,
43+
user_users.sender,
44+
previousMinFeeUSD_0,
45+
newMinFeeUSD_feeUSD
4246
);
4347
emit ISablierComptroller.DisableCustomFeeUSD({
4448
protocol: protocol,
@@ -54,12 +58,7 @@ function emitEvent() {
5458
funder: msg.sender,
5559
sender: sender,
5660
recipient: recipient,
57-
depositAmount: depositAmount,
58-
token: token,
59-
cancelable: cancelable,
60-
transferable: transferable,
61-
timestamps: timestamps,
62-
shape: shape
61+
depositAmount: depositAmount
6362
}),
6463
cliffTime: cliffTime,
6564
unlockAmounts: unlockAmounts

crates/fmt/testdata/EmitStatement/original.sol

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function emitEvent() {
3131
);
3232

3333
// https://github.com/foundry-rs/foundry/issues/12146
34-
emit ISablierComptroller.DisableCustomFeeUSD(protocol, caller, users.sender, 0, feeUSD);
34+
emit ISablierComptroller.DisableCustomFeeUSD(protocol_protocol, caller_caller, user_users.sender, previousMinFeeUSD_0, newMinFeeUSD_feeUSD);
3535
emit ISablierComptroller.DisableCustomFeeUSD({ protocol: protocol, caller: caller, user: users.sender, previousMinFeeUSD: 0, newMinFeeUSD: feeUSD });
3636

3737
emit ISablierLockupLinear.CreateLockupLinearStream({
@@ -40,12 +40,7 @@ function emitEvent() {
4040
funder: msg.sender,
4141
sender: sender,
4242
recipient: recipient,
43-
depositAmount: depositAmount,
44-
token: token,
45-
cancelable: cancelable,
46-
transferable: transferable,
47-
timestamps: timestamps,
48-
shape: shape
43+
depositAmount: depositAmount
4944
}),
5045
cliffTime: cliffTime,
5146
unlockAmounts: unlockAmounts

crates/fmt/testdata/NamedFunctionCallExpression/fmt.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// config: call_compact_args = false
1+
// config: prefer_compact = "events_errors"
22
contract NamedFunctionCallExpression {
33
struct SimpleStruct {
44
uint256 val;

0 commit comments

Comments
 (0)