2013-05-20 05:28:56 +00:00
|
|
|
module Language.Haskell.GhcMod.Info (
|
2014-07-11 01:10:37 +00:00
|
|
|
info
|
2014-04-21 05:04:58 +00:00
|
|
|
, types
|
2013-05-20 05:28:56 +00:00
|
|
|
) where
|
2010-11-12 07:27:50 +00:00
|
|
|
|
2014-01-08 03:03:32 +00:00
|
|
|
import Control.Applicative ((<$>))
|
|
|
|
import Data.Function (on)
|
2014-06-21 09:38:44 +00:00
|
|
|
import Data.List (sortBy)
|
|
|
|
import Data.Maybe (catMaybes)
|
2014-04-11 02:51:25 +00:00
|
|
|
import Exception (ghandle, SomeException(..))
|
2014-06-28 19:43:51 +00:00
|
|
|
import GHC (GhcMonad, LHsBind, LHsExpr, LPat, Id, TypecheckedModule(..), SrcSpan, Type)
|
2014-03-27 06:56:14 +00:00
|
|
|
import qualified GHC as G
|
2014-06-21 09:38:44 +00:00
|
|
|
import Language.Haskell.GhcMod.Doc (showPage)
|
|
|
|
import Language.Haskell.GhcMod.Gap (HasType(..))
|
2014-03-27 06:56:14 +00:00
|
|
|
import qualified Language.Haskell.GhcMod.Gap as Gap
|
2014-06-28 19:43:51 +00:00
|
|
|
import Language.Haskell.GhcMod.Monad
|
2014-06-21 09:38:44 +00:00
|
|
|
import Language.Haskell.GhcMod.SrcUtils
|
2013-05-17 01:00:01 +00:00
|
|
|
import Language.Haskell.GhcMod.Types
|
2014-05-11 22:40:00 +00:00
|
|
|
import Language.Haskell.GhcMod.Convert
|
2010-11-17 08:07:33 +00:00
|
|
|
|
2012-02-14 07:09:53 +00:00
|
|
|
----------------------------------------------------------------
|
2011-08-24 06:58:12 +00:00
|
|
|
|
2013-05-20 05:28:56 +00:00
|
|
|
-- | Obtaining information of a target expression. (GHCi's info:)
|
2014-07-12 09:16:16 +00:00
|
|
|
info :: IOish m
|
|
|
|
=> FilePath -- ^ A target file.
|
2013-09-05 05:35:28 +00:00
|
|
|
-> Expression -- ^ A Haskell expression.
|
2014-07-12 09:16:16 +00:00
|
|
|
-> GhcModT m String
|
2014-06-28 19:43:51 +00:00
|
|
|
info file expr = do
|
|
|
|
opt <- options
|
|
|
|
convert opt <$> ghandle handler body
|
2014-04-23 07:37:24 +00:00
|
|
|
where
|
2014-04-27 12:26:03 +00:00
|
|
|
body = inModuleContext file $ \dflag style -> do
|
2014-04-23 07:37:24 +00:00
|
|
|
sdoc <- Gap.infoThing expr
|
|
|
|
return $ showPage dflag style sdoc
|
2014-04-26 02:43:30 +00:00
|
|
|
handler (SomeException _) = return "Cannot show info"
|
2011-01-14 02:18:33 +00:00
|
|
|
|
2012-02-12 12:04:18 +00:00
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2013-05-20 05:28:56 +00:00
|
|
|
-- | Obtaining type of a target expression. (GHCi's type:)
|
2014-07-12 09:16:16 +00:00
|
|
|
types :: IOish m
|
|
|
|
=> FilePath -- ^ A target file.
|
2014-04-21 05:04:58 +00:00
|
|
|
-> Int -- ^ Line number.
|
|
|
|
-> Int -- ^ Column number.
|
2014-07-12 09:16:16 +00:00
|
|
|
-> GhcModT m String
|
2014-06-28 19:43:51 +00:00
|
|
|
types file lineNo colNo = do
|
|
|
|
opt <- options
|
|
|
|
convert opt <$> ghandle handler body
|
2014-04-23 07:37:24 +00:00
|
|
|
where
|
2014-04-27 12:26:03 +00:00
|
|
|
body = inModuleContext file $ \dflag style -> do
|
|
|
|
modSum <- Gap.fileModSummary file
|
2014-04-23 07:37:24 +00:00
|
|
|
srcSpanTypes <- getSrcSpanType modSum lineNo colNo
|
2014-04-26 14:03:50 +00:00
|
|
|
return $ map (toTup dflag style) $ sortBy (cmp `on` fst) srcSpanTypes
|
2014-04-26 02:43:30 +00:00
|
|
|
handler (SomeException _) = return []
|
2012-02-15 05:52:48 +00:00
|
|
|
|
2014-06-28 19:43:51 +00:00
|
|
|
getSrcSpanType :: GhcMonad m => G.ModSummary -> Int -> Int -> m [(SrcSpan, Type)]
|
2014-04-11 03:19:42 +00:00
|
|
|
getSrcSpanType modSum lineNo colNo = do
|
|
|
|
p <- G.parseModule modSum
|
|
|
|
tcm@TypecheckedModule{tm_typechecked_source = tcs} <- G.typecheckModule p
|
|
|
|
let bs = listifySpans tcs (lineNo, colNo) :: [LHsBind Id]
|
|
|
|
es = listifySpans tcs (lineNo, colNo) :: [LHsExpr Id]
|
|
|
|
ps = listifySpans tcs (lineNo, colNo) :: [LPat Id]
|
|
|
|
bts <- mapM (getType tcm) bs
|
|
|
|
ets <- mapM (getType tcm) es
|
|
|
|
pts <- mapM (getType tcm) ps
|
|
|
|
return $ catMaybes $ concat [ets, bts, pts]
|