<package>:<module> style.

refactoring for #159.
This commit is contained in:
Kazu Yamamoto 2014-04-24 11:26:30 +09:00
parent 2e06c2f938
commit 0c1469e53c
4 changed files with 20 additions and 14 deletions

View File

@ -31,9 +31,9 @@ browseModule :: Options
-> Cradle
-> ModuleString -- ^ A module name. (e.g. \"Data.List\")
-> IO String
browseModule opt cradle mdlName = withGHC' $ do
browseModule opt cradle pkgmdl = withGHC' $ do
void $ initializeFlagsWithCradle opt cradle [] False
browse opt mdlName
browse opt pkgmdl
-- | Getting functions, classes, etc from a module.
-- If 'detailed' is 'True', their types are also obtained.
@ -41,12 +41,13 @@ browseModule opt cradle mdlName = withGHC' $ do
browse :: Options
-> ModuleString -- ^ A module name. (e.g. \"Data.List\")
-> Ghc String
browse opt mdlName = do
browse opt pkgmdl = do
convert opt . sort <$> (getModule >>= G.getModuleInfo >>= listExports)
where
(mpkg,mdl) = splitPkgMdl pkgmdl
mdlname = G.mkModuleName mdl
mpkgid = mkFastString <$> mpkg
getModule = G.findModule mdlname mpkgid `G.gcatch` fallback
mdlname = G.mkModuleName mdlName
mpkgid = mkFastString <$> packageId opt
listExports Nothing = return []
listExports (Just mdinfo) = processExports opt mdinfo
-- findModule works only for package modules, moreover,
@ -57,10 +58,21 @@ browse opt mdlName = do
fallback (CmdLineError _) = loadAndFind
fallback e = throwGhcException e
loadAndFind = do
setTargetFiles [mdlName]
setTargetFiles [mdl]
void $ G.load G.LoadAllTargets
G.findModule mdlname Nothing
-- |
--
-- >>> splitPkgMdl "base:Prelude"
-- (Just "base","Prelude")
-- >>> splitPkgMdl "Prelude"
-- (Nothing,"Prelude")
splitPkgMdl :: String -> (Maybe String,String)
splitPkgMdl pkgmdl = case break (==':') pkgmdl of
(mdl,"") -> (Nothing,mdl)
(pkg,_:mdl) -> (Just pkg,mdl)
processExports :: Options -> ModuleInfo -> Ghc [String]
processExports opt minfo = mapM (showExport opt minfo) $ removeOps $ G.modInfoExports minfo
where

View File

@ -25,8 +25,6 @@ data Options = Options {
, expandSplice :: Bool
-- | Line separator string.
, lineSeparator :: LineSeparator
-- | Package id of module
, packageId :: Maybe String
}
-- | A default 'Options'.
@ -40,7 +38,6 @@ defaultOptions = Options {
, qualified = False
, expandSplice = False
, lineSeparator = LineSeparator "\0"
, packageId = Nothing
}
----------------------------------------------------------------

View File

@ -29,7 +29,7 @@ usage = "ghc-mod version " ++ showVersion version ++ "\n"
++ "\t ghc-mod list" ++ ghcOptHelp ++ "[-l] [-d]\n"
++ "\t ghc-mod lang [-l]\n"
++ "\t ghc-mod flag [-l]\n"
++ "\t ghc-mod browse" ++ ghcOptHelp ++ "[-l] [-o] [-d] [-q] [-p package] <module> [<module> ...]\n"
++ "\t ghc-mod browse" ++ ghcOptHelp ++ "[-l] [-o] [-d] [-q] [<package>:]<module> [[<package>:]<module> ...]\n"
++ "\t ghc-mod check" ++ ghcOptHelp ++ "<HaskellFiles...>\n"
++ "\t ghc-mod expand" ++ ghcOptHelp ++ "<HaskellFiles...>\n"
++ "\t ghc-mod debug" ++ ghcOptHelp ++ "\n"
@ -65,9 +65,6 @@ argspec = [ Option "l" ["tolisp"]
, Option "q" ["qualified"]
(NoArg (\opts -> opts { qualified = True }))
"show qualified names"
, Option "p" ["package"]
(ReqArg (\p opts -> opts { packageId = Just p, ghcOpts = ("-package " ++ p) : ghcOpts opts }) "package-id")
"specify package of module"
, Option "b" ["boundary"]
(ReqArg (\s opts -> opts { lineSeparator = LineSeparator s }) "sep")
"specify line separator (default is Nul string)"

View File

@ -10,7 +10,7 @@
-- the format of hlint options is [String] because they may contain
-- spaces and also <file> may contain spaces.
-- boot
-- browse <module>
-- browse [<package>:]<module>
--
-- Session separators:
-- OK -- success