Expand description
A small, self-contained pretty-printing engine in the Wadler → Prettier → rome/biome → ruff
lineage. The grammar-agnostic core is a Doc intermediate representation plus a width-aware
print()er; SQL-specific rules live in the formatter’s SQL lowering module and only ever
build Docs.
The model has three moving parts:
- Builders (
text(),concat(),group(),indent(),line(),soft_line(),hard_line(),space(),join()) construct the IR. - Groups are the unit of layout choice: a group is printed flat (its soft lines become spaces or nothing) when it fits within the remaining width, otherwise it breaks (its soft lines become newlines + indentation).
- A hard line forces every group that contains it to break — the classic
breakParentpropagation — so caller-mandated line breaks are never silently collapsed.
See docs/research/prior-art.md for the design rationale and references.
Macros§
- doc_
write - Push a comma-bracketed list of [
Format] elements into a [DocBuffer], in order.
Structs§
- Print
Options - Knobs for the printer. Opinionated by design: just a target width and an indent step.
Enums§
- Doc
- The pretty-printer intermediate representation.
- Line
Kind - How a
Doc::Linerenders, as a function of the enclosing group’s mode.
Functions§
- break_
parent - Force enclosing groups to break without emitting a newline here.
- concat
- Lay out the parts one after another.
- empty
- The empty document (renders to nothing).
- group
- A layout-choice group: flat when it fits on the line, broken otherwise.
- group_
expanded - A group forced to break (its soft lines always become newlines), without forcing enclosing groups to break. The building block for magic-trailing-comma “keep this exploded”.
- hard_
line - Always a newline; forces enclosing groups to break.
- indent
- Indent every line break that occurs inside
innerby one more level. - join
- Interleave
sepbetweenitems(no separator before the first or after the last). - line
- A space when flat, a newline when broken.
- line_
suffix - Defer
innerto just before the next newline (used for trailing line comments). - Render
docto a string. Trailing whitespace is trimmed from every line and the result ends with exactly one newline (or is empty), so the output is stable under re-formatting. - soft_
line - Nothing when flat, a newline when broken.
- source_
code_ slice - A verbatim slice of source that may span multiple lines. Use this — not
text— for fallback regions that can contain embedded newlines: it re-bases the column at each newline so the slice stays aligned under indentation, and caches its last line’s display width for fit decisions. - space
- A literal, non-collapsible space.
- text
- Verbatim text (a borrowed
&'static stror an ownedString). The display width is measured once here and cached on the node, so the printer’s fit/break measurement never re-scans it.