@@ -13,6 +13,7 @@ import Control.Monad
1313import GHC
1414import Ormolu.Printer.Combinators
1515import Ormolu.Printer.Meat.Common
16+import Ormolu.Utils (RelativePos (..), attachRelativePos)
1617
1718p_hsmodExports :: [LIE GhcPs] -> R ()
1819p_hsmodExports [] = do
@@ -22,7 +23,10 @@ p_hsmodExports [] = do
2223p_hsmodExports xs =
2324 parens N . sitcc $ do
2425 layout <- getLayout
25− sep breakpoint (sitcc . located' (uncurry (p_lie layout))) (attachPositions xs)
26+ sep
27+ breakpoint
28+ (\(p, l) -> sitcc (located l (p_lie layout p)))
29+ (attachRelativePos xs)
2630
2731p_hsmodImport :: Bool -> ImportDecl GhcPs -> R ()
2832p_hsmodImport useQualifiedPost ImportDecl {..} = do
@@ -63,12 +67,15 @@ p_hsmodImport useQualifiedPost ImportDecl {..} = do
6367 breakpoint
6468 parens N . sitcc $ do
6569 layout <- getLayout
66− sep breakpoint (sitcc . located' (uncurry (p_lie layout))) (attachPositions xs)
70+ sep
71+ breakpoint
72+ (\(p, l) -> sitcc (located l (p_lie layout p)))
73+ (attachRelativePos xs)
6774 newline
6875p_hsmodImport _ (XImportDecl x) = noExtCon x
6976
70−p_lie :: Layout -> (Int, Int) -> IE GhcPs -> R ()
71−p_lie encLayout (i, totalItems) = \case
77+p_lie :: Layout -> RelativePos -> IE GhcPs -> R ()
78+p_lie encLayout relativePos = \case
7279 IEVar NoExtField l1 -> do
7380 located l1 p_ieWrappedName
7481 p_comma
@@ -98,7 +105,11 @@ p_lie encLayout (i, totalItems) = \case
98105 located l1 p_hsmodName
99106 p_comma
100107 IEGroup NoExtField n str -> do
101− unless (i == 0) newline
108+ case relativePos of
109+ SinglePos -> return ()
110+ FirstPos -> return ()
111+ MiddlePos -> newline
112+ LastPos -> newline
102113 p_hsDocString (Asterisk n) False (noLoc str)
103114 IEDoc NoExtField str ->
104115 p_hsDocString Pipe False (noLoc str)
@@ -107,14 +118,10 @@ p_lie encLayout (i, totalItems) = \case
107118 where
108119 p_comma =
109120 case encLayout of
110− SingleLine -> unless (i + 1 == totalItems) comma
121+ SingleLine ->
122+ case relativePos of
123+ SinglePos -> return ()
124+ FirstPos -> comma
125+ MiddlePos -> comma
126+ LastPos -> return ()
111127 MultiLine -> comma
112−
113−-- | Attach positions to 'Located' things in a list.
114−attachPositions ::
115− [Located a] ->
116− [Located ((Int, Int), a)]
117−attachPositions xs =
118− let f i (L l x) = L l ((i, n), x)
119− n = length xs
120− in zipWith f [0 ..] xs