summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJasper Van der Jeugt <m@jaspervdj.be>2020-10-07 20:37:43 +0200
committerJasper Van der Jeugt <m@jaspervdj.be>2020-10-07 20:38:03 +0200
commit1bc2b2c5c3377ed7fe55d53175580eccebb631aa (patch)
treee26746a8100940cd0399de47489b21e5bf8de51f
parent1f7f23db8ab5c048b837140d9633a82b1e642bae (diff)
downloadstylish-haskell-1bc2b2c5c3377ed7fe55d53175580eccebb631aa.tar.gz
ModuleHeader: reuse printImport from Imports
-rw-r--r--lib/Language/Haskell/Stylish/Step/Imports.hs13
-rw-r--r--lib/Language/Haskell/Stylish/Step/ModuleHeader.hs72
2 files changed, 31 insertions, 54 deletions
diff --git a/lib/Language/Haskell/Stylish/Step/Imports.hs b/lib/Language/Haskell/Stylish/Step/Imports.hs
index f2439dc..b89d73f 100644
--- a/lib/Language/Haskell/Stylish/Step/Imports.hs
+++ b/lib/Language/Haskell/Stylish/Step/Imports.hs
@@ -11,6 +11,8 @@ module Language.Haskell.Stylish.Step.Imports
, EmptyListAlign (..)
, ListPadding (..)
, step
+
+ , printImport
) where
--------------------------------------------------------------------------------
@@ -213,7 +215,7 @@ printQualified Options{..} padNames stats (L _ decl) = do
_ -> space >> putText "()"
Just (L _ imports) -> do
let printedImports = flagEnds $ -- [P ()]
- fmap ((printImport Options{..}) . unLocated)
+ fmap ((printImport separateLists) . unLocated)
(prepareImportList imports)
-- Since we might need to output the import module name several times, we
@@ -308,18 +310,20 @@ printQualified Options{..} padNames stats (L _ decl) = do
--------------------------------------------------------------------------------
-printImport :: Options -> IE GhcPs -> P ()
+printImport :: Bool -> IE GhcPs -> P ()
printImport _ (IEVar _ name) = do
printIeWrappedName name
printImport _ (IEThingAbs _ name) = do
printIeWrappedName name
-printImport Options{..} (IEThingAll _ name) = do
+printImport separateLists (IEThingAll _ name) = do
printIeWrappedName name
when separateLists space
putText "(..)"
printImport _ (IEModuleContents _ (L _ m)) = do
+ putText "module"
+ space
putText (moduleNameString m)
-printImport Options{..} (IEThingWith _ name _wildcard imps _) = do
+printImport separateLists (IEThingWith _ name _wildcard imps _) = do
printIeWrappedName name
when separateLists space
parenthesize $
@@ -333,6 +337,7 @@ printImport _ (IEDocNamed _ _) =
printImport _ (XIE ext) =
GHC.noExtCon ext
+
--------------------------------------------------------------------------------
printIeWrappedName :: LIEWrappedName RdrName -> P ()
printIeWrappedName lie = unLocated lie & \case
diff --git a/lib/Language/Haskell/Stylish/Step/ModuleHeader.hs b/lib/Language/Haskell/Stylish/Step/ModuleHeader.hs
index 0c33298..728ce4a 100644
--- a/lib/Language/Haskell/Stylish/Step/ModuleHeader.hs
+++ b/lib/Language/Haskell/Stylish/Step/ModuleHeader.hs
@@ -7,27 +7,26 @@ module Language.Haskell.Stylish.Step.ModuleHeader
) where
--------------------------------------------------------------------------------
-import ApiAnnotation (AnnKeywordId (..),
- AnnotationComment (..))
-import Control.Monad (forM_, join, when)
-import Data.Bifunctor (second)
-import Data.Foldable (find, toList)
-import Data.Function (on, (&))
-import qualified Data.List as L
-import Data.List.NonEmpty (NonEmpty (..))
-import qualified Data.List.NonEmpty as NonEmpty
-import Data.Maybe (isJust, listToMaybe)
-import qualified GHC.Hs.Doc as GHC
-import GHC.Hs.Extension (GhcPs)
-import qualified GHC.Hs.Extension as GHC
-import GHC.Hs.ImpExp (IE (..))
-import qualified GHC.Hs.ImpExp as GHC
-import qualified Module as GHC
-import SrcLoc (GenLocated (..), Located,
- RealLocated, SrcSpan (..),
- srcSpanEndLine,
- srcSpanStartLine, unLoc)
-import Util (notNull)
+import ApiAnnotation (AnnKeywordId (..),
+ AnnotationComment (..))
+import Control.Monad (forM_, join, when)
+import Data.Bifunctor (second)
+import Data.Foldable (find, toList)
+import Data.Function ((&))
+import qualified Data.List as L
+import Data.List.NonEmpty (NonEmpty (..))
+import qualified Data.List.NonEmpty as NonEmpty
+import Data.Maybe (isJust, listToMaybe)
+import qualified GHC.Hs.Doc as GHC
+import GHC.Hs.Extension (GhcPs)
+import qualified GHC.Hs.ImpExp as GHC
+import qualified Module as GHC
+import SrcLoc (GenLocated (..),
+ Located, RealLocated,
+ SrcSpan (..),
+ srcSpanEndLine,
+ srcSpanStartLine, unLoc)
+import Util (notNull)
--------------------------------------------------------------------------------
import Language.Haskell.Stylish.Block
@@ -37,6 +36,7 @@ import Language.Haskell.Stylish.Module
import Language.Haskell.Stylish.Ordering
import Language.Haskell.Stylish.Printer
import Language.Haskell.Stylish.Step
+import qualified Language.Haskell.Stylish.Step.Imports as Imports
data Config = Config
@@ -221,32 +221,4 @@ printExportList conf (L srcLoc exports) = do
-- NOTE(jaspervdj): This code is almost the same as the import printing
-- in 'Imports' and should be merged.
printExport :: GHC.LIE GhcPs -> P ()
- printExport (L _ export) = case export of
- IEVar _ name -> putOutputable name
- IEThingAbs _ name -> putOutputable name
- IEThingAll _ name -> do
- putOutputable name
- when (separateLists conf) space
- putText "(..)"
- IEModuleContents _ (L _ m) -> do
- putText "module"
- space
- putText (showOutputable m)
- IEThingWith _ name _wildcard imps _ -> do
- putOutputable name
- when (separateLists conf) space
- putText "("
- sep (comma >> space) $
- fmap putOutputable $ L.sortBy (compareWrappedName `on` unLoc) imps
- putText ")"
- IEGroup _ _ _ ->
- error $
- "Language.Haskell.Stylish.Printer.Imports.printImportExport: unhandled case 'IEGroup'" <> showOutputable export
- IEDoc _ _ ->
- error $
- "Language.Haskell.Stylish.Printer.Imports.printImportExport: unhandled case 'IEDoc'" <> showOutputable export
- IEDocNamed _ _ ->
- error $
- "Language.Haskell.Stylish.Printer.Imports.printImportExport: unhandled case 'IEDocNamed'" <> showOutputable export
- XIE ext ->
- GHC.noExtCon ext
+ printExport = Imports.printImport (separateLists conf) . unLoc