defining withContext.

This commit is contained in:
Kazu Yamamoto 2014-04-27 21:26:03 +09:00
parent 64365807f9
commit dede115731
2 changed files with 30 additions and 24 deletions

View File

@ -8,7 +8,7 @@ module Language.Haskell.GhcMod.Gap (
, supportedExtensions , supportedExtensions
, getSrcSpan , getSrcSpan
, getSrcFile , getSrcFile
, setCtx , withContext
, fOptions , fOptions
, toStringBuffer , toStringBuffer
, showSeverityCaption , showSeverityCaption
@ -32,6 +32,7 @@ module Language.Haskell.GhcMod.Gap (
, showDocWith , showDocWith
, GapThing(..) , GapThing(..)
, fromTyThing , fromTyThing
, fileModSummary
) where ) where
import Control.Applicative hiding (empty) import Control.Applicative hiding (empty)
@ -187,34 +188,38 @@ fOptions = [option | (option,_,_) <- fFlags]
---------------------------------------------------------------- ----------------------------------------------------------------
---------------------------------------------------------------- ----------------------------------------------------------------
setCtx :: FilePath -> Ghc ModSummary fileModSummary :: FilePath -> Ghc ModSummary
#if __GLASGOW_HASKELL__ >= 704 fileModSummary file = do
setCtx file = do mss <- getModuleGraph
let [ms] = filter (\m -> ml_hs_file (ms_location m) == Just file) mss
return ms
withContext :: Ghc a -> Ghc a
withContext action = gbracket setup teardown body
where
setup = getContext
teardown = setContext
body _ = do
topImports >>= setContext
action
topImports :: Ghc [InteractiveImport]
topImports = do
mss <- getModuleGraph mss <- getModuleGraph
#if __GLASGOW_HASKELL__ >= 706 #if __GLASGOW_HASKELL__ >= 706
let modName = IIModule . moduleName . ms_mod let modName = IIModule . moduleName . ms_mod
#else #elif __GLASGOW_HASKELL__ >= 704
let modName = IIModule . ms_mod let modName = IIModule . ms_mod
#endif
top <- map modName <$> filterM isTop mss
setContext top
let [ms] = filter (\m -> ml_hs_file (ms_location m) == Just file) mss
return ms
#else #else
setCtx file = do let modName = ms_mod
mss <- getModuleGraph
top <- map ms_mod <$> filterM isTop mss
setContext top []
let [ms] = filter (\m -> ml_hs_file (ms_location m) == Just file) mss
return ms
#endif #endif
map modName <$> filterM isTop mss
where where
isTop mos = lookupMod ||> returnFalse isTop mos = lookupMod ||> returnFalse
where where
lookupMod = lookupModule (ms_mod_name mos) Nothing >> return True lookupMod = lookupModule (ms_mod_name mos) Nothing >> return True
returnFalse = return False returnFalse = return False
showSeverityCaption :: Severity -> String showSeverityCaption :: Severity -> String
#if __GLASGOW_HASKELL__ >= 706 #if __GLASGOW_HASKELL__ >= 706
showSeverityCaption SevWarning = "Warning: " showSeverityCaption SevWarning = "Warning: "

View File

@ -47,7 +47,7 @@ info :: Options
-> Ghc String -> Ghc String
info opt file expr = convert opt <$> ghandle handler body info opt file expr = convert opt <$> ghandle handler body
where where
body = inModuleContext file $ \_ dflag style -> do body = inModuleContext file $ \dflag style -> do
sdoc <- Gap.infoThing expr sdoc <- Gap.infoThing expr
return $ showPage dflag style sdoc return $ showPage dflag style sdoc
handler (SomeException _) = return "Cannot show info" handler (SomeException _) = return "Cannot show info"
@ -84,7 +84,8 @@ types :: Options
-> Ghc String -> Ghc String
types opt file lineNo colNo = convert opt <$> ghandle handler body types opt file lineNo colNo = convert opt <$> ghandle handler body
where where
body = inModuleContext file $ \modSum dflag style -> do body = inModuleContext file $ \dflag style -> do
modSum <- Gap.fileModSummary file
srcSpanTypes <- getSrcSpanType modSum lineNo colNo srcSpanTypes <- getSrcSpanType modSum lineNo colNo
return $ map (toTup dflag style) $ sortBy (cmp `on` fst) srcSpanTypes return $ map (toTup dflag style) $ sortBy (cmp `on` fst) srcSpanTypes
handler (SomeException _) = return [] handler (SomeException _) = return []
@ -126,10 +127,10 @@ pretty dflag style = showOneLine dflag style . Gap.typeForUser
---------------------------------------------------------------- ----------------------------------------------------------------
inModuleContext :: FilePath -> (G.ModSummary -> DynFlags -> PprStyle -> Ghc a) -> Ghc a inModuleContext :: FilePath -> (DynFlags -> PprStyle -> Ghc a) -> Ghc a
inModuleContext file action = withDynFlags setDeferTypeErrors $ do inModuleContext file action = withDynFlags setDeferTypeErrors $ do
setTargetFiles [file] setTargetFiles [file]
modSum <- Gap.setCtx file Gap.withContext $ do
dflag <- G.getSessionDynFlags dflag <- G.getSessionDynFlags
style <- getStyle style <- getStyle
action modSum dflag style action dflag style