Skip to content

Commit 46bf86b

Browse files
authored
fix(fmt): fn estimate_size() account for opening brackets (#11887)
* fix(fmt): `fn estimate_size()` account for opening brackets if `config.bracket_spacing = true` * fix: rmv debug logs * fix: match on `[` too
1 parent 6d69231 commit 46bf86b

File tree

5 files changed

+36
-8
lines changed

5 files changed

+36
-8
lines changed

crates/fmt/src/state/mod.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -386,12 +386,17 @@ impl State<'_, '_> {
386386

387387
if prev_needs_space {
388388
size += 1;
389-
} else if !first
390-
&& let Some(c) = line.chars().next()
391-
&& matches!(c, '&' | '|' | '=' | '>' | '<' | '+' | '-' | '*' | '/' | '%' | '^')
392-
{
393-
// if the line starts with an operator, a space or a line break are required.
394-
size += 1
389+
} else if !first && let Some(char) = line.chars().next() {
390+
// A line break or a space are required if this line:
391+
// - starts with an operator.
392+
// - starts with a bracket and fmt config forces bracket spacing.
393+
match char {
394+
'&' | '|' | '=' | '>' | '<' | '+' | '-' | '*' | '/' | '%' | '^' => {
395+
size += 1
396+
}
397+
'}' | ')' | ']' if self.config.bracket_spacing => size += 1,
398+
_ => (),
399+
}
395400
}
396401
first = false;
397402

@@ -415,7 +420,7 @@ impl State<'_, '_> {
415420
// - ends with ',' a line break or a space are required.
416421
// - ends with ';' a line break is required.
417422
prev_needs_space = match line.chars().next_back() {
418-
Some('(') | Some('{') => self.config.bracket_spacing,
423+
Some('[') | Some('(') | Some('{') => self.config.bracket_spacing,
419424
Some(',') | Some(';') => true,
420425
_ => false,
421426
};

crates/fmt/testdata/ReprosCalls/110.fmt.sol

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ contract Orchestrator {
132132
self.feeGrowthGlobal0X128 - lower.feeGrowthOutside0X128 - upper.feeGrowthOutside0X128;
133133
feeGrowthInside0X128 =
134134
self.feeGrowthGlobal0X128 - lower.feeGrowthOutside0X128 - upper.feeGrowthOutside0X128;
135+
136+
// https://github.com/foundry-rs/foundry/issues/11875
137+
lpTail = LpPosition({
138+
tickLower: posTickLower, tickUpper: posTickUpper, liquidity: lpTailLiquidity, id: uint16(id)
139+
});
135140
}
136141

137142
// https://github.com/foundry-rs/foundry/issues/11834

crates/fmt/testdata/ReprosCalls/120.fmt.sol

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// config: line_length = 120
2+
// config: bracket_spacing = true
23
function test() public {
34
require(
45
keccak256(abi.encodePacked("this is a long string")) == keccak256(abi.encodePacked("some other long string")),
@@ -25,7 +26,7 @@ function test() public {
2526
);
2627

2728
oracleInfo =
28-
abi.encode(LidoOracleInfo({base: IOracle(oracleAddress).WSTETH(), quote: IOracle(oracleAddress).STETH()}));
29+
abi.encode(LidoOracleInfo({ base: IOracle(oracleAddress).WSTETH(), quote: IOracle(oracleAddress).STETH() }));
2930

3031
return someFunction().getValue().modifyValue().negate().scaleBySomeFactor(1000).transformToTuple();
3132

@@ -119,6 +120,11 @@ contract Orchestrator {
119120
// https://github.com/foundry-rs/foundry/issues/11835
120121
feeGrowthInside0X128 = self.feeGrowthGlobal0X128 - lower.feeGrowthOutside0X128 - upper.feeGrowthOutside0X128;
121122
feeGrowthInside0X128 = self.feeGrowthGlobal0X128 - lower.feeGrowthOutside0X128 - upper.feeGrowthOutside0X128;
123+
124+
// https://github.com/foundry-rs/foundry/issues/11875
125+
lpTail = LpPosition({
126+
tickLower: posTickLower, tickUpper: posTickUpper, liquidity: lpTailLiquidity, id: uint16(id)
127+
});
122128
}
123129

124130
// https://github.com/foundry-rs/foundry/issues/11834

crates/fmt/testdata/ReprosCalls/80.fmt.sol

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,14 @@ contract Orchestrator {
189189
- lower.feeGrowthOutside0X128 - upper.feeGrowthOutside0X128;
190190
feeGrowthInside0X128 = self.feeGrowthGlobal0X128
191191
- lower.feeGrowthOutside0X128 - upper.feeGrowthOutside0X128;
192+
193+
// https://github.com/foundry-rs/foundry/issues/11875
194+
lpTail = LpPosition({
195+
tickLower: posTickLower,
196+
tickUpper: posTickUpper,
197+
liquidity: lpTailLiquidity,
198+
id: uint16(id)
199+
});
192200
}
193201

194202
// https://github.com/foundry-rs/foundry/issues/11834

crates/fmt/testdata/ReprosCalls/original.sol

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ contract Orchestrator {
125125
self.feeGrowthGlobal0X128 - lower.feeGrowthOutside0X128 - upper.feeGrowthOutside0X128;
126126
feeGrowthInside0X128 = self.feeGrowthGlobal0X128
127127
- lower.feeGrowthOutside0X128 - upper.feeGrowthOutside0X128;
128+
129+
// https://github.com/foundry-rs/foundry/issues/11875
130+
lpTail =
131+
LpPosition({ tickLower: posTickLower, tickUpper: posTickUpper, liquidity: lpTailLiquidity, id: uint16(id) });
128132
}
129133

130134
// https://github.com/foundry-rs/foundry/issues/11834

0 commit comments

Comments
 (0)