Skip to content

Conversation

0xrusowsky
Copy link
Contributor

@0xrusowsky 0xrusowsky commented Jul 2, 2025

Motivation

Rewrite forge-fmt entirely to use Solar as the Solidity/Yul parser and AST, and using a structured pretty-printing algorithm.

The language-agnostic implementation of the pretty-printer (src/pp) is based on rustc_ast_pretty and prettyplease, both MIT and Apache licensed. See prettyplease's README for algorithm notes, as well as the new fmt README.md for more details.

The goal of this PR is to make progress with #9317, and fixing existing issues with the formatter while trying to maintain compatibility as much as possible.

This PR will not be replacing the formatter yet, opting to create a separate crate crates/fmt-2 to reduce unrelated changes and aid with review. A follow-up PR will replace crates/fmt and deal with fallout in-tree. This was done with #11570 and merged into this PR.

Issues

Issues that will be fixed with the new implementation. Not closed by this PR, but by the follow-up that will hook the new implementation up; see paragraph above.

Tests state

  • ArrayExpressions
  • BlockComments
  • BlockCommentsFunction
  • ConditionalOperatorExpression
  • ConstructorDefinition
  • ConstructorModifierStyle
  • ContractDefinition
  • DocComments
  • DoWhileStatement
  • EmitStatement
  • EnumDefinition
  • EnumVariants
  • ErrorDefinition
  • EventDefinition
  • ForStatement
  • FunctionCall
  • FunctionCallArgsStatement
  • FunctionDefinition
  • FunctionDefinitionWithFunctionReturns
  • FunctionType
  • HexUnderscore
  • IfStatement
  • IfStatement2
  • ImportDirective
  • InlineDisable
  • IntTypes
  • LiteralExpression
  • MappingType
  • ModifierDefinition
  • NamedFunctionCallExpression
  • NumberLiteralUnderscore
  • OperatorExpressions
  • PragmaDirective
  • Repros
  • ReturnStatement
  • RevertNamedArgsStatement
  • RevertStatement
  • SimpleComments
  • SortedImports
  • StatementBlock
  • StructDefinition
  • ThisExpression
  • TrailingComma
    • NOTE: solar error
  • TryStatement
  • TypeDefinition
  • UnitExpression
  • UsingDirective
  • VariableAssignment
  • VariableDefinition
  • WhileStatement
  • Yul
    • STYLE: pending review
  • YulStrings

Supported Configs:

  • indent_style = IndentStyle
  • multiline_func_header: MultilineFuncHeaderStyle
  • line_length: usize
  • tab_width: usize
  • bracket_spacing: bool
  • int_types: IntTypes
  • quote_style: QuoteStyle
  • number_underscore: NumberUnderscore
  • hex_underscore: HexUnderscore
  • single_line_statement_blocks: SingleLineBlockStyle
  • override_spacing: bool
  • wrap_comments: bool
  • ignore: Vec<String>
  • contract_new_lines: bool
  • sort_imports: bool
  • pow_no_space: bool Fixes feat(fmt): option to remove spaces between number and exponent #4512

Pending TODOs

  • update README

0xrusowsky and others added 6 commits September 23, 2025 00:18
* nits

* Try no format

* chore: simplify call logic

* fix: reenable 2nd pass

* chore: cleanup

* docs: solar cmnt

* format testdata with new style

* fix: extra space in function type

* Readd relevant todo

* style: use span builder methods

* Review changes

* fix: remove outdated cmnt

* fix: test spacing

* Revert "fix: test spacing"

This reverts commit 541f4c8.

---------

Co-authored-by: 0xrusowsky <0xrusowsky@proton.me>
Copy link
Collaborator

@grandizzy grandizzy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

Copy link
Member

@DaniPopes DaniPopes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

boom

@grandizzy grandizzy merged commit 9d38af9 into master Sep 24, 2025
25 checks passed
@grandizzy grandizzy deleted the rusowsky/fmt-solar branch September 24, 2025 14:07
@github-project-automation github-project-automation bot moved this from In Progress to Done in Foundry Sep 24, 2025
@0xferrous
Copy link
Contributor

not sure if this was expected but i think this change yields different formatting result than previously leading to broken formatting checks without any changes. just fyi.

@grandizzy
Copy link
Collaborator

not sure if this was expected but i think this change yields different formatting result than previously leading to broken formatting checks without any changes. just fyi.

@0xferrous some of them are intended, interested to check your, mind to open a ticket with diff and we'll review? Thank you

@0xferrous
Copy link
Contributor

0xferrous commented Sep 26, 2025

oh the diff is too big, i will have to set some time aside to dedupe. but one that seems obviously wrong/unexpected is

    nestedStruct.val.contract.function(
        param1, param2, param3 // long line
    );

getting converted to:

    nestedStruct.val.contract
    .function(param1, param2, param3);

@grandizzy
Copy link
Collaborator

oh the diff is too big, i will have to set some time aside to dedupe. but one that seems obviously wrong/unexpected is

    nestedStruct.val.contract.function(
        param1, param2, param3 // long line
    );

getting converted to:

    nestedStruct.val.contract
    .function(param1, param2, param3);

@0xferrous we are aware of that and working for a fix with #11834

@grandizzy
Copy link
Collaborator

grandizzy commented Sep 27, 2025

@0xferrous also, is your repo public so we can give it a try with new formatter?

@0xferrous
Copy link
Contributor

unfortunately its not public and not my project

@grandizzy grandizzy moved this from Done to Completed in Foundry Sep 29, 2025
@@ -0,0 +1,26 @@
alias forge-fmt="cargo r --quiet -p forge-fmt --"
forge-fmt-cmp() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@0xrusowsky nit: did we intend for this to be pushed to master, looks like a dev leftover

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@foundry-rs foundry-rs locked as resolved and limited conversation to collaborators Sep 30, 2025
@DaniPopes
Copy link
Member

if you encounter any presumed inconsistencies or invalid fmt output please open an issue!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.