quchen / prettyprinter

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

'line' not being 'group'ed #162

Open georgefst opened 4 years ago

georgefst commented 4 years ago

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".

sjakobi commented 4 years ago

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.

georgefst commented 4 years ago

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.

sjakobi commented 4 years ago

@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?

georgefst commented 4 years ago

Thanks. That makes it a lot more obvious.

quchen commented 4 years ago

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.

sjakobi commented 4 years ago

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