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
3 changes: 3 additions & 0 deletions crates/config/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ pub struct FormatterConfig {
/// Style that determines if a broken list, should keep its elements together on their own
/// line, before breaking individually.
pub prefer_compact: PreferCompact,
/// Keep single imports on a single line even if they exceed line length.
pub single_line_imports: bool,
}

/// Style of integer types.
Expand Down Expand Up @@ -251,6 +253,7 @@ impl Default for FormatterConfig {
pow_no_space: false,
prefer_compact: PreferCompact::default(),
docs_style: DocCommentStyle::default(),
single_line_imports: false,
}
}
}
1 change: 1 addition & 0 deletions crates/fmt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ The formatter supports multiple configuration options defined in `foundry.toml`.
| `contract_new_lines` | `false` | Add a new line at the start and end of contract declarations. |
| `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 (`**`). |
| `single_line_imports` | `false` | Keep single imports on a single line, even if they exceed the line length limit. |

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

Expand Down
31 changes: 24 additions & 7 deletions crates/fmt/src/state/sol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,19 @@ impl<'ast> State<'_, 'ast> {
}

ast::ImportItems::Aliases(aliases) => {
self.s.cbox(self.ind);
self.word("{");
self.braces_break();
// Check if we should keep single imports on one line
let use_single_line = self.config.single_line_imports && aliases.len() == 1;

if use_single_line {
self.word("{");
if self.config.bracket_spacing {
self.nbsp();
}
} else {
self.s.cbox(self.ind);
self.word("{");
self.braces_break();
}

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

self.braces_break();
self.s.offset(-self.ind);
self.word("}");
self.end();
if use_single_line {
if self.config.bracket_spacing {
self.nbsp();
}
self.word("}");
} else {
self.braces_break();
self.s.offset(-self.ind);
self.word("}");
self.end();
}
self.word(" from ");
self.print_ast_str_lit(path);
}
Expand Down
5 changes: 5 additions & 0 deletions crates/fmt/testdata/ImportDirective/bracket-spacing.fmt.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ import {
symbol3 as alias3,
symbol4
} from "File2.sol";

// Single import that exceeds line length (121 chars)
import {
ITransparentUpgradeableProxy
} from "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";
5 changes: 5 additions & 0 deletions crates/fmt/testdata/ImportDirective/fmt.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ import {
symbol3 as alias3,
symbol4
} from "File2.sol";

// Single import that exceeds line length (121 chars)
import {
ITransparentUpgradeableProxy
} from "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";
3 changes: 3 additions & 0 deletions crates/fmt/testdata/ImportDirective/original.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ import {symbol1 as alias0, symbol2} from "File.sol";
import {symbol1 as alias0, symbol2} from 'File.sol';
import {symbol1 as alias1, symbol2 as alias2, symbol3 as alias3, symbol4} from "File2.sol";
import {symbol1 as alias1, symbol2 as alias2, symbol3 as alias3, symbol4} from 'File2.sol';

// Single import that exceeds line length (121 chars)
import { ITransparentUpgradeableProxy } from "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";
5 changes: 5 additions & 0 deletions crates/fmt/testdata/ImportDirective/preserve-quote.fmt.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ import {
symbol3 as alias3,
symbol4
} from 'File2.sol';

// Single import that exceeds line length (121 chars)
import {
ITransparentUpgradeableProxy
} from "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";
5 changes: 5 additions & 0 deletions crates/fmt/testdata/ImportDirective/single-quote.fmt.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ import {
symbol3 as alias3,
symbol4
} from 'File2.sol';

// Single import that exceeds line length (121 chars)
import {
ITransparentUpgradeableProxy
} from '@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol';
24 changes: 24 additions & 0 deletions crates/fmt/testdata/ImportDirective/single_line_import.fmt.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// config: single_line_imports = true
import "SomeFile.sol";
import "SomeFile.sol";
import "SomeFile.sol" as SomeOtherFile;
import "SomeFile.sol" as SomeOtherFile;
import "AnotherFile.sol" as SomeSymbol;
import "AnotherFile.sol" as SomeSymbol;
import {symbol1 as alias0, symbol2} from "File.sol";
import {symbol1 as alias0, symbol2} from "File.sol";
import {
symbol1 as alias1,
symbol2 as alias2,
symbol3 as alias3,
symbol4
} from "File2.sol";
import {
symbol1 as alias1,
symbol2 as alias2,
symbol3 as alias3,
symbol4
} from "File2.sol";

// Single import that exceeds line length (121 chars)
import {ITransparentUpgradeableProxy} from "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";
4 changes: 3 additions & 1 deletion crates/forge/tests/cli/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ contract_new_lines = false
sort_imports = false
pow_no_space = false
prefer_compact = "all"
single_line_imports = false

[lint]
severity = []
Expand Down Expand Up @@ -1317,7 +1318,8 @@ forgetest_init!(test_default_config, |prj, cmd| {
"contract_new_lines": false,
"sort_imports": false,
"pow_no_space": false,
"prefer_compact": "all"
"prefer_compact": "all",
"single_line_imports": false
},
"lint": {
"severity": [],
Expand Down
Loading