Skip to content

Add a layout solver to google-java-format based on Wadler's prettier printer. - #1482

Merged
copybara-service[bot] merged 1 commit into
masterfrom
test_986705449
Oct 14, 2026
Merged

copybara-service[bot] merged 1 commit into
masterfrom
test_986705449

Conversation

@copybara-service

Copy link
Copy Markdown

Add a layout solver to google-java-format based on Wadler's prettier printer.

Replace the Oppen-style Doc.Level break computation with an immutable
document model and a separate solver, based on Philip Wadler's "A prettier
printer" (https://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf).
A layout.Doc is built from text, line and group, and
layout.LayoutSolver lays it out greedily in a single pass: each group is
printed flat if it fits (Wadler's group x = flatten x <|> x), and otherwise
its breaks are taken. Fill breaks correspond to Wadler's fill.

layout.LayoutBuilder bridges the existing visitor to the new model. It
provides the same operations as OpsBuilder, so JavaInputAstVisitor is
unchanged apart from the builder type. The builder applies the same rules as
the existing engine, for example the group-local fit test and attaching
trailing text to the group of the most recent break. All existing formatting
is preserved, and every integration test golden is unchanged.

…printer.

Replace the Oppen-style `Doc.Level` break computation with an immutable
document model and a separate solver, based on Philip Wadler's "A prettier
printer" (https://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf).
A `layout.Doc` is built from `text`, `line` and `group`, and
`layout.LayoutSolver` lays it out greedily in a single pass: each group is
printed flat if it fits (Wadler's `group x = flatten x <|> x`), and otherwise
its breaks are taken. Fill breaks correspond to Wadler's `fill`.

`layout.LayoutBuilder` bridges the existing visitor to the new model. It
provides the same operations as `OpsBuilder`, so `JavaInputAstVisitor` is
unchanged apart from the builder type. The builder applies the same rules as
the existing engine, for example the group-local fit test and attaching
trailing text to the group of the most recent break. All existing formatting
is preserved, and every integration test golden is unchanged.

PiperOrigin-RevId: 999176908
@copybara-service
copybara-service Bot merged commit 332bbc2 into master Oct 14, 2026
4 checks passed
@copybara-service
copybara-service Bot deleted the test_986705449 branch October 14, 2026 07:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant