tweag / ormolu

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

Line breaks ignored in data type declarations. #947

Closed jonathanknowles closed 8 months ago

jonathanknowles commented 1 year ago

Describe the bug Ormolu sometimes eats line breaks within data type declarations.

To Reproduce Run ormolu on code similar to the following, with a line break before the =:

 data SomeReasonablyDescriptiveRecordType someParam1 someParam2 someParam3
  = SomeReasonablyDescriptiveRecordType
    { foo :: someParam1,
      bar :: someParam2,
      baz :: someParam3
    }

or with the line break after the =:

 data SomeReasonablyDescriptiveRecordType someParam1 someParam2 someParam3 =
  SomeReasonablyDescriptiveRecordType
    { foo :: someParam1,
      bar :: someParam2,
      baz :: someParam3
    }

Expected behaviour The line break is preserved, and the data constructor name is indented one level to the right.

Actual behaviour Ormolu eats the line break, and prints the data constructor name on the same line, resulting in a rather long line:

data SomeReasonablyDescriptiveRecordType someParam1 someParam2 someParam3 = SomeReasonablyDescriptiveRecordType
{ foo :: someParam1,
  bar :: someParam2,
  baz :: someParam3
}

Environment

Additional context

We realise that ormolu does not provide support for a column limit, but we're wondering if we can get by with manually inserting line breaks where necessary.

Many thanks! Jonathan

amesgen commented 1 year ago

I think respecting line breaks here sounds reasonable, it gets even more annoying with CTYPE pragmas:

data
  {-# CTYPE "header.h" "an-ffi-type-with-along-name"  #-} 
  AnFFITypeWithAlongName = AnFFITypeWithAlongName
  { a :: X,
    b :: Y
  }

is formatted to

data {-# CTYPE "header.h" "an-ffi-type-with-along-name" #-} AnFFITypeWithAlongName = AnFFITypeWithAlongName
  { a :: X,
    b :: Y
  }

without a way to force line breaks without adding bogus comments.