protocompile icon indicating copy to clipboard operation
protocompile copied to clipboard

Add `printer/dom` package for AST pretty printer

Open doriable opened this issue 9 months ago • 0 comments

This adds a package for formatting and rendering text. It provides a DOM-like tree structure, Dom, which is a tree of Chunks. A Chunk represents a line of text with configurations around indentation and whitespace.

When formatting the Dom, the caller will provide a line limit and an indent string, e.g. " " for a 2-space indent. Indentation controls the leading whitespace of a Chunk, and it is based on the indent string set, indent depth of the Chunk, and the splitting pattern at render time. Splitting controls the trailing whitespace for a Chunk. A SoftSplit means that based on the line length set on the formatting, the Chunk may have a newline (hard split) or double newline following its text. However, if the Chunk remains unsplit at render time, then a trailing space will be added based on spaceifUnsplit.

TODOs:

  • [ ] Fix braces parser in dom_test.go
  • [ ] Consider whether we need a Zero concept for Chunk and Chunks
  • [ ] Some in-line questions/comments

doriable avatar Mar 05 '25 23:03 doriable