// Doc Printer — Prettier's line-breaking algorithm // Ported from Prettier's src/document/printer/printer.js // // Converts a Doc IR into a formatted string, handling line breaking, // indentation, and group flattening. import { StringBuilder, growable } from 'zena:core'; import { Doc, DocKind, TextDoc, ArrayDoc, GroupDoc, IndentDoc, DedentDoc, AlignDoc, IfBreakDoc, FillDoc, LineSuffixDoc, IndentIfBreakDoc, LabelDoc } from './doc.zena'; // ============================================================================ // Mode: flat (try one line) or break (broken across lines) // ============================================================================ export enum Mode { Flat, Break, } // ============================================================================ // Indent representation // ============================================================================ // Indentation is tracked as a simple level count. // Each level = 2 spaces (matching Zena's style). let INDENT_WIDTH: i32 = 2; // ============================================================================ // Print Options // ============================================================================ export class PrintOptions { printWidth: i32; tabWidth: i32; useTabs: boolean; new(printWidth: i32) : printWidth = printWidth, tabWidth = 2, useTabs = false; } // ============================================================================ // Command on the print stack // ============================================================================ class PrintCommand { indent: i32; mode: Mode; doc: Doc; new(indent: i32, mode: Mode, doc: Doc) : indent = indent, mode = mode, doc = doc; } // ============================================================================ // Group break tracking (for ifBreak with groupId) // ============================================================================ // Track which group IDs have broken. // Simple approach: fixed-size array indexed by group ID. let MAX_GROUPS: i32 = 1024; class GroupBreakMap { #broken: GrowableArray; new() : #broken = new Array() { var i = 0; while (i < MAX_GROUPS) { this.#broken.push(false); i += 1; } } set(id: i32, broken: boolean): void { if (id > 0 && id < MAX_GROUPS) { this.#broken[id] = broken; } } get(id: i32): boolean { if (id > 0 && id < MAX_GROUPS) { return this.#broken[id]; } return false; } } // ============================================================================ // propagateBreaks — pre-pass to propagate hardlines/breakParent upward // ============================================================================ // Stack entry for the propagateBreaks traversal. When isExit is true, // this is a "group exit" marker: after all children of a group have been // processed, we pop the group stack and propagate its break status upward. class PropagateEntry { doc: Doc; isExit: boolean; new(doc: Doc, isExit: boolean) : doc = doc, isExit = isExit; } function propagateBreaks(doc: Doc) { let stack = new Array(); let groupStack = new Array(); stack.push(new PropagateEntry(doc, false)); while (stack.length > 0) { let entry = stack.pop(); let d = entry.doc; if (entry.isExit) { // Exiting a group: pop from groupStack, propagate if broken let g = groupStack.pop(); if (g.shouldBreak && groupStack.length > 0) { groupStack[groupStack.length - 1].shouldBreak = true; } } else if (d.kind == DocKind.BreakParent || d.kind == DocKind.Hardline || d.kind == DocKind.LiteralLine) { // These all force the enclosing group to break if (groupStack.length > 0) { groupStack[groupStack.length - 1].shouldBreak = true; } } else if (d.kind == DocKind.Group) { let g = d as GroupDoc; groupStack.push(g); // Push exit marker (processed after all children) stack.push(new PropagateEntry(d, true)); // Push contents (processed before exit marker — LIFO) stack.push(new PropagateEntry(g.contents, false)); // If already broken, propagate to parent if (g.shouldBreak && groupStack.length > 1) { groupStack[groupStack.length - 2].shouldBreak = true; } } else { // Push children for traversal if (d.kind == DocKind.Array) { let ad = d as ArrayDoc; var i = ad.parts.length - 1; while (i >= 0) { stack.push(new PropagateEntry(ad.parts[i], false)); i = i - 1; } } else if (d.kind == DocKind.Indent) { stack.push(new PropagateEntry((d as IndentDoc).contents, false)); } else if (d.kind == DocKind.Dedent) { stack.push(new PropagateEntry((d as DedentDoc).contents, false)); } else if (d.kind == DocKind.Align) { stack.push(new PropagateEntry((d as AlignDoc).contents, false)); } else if (d.kind == DocKind.IfBreak) { let ib = d as IfBreakDoc; stack.push(new PropagateEntry(ib.flatContents, false)); stack.push(new PropagateEntry(ib.breakContents, false)); } else if (d.kind == DocKind.Fill) { let fd = d as FillDoc; var i = fd.parts.length - 1; while (i >= 0) { stack.push(new PropagateEntry(fd.parts[i], false)); i = i - 1; } } else if (d.kind == DocKind.LineSuffix) { stack.push(new PropagateEntry((d as LineSuffixDoc).contents, false)); } else if (d.kind == DocKind.IndentIfBreak) { stack.push(new PropagateEntry((d as IndentIfBreakDoc).contents, false)); } else if (d.kind == DocKind.Label) { stack.push(new PropagateEntry((d as LabelDoc).contents, false)); } // Leaf nodes (Text, Line, Softline, etc.): no children } } } // ============================================================================ // fits — check if doc fits within remaining width without breaking // // Ported from Prettier's fits() function. Key improvement over the naive // version: when the local command stack is exhausted, continues scanning // into restCmds (the main print stack) so that a group that fits in // isolation but is followed by content that overflows will correctly break. // Also tracks lineSuffix state: if a lineSuffix has been seen and we hit // a lineSuffixBoundary, the line doesn't fit (suffix content may overflow). // ============================================================================ function fits( cmds: GrowableArray, restCmds: GrowableArray, groupBreaks: GroupBreakMap, hasLineSuffix: boolean, width: i32 ): boolean { var remaining = width; var restIdx = restCmds.length; var hasSuffix = hasLineSuffix; while (remaining >= 0) { if (cmds.length == 0) { if (restIdx == 0) { return true; } restIdx = restIdx - 1; cmds.push(restCmds[restIdx]); continue; } let cmd = cmds.pop(); let doc = cmd.doc; if (doc.kind == DocKind.Text) { let td = doc as TextDoc; remaining = remaining - td.value.length; } else if (doc.kind == DocKind.Array) { let ad = doc as ArrayDoc; var j = ad.parts.length - 1; while (j >= 0) { cmds.push(new PrintCommand(cmd.indent, cmd.mode, ad.parts[j])); j = j - 1; } } else if (doc.kind == DocKind.Group) { let gd = doc as GroupDoc; if (gd.shouldBreak) { // Pre-broken group: use Break mode so it hits line breaks early cmds.push(new PrintCommand(cmd.indent, Mode.Break, gd.contents)); } else { cmds.push(new PrintCommand(cmd.indent, Mode.Flat, gd.contents)); } } else if (doc.kind == DocKind.Indent) { cmds.push(new PrintCommand(cmd.indent + 1, cmd.mode, (doc as IndentDoc).contents)); } else if (doc.kind == DocKind.Dedent) { var newIndent = cmd.indent - 1; if (newIndent < 0) { newIndent = 0; } cmds.push(new PrintCommand(newIndent, cmd.mode, (doc as DedentDoc).contents)); } else if (doc.kind == DocKind.Align) { cmds.push(new PrintCommand(cmd.indent + 1, cmd.mode, (doc as AlignDoc).contents)); } else if (doc.kind == DocKind.IfBreak) { let ib = doc as IfBreakDoc; if (ib.groupId > 0) { if (groupBreaks.get(ib.groupId)) { cmds.push(new PrintCommand(cmd.indent, cmd.mode, ib.breakContents)); } else { cmds.push(new PrintCommand(cmd.indent, cmd.mode, ib.flatContents)); } } else if (cmd.mode == Mode.Break) { cmds.push(new PrintCommand(cmd.indent, cmd.mode, ib.breakContents)); } else { cmds.push(new PrintCommand(cmd.indent, cmd.mode, ib.flatContents)); } } else if (doc.kind == DocKind.Fill) { let fd = doc as FillDoc; var j = fd.parts.length - 1; while (j >= 0) { cmds.push(new PrintCommand(cmd.indent, cmd.mode, fd.parts[j])); j = j - 1; } } else if (doc.kind == DocKind.Line) { if (cmd.mode == Mode.Flat) { remaining = remaining - 1; // space } else { return true; // line break = fits } } else if (doc.kind == DocKind.Softline) { if (cmd.mode == Mode.Flat) { // nothing } else { return true; } } else if (doc.kind == DocKind.Hardline) { return true; } else if (doc.kind == DocKind.LiteralLine) { return true; } else if (doc.kind == DocKind.LineSuffix) { hasSuffix = true; } else if (doc.kind == DocKind.LineSuffixBoundary) { if (hasSuffix) { return false; } } else if (doc.kind == DocKind.IndentIfBreak) { cmds.push(new PrintCommand(cmd.indent, cmd.mode, (doc as IndentIfBreakDoc).contents)); } else if (doc.kind == DocKind.Label) { cmds.push(new PrintCommand(cmd.indent, cmd.mode, (doc as LabelDoc).contents)); } // BreakParent, Trim: no width effect in fits check } return false; } // ============================================================================ // printDocToString — the main algorithm // ============================================================================ /** * Writes `pending` spaces of deferred indentation and returns the new pending * count, which is zero. * * Indentation is written when the line turns out to have content on it rather * than when the newline before it is emitted. A line that holds nothing — * the blank line between two class members, say — then ends at its newline * instead of carrying the indentation of the member that follows. Deferring * only ever withholds spaces the printer was about to add; it never removes * bytes a `text` contributed, so trailing spaces written inside a template * literal survive. */ function flushIndent(out: StringBuilder, pending: i32): i32 { var remaining = pending; while (remaining > 0) { out.append(' '); remaining -= 1; } return 0; } export function printDocToString(doc: Doc, options: PrintOptions): String { propagateBreaks(doc); let out = new StringBuilder(); let cmds = new Array(); let groupBreaks = new GroupBreakMap(); var lineSuffixes = new Array(); cmds.push(new PrintCommand(0, Mode.Break, doc)); var pos = 0; // current column position // Indentation owed to the current line, written once it has content. var pendingIndent = 0; while (cmds.length > 0) { let cmd = cmds.pop(); let indentLevel = cmd.indent; let mode = cmd.mode; let d = cmd.doc; if (d.kind == DocKind.Text) { let td = d as TextDoc; pendingIndent = flushIndent(out, pendingIndent); out.append(td.value); pos = pos + td.value.length; } else if (d.kind == DocKind.Array) { let ad = d as ArrayDoc; // Push parts in reverse order (stack = LIFO) var i = ad.parts.length - 1; while (i >= 0) { cmds.push(new PrintCommand(indentLevel, mode, ad.parts[i])); i = i - 1; } } else if (d.kind == DocKind.Group) { let gd = d as GroupDoc; if (gd.shouldBreak) { // Pre-broken group if (gd.id > 0) { groupBreaks.set(gd.id, true); } cmds.push(new PrintCommand(indentLevel, Mode.Break, gd.contents)); } else { // Try flat: build a temporary stack and check fits let fitsCmds = growable([ new PrintCommand(indentLevel, Mode.Flat, gd.contents), ]); let hasSuffix = lineSuffixes.length > 0; if (fits(fitsCmds, cmds, groupBreaks, hasSuffix, options.printWidth - pos)) { // Fits on one line if (gd.id > 0) { groupBreaks.set(gd.id, false); } cmds.push(new PrintCommand(indentLevel, Mode.Flat, gd.contents)); } else { // Must break if (gd.id > 0) { groupBreaks.set(gd.id, true); } cmds.push(new PrintCommand(indentLevel, Mode.Break, gd.contents)); } } } else if (d.kind == DocKind.Indent) { let id = d as IndentDoc; cmds.push(new PrintCommand(indentLevel + 1, mode, id.contents)); } else if (d.kind == DocKind.Dedent) { let dd = d as DedentDoc; var newIndent = indentLevel - 1; if (newIndent < 0) { newIndent = 0; } cmds.push(new PrintCommand(newIndent, mode, dd.contents)); } else if (d.kind == DocKind.Align) { let ad = d as AlignDoc; // Align adds spaces, not indent levels — approximate with indent cmds.push(new PrintCommand(indentLevel + 1, mode, ad.contents)); } else if (d.kind == DocKind.IfBreak) { let ib = d as IfBreakDoc; let broken = if (ib.groupId > 0) { groupBreaks.get(ib.groupId) } else { mode == Mode.Break }; if (broken) { cmds.push(new PrintCommand(indentLevel, mode, ib.breakContents)); } else { cmds.push(new PrintCommand(indentLevel, mode, ib.flatContents)); } } else if (d.kind == DocKind.Fill) { let fd = d as FillDoc; let parts = fd.parts; if (parts.length == 0) { // Empty fill — nothing to do } else if (parts.length == 1) { // Single content item cmds.push(new PrintCommand(indentLevel, Mode.Flat, parts[0])); } else { let content = parts[0]; let whitespace = parts[1]; // Build remaining parts (parts[2..]) let remaining = new Array(); var j = 2; while (j < parts.length) { remaining.push(parts[j]); j = j + 1; } if (remaining.length == 0) { // Last content+whitespace pair — print both flat cmds.push(new PrintCommand(indentLevel, Mode.Flat, whitespace)); cmds.push(new PrintCommand(indentLevel, Mode.Flat, content)); } else { // Greedy: check if content fits flat let contentFitsCmds = growable([ new PrintCommand(indentLevel, Mode.Flat, content), ]); let emptyRest = new Array(); let contentFits = fits(contentFitsCmds, emptyRest, groupBreaks, false, options.printWidth - pos); let remainingFill = new FillDoc(remaining); if (contentFits) { // Check if content + whitespace(flat) + nextContent also fits let bothCmds = new Array(); // Push in reverse for stack (LIFO): content processed first bothCmds.push(new PrintCommand(indentLevel, Mode.Flat, remaining[0])); bothCmds.push(new PrintCommand(indentLevel, Mode.Flat, whitespace)); bothCmds.push(new PrintCommand(indentLevel, Mode.Flat, content)); let bothFit = fits(bothCmds, emptyRest, groupBreaks, false, options.printWidth - pos); if (bothFit) { // Everything fits: content flat, whitespace flat, continue cmds.push(new PrintCommand(indentLevel, mode, remainingFill)); cmds.push(new PrintCommand(indentLevel, Mode.Flat, whitespace)); cmds.push(new PrintCommand(indentLevel, Mode.Flat, content)); } else { // Content fits but next won't: break whitespace cmds.push(new PrintCommand(indentLevel, mode, remainingFill)); cmds.push(new PrintCommand(indentLevel, Mode.Break, whitespace)); cmds.push(new PrintCommand(indentLevel, Mode.Flat, content)); } } else { // Content doesn't fit: break both cmds.push(new PrintCommand(indentLevel, mode, remainingFill)); cmds.push(new PrintCommand(indentLevel, Mode.Break, whitespace)); cmds.push(new PrintCommand(indentLevel, Mode.Break, content)); } } } } else if (d.kind == DocKind.Line) { if (mode == Mode.Flat) { pendingIndent = flushIndent(out, pendingIndent); out.append(' '); pos = pos + 1; } else { // Flush line suffixes before newline if (lineSuffixes.length > 0) { // Push line first (bottom of stack, processed second) cmds.push(new PrintCommand(indentLevel, mode, d)); // Push suffixes on top (processed first) var i = 0; while (i < lineSuffixes.length) { cmds.push(lineSuffixes[i]); i += 1; } lineSuffixes = new Array(); } else { out.append('\n'); pendingIndent = indentLevel * INDENT_WIDTH; pos = pendingIndent; } } } else if (d.kind == DocKind.Softline) { if (mode == Mode.Flat) { // nothing } else { // Same as line in break mode if (lineSuffixes.length > 0) { // Push softline first (bottom of stack, processed second) cmds.push(new PrintCommand(indentLevel, mode, d)); // Push suffixes on top (processed first) var i = 0; while (i < lineSuffixes.length) { cmds.push(lineSuffixes[i]); i += 1; } lineSuffixes = new Array(); } else { out.append('\n'); pendingIndent = indentLevel * INDENT_WIDTH; pos = pendingIndent; } } } else if (d.kind == DocKind.Hardline) { // Flush line suffixes before the newline if (lineSuffixes.length > 0) { // Push hardline first (bottom of stack, processed second) cmds.push(new PrintCommand(indentLevel, mode, d)); // Push suffixes on top (processed first, before the hardline) var i = 0; while (i < lineSuffixes.length) { cmds.push(lineSuffixes[i]); i += 1; } lineSuffixes = new Array(); } else { out.append('\n'); pendingIndent = indentLevel * INDENT_WIDTH; pos = pendingIndent; } } else if (d.kind == DocKind.LiteralLine) { out.append('\n'); pendingIndent = 0; pos = 0; // No indentation for literal lines } else if (d.kind == DocKind.LineSuffix) { let ls = d as LineSuffixDoc; lineSuffixes.push(new PrintCommand(indentLevel, mode, ls.contents)); } else if (d.kind == DocKind.LineSuffixBoundary) { // Force-flush line suffixes if (lineSuffixes.length > 0) { var i = 0; while (i < lineSuffixes.length) { cmds.push(lineSuffixes[i]); i += 1; } lineSuffixes = new Array(); } } else if (d.kind == DocKind.BreakParent) { // breakParent is handled during propagateBreaks pre-pass. // For now, it's a no-op in the printer. } else if (d.kind == DocKind.Trim) { // Remove trailing whitespace — not yet implemented // Would need to scan backwards in the StringBuilder } else if (d.kind == DocKind.IndentIfBreak) { let iib = d as IndentIfBreakDoc; let broken = groupBreaks.get(iib.groupId); if (broken) { cmds.push(new PrintCommand(indentLevel + 1, mode, iib.contents)); } else { cmds.push(new PrintCommand(indentLevel, mode, iib.contents)); } } else if (d.kind == DocKind.Label) { let ld = d as LabelDoc; // Labels are transparent — just print contents cmds.push(new PrintCommand(indentLevel, mode, ld.contents)); } } // Flush remaining line suffixes for (let cmd in lineSuffixes) { let d = cmd.doc; if (d.kind == DocKind.Text) { let td = d as TextDoc; pendingIndent = flushIndent(out, pendingIndent); out.append(td.value); } } return out.toString(); }