ormolu
ormolu copied to clipboard
newline inserted between pattern definition and complete pragma
Describe the bug
pattern A :: Int -> Int
pattern A a = a
{-# COMPLETE A #-}
gets formatted into
pattern A :: Int -> Int
pattern A a = a
{-# COMPLETE A #-}
Expected behavior There should be no lines between, similar to inline/inlineable pragmas.
I implemented a solution to this at ud/grouping-complete-pragmas branch; simply by handling COMPLETE pragma as we handle others, where we omit the newline if a pragma refers to a name on the previous declaration. However I am not completely sure if this is the right solution in this case.
In your code snippet, I agree that there shouldn't be a newline. However, I feel like it is more likely that a COMPLETE pragma would refer to more patterns. Consider two variations of the example mentioned on GHC manual:
Case 1:
data Choice a = Choice Bool a
pattern LeftChoice :: a -> Choice a
pattern LeftChoice a = Choice False a
pattern RightChoice :: a -> Choice a
pattern RightChoice a = Choice True a
{-# COMPLETE LeftChoice, RightChoice #-}
Case 2:
pattern LeftChoice, RightChoice :: a -> Choice a
pattern LeftChoice a = Choice False a
pattern RightChoice a = Choice True a
{-# COMPLETE LeftChoice, RightChoice #-}
I think, ideally there should be a newline at the first case; and on the second case there shouldn't. My branch would remove the newline on both cases, since both pragmas refer to the same name as the previous one (RightChoice). This result would be less than ideal on the first case, since it would look like the pragma is more relevant to the RightChoice than to the LeftChoice.
Handling that would require some ad-hoc heuristics, and also we currently don't handle any pragma specially. So I do not think we should; but I wanted to have your opinion before I open a PR.
@mrkkrp what do you think? If you think the branch works. please feel free to merge, otherwise I can open a PR.
This is a variant of #635. I think the solution is for Ormolu to preserve groupings in all cases: don't insert blank lines where there were none, and don't remove blank lines where they existed.