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 [crate::sql] 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§
Structs§
- DocBuffer
- A growable sink of
Docelements, the target of thedoc_write!macro and ofFormatimplementors. - 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.
Traits§
- Format
- A value that knows how to render itself into a
DocBuffer. - Format
Rule - Formatting logic for a
Tkept outsideT, matching the biome/ruff rule pattern.
Functions§
- best_
fitting - Try the candidate documents in order, printing the first that fits in the remaining width and falling back to the last candidate when none fit. Empty candidates render as empty.
- block_
indent - A hard-line-delimited, indented block:
inneris placed on its own indented line(s), framed by a hard line before and after (Prettier/biome’sblock_indent). - 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).
- format_
value - Run a single
Formatvalue to completion, returning its assembledDoc. - 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.
- if_
group_ breaks - Choose
brokenwhen the enclosing group breaks andflatwhen it stays flat (Prettier’sifBreak). Neither arm emits a newline by itself; this only selects which content appears. - 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.