Skip to content

feat(fmt): rewrite formatter using Solar and a structured algorithm #10350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 58 commits into
base: master
Choose a base branch
from

Conversation

DaniPopes
Copy link
Member

@DaniPopes DaniPopes commented Apr 22, 2025

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.

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.

WIP

Progress

Tests:

13 passed; 40 failed; 1 ignored

Implementation:

  • items
    • pragma
    • import
    • using
    • contract/interface/library
    • function/modifier
    • variable declaration
    • struct
    • enum
    • UDVT
    • error
    • event
  • types
  • expressions
    • literals
    • ...
  • statements
    • if
    • while
    • do while
    • for
    • try-catch
    • ...
  • yul
    • expressions
    • statements
    • functions

Other:

  • Update README.md
  • Refactor printing delimited groups to handle span/comments
  • Print docs that get discarded during parsing like other comments
  • Add a non-fatal error that returns the formatted source with the errors
  • Add snapshot infra to tests to automatically update 'expected' files

Configs:

  • line_length: usize
  • tab_width: usize
  • bracket_spacing: bool
  • int_types: IntTypes
  • multiline_func_header: MultilineFuncHeaderStyle
  • 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

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.

@jenpaff jenpaff moved this to In Progress in Foundry May 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

1 participant