Wadler-style pretty printer for MoonBit: documents, groups and layout that fits a line width
Dependencies
let items = [@pretty.text("a"), @pretty.text("b"), @pretty.text("c")]
let doc = @pretty.group(
@pretty.text("[") +
@pretty.nest(2, @pretty.line() + @pretty.join(items, @pretty.text(",") + @pretty.line())) +
@pretty.line() +
@pretty.text("]"),
)
@pretty.render(doc) // "[ a, b, c ]"
@pretty.render(doc, width=6) // "[\n a,\n b,\n c\n]"| Function | Flat | Broken |
|---|---|---|
| text(s) | s | s |
| verbatim(s) | s | s, no indentation after its line feeds |
| line() | a space | a line break |
| softline() | nothing | a line break |
| hardline() | always a line break | |
| nest(n, d) | d | d, indented by n more |
| align(d) | d | d, indented to its start column |
| tab(n, d) | d | d, indented to the next multiple of n |
| group(d) | flat if it fits | else broken |
| if_break(b, f) | f | b |
type Doctest {
let d = @pretty.group(@pretty.text("a") + @pretty.line() + @pretty.text("b"))
inspect(@pretty.render(d, width=3), content="a b")
inspect(@pretty.render(d, width=2), content="a\nb")
}test {
let items = [@pretty.text("a"), @pretty.text("b")]
let d = @pretty.group(
@pretty.text("[") +
@pretty.nest(
2,
@pretty.line() + @pretty.join(items, @pretty.text(",") + @pretty.line()),
) +
@pretty.line() +
@pretty.text("]"),
)
inspect(@pretty.render(d), content="[ a, b ]")
inspect(@pretty.render(d, width=4), content="[\n a,\n b\n]")
}test {
let body = @pretty.text("x") +
@pretty.tab(4, @pretty.hardline() + @pretty.text("y"))
inspect(
@pretty.render(@pretty.text("ab") + @pretty.align(body)),
content="abx\n y",
)
inspect(
@pretty.render(@pretty.text("abcd") + @pretty.align(body)),
content="abcdx\n y",
)
}test {
inspect(
@pretty.render(@pretty.nest(2, @pretty.text("a\nb"))),
content="a\n b",
)
}Install
Download zipWadler-style pretty printer for MoonBit: documents, groups and layout that fits a line width
Dependencies