/** * Wadler-Lindig document algebra for the CST formatter. * * A Doc is a tree of formatting instructions that the renderer converts to * a string given a target line width. The key abstraction is `Group`: in a * Group, the renderer first tries to print everything flat (on one line). * If that doesn't fit within the target width, it breaks the Group and * renders each `Line` inside it as a newline + indentation. * * Doc types: * Text(s) — literal text, never broken * Line — in flat mode: space; in break mode: newline + indent * SoftLine — in flat mode: nothing; in break mode: newline + indent * HardLine — always newline + indent (forces enclosing Group to break) * LineComment(s) — text followed by a mandatory hard break * Concat(docs) — concatenation of multiple docs * Nest(n, doc) — increase indent by n for the inner doc * Group(doc) — try flat first, break if it doesn't fit * IfBreak(b, f) — `b` when enclosing Group breaks, `f` when flat */ interface TextDoc { type: 'text'; text: string; } interface LineDoc { type: 'line'; } interface SoftLineDoc { type: 'softline'; } interface HardLineDoc { type: 'hardline'; } interface LineCommentDoc { type: 'lineComment'; text: string; } interface ConcatDoc { type: 'concat'; parts: Doc[]; } interface NestDoc { type: 'nest'; indent: number; doc: Doc; } interface GroupDoc { type: 'group'; doc: Doc; } interface IfBreakDoc { type: 'ifBreak'; broken: Doc; flat: Doc; } export type Doc = TextDoc | LineDoc | SoftLineDoc | HardLineDoc | LineCommentDoc | ConcatDoc | NestDoc | GroupDoc | IfBreakDoc; /** Literal text — never broken. */ export declare function text(s: string): Doc; /** In flat mode: space. In break mode: newline + indent. */ export declare const line: Doc; /** In flat mode: nothing. In break mode: newline + indent. */ export declare const softLine: Doc; /** Always a newline + indent. Forces enclosing Group to break. */ export declare const hardLine: Doc; /** A line comment — text followed by a mandatory hard break. */ export declare function lineComment(s: string): Doc; /** Concatenate multiple docs. Flattens nested concats. */ export declare function concat(...parts: Doc[]): Doc; /** Increase indent by `n` for the inner doc. */ export declare function nest(n: number, doc: Doc): Doc; /** Try to render `doc` flat (on one line). If it doesn't fit, break. */ export declare function group(doc: Doc): Doc; /** Render `broken` when the enclosing Group breaks, `flat` when it fits. */ export declare function ifBreak(broken: Doc, flat: Doc): Doc; /** Join docs with a separator between each pair. */ export declare function join(sep: Doc, docs: Doc[]): Doc; /** Trailing comma when the enclosing group breaks, nothing when flat. */ export declare const trailingComma: Doc; /** * Render a Doc tree to a string. * * Uses the Wadler-Lindig "best fit" algorithm: a stack-based traversal * that decides, for each Group, whether to print it flat or broken. * * @param doc The document tree to render. * @param width Target line width (default 80). */ export declare function render(doc: Doc, width?: number): string; export {};