quchen / prettyprinter

A modern, extensible and well-documented prettyprinter.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

new hardline behavior changes multiline strings

vmchale opened this issue · comments

With prettyprinter (due to #139 I think) <1.7 it prints

vanessa@vanessa-desktop ~/programming/haskell/done/dickinson 🌸 cat lib/cowsay.dck -E
%-$
$
(:def cowsay$
  (:lambda txt text$
    '''$
    $
    ${txt}$
    ------$
          \   ^__^$
           \  (oo)\_______$
              (__)\       )\/\$
                  ||----w |$
                  ||     ||$
    '''))$

With the new behavior, this is:

%-$
$
(:def cowsay$
  (:lambda txt text$
    '''$
$
    ${txt}$
    ------$
          \   ^__^$
           \  (oo)\_______$
              (__)\       )\/\$
                  ||----w |$
                  ||     ||$
    '''))

Unfortunately there's no way to say "keep indentation that would otherwise be blank after newline" - and getting rid of the empty whitespace changes the meaning of the whole string (I think something similar might happen in Dhall?).

Perhaps a new combinator?

Oh! Could you share the code that produces this output?

Ah, I think I get it, and the culprit is indeed #139. I hadn't expected that the whitespace produced by indenting empty lines could be significant, and I still kind of feel that it shouldn't be.

In Dhall, this leading whitespace doesn't matter, and in fact the dhall pretty-printer actively ensures that indented empty lines of multi-line strings don't result in trailing whitespace:

https://github.com/dhall-lang/dhall-haskell/blob/2662044b3ceaccc31898a67ab53cc05b382118cd/dhall/src/Dhall/Pretty/Internal.hs#L1393-L1397

In your case, you can simply annotate each line of the multi-line string. The annotation then prevents the layouter from perceiving the line as empty, so it will always produce the space characters.

In Dhall, this leading whitespace doesn't matter, and in fact the dhall pretty-printer actively ensures that indented empty lines of multi-line strings don't result in trailing whitespace:

That might be the thing to here!

And thanks for the pointer w.r.t. annotations :)

You're welcome, @vmchale! :)

I'm somewhat wondering whether this is something that we should document in the haddocks… I'm not sure where though.

Let's maybe see whether this is an issue that more people will stumble upon.