Skip to content

Commit 1bc2b2c

Browse files
committed
ModuleHeader: reuse printImport from Imports
1 parent 1f7f23d commit 1bc2b2c

File tree

2 files changed

+31
-54
lines changed

2 files changed

+31
-54
lines changed

lib/Language/Haskell/Stylish/Step/Imports.hs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ module Language.Haskell.Stylish.Step.Imports
1111
, EmptyListAlign (..)
1212
, ListPadding (..)
1313
, step
14+
15+
, printImport
1416
) where
1517

1618
--------------------------------------------------------------------------------
@@ -213,7 +215,7 @@ printQualified Options{..} padNames stats (L _ decl) = do
213215
_ -> space >> putText "()"
214216
Just (L _ imports) -> do
215217
let printedImports = flagEnds $ -- [P ()]
216-
fmap ((printImport Options{..}) . unLocated)
218+
fmap ((printImport separateLists) . unLocated)
217219
(prepareImportList imports)
218220

219221
-- Since we might need to output the import module name several times, we
@@ -308,18 +310,20 @@ printQualified Options{..} padNames stats (L _ decl) = do
308310

309311

310312
--------------------------------------------------------------------------------
311-
printImport :: Options -> IE GhcPs -> P ()
313+
printImport :: Bool -> IE GhcPs -> P ()
312314
printImport _ (IEVar _ name) = do
313315
printIeWrappedName name
314316
printImport _ (IEThingAbs _ name) = do
315317
printIeWrappedName name
316-
printImport Options{..} (IEThingAll _ name) = do
318+
printImport separateLists (IEThingAll _ name) = do
317319
printIeWrappedName name
318320
when separateLists space
319321
putText "(..)"
320322
printImport _ (IEModuleContents _ (L _ m)) = do
323+
putText "module"
324+
space
321325
putText (moduleNameString m)
322-
printImport Options{..} (IEThingWith _ name _wildcard imps _) = do
326+
printImport separateLists (IEThingWith _ name _wildcard imps _) = do
323327
printIeWrappedName name
324328
when separateLists space
325329
parenthesize $
@@ -333,6 +337,7 @@ printImport _ (IEDocNamed _ _) =
333337
printImport _ (XIE ext) =
334338
GHC.noExtCon ext
335339

340+
336341
--------------------------------------------------------------------------------
337342
printIeWrappedName :: LIEWrappedName RdrName -> P ()
338343
printIeWrappedName lie = unLocated lie & \case

lib/Language/Haskell/Stylish/Step/ModuleHeader.hs

Lines changed: 22 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,26 @@ module Language.Haskell.Stylish.Step.ModuleHeader
77
) where
88

99
--------------------------------------------------------------------------------
10-
import ApiAnnotation (AnnKeywordId (..),
11-
AnnotationComment (..))
12-
import Control.Monad (forM_, join, when)
13-
import Data.Bifunctor (second)
14-
import Data.Foldable (find, toList)
15-
import Data.Function (on, (&))
16-
import qualified Data.List as L
17-
import Data.List.NonEmpty (NonEmpty (..))
18-
import qualified Data.List.NonEmpty as NonEmpty
19-
import Data.Maybe (isJust, listToMaybe)
20-
import qualified GHC.Hs.Doc as GHC
21-
import GHC.Hs.Extension (GhcPs)
22-
import qualified GHC.Hs.Extension as GHC
23-
import GHC.Hs.ImpExp (IE (..))
24-
import qualified GHC.Hs.ImpExp as GHC
25-
import qualified Module as GHC
26-
import SrcLoc (GenLocated (..), Located,
27-
RealLocated, SrcSpan (..),
28-
srcSpanEndLine,
29-
srcSpanStartLine, unLoc)
30-
import Util (notNull)
10+
import ApiAnnotation (AnnKeywordId (..),
11+
AnnotationComment (..))
12+
import Control.Monad (forM_, join, when)
13+
import Data.Bifunctor (second)
14+
import Data.Foldable (find, toList)
15+
import Data.Function ((&))
16+
import qualified Data.List as L
17+
import Data.List.NonEmpty (NonEmpty (..))
18+
import qualified Data.List.NonEmpty as NonEmpty
19+
import Data.Maybe (isJust, listToMaybe)
20+
import qualified GHC.Hs.Doc as GHC
21+
import GHC.Hs.Extension (GhcPs)
22+
import qualified GHC.Hs.ImpExp as GHC
23+
import qualified Module as GHC
24+
import SrcLoc (GenLocated (..),
25+
Located, RealLocated,
26+
SrcSpan (..),
27+
srcSpanEndLine,
28+
srcSpanStartLine, unLoc)
29+
import Util (notNull)
3130

3231
--------------------------------------------------------------------------------
3332
import Language.Haskell.Stylish.Block
@@ -37,6 +36,7 @@ import Language.Haskell.Stylish.Module
3736
import Language.Haskell.Stylish.Ordering
3837
import Language.Haskell.Stylish.Printer
3938
import Language.Haskell.Stylish.Step
39+
import qualified Language.Haskell.Stylish.Step.Imports as Imports
4040

4141

4242
data Config = Config
@@ -221,32 +221,4 @@ printExportList conf (L srcLoc exports) = do
221221
-- NOTE(jaspervdj): This code is almost the same as the import printing
222222
-- in 'Imports' and should be merged.
223223
printExport :: GHC.LIE GhcPs -> P ()
224-
printExport (L _ export) = case export of
225-
IEVar _ name -> putOutputable name
226-
IEThingAbs _ name -> putOutputable name
227-
IEThingAll _ name -> do
228-
putOutputable name
229-
when (separateLists conf) space
230-
putText "(..)"
231-
IEModuleContents _ (L _ m) -> do
232-
putText "module"
233-
space
234-
putText (showOutputable m)
235-
IEThingWith _ name _wildcard imps _ -> do
236-
putOutputable name
237-
when (separateLists conf) space
238-
putText "("
239-
sep (comma >> space) $
240-
fmap putOutputable $ L.sortBy (compareWrappedName `on` unLoc) imps
241-
putText ")"
242-
IEGroup _ _ _ ->
243-
error $
244-
"Language.Haskell.Stylish.Printer.Imports.printImportExport: unhandled case 'IEGroup'" <> showOutputable export
245-
IEDoc _ _ ->
246-
error $
247-
"Language.Haskell.Stylish.Printer.Imports.printImportExport: unhandled case 'IEDoc'" <> showOutputable export
248-
IEDocNamed _ _ ->
249-
error $
250-
"Language.Haskell.Stylish.Printer.Imports.printImportExport: unhandled case 'IEDocNamed'" <> showOutputable export
251-
XIE ext ->
252-
GHC.noExtCon ext
224+
printExport = Imports.printImport (separateLists conf) . unLoc

0 commit comments

Comments
 (0)