quchen / prettyprinter

A modern, extensible and well-documented prettyprinter.
BSD 2-Clause "Simplified" License
293 stars 34 forks source link

Align mid-line? #221

Open isovector opened 2 years ago

isovector commented 2 years ago

I have a doc that currently pretty prints as

S : Set   (not in scope)
A : Set        (not in scope)
B : Set      (not in scope)
ma : State' S A 
f : A → State' S B 
s : S 
s' : S 
a : A 

but would instead like to produce

S  : Set        (not in scope)
A  : Set        (not in scope)
B  : Set        (not in scope)
ma : State' S A 
f  : A → State' S B 
s  : S 
s' : S 
a  : A 

aligning on both the : and the (not in scope). Is this possible? I can't figure out how to get align to do this.

sjakobi commented 2 years ago

I don't know how to do this. AFAIK Wadler-Leijen prettyprinters don't really support this.

53, especially https://github.com/quchen/prettyprinter/issues/53#issuecomment-373877920 is related.

https://github.com/natefaubion/purescript-dodo-printer/pull/8 is related work: Maybe it can be done after all?! We'll see.

natefaubion commented 2 years ago

The dodo-printer PR handles this by:

It also has stuff for box alignment (like align-self in CSS) and padding, and omits trailing line alignment. This is kind of ridiculously complicated in PureScript because of stack safety and efficiency, but can be done straightforwardly in Haskell (depending on how you'd scan lines).

Edit: I should also say it assumes distributivity of annotations, which works well for things like ansi colors, but doesn't necessarily work for everything.