pretty printer for moonbit
let map = {
"name": ["John", "Mike"], "age": ["15","18"], "id": ["11109121","2000012312"]
}
map |> @prettyprinter.render() |> println(){
"name": ["John", "Mike"],
"age": ["15", "18"],
"id": ["11109121", "2000012312"]
}impl Pretty for FixedArray[A]let user : Json = {
"name": "John Doe",
"age": 30,
"is_student": false,
"grades": [100, 90, 80],
"address": { "street": "123 Main St", "city": "Springfield", "state": "IL" },
}
inspect(
pretty(user),
content=(
#|{
#| "name": John Doe,
#| "age": 30,
#| "is_student": false,
#| "grades": [100, 90, 80],
#| "address": {"street": 123 Main St, "city": Springfield, "state": IL}
#|}
),
)let score : Map[String, Int] = {
"player1": 1009,
"player2": 200,
"player3": 30,
"player4": 999999999,
"player5": 999999999,
}
inspect(
pretty(score),
content=(
#|{
#| "player1": 1009,
#| "player2": 200,
#| "player3": 30,
#| "player4": 999999999,
#| "player5": 999999999
#|}
),
)impl Pretty for PriorityQueue[A]pub struct Context {
indent : Int
next_indent : Int
width : Int
}type Documentpub(all) enum Requirement {
Space(Int)
Infinite
}impl Add for Requirementimpl Compare for Requirementimpl Eq for Requirementimpl Show for Requirementinspect(braces(text("doc")), content="{doc}")inspect(brackets(text("doc")), content="[doc]")let doc = ctor(pkg="pkg", ty="Enum", "Constr", [pretty(1), pretty(2)], labeled={
"loc": pretty(100),
"name": pretty("cons"),
})
inspect(
doc,
content=(
#|@pkg.Enum::Constr(1, 2, loc=100, name="cons")
),
)
let doc = ctor(
pkg="pkg",
ty="Enum",
"LongConstructor",
[
pretty(100000000),
pretty(200000000),
pretty(100000000),
pretty(200000000),
pretty(100000000),
pretty(200000000),
pretty(100000000),
pretty(200000000),
],
labeled={ "loc": pretty(100), "name": pretty("cons") },
)
inspect(
doc,
content=(
#|@pkg.Enum::LongConstructor(
#| 100000000,
#| 200000000,
#| 100000000,
#| 200000000,
#| 100000000,
#| 200000000,
#| 100000000,
#| 200000000,
#| loc=100,
#| name="cons",
#|)
),
)let words = [
"The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog",
]
let doc = words.map(fn(x){text(x) + space}) |> flow()
inspect(doc, content="The quick brown fox jumps over the lazy dog ")
inspect(
render(doc, width=10),
content=(
#|The quick brown
#|fox jumps over
#|the lazy dog
),
)render(text("hello") + nest(hardline + text("world")))
|> inspect(
content=(
#|hello
#| world
),
)inspect(
mandatory_line + nest(line + text("abc")),
content=(
#|
#| abc
),
)
inspect(
mandatory_line + nest(text("abc")),
content=(
#|
#|abc
),
)inspect(
text("f();") + mandatory_space + text("//todo"),
content="f(); //todo",
)
inspect(
text("f();") + space + mandatory_space + text("//todo"),
content="f(); //todo",
)
inspect(
text("f();") + hardline + mandatory_space + text("//todo"),
content=(
#|f();
#|//todo
),
)inspect(parens(text("doc")), content="(doc)")let doc = record(pkg="pkg", ty="Struct", {
"spos": pretty(1),
"epos": pretty(2),
"file": pretty("abc"),
})
inspect(
doc,
content=(
#|@pkg.Struct::{spos: 1, epos: 2, file: "abc"}
),
)
let doc = record(pkg="pkg", ty="Struct", {
"spos": pretty(1000000000),
"epos": pretty(2000000000),
"file": pretty("abc"),
"spos1": pretty(1000000000),
"epos1": pretty(2000000000),
"file1": pretty("abc"),
})
inspect(
doc,
content=(
#|@pkg.Struct::{
#| spos: 1000000000,
#| epos: 2000000000,
#| file: "abc",
#| spos1: 1000000000,
#| epos1: 2000000000,
#| file1: "abc",
#|}
),
)let sep = text("-")
let docs = [text("Hello"), text("World"), text("!")]
separate(sep, docs).pretty() |> inspect(content="Hello-World-!")let doc = separate_map(text(","), [1,2,3], pretty)
inspect(doc, content="1,2,3") inspect(string("Hello, World!"), content="\"Hello, World!\"")inspect(surround(char('['), char(']'), text("doc")), content="[doc]")let doc = tuple([pretty(1), pretty('a'), pretty(true)])
inspect(doc, content="(1, a, true)")
let elem = pretty("very long string")
let doc = tuple(Array::make(5, elem))
inspect(
doc,
content=(
#|(
#| "very long string",
#| "very long string",
#| "very long string",
#| "very long string",
#| "very long string",
#|)
),
)pretty printer for moonbit