Skip to content

Commit 4613cd6

Browse files
silvekkk0xrusowskygrandizzy
committed
feat(fmt): add single_line_imports option to keep single imports on one line (foundry-rs#12303)
* add single_line_imports option to keep single imports on one line * update readme * add tests for single_line_imports feature + fmt * fix clippy and simplify the code * fix test failed * fix: simplify --------- Co-authored-by: 0xrusowsky <[email protected]> Co-authored-by: grandizzy <[email protected]>
1 parent 116779e commit 4613cd6

File tree

10 files changed

+78
-8
lines changed

10 files changed

+78
-8
lines changed

crates/config/src/fmt.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ pub struct FormatterConfig {
4242
/// Style that determines if a broken list, should keep its elements together on their own
4343
/// line, before breaking individually.
4444
pub prefer_compact: PreferCompact,
45+
/// Keep single imports on a single line even if they exceed line length.
46+
pub single_line_imports: bool,
4547
}
4648

4749
/// Style of integer types.
@@ -250,6 +252,7 @@ impl Default for FormatterConfig {
250252
pow_no_space: false,
251253
prefer_compact: PreferCompact::default(),
252254
docs_style: DocCommentStyle::default(),
255+
single_line_imports: false,
253256
}
254257
}
255258
}

crates/fmt/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ The formatter supports multiple configuration options defined in `foundry.toml`.
128128
| `contract_new_lines` | `false` | Add a new line at the start and end of contract declarations. |
129129
| `sort_imports` | `false` | Sort import statements alphabetically in groups. A group is a set of imports separated by a newline. |
130130
| `pow_no_space` | `false` | Suppress spaces around the power operator (`**`). |
131+
| `single_line_imports` | `false` | Keep single imports on a single line, even if they exceed the line length limit. |
131132

132133

133134
### Inline Configuration

crates/fmt/src/state/sol.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,19 @@ impl<'ast> State<'_, 'ast> {
215215
}
216216

217217
ast::ImportItems::Aliases(aliases) => {
218-
self.s.cbox(self.ind);
219-
self.word("{");
220-
self.braces_break();
218+
// Check if we should keep single imports on one line
219+
let use_single_line = self.config.single_line_imports && aliases.len() == 1;
220+
221+
if use_single_line {
222+
self.word("{");
223+
if self.config.bracket_spacing {
224+
self.nbsp();
225+
}
226+
} else {
227+
self.s.cbox(self.ind);
228+
self.word("{");
229+
self.braces_break();
230+
}
221231

222232
if self.config.sort_imports {
223233
let mut sorted: Vec<_> = aliases.iter().collect();
@@ -227,10 +237,17 @@ impl<'ast> State<'_, 'ast> {
227237
self.print_commasep_aliases(aliases.iter());
228238
};
229239

230-
self.braces_break();
231-
self.s.offset(-self.ind);
232-
self.word("}");
233-
self.end();
240+
if use_single_line {
241+
if self.config.bracket_spacing {
242+
self.nbsp();
243+
}
244+
self.word("}");
245+
} else {
246+
self.braces_break();
247+
self.s.offset(-self.ind);
248+
self.word("}");
249+
self.end();
250+
}
234251
self.word(" from ");
235252
self.print_ast_str_lit(path);
236253
}

crates/fmt/testdata/ImportDirective/bracket-spacing.fmt.sol

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,8 @@ import {
1919
symbol3 as alias3,
2020
symbol4
2121
} from "File2.sol";
22+
23+
// Single import that exceeds line length (121 chars)
24+
import {
25+
ITransparentUpgradeableProxy
26+
} from "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";

crates/fmt/testdata/ImportDirective/fmt.sol

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,8 @@ import {
1818
symbol3 as alias3,
1919
symbol4
2020
} from "File2.sol";
21+
22+
// Single import that exceeds line length (121 chars)
23+
import {
24+
ITransparentUpgradeableProxy
25+
} from "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";

crates/fmt/testdata/ImportDirective/original.sol

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ import {symbol1 as alias0, symbol2} from "File.sol";
88
import {symbol1 as alias0, symbol2} from 'File.sol';
99
import {symbol1 as alias1, symbol2 as alias2, symbol3 as alias3, symbol4} from "File2.sol";
1010
import {symbol1 as alias1, symbol2 as alias2, symbol3 as alias3, symbol4} from 'File2.sol';
11+
12+
// Single import that exceeds line length (121 chars)
13+
import { ITransparentUpgradeableProxy } from "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";

crates/fmt/testdata/ImportDirective/preserve-quote.fmt.sol

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,8 @@ import {
1919
symbol3 as alias3,
2020
symbol4
2121
} from 'File2.sol';
22+
23+
// Single import that exceeds line length (121 chars)
24+
import {
25+
ITransparentUpgradeableProxy
26+
} from "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";

crates/fmt/testdata/ImportDirective/single-quote.fmt.sol

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,8 @@ import {
1919
symbol3 as alias3,
2020
symbol4
2121
} from 'File2.sol';
22+
23+
// Single import that exceeds line length (121 chars)
24+
import {
25+
ITransparentUpgradeableProxy
26+
} from '@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol';
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// config: single_line_imports = true
2+
import "SomeFile.sol";
3+
import "SomeFile.sol";
4+
import "SomeFile.sol" as SomeOtherFile;
5+
import "SomeFile.sol" as SomeOtherFile;
6+
import "AnotherFile.sol" as SomeSymbol;
7+
import "AnotherFile.sol" as SomeSymbol;
8+
import {symbol1 as alias0, symbol2} from "File.sol";
9+
import {symbol1 as alias0, symbol2} from "File.sol";
10+
import {
11+
symbol1 as alias1,
12+
symbol2 as alias2,
13+
symbol3 as alias3,
14+
symbol4
15+
} from "File2.sol";
16+
import {
17+
symbol1 as alias1,
18+
symbol2 as alias2,
19+
symbol3 as alias3,
20+
symbol4
21+
} from "File2.sol";
22+
23+
// Single import that exceeds line length (121 chars)
24+
import {ITransparentUpgradeableProxy} from "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";

crates/forge/tests/cli/config.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ contract_new_lines = false
138138
sort_imports = false
139139
pow_no_space = false
140140
prefer_compact = "all"
141+
single_line_imports = false
141142
142143
[lint]
143144
severity = []
@@ -1312,7 +1313,8 @@ forgetest_init!(test_default_config, |prj, cmd| {
13121313
"contract_new_lines": false,
13131314
"sort_imports": false,
13141315
"pow_no_space": false,
1315-
"prefer_compact": "all"
1316+
"prefer_compact": "all",
1317+
"single_line_imports": false
13161318
},
13171319
"lint": {
13181320
"severity": [],

0 commit comments

Comments
 (0)