quchen / prettyprinter

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

Define `vsepHard` #237

Open michaelpj opened 1 year ago

michaelpj commented 1 year ago

Define

vsepHard :: [ Doc a ] -> Doc a
vsepHard = concatWith (\x y -> x <> hardline <> y)

This is a missing function. We have n-ary concatenation functions for various separators: non-breaking spaces, breaking spaces, lines, softlines... but not hardlines.

vsepHard is useful when you have things that you just never want to lay out on a single line. For example, in a programming language you might define let-bindings like so

let 
  x = 1
  y = 2
in t

I always want the x and y bindings to be on separate lines. A more sophisticated version might use flatAlt to use a different syntax when grouped, but I think the simple approach of "just always break" is often useful.