quchen/prettyprinter

Can Boxes be integrated?

Closed this issue · 3 comments

There's this great boxes library that does exactly what prettyprinter cannot. If only we could integrate them, so that Docs could be combined side by side as if they were Boxes!

What I mean is something around the lines of:

λ columns = pretty <$> ["l\na", "l\na", "f\na"]
λ boxToDoc . Boxes.hsep 1 Boxes.top $ docToBox <$> columns
l l f
a a a

− Which I achieved with:

docToBox :: Doc ann -> Box
docToBox = Boxes.vcat Boxes.left . fmap Boxes.text . lines . show

boxToDoc :: Box -> Doc ann
boxToDoc = pretty . Boxes.render

I thought about this as well, to render tables – but unfortunately, it would be a major overhaul of pretty much the entire library. I tried implementing a »local layout options« feature that would allow you to change the ribbon width for only part of a module, but could not make it work.

The reason for this is that the Wadler/Leijen algorithm simply adds things after its cursor, whereas painting a box around a document would require knowing how everything that would follow is going to be rendered, and then adding a box around it all. It’s a bit like the difference between a parser with infinite lookahead and one with only one token.

You could render boxes using render :: Box -> String, and then split that on newlines, and convert it to unmodifiable Doc using concatWith (\x y -> x <> hardline <> y). This allows putting boxes in Documents, but I don’t think there’s a good (or even possible) way to incorporate the general idea of boxes/tables into the WL algorithm.

I’d like to be proven wrong, if you’re up to it I’d welcome it to the library! But until then, I’m closing this as »won’t do«. Sound alright?

Yes, thank you.