Files
lucisv/crates/vlog_formatter/src/doc.rs
T
lucioandClaude Fable 5 53f226981f printer: multi-line raw text keeps the column honest
Raw text carrying a string literal across a line break, and the #( ... )
of a parameterized type, were emitted as a single Doc::Text with embedded
newlines -- violating that variant's contract and leaving the printer's
column tracker stale, so every later fits() on the line broke
conservatively. Both now emit Doc::Inline, whose printer arm resumes
measuring from the text's own last line. Output bytes are identical
except where the stale column forced a spurious break: one corpus file
(id_queue_tb.sv) improves, pass set unchanged at 813/820, verify.rs
untouched. Found by QA in the directive-split round; pinned by a printer
test and a classes.cases entry.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-08-26 02:10:10 -07:00

73 lines
2.6 KiB
Rust

//! Wadler-style document IR, the same core idea as `ruff_formatter` (and
//! prettier): build a tree of texts, line-break opportunities, groups, and
//! indents; the printer then picks flat or broken layouts based on the
//! configured line width.
#[derive(Debug, Clone)]
pub enum Doc {
/// Verbatim text. Must not contain newlines (use `HardLine`).
Text(String),
/// Verbatim text spliced into the *middle* of a line, newlines and all:
/// the `{ ... }` of a `randomize() with { ... }` constraint block, the
/// `#( ... )` of a parameterized type reference, or raw text that
/// carries a string literal across a line break. It
/// carries its own indentation, so the printer copies it through
/// untouched and resumes measuring from its last line. Unlike
/// [`Doc::Verbatim`] it does not force enclosing groups to break — the
/// text starts on the current line, and what follows it continues on the
/// line the text ends on.
Inline(String),
/// A space when flat, a newline when the enclosing group breaks.
Line,
/// Nothing when flat, a newline when the enclosing group breaks.
SoftLine,
/// Always a newline. Forces every enclosing group to break.
HardLine,
/// Two newlines (one blank line).
BlankLine,
/// Content that the printer tries to fit on one line; if it does not
/// fit (or contains a hard line), every `Line`/`SoftLine` directly
/// inside becomes a newline.
Group(Vec<Doc>),
/// Increase indentation for any line breaks inside.
Indent(Vec<Doc>),
/// Sequence with no other effect.
Concat(Vec<Doc>),
/// Emitted only when the enclosing group is broken.
IfBreak(String),
/// Source bytes copied through untouched, newlines and all, starting at
/// column 0: the indentation the preceding line break wrote is stripped
/// first, so the text keeps its own. Always forces enclosing groups to
/// break. Only valid right after a line break or at the start of the
/// output — see `docs/features/format-fences/README.md`.
Verbatim(String),
}
pub fn text(s: impl Into<String>) -> Doc {
Doc::Text(s.into())
}
pub fn group(docs: Vec<Doc>) -> Doc {
Doc::Group(docs)
}
pub fn indent(docs: Vec<Doc>) -> Doc {
Doc::Indent(docs)
}
pub fn concat(docs: Vec<Doc>) -> Doc {
Doc::Concat(docs)
}
/// Join docs with a separator.
pub fn join(items: Vec<Doc>, sep: Doc) -> Vec<Doc> {
let mut out = Vec::new();
for (i, item) in items.into_iter().enumerate() {
if i > 0 {
out.push(sep.clone());
}
out.push(item);
}
out
}