tweag / ormolu

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

Newline after instance declaration #170

Closed pkamenarsky closed 5 years ago

pkamenarsky commented 5 years ago
instance A.FromJSON Service where
  parseJSON =
    A.genericParseJSON $
      A.defaultOptions
        { A.fieldLabelModifier = A.camelTo2 '_' . drop 2
        }

gets formatted to

instance A.FromJSON Service where

  parseJSON =
    A.genericParseJSON $
      A.defaultOptions
        { A.fieldLabelModifier = A.camelTo2 '_' . drop 2
        }

Is this intentional? This is of course subjective, but the extra newline makes it look like instance A.FromJSON Service where is a separate thing from parseJSON, which it is not.

mrkkrp commented 5 years ago

It is intentional: this was my request. My logic here is that if there are several methods, then they should be separated by newlines and it's odd to "glue" the first method with instance header by omitting a newline there. Feel free to argue otherwise, we might change it later.

mboes commented 5 years ago

@mrkkrp The explanation makes sense, except that if we were to apply it consistently, we would do the same after where in a function definition. Most existing code doesn't do that - whether in a function or in instance declarations.

pkamenarsky commented 5 years ago

Maybe related: https://github.com/tweag/ormolu/issues/74.

A single instance ... line looks like a deriving instance or an empty instance at a quick glance to me.

A compromise would be to not collapse empty lines present in the original layout or conversely, introduce new ones where there were none before. This would allow the programmer to group semantic constructs as they see fit, which I'd argue is paramount for readability.

mrkkrp commented 5 years ago

My plan currently is to return to more subtle stylistic questions after initial release and making sure we can format all existing code. So, this one (and others tagged "style") is lower-priority for now.

neongreen commented 5 years ago

Agreed with the OP, seeing code like this is kinda weird:


-- | A category was found.
data CSRCategory
  = CSRCategory
      { csrcInfo :: CCategoryInfo
      , csrcDescription :: CMarkdown
      }
  deriving (Show, Generic)

instance A.ToJSON CSRCategory where

  toJSON = A.genericToJSON jsonOptions

instance ToSchema CSRCategory where

  declareNamedSchema = genericDeclareNamedSchema schemaOptions
mrkkrp commented 5 years ago

Perhaps the leading newline should be inserted only sometimes:

Ciantic commented 4 years ago

What makes this odd, is that it makes long list of these hard to read, e.g.


data Foo a = Foo a

data Boo a = Boo a

instance Functor Foo where
  fmap f foo = undefined

instance Applicative Foo where

  pure x = undefined

  f <*> foo = undefined

instance Functor Boo where
  fmap f foo = undefined

instance Applicative Boo where

  pure x = undefined

  f <*> foo = undefined
mboes commented 4 years ago

Omitting the blank line in all cases after the instance head makes sense.