Expand description
Deterministic formatting of Cove source.
The formatter prints an ast::SourceUnit back to source text. It is
deterministic and idempotent: formatting twice produces exactly what
formatting once produced, and re-parsing the result produces the same tree.
§Layout
Two spaces of indentation, no tabs, no trailing whitespace, one trailing
newline. Lines are kept within MAX_WIDTH columns where a legal break
exists.
§The newline rule
Cove statements end at the end of a line, so a formatter cannot break a
line wherever it likes: a break in the wrong place silently splits one
statement into two. Every break this module introduces is one the parser
reads as a continuation — inside a (, [, or < group, immediately
after a binary operator, or before a leading . — which is why long
argument lists break one argument per line, a binary expression breaks
after its operator, and a method chain breaks before its dots.
§Comments
The tree carries /// doc comments but not // or /* */ comments, and
not the blank lines an author wrote between statements. format_source
reads those back out of the source text and re-attaches them by position:
a comment on its own line attaches to the item or statement that follows
it, and a comment at the end of a line stays at the end of that line. No
comment is ever dropped; one the formatter cannot place exactly is moved
to the nearest following boundary rather than lost.
Constants§
- MAX_
WIDTH - The column the formatter keeps lines within when a legal break exists.
Functions§
- format_
expr - Renders one expression on a single line, from the tree alone.
- format_
source - Formats
unit, which must be the tree parsed fromsource. - format_
unit - Formats one parsed source unit deterministically.