Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
59 changes: 43 additions & 16 deletions crates/fmt/src/state/sol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,24 +215,51 @@ impl<'ast> State<'_, 'ast> {
}

ast::ImportItems::Aliases(aliases) => {
self.s.cbox(self.ind);
self.word("{");
self.braces_break();

if self.config.sort_imports {
let mut sorted: Vec<_> = aliases.iter().collect();
sorted.sort_by_key(|(ident, _alias)| ident.name.as_str());
self.print_commasep_aliases(sorted.into_iter());
// 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 {
// Single import on one line - no box at all to avoid line wrapping
self.word("{");
if self.config.bracket_spacing {
self.word(" ");
}

// Print single alias directly without commasep to avoid spaces
if let Some((ident, alias)) = aliases.first() {
self.print_ident(ident);
if let Some(alias) = alias {
self.word(" as ");
self.print_ident(alias);
}
}

if self.config.bracket_spacing {
self.word(" ");
}
self.word("} from ");
self.print_ast_str_lit(path);
} else {
self.print_commasep_aliases(aliases.iter());
};
// Multi-line format for multiple imports or when single_line_imports is false
self.s.cbox(self.ind);
self.word("{");
self.braces_break();

if self.config.sort_imports {
let mut sorted: Vec<_> = aliases.iter().collect();
sorted.sort_by_key(|(ident, _alias)| ident.name.as_str());
self.print_commasep_aliases(sorted.into_iter());
} else {
self.print_commasep_aliases(aliases.iter());
};

self.braces_break();
self.s.offset(-self.ind);
self.word("}");
self.end();
self.word(" from ");
self.print_ast_str_lit(path);
self.braces_break();
self.s.offset(-self.ind);
self.word("}");
self.end();
self.word(" from ");
self.print_ast_str_lit(path);
}
}
}
self.word(";");
Expand Down
Loading