prettyprinter icon indicating copy to clipboard operation
prettyprinter copied to clipboard

'line' not being 'group'ed

Open georgefst opened this issue 4 years ago • 6 comments

I'm not quite experienced enough with this library to confidently call this a bug, but the following behaviour certainly seems odd:

putDoc $ group $ "a" <> line <> "b" <> hardline <> "c"

outputs:

a
b
c

as though the use of hardline has made the line hard as well. The same result is seen with "a" <> hardline <> "b" <> line <> "c".

georgefst avatar Jun 09 '20 09:06 georgefst

Yeah. When I started using this library, I believe I found this behaviour a bit strange, too.

The documentation for group tries to point it out though, at least if you read it closely:

(group x) tries laying out x into a single line by removing the contained line breaks; if this does not fit the page, x is laid out without any changes.

Importantly, the single-line layout also fails when the document contains a hard linebreak.

Do you think this should be made clearer, and would you possibly have a suggestion how to clarify it?

I don't think this behaviour can be easily changed, in any case. Sometimes you want to group a document where you don't know yet whether some inner part has a hard linebreak or not. In that case the current behaviour is actually desirable.

In your case, you could possibly use softline instead of line.

sjakobi avatar Jun 09 '20 10:06 sjakobi

For me, it's far from clear from that Haddock coment... But I'm sorry I don't really understand the behaviour well enough to suggest something better.

Anyway, I ended up solving my problem by going with a very different approach.

georgefst avatar Jun 09 '20 22:06 georgefst

@georgefst I've tried to clarify the documentation for group a bit in https://github.com/quchen/prettyprinter/pull/168.

Could you check whether that's better?

sjakobi avatar Jun 13 '20 15:06 sjakobi

Thanks. That makes it a lot more obvious.

georgefst avatar Jun 14 '20 00:06 georgefst

I agree that this is surprising behavior, despite the documentation mentioning it. group is a bit tricky at times… #168 improves the docs, but you still have to read that section though in order to understand the behavior. Unfortunately, I don’t think this is easily fixed in code, so the updated documentation will have to do.

quchen avatar Jun 17 '20 09:06 quchen

I'll leave this open for a bit, just in case someone comes up with an idea to improve the documentation on this further.

sjakobi avatar Jun 17 '20 13:06 sjakobi