Pretty-Printing Combinators

A Dart port of Christian Lindig's strict version of Wadler's pretty-printing combinators. The present version deforests away the second intermediate tree of Lindig's implementation, which should make it slightly more efficient.

This is what this library allows for:

class Tree {
  final String name;
  final List<Tree> children;
  Tree(this.name, this.children);

  Document get pretty => // see below
}

final someTree = new Tree("aaa",
    [new Tree("bbbbb",
        [new Tree("ccc", []),
         new Tree("dd", [])]),
    new Tree("eee", []),
    new Tree("ffff",
        [new Tree("gg", []),
         new Tree("hhh", []),
         new Tree("ii", [])])]);

void main() {
  for (int width in [100, 50, 30, 10]) {
    print(someTree.pretty.render(width));
    print("");
  }
}

Output:

aaa { bbbbb { ccc, dd }, eee, ffff { gg, hhh, ii } }

aaa {
  bbbbb { ccc, dd },
  eee,
  ffff { gg, hhh, ii }
}

aaa {
  bbbbb {
    ccc,
    dd
  },
  eee,
  ffff { gg, hhh, ii }
}

aaa {
  bbbbb {
    ccc,
    dd
  },
  eee,
  ffff {
    gg,
    hhh,
    ii
  }
}

The set of combinators exposed by the library helps defining Tree's pretty method:

Document get pretty => (text(name) + _brackets).group;

Document get _brackets {
  return children.isEmpty
      ? empty
      : text(" {") + (line + _prettyChildren).nest(2) + line + text("}");
}

Document get _prettyChildren {
  return (text(",") + line).join(children.map((c) => c.pretty));
}

More documentation is underway.

Libraries

pretty