Try loading a module if findModule failed for it

This commit is contained in:
Sergey Khorev 2013-11-20 08:57:45 +04:00
parent dfecb22123
commit 429cdfa83b
2 changed files with 27 additions and 1 deletions

View File

@ -7,6 +7,7 @@ import Data.List
import Data.Maybe (fromMaybe) import Data.Maybe (fromMaybe)
import DataCon (dataConRepType) import DataCon (dataConRepType)
import GHC import GHC
import Panic(throwGhcException)
import Language.Haskell.GhcMod.Doc (showUnqualifiedPage) import Language.Haskell.GhcMod.Doc (showUnqualifiedPage)
import Language.Haskell.GhcMod.GHCApi import Language.Haskell.GhcMod.GHCApi
import Language.Haskell.GhcMod.Types import Language.Haskell.GhcMod.Types
@ -50,11 +51,26 @@ browse opt cradle mdlName = do
void $ initializeFlagsWithCradle opt cradle [] False void $ initializeFlagsWithCradle opt cradle [] False
getModule >>= getModuleInfo >>= listExports getModule >>= getModuleInfo >>= listExports
where where
getModule = findModule (mkModuleName mdlName) Nothing getModule =
findModule (mkModuleName mdlName) Nothing
`gcatch` (\e ->
case e of
-- findModule works only for package modules, moreover,
-- you cannot load a package module. On the other hand,
-- to browse a local module you need to load it first.
-- If CmdLineError is signalled, we assume the user
-- tried browsing a local module.
CmdLineError _ -> loadAndFind
_ -> throwGhcException e)
listExports Nothing = return [] listExports Nothing = return []
listExports (Just mdinfo) listExports (Just mdinfo)
| detailed opt = processModule mdinfo | detailed opt = processModule mdinfo
| otherwise = return (processExports mdinfo) | otherwise = return (processExports mdinfo)
loadAndFind = do
setTargetFiles [mdlName]
checkSlowAndSet
void $ load LoadAllTargets
findModule (mkModuleName mdlName) Nothing
processExports :: ModuleInfo -> [String] processExports :: ModuleInfo -> [String]
processExports = map getOccString . modInfoExports processExports = map getOccString . modInfoExports

View File

@ -2,8 +2,11 @@ module BrowseSpec where
import Control.Applicative import Control.Applicative
import Language.Haskell.GhcMod import Language.Haskell.GhcMod
import Language.Haskell.GhcMod.Cradle
import Test.Hspec import Test.Hspec
import Dir
spec :: Spec spec :: Spec
spec = do spec = do
describe "browseModule" $ do describe "browseModule" $ do
@ -22,3 +25,10 @@ spec = do
cradle <- findCradle cradle <- findCradle
syms <- lines <$> browseModule defaultOptions { detailed = True} cradle "Data.Either" syms <- lines <$> browseModule defaultOptions { detailed = True} cradle "Data.Either"
syms `shouldContain` ["Left :: a -> Either a b"] syms `shouldContain` ["Left :: a -> Either a b"]
describe "browseModule local" $ do
it "lists symbols in a local module" $ do
withDirectory_ "test/data" $ do
cradle <- findCradleWithoutSandbox
syms <- lines <$> browseModule defaultOptions cradle "Baz"
syms `shouldContain` ["baz"]