module Language.Haskell.Tools.Rewrite.Create.Modules where
import Data.String (IsString(..), String)
import Language.Haskell.Tools.AST
import Language.Haskell.Tools.PrettyPrint.Prepare
import Language.Haskell.Tools.Rewrite.Create.Names (mkStringNode)
import Language.Haskell.Tools.Rewrite.Create.Utils
import Language.Haskell.Tools.Rewrite.ElementTypes
mkModule :: [FilePragma] -> Maybe ModuleHead -> [ImportDecl] -> [Decl] -> Module
mkModule filePrags head imps decls
= mkAnn (child <> child <> child <> child)
$ UModule (mkAnnList (followedBy "\n" $ separatedBy "\n" list) filePrags) (mkAnnMaybe opt head)
(mkAnnList (after "\n" $ indented list) imps) (mkAnnList (after "\n" $ indented list) decls)
mkModuleHead :: ModuleName -> Maybe ModulePragma -> Maybe ExportSpecs -> ModuleHead
mkModuleHead n pr es = mkAnn ("module " <> child <> child <> child <> " where")
$ UModuleHead n (mkAnnMaybe (after "\n" opt) pr) (mkAnnMaybe opt es)
mkExportSpecs :: [ExportSpec] -> ExportSpecs
mkExportSpecs = mkAnn ("(" <> child <> ")") . UExportSpecs . mkAnnList (separatedBy ", " list)
mkExportSpec :: IESpec -> ExportSpec
mkExportSpec = mkAnn child . UDeclExport
mkModuleExport :: ModuleName -> ExportSpec
mkModuleExport = mkAnn ("module " <> child) . UModuleExport
mkIESpec :: Name -> Maybe SubSpec -> IESpec
mkIESpec name ss = mkAnn (child <> child <> child) (UIESpec noth name (mkAnnMaybe (after "(" $ followedBy ")" opt) ss))
mkPatternIESpec :: Name -> IESpec
mkPatternIESpec name = mkAnn (child <> child) (UIESpec (justVal $ mkAnn child UImportPattern) name noth)
mkSubList :: [Name] -> SubSpec
mkSubList = mkAnn child . USubSpecList . mkAnnList (separatedBy ", " list)
mkSubAll :: SubSpec
mkSubAll = mkAnn ".." USubSpecAll
mkImportDecl :: Bool -> Bool -> Bool -> Maybe String -> ModuleName -> Maybe ModuleName -> Maybe ImportSpec
-> ImportDecl
mkImportDecl source qualified safe pkg name rename spec
= mkAnn ("import " <> child <> child <> child <> child <> child <> child <> child) $
UImportDecl (if source then justVal (mkAnn "{-# SOURCE #-} " UImportSource) else noth)
(if qualified then justVal (mkAnn "qualified " UImportQualified) else noth)
(if safe then justVal (mkAnn "safe " UImportSafe) else noth)
(case pkg of Just str -> justVal (mkStringNode str); _ -> noth)
name (mkAnnMaybe opt (fmap (mkAnn (" as " <> child) . UImportRenaming) rename)) (mkAnnMaybe opt spec)
mkImportSpecList :: [IESpec] -> ImportSpec
mkImportSpecList = mkAnn ("(" <> child <> ")") . UImportSpecList . mkAnnList (separatedBy ", " list)
mkImportHidingList :: [IESpec] -> ImportSpec
mkImportHidingList = mkAnn (" hiding (" <> child <> ")") . UImportSpecHiding . mkAnnList (separatedBy ", " list)
mkModuleName :: String -> ModuleName
mkModuleName s = mkAnn (fromString s) (UModuleName s)
mkLanguagePragma :: [String] -> FilePragma
mkLanguagePragma extensions
= mkAnn ("{-# LANGUAGE " <> child <> " #-}") $ ULanguagePragma
$ mkAnnList (separatedBy ", " list) (map (\ext -> mkAnn (fromString ext) (ULanguageExtension ext)) extensions)
mkOptionsGHC :: String -> FilePragma
mkOptionsGHC opts
= mkAnn ("{-# OPTIONS_GHC " <> child <> " #-}") $ UOptionsPragma
$ mkStringNode opts
mkModuleWarningPragma :: [String] -> ModulePragma
mkModuleWarningPragma msg
= mkAnn ("{-# WARNING " <> child <> " #-}") $ UModuleWarningPragma
$ mkAnnList (separatedBy " " list) $ map mkStringNode msg
mkModuleDeprecatedPragma :: [String] -> ModulePragma
mkModuleDeprecatedPragma msg
= mkAnn ("{-# DEPRECATED " <> child <> " #-}") $ UModuleDeprecatedPragma
$ mkAnnList (separatedBy " " list) $ map mkStringNode msg