ormolu icon indicating copy to clipboard operation
ormolu copied to clipboard

StandaloneDeriving: preserve choice of blank lines

Open amesgen opened this issue 4 years ago • 5 comments

StandaloneDeriving requires separate lines for each derived instance:

data Foo

deriving stock instance Generic Foo
deriving anyclass instance FromJSON Foo
deriving anyclass instance ToJSON Foo
deriving anyclass instance FromDhall Foo
deriving anyclass instance ToDhall Foo

Right now, ormolu will insert blank lines inbetween the deriving statements, which is unnecessary IMHO and also prevents grouping of related derivings.

data Foo

deriving stock instance Generic Foo

deriving anyclass instance FromJSON Foo

deriving anyclass instance ToJSON Foo

deriving anyclass instance FromDhall Foo

deriving anyclass instance ToDhall Foo

What about preserving the choice of blank lines, similar to #554? Happy to put a PR together in this case.

amesgen avatar Jan 24 '21 02:01 amesgen

I'm not sure. If the declarations are multi-line it becomes desirable to separate them for readability. If so, should preservation of blank lines be conditional?

mrkkrp avatar Jan 24 '21 11:01 mrkkrp

Yes, blank lines are sometimes useful for separation. To clarify, I would like both of my code examples above to be unchanged when formatted, as well as

data Foo

deriving stock instance Generic Foo

deriving anyclass instance FromJSON Foo
deriving anyclass instance ToJSON Foo

deriving anyclass instance FromDhall Foo
deriving anyclass instance ToDhall Foo

amesgen avatar Jan 24 '21 17:01 amesgen

Sorry, I meant, should it be conditional on multi-line vs single line layout of the deriving declarations?

mrkkrp avatar Jan 24 '21 21:01 mrkkrp

Ah, so you mean that if we had

deriving stock instance Generic Foo
deriving via
  CustomJSON
    '[ OmitNothingFields,
       FieldLabelModifier (StripPrefix "foo", CamelToSnake)
     ]
    Foo
  instance
    FromJSON Foo

then the question is whether ormolu should add a blank line inbetween as the second deriving declaration is multiline?

I have no strong opinion here, but I don't think it is strictly necessary to preserve readability.

amesgen avatar Jan 24 '21 21:01 amesgen

Variant of #635.

ntc2 avatar Jul 07 '22 12:07 ntc2