The +
, .nest
, and .group
methods¤
Whilst the __pdoc__
method example explicitly spelled out each and every doc, in practice we can use:
doc + doc
to formwadler_lindig.ConcatDoc
,doc.nest(indent)
to formwadler_lindig.NestDoc
,- and
doc.group()
to formwadler_lindig.GroupDoc
.
These are usually offer a more compact representation.
For example, this:
from wadler_lindig import BreakDoc as Brk, TextDoc as Txt
doc = (
Txt("begin")
+ (
Brk(" ")
+ (Txt("stmt;") + Brk(" ") + Txt("stmt;") + Brk(" ") + Txt("stmt;")).group()
).nest(3)
+ Brk(" ")
+ Txt("end")
)
Expands to this:
from wadler_lindig import BreakDoc, ConcatDoc, GroupDoc, NestDoc, TextDoc
doc = ConcatDoc(
children=(
TextDoc(text="begin"),
NestDoc(
child=ConcatDoc(
children=(
BreakDoc(text=" "),
GroupDoc(
child=ConcatDoc(
children=(
TextDoc(text="stmt;"),
BreakDoc(text=" "),
TextDoc(text="stmt;"),
BreakDoc(text=" "),
TextDoc(text="stmt;"),
)
)
),
)
),
indent=3,
),
BreakDoc(text=" "),
TextDoc(text="end"),
)
)
Here's what the result looks like, for completeness:
from wadler_lindig import pprint
pprint(doc, width=25)
Next example: take a look at the (break-group).nest-break
example for a common pattern worth knowing.