justinpombrio / partial-pretty-printer

A pretty printing library for formatting source code in any language, which can efficiently print just part of a document.
Apache License 2.0
3 stars 0 forks source link

Support all-on-one/all-on-many #6

Closed justinpombrio closed 8 months ago

justinpombrio commented 1 year ago

Support notations where a listy thing can be written either all on one line:

{ key1: val1, key2: val2, key3: val3 }

or all on separate lines:

{
    key1: val1,
    key2: val2,
    key3: val3
}

but not mixed:

{
    key1: val1, key2: val2,
    key3: val3
}

Right now this is impossible because choices inside repeat.join are always independent of each other.

Solution

This can be done with an if_flat construct (different than things I've called if_flat in the past...). if_flat(a, b) means "if inside a flat, then pick a otherwise pick b". Wadler can then be emulated by:

break => if_flat(" ", "\n")
group(x) => flat(x) | x
root(x) => group(x)

This will be very easy to implement once the NotationRef refactoring---where Indent and Flat are no longer exposed in NotationCase---is done.

justinpombrio commented 8 months ago

Done, by making fold first class! if_flat wasn't necessary.