tweag / ormolu

A formatter for Haskell source code
https://ormolu-live.tweag.io
Other
958 stars 83 forks source link

Aeson parser formatting #357

Closed torgeirsh closed 5 years ago

torgeirsh commented 5 years ago

I've been experimenting with Ormolu, and came across some bad formatting for Aeson code. Consider the following parser:

instance FromJSON SomeType where
  parseJSON = withObject "SomeType" $ \o ->
    SomeType
      <$> o .: "a"
      <*> o .: "b"
      <*> o .: "c"

Ormolu transforms it into:

instance FromJSON SomeType where
  parseJSON = withObject "SomeType" $ \o ->
    SomeType
      <$> o
      .: "a"
      <*> o
      .: "b"
      <*> o
      .: "c"

I think it's harder to see the purpose of the code at a glance.

mrkkrp commented 5 years ago

Yes we know, this will be implemented better in future. Duplicate of #315, which see.

torgeirsh commented 5 years ago

Is it necessary to consider operator precedence? Couldn't it preserve the original grouping?

mrkkrp commented 5 years ago

Precedence determines the grouping. GHC first parses everything as if all operators have the same precedence and the same fixity. This way things on the same line end up not grouped together but as parts of different nested expressions so the span information we infer tells Ormolu that the current layout is multi-line, so you get every operator on its own line. Long story short, yes, we need to infer relative precedence (not necessarily exact precedence though) to preserve the way operators are grouped.