cdepillabout / pretty-simple

pretty-printer for Haskell data types that have a Show instance
https://hackage.haskell.org/package/pretty-simple
BSD 3-Clause "New" or "Revised" License
243 stars 29 forks source link

make more newlines in the output #17

Closed cdepillabout closed 6 years ago

cdepillabout commented 7 years ago

The output is pretty hard to read when there are not enough newlines.

bad-formatting

Everything after TypeDeclaration should really have newlines.

andrew-lei commented 6 years ago

Should there be a newline for every paren/bracket/brace layer?

cdepillabout commented 6 years ago

@andrew-lei To be honest, I haven't thought about this hard enough to figure out exactly what the rules should be on when to create newlines.

I think the current rules are something like the following:

In the picture above, I think the reason that nothing after TypeDeclaration has a newline is that there are no commas separating values. This is not ideal, because it is hard to read.

In order to solve this problem, we would need to come up with a good algorithm for when to insert newlines. We then need to come up with a couple doctests to test this.

Should there be a newline for every paren/bracket/brace layer?

I'm not sure if this would be the best algorithm or not, but it could be a good start!

We could also take a look at code formatting tools like hindent or brittany to figure out when they add new lines, and basically just copy how they do it.

andrew-lei commented 6 years ago

I think perhaps alternatively:

Then A ( A B ) stays A ( A B ) (since it's a bit unnecessary to do a newline here), but A ( A ( A B ) ) becomes

A
    ( A ( A B ) )

and A ( A ( A ( A B ) ) ) becomes

A
    ( A
        ( A ( A B ) )
    )
andrew-lei commented 6 years ago

If I'm reading this correctly, it looks like brittany keeps track of how many columns are used. That does sound like a pretty good idea, although it seems like that would require a bit more work. One of the other solutions could work in the meantime and would only require a couple lines of code to be different (other than docstrings I guess), unless you'd rather hold off for a solution like that.

cdepillabout commented 6 years ago

If you wanted to send a PR trying to fix this, then I would leave it up to you to decide how to go about fixing it.

It sounds like keeping track of how many columns are used would be a good solution, but you're right that it would require more work.

If you wanted to go ahead with your suggested solution above, then that would be completely fine! You might want to add a couple more doctests just to make sure everything looks fine! Or you could even go ahead with adding normal unit tests, using hspec or tasty or something.