Commit Graph

155 Commits

Author SHA1 Message Date
Utku Demir
8662581ad5 Correctly place comments inside enclosing expressions at the end of the module 2019-08-28 12:04:14 +02:00
mrkkrp
8485e34ec7
Use ‘sitcc’ around let-in construction
Due to the “weird” leading space before ‘in’ we get rather unpleasing
results without ‘sitcc’.
2019-08-27 18:28:49 +02:00
mrkkrp
96390b2405 Fix idempotence bug due to wandering comments
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.
2019-08-27 17:32:11 +02:00
Utku Demir
effb5cd306 Group declarations referring to multiple names 2019-08-27 11:08:43 +02:00
Utku Demir
721ba7e367 Only force braces on hsLocalBinds on a single-line layout 2019-08-27 10:33:11 +02:00
Utku Demir
e5b99e8e66 Be more conservative when inserting braces 2019-08-26 11:58:19 +02:00
Utku Demir
af907ecb9e Make the first newline optional on class declarations 2019-08-26 10:49:23 +02:00
Utku Demir
25c609ca23 Fix an issue with detecting spans of patterns with guards 2019-08-26 10:39:25 +02:00
Utku Demir
de279d8012 Align multi-line lambda arguments nicer 2019-08-25 13:36:47 +02:00
Utku Demir
b2c26b7859 Conditionally add an extra newline on instances 2019-08-25 13:03:45 +02:00
Utku Demir
8cb86d3fac Hang expressions in if-then-else 2019-08-25 12:43:12 +02:00
Utku Demir
9a1f5ebd87 Split large examples
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.
2019-08-25 12:35:31 +02:00
mrkkrp
f94400b486 Indent ‘then’ and ‘else’ clauses one level further
This arguably improves readability especially when if-then-else is placed
inside of a ‘do’-block.
2019-08-16 22:38:38 +02:00
Utku Demir
e598b72be5 Preserve the name of the expression bracket
Both `[e|...|]` and `[||]` mean the same thing, and instead of sticking to a
single style, we can just preserve what user wrote.
2019-08-16 22:17:53 +02:00
Yuriy Syrovetskiy
ee3c506889 Change multiway if layout to keep consistent 2-space indent increase 2019-08-16 17:03:11 +02:00
mrkkrp
d0a44c23cb Add a couple of tests for lambdas with block arguments 2019-08-16 16:48:39 +02:00
mrkkrp
0f76bd4a56
Fix the build 2019-08-16 16:42:35 +02:00
Utku Demir
118558738b Hang after block arguments 2019-08-16 16:20:33 +02:00
yumiova
682a5f4672 Dedent parallel list comprehensions by one step 2019-08-16 16:20:20 +02:00
Utku Demir
159e5e7761 Use new operator style in types & declarations
```
a
  + b
```

instead of

```
a +
  b
2019-08-16 16:15:28 +02:00
Utku Demir
f86155a8ea Try to contain comments inside enclosing constructs
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.
2019-08-16 16:11:33 +02:00
Utku Demir
58f81876e9 Hang record constructor fields 2019-08-16 16:08:17 +02:00
Utku Demir
625f23959c Implement implicit parameter bindings 2019-08-16 15:57:20 +02:00
Utku Demir
16f2c644a7 Implement handling of semicolons 2019-08-16 15:41:40 +02:00
Utku Demir
c3d69d5853 Print multiline comments without changing the contents
Changing the relative position of the closing brace can cause whitespace
changes.
2019-08-16 15:41:26 +02:00
Utku Demir
b3f6e1b4f6 Correctly format when adding annotations to types 2019-08-14 21:35:35 +02:00
Utku Demir
f55021d89b Fix printing of the rewrite rule names 2019-08-14 21:34:05 +02:00
Utku Demir
a392aef71e Fix the issue with promoted containers with promoted elements 2019-08-14 21:31:50 +02:00
Utku Demir
57373e536d Fix formatting of infix type synonyms using ‘p_infixDefHelper’ 2019-08-14 21:30:43 +02:00
Utku Demir
acc77002e4 Support inline specs inside specialize pragmas 2019-08-14 21:24:04 +02:00
Utku Demir
0cf6d654eb Fix TH quoting of operators 2019-08-14 21:23:21 +02:00
mrkkrp
e21e4ef119 Fix mis-formatting of per-argument comments in certain cases 2019-08-13 20:59:22 +02:00
mrkkrp
825171a16d Group deprecation declarations with data types as well 2019-08-11 21:41:23 +02:00
Utku Demir
b6c29bdf12 Handle OPTIONS_GHC and OPTIONS_HADDOCK pragmas
This change adds an ad-hoc parser for module pragmas to handle
OPTIONS_* pragmas. I did not want to use an existing tokenizer,
because I felt like tokenizing and pretty printing the GHC options
are more prone to error without providing much benefit.
2019-08-11 16:04:42 +02:00
Utku Demir
be6d09d148 Workaround GHC tokenizer bug while extracting LANGUAGE pragmas 2019-08-09 11:24:11 +02:00
Utku Demir
c2b32da3b5 Tweak formatting of warning pragmas 2019-08-09 11:00:06 +02:00
mrkkrp
93271b2551 Make sure no comments are swallowed at the end 2019-08-08 20:20:17 +02:00
Utku Demir
a0aa410741 Fix printing of TH brackets
The issue was simply indenting the closing `|]` one more level. However
there were a few more issues around them, which led me to a slightly
bigger refactor.

main reason is that, all `p_*Decl` functions used to print a trailing
newline. This makes sense for top-level constructs, however it was making
printing something like `[d|data Foo = Foo|]` impossible.

In this commit, I removed trailing newlines from individual printers
and gave that responsibility to `p_hsDecls`, and inserted an additional
trailing newline when printing modules.

While doing that, I noticed a few bugs/inconsistencies, and I had to
fix them in the process:

* Warning pragmas used to not print a trailing newline, so they were
always attached to the next expression. I made it more like the other
pragmas, where we attach it to a neighbour function if the name matches,
otherwise we separate it with a newline.
* We used to print single line GADT's and single line `do` notations
using multiple lines, which breaks idempotency. I tweak them to prefer
single line layout if possible (sometimes it is not possible because
of the semicolon syntax).
2019-08-08 11:30:20 +02:00
Utku Demir
e2a6305bd3 Change the way operators are formatted 2019-08-07 20:50:30 +02:00
Teodoro Freund
5955c514ee Fix backticks and parens in multiline function declarations
Function id obtained through pattern matching on ‘FunBind’ should not be
used to print the actual equations because the different ‘RdrNames’ used in
the equations may have different “decorations” (such as backticks and
paretheses) associated with them. It is necessary to use per-equation names
obtained from ‘m_ctxt’ of ‘Match’.
2019-08-07 19:14:59 +02:00
mrkkrp
094e70a2eb Avoid failing when comments get before an operator
This puts out the fire, but I'm not fully content with the solution. I also
do not understand why it fails in the original issue but succeeds for e.g.:

  foo = do
    1
    +
    2
2019-08-05 22:25:38 +02:00
mrkkrp
8fdc9739f0 Fine-tune placement of lambdas after operators 2019-08-05 21:25:34 +02:00
mrkkrp
8870acd41f
Replace weird whitespace characters by normal spaces
I'm just curios of their origin. If I'm not mistaken they were introduced by
@yumiova.
2019-08-05 21:01:52 +02:00
Basile Henry
09e2a538ef Add printer for ‘HsSpliceTy’ 2019-08-05 19:46:47 +02:00
mrkkrp
f86e01ea3b Avoid crashing on unboxed string literals 2019-08-04 23:27:31 +02:00
Mauricio Fierro
bdb22b96ac Fix record pattern synonyms 2019-08-04 22:41:48 +02:00
gabriele.lana
be53b3e504 Format unboxed sum pattern 2019-08-04 21:47:52 +02:00
mrkkrp
e344f61d33 Fix some inconsistency in indentation 2019-08-04 16:50:38 +02:00
mrkkrp
85de7ce38c Add support for the CONLIKE modifier in inlining pragmas 2019-08-04 13:54:08 +02:00
mrkkrp
85d5f78b4b Change parens style and how separated lists of items are printed
Goals:

* Make the set of combinators clearer and smaller.
* Solve a number of issues, such as those about parse failures related to
  patterns.
* Solve the bug from #244.

The idea is very simple, we stop doing this

  ( foo
  , bar
  )

and start doing this

( foo,
  bar
  )

* We switch to trailing commas which solves the indentation issues for
  patterns automatically.
* The new general ‘sep’ combinator finally is clear enough, and all the old
  zoo of ‘velt’ and ‘velt'’ and ‘sepWith’, etc. which was confusing and
  overlapping goes away.
2019-08-04 13:32:06 +02:00