This removes (or rather puts it to a lower level) logic around “modifying
newline” because it was very hard to reason about and almost blocked my work
on fixing issue #337.
I also dropped debugging output because it's too verbose and I'm not using
it anyway.
As part of these changes I also changed now the ‘newline’ combinator works.
Now, similar to ‘space’, the second ‘newline’ in a row just tells the
rendering engine to prefix next thing with a newline, using the ‘newline’
combinator more than twice in a row has no effect.
To take full advantage of the new feature I also went through the code and
simplified some logic around outputting exact amount of newlines because now
it's harder to get things wrong, so we can be less careful with counting
newlines.
Turns out when ‘--unsafe’ is not set, exceptions thrown in formatting code
are caught in the subsequent parsing check, which wraps them as GHC errors
adding a text like:
Please report this as a GHC bug: http://www.haskell.org/ghc/reportabug
Which is misleading since the error is not caused by GHC.
This PR avoids it by strictly binding the result of ‘printModule’.
Previously, if an operator had preceding comments attached to its second
argument, they would end up printed right after the operator:
a
+ -- b comment
b
On second run however, the comment would be interpreted as attached to ‘(+)’
and the result would be:
a
+ b -- b comment
Breaking the idempotence guarantees.
The solution that this commit implements includes several steps:
* Introduce the concept of “dirty line”. A line is dirty if it has something
on it that can have a comment attached to it.
* ‘txt’ is supposed to output fixed bits of syntax that cannot have comments
attached to them (at least in Ormolu's model).
* ‘atom’ on the other hand outputs things that mark the current line dirty.
* When we're to print preceding comments for the second argument we check if
the current line is dirty. If it is, we output an extra newline to prevent
the first comment from changing “hosts”.
* Now there is another problem with trailing whitespace after the operator
in that case. We solve that by making spaces a bit “lazy”. When the ‘space’
combinator is used (which is the recommeneded way to separate different
constructs now) it just guarantees that the next thing we'll output on the
same line will be separated from previous output by a single space.
So, using ‘space’ twice results in single space in output still. This has
the extra benefit of simplifying all the logic that made sure that we have
only single space and not 0 or 2 spaces when spaces are inserted
conditionally and independently.
There has been a lot of good intense work lately and as a result of that
some examples have grown considerably. The problem is that we do not show
diffs when something is not formatted as expected, we show entire
"expected/got" files. It works well when files are small, but not so well
where they are huge (some of our examples are well beyond 100 lines). It can
be hard to understand where the problem is.
This commit split long examples into smaller ones to make it easier to see
what went wrong when a test fails.
Although Cabal 3 is now released, not everyone is using it yet. This is why
we cannot just write ‘data/**/*.hs’. We have to have compatibility with
older Cabal versions. For that I'm adding the full list of data
sub-directories constructed using ‘find data -type d’.
Attach the comment if the next element is not a sibling. I think this is
quite often what we want, since if we put a comment inside a construct, we
prefer it to stay inside the same element.