1
1
mirror of https://github.com/google/ormolu.git synced 2024-10-26 08:31:36 +03:00
Commit Graph

219 Commits

Author SHA1 Message Date
Mark Karpov
ec1a432eed Add a CPP macro continuation test 2020-04-22 15:48:53 +02:00
Mark Karpov
53f582a575
Implement experimental support for CPP 2020-04-22 14:47:09 +02:00
Mark Karpov
dde7560291 Allow disabling of Ormolu with special comments
Ormolu can be turned on and off via the special comments:

{- ORMOLU_DISABLE -}

and

{- ORMOLU_ENABLE -}

This allows us to disable formatting selectively for code between these
markers or disable it for the entire file. To achieve the latter, just put
{- ORMOLU_DISABLE -} at the very top. Note that the source code should still
be parseable even without the “excluded” part. Because of that the magic
comments cannot be placed arbitrary, but should rather enclose independent
top-level definitions.
2020-04-21 12:44:39 +02:00
Mark Karpov
c364a30555 Preserve grouping of TH splices 2020-04-20 14:06:22 +02:00
Mark Karpov
490720fedc Preserve comments on pragmas 2020-04-17 17:01:34 +02:00
Mark Karpov
145f7296f5 Preserve grouping of do statements 2020-04-16 20:41:36 +02:00
Mark Karpov
d09429f6ec Format records with a single data constructor more compactly 2020-04-10 18:02:26 +02:00
Mark Karpov
d96f7e3e00 Do not lump together forall and constructors when deciding on layout 2020-04-10 14:37:15 +02:00
Mark Karpov
9b342488be Render type applications similar to value applications 2020-04-10 14:37:15 +02:00
Mark Karpov
9b7b6eb239 Fix rendering of arrow notation with multiline expressions 2020-04-10 12:42:05 +02:00
Utku Demir
100fa44dbb Respect the ‘ImportQualifiedPost’ language extension
It is tempting to enable ‘ImportQualifiedPost’ automatically for all files,
but in that case we won't be able to tell when to format in the
classical (prefix) style vs the new postfix style. Checking the list of
pragmas on per-file basis is not good enough because the extension can be
enabled in other ways, such as via the --ghc-opt flag.
2020-04-05 12:22:07 +02:00
Utku Demir
b0a261fe5b Support StandaloneKindSignatures 2020-04-05 11:06:32 +02:00
Utku Demir
2c5472944b Update ghc-lib-parser to 8.10.1
GHC 8.10.1 comes with some changes to the AST, which works great for
Ormolu, but causes this commit to be a bit large:

* Trees That Grow extension points for new constructors are now statically
  proven to be uninhabited, via noExtCon :: NoExtCon -> a. Thanks to this
  change I got rid of many notImplemented calls.
* LPat constructor is now a lot more usable, so we don't need to use
  the locatedPat combinator and can remove some boilerplate code.

Also it comes with ImportQualifiedPost and StandaloneKindSignatures
we should support. I did not implement them in this commit, they'll
be merged in later on.

It causes one behaviour change, where the ordering of qualified and
non-qualified imports of the same module is changed. This is due to
our usage of gcompare resulting a different ordering because of the
AST change caused by the ImportQualifiedPost extension. I think this is
acceptable and we shouldn't try to keep backwards compatibility there.

Another behaviour change is that previously HsExpr had a few extra
constructors for arrows and patterns used in expression context. Those
programs were syntactically incorrect, but refused on a later stage. But
we nonetheless formatted those constructs so Ormolu didn't fail there
while keeping the source code intact. However, now those constructors
are removed, so Ormolu fails with a parse error in this case (same as
GHC). I also removed some tests exhibiting this behaviour.
2020-04-05 10:53:40 +02:00
Mark Karpov
ac719f3e99 Improve rendering of "where" in class declarations and such 2020-02-10 11:21:34 +01:00
Mark Karpov
84c2a53349 More compact rendering of type family injectivity constraints 2020-02-01 20:03:41 +01:00
Mark Karpov
a37765cbcb Make rendering of operator patterns consistent with other cases 2020-01-31 14:36:40 +01:00
Avi Dessauer
77424882fd Differentiate recordDot & standard record updates 2020-01-17 11:12:08 +01:00
Sridhar Ratnakumar
116fe842b4 Preserve newlines in class and instance declarations
Preserve user-added newlines with the following normalizations:

* No newlines below where
* Consecutive newlines are compressed into a single newline
* Always surround documented declarations with blank lines
2020-01-11 13:05:18 +01:00
Mark Karpov
6ee0365f2c Fix indentation in presence of type applications 2020-01-10 16:53:29 +01:00
Mark Karpov
1eefa97cc2 Do not hang record constructors
Previously we hanged record constructors but not record updates. It looks
like unlike other hanging constructions (lambdas, case expressions, and
do-blocks), record constructors and updates should rather be placed
normally. Indeed, when we stop hanging them, many constructions start to
look more reasonable and predictable (see the updated test cases).

Yet the change is not enough to fix the problem in general case: it is
enough to replace the record with a e.g. do-block to get the same failure.
To correct that we adjust what should fit in one line for hanging placement
to fire: now we consider the span between beginning of function and the
start of potentially-hanging construction.
2020-01-09 20:43:33 +01:00
Avi Dessauer
6f7c9dea73 Support RecordDotSyntax plugin and preprocessor
This supports the two forms of ‘RecordDotSyntax’ that are likely to be
upstreamed ‘expr.lbl’ and ‘(.lbl)/ .lbl’.

We detect -fplugin=RecordDotPreprocessor and -pgmF=record-dot-preprocessor
in -o arguments or pragmas in files and just do the right thing.
2020-01-07 16:33:23 +01:00
Mark Karpov
4e253c9eb2 Fix application of multi-way if to arguments 2020-01-06 16:06:39 +01:00
Alexander Esgen
3b745c723e remove do-block braces in arguments 2019-12-03 11:09:55 +01:00
mrkkrp
5b9861b57f Fix formatting of applications where function is a complex expression
Usually we want to bump indentation for arguments for the sake of
readability. However, when the function itself is a do-block or case
expression it is not a good idea. It seems to be safe to always bump
indentation when the function expression is parenthesised.
2019-11-29 16:04:36 +01:00
mrkkrp
5ca92059fb Take non-commutative nature of language pragmas into account
The order in which language pragmas are put in the input sometimes matters.
This is because some language extensions can enable other extensions, yet
the extensions coming later in the list have the ability to change it. So
here we classify all extensions by assigning one of the four groups to them.
Then we only sort inside of the groups.
2019-11-29 15:05:34 +01:00
mrkkrp
6e07126aca Handle stack headers correctly
More about the feature:

https://docs.haskellstack.org/en/stable/GUIDE/#script-interpreter
2019-11-28 12:17:15 +01:00
mrkkrp
39592b2e88 Implement correct handling of shebangs
Also: rename and document some functions we took from ‘ghc-exact-print’.
2019-11-21 17:52:07 +01:00
mrkkrp
524f8763f3 Insert comments between declarations and their preceding Haddocks
I originally thought it's a good idea to prevent this, but it looks like it
makes things arguably worse in certain cases even though the Haddocks end up
not associated with their declarations (but they are not associated with
them in the input either, so it's OK).
2019-11-19 13:24:11 +01:00
Utku Demir
0a8cfd62ef Use mode instead of mean when calculating operator fixities
When deciding on the fixity of an operator, we assign a score based on some
heuristics. When an operator is used multiple times in an operator chain, we
used to calculate the mean of its scores to decide on the final fixity.
However, this was prone to outliers.

In an example like this:

  b & c .~ d
    & e
      %~ f
          g

The first use of & is surrounded by other values, so we assign it a higher
score (binds tighter). However the second occurence is at the beginning of a
line, and this has a lower score. In the end, when we average those two
values the result binds tighter than %~, which is not what we want.

This PR replaces the average method with a mode operation instead, so it is
less prone to outliers.

This PR fixes this specific issue, and does not cause any change on other
test cases, but it might cause some diffs in the wild.
2019-11-19 11:14:27 +01:00
Utku Demir
81e60bb342 Use braces when placing $ on the same line 2019-11-17 12:17:11 +01:00
mrkkrp
2517d98ba1 Preserve empty ‘forall’s 2019-11-02 15:06:18 +01:00
Artyom Kazak
b08af17217 Migrate to ‘ghc-lib-parser’
This would let us have features from GHC 8.8.1 while still using GHC 8.6.5
from Nixpkgs. Bonus: Ormolu will be compilable with GHCJS.
2019-11-01 18:19:34 +01:00
mrkkrp
647e32299f Fix moving comments before module header into the export list 2019-11-01 18:12:05 +01:00
mrkkrp
e966848e52 Fix printing of result type in closed type families
Apparently ‘KindSig’ always means double colon, while ‘TyVarSig’ always
means equal sign.
2019-10-26 15:44:03 +02:00
mrkkrp
43c6f45634 Fix a minor inconsistency between normal and foreign type signatures 2019-10-19 13:29:29 +02:00
Utku Demir
3abadaefa5 Make function arguments look more consistent
This change modifies the ‘HsApp’ printer to handle all function parameters
at once instead of following the AST order.

This allows us to consistently put each function argument on separate lines.
Previously, when printing the function f a b c, the AST would look like (((f
a) b) c), which would cause inconsistencies regarding to newlines (imagine f
a\n b).

Otherwise I tried to preserve the same behaviour as the previous printer,
and added a few tests.

This only changes the ‘p_hsExpr’ function, rest of the changes are caused by
the formatting change.
2019-10-08 11:55:04 +02:00
Utku Demir
1d3eeba701 Don't use braces inside parens 2019-10-08 11:08:07 +02:00
mrkkrp
d02483a8ab Fix the “overindentation” issues with function applications 2019-10-04 20:54:04 +02:00
mrkkrp
368b2da09b Hang expressions in RHS of (<-) in do blocks
Plus some refactoring.
2019-10-03 15:28:38 +02:00
mrkkrp
519136f062 More precise layouts in precise of Haddocks in data definitions 2019-10-03 11:53:05 +02:00
mrkkrp
397fc55b67 Keep the ($) operator on the same line in certain cases 2019-10-02 21:46:15 +02:00
mrkkrp
8466d6e743 Change the style of type signatures
The commit changes how type signatures are printed. The new style looks like
this:

  foo ::
    Int ->
    Char ->
    String

This works better with foralls and other features of the type system that
will be added in the near future, like linear arrows.

In order to print Haddocks nicely (this seems to be the only acceptable
placement):

  foo ::
    -- | First argument
    Int ->
    -- | Second argument
    Char ->
    -- | Result
    String

It is often necessary to re-arrange them completely and use the “pipe style”
instead of “caret style”. It proved to be a very hard task with our older
comment-handling system, if not impossible.

Here we start parsing Haddocks so that they are treated as components of AST
and we now render them as part of rendering of those components. The
existing framework for handling comments only prints non-Haddock comments
now.

The change caused a fair number of new problems and failures which I added
new tests for.
2019-10-02 16:52:42 +02:00
mrkkrp
87406cbd8f Align opening/closing parentheses and other similar punctuation 2019-09-17 11:58:22 +02:00
mrkkrp
a4e97da37d Introduce a breakpoint after ‘forall’ in type signatures 2019-09-16 13:16:26 +02:00
Utku Demir
c16fe06df3 Tweak hanging logic on HsApp 2019-09-16 10:56:18 +02:00
Utku Demir
ed27fb4a9a Handle operators as record fields 2019-09-16 10:38:07 +02:00
mrkkrp
a83796bccb Fix positioning of shebangs 2019-09-14 16:47:19 +02:00
mrkkrp
75ac291f13 Hang guarded equations
Previously guarded expressions were always printed in non-hanging fashion
even if they had a hanging form. This change fixes this behavior.
2019-09-14 14:34:39 +02:00
Utku Demir
1b818c0064 Correct indentation on string gaps
Haskell strings can have "gaps", where any amount of whitespace between
two backslashes are ignored. This allows writing multi-line strings. As
an example, all strings below are the same:

```
"foobar"
"foo\   \bar"
"foo\

   \bar"
```

When parsing a string literal, lexer usually produces two fields, one
of them is the actual string user wrote as a 'SourceText', the other one
is the sanitized version where gaps and other special characters removed.

While printing the string, GHC's Outputable instance uses the 'SourceText'
field, however since that text contains gaps as-is, we can not change
the original indentation. In order to fix this, this commit splits the
strings by the gaps and print each line separately applying the layout
rules.

Also, it applies the same logic to type-level strings.
2019-09-13 11:26:05 +02:00
mrkkrp
c9c3876410 Handle comments inside of import statements better 2019-09-12 21:59:43 +02:00