Files
ghc-mod/GhcMod/Info.hs

80 lines
2.6 KiB
Haskell
Raw Normal View History

module GhcMod.Info (
info
2014-04-21 14:04:58 +09:00
, types
2013-05-20 14:28:56 +09:00
) where
2010-11-12 16:27:50 +09:00
2014-01-08 12:03:32 +09:00
import Data.Function (on)
import Data.List (sortBy)
import System.FilePath
import Exception (ghandle, SomeException(..))
import GHC (GhcMonad, SrcSpan)
2015-08-03 03:09:56 +02:00
import Prelude
2014-03-27 15:56:14 +09:00
import qualified GHC as G
import qualified Language.Haskell.GhcMod.Gap as Gap
import Language.Haskell.GhcMod.Convert
import Language.Haskell.GhcMod.Doc
import Language.Haskell.GhcMod.DynFlags
import Language.Haskell.GhcMod.Gap
import Language.Haskell.GhcMod.Logging
import Language.Haskell.GhcMod.Monad
import Language.Haskell.GhcMod.SrcUtils
2013-05-17 10:00:01 +09:00
import Language.Haskell.GhcMod.Types
2015-07-04 17:49:48 +03:00
import Language.Haskell.GhcMod.Utils (mkRevRedirMapFunc)
2015-07-03 22:31:52 +03:00
import Language.Haskell.GhcMod.FileMapping (fileModSummaryWithMapping)
2012-02-14 16:09:53 +09:00
----------------------------------------------------------------
2011-08-24 15:58:12 +09:00
2013-05-20 14:28:56 +09:00
-- | Obtaining information of a target expression. (GHCi's info:)
info :: IOish m
=> FilePath -- ^ A target file.
2013-09-05 14:35:28 +09:00
-> Expression -- ^ A Haskell expression.
-> GhcModT m String
info file expr =
2015-06-01 17:54:50 +03:00
ghandle handler $
runGmlT' [Left file] deferErrors $
withInteractiveContext $ do
convert' =<< body
2014-04-23 16:37:24 +09:00
where
handler (SomeException ex) = do
2016-12-15 19:16:37 +01:00
gmLog GmException "info" $ text "" $$ nest 4 (showToDoc ex)
2015-06-01 17:54:50 +03:00
convert' "Cannot show info"
2015-07-04 17:49:48 +03:00
body :: (GhcMonad m, GmState m, GmEnv m) => m String
body = do
2015-07-04 17:49:48 +03:00
m <- mkRevRedirMapFunc
sdoc <- Gap.infoThing m expr
2015-06-01 17:54:50 +03:00
st <- getStyle
dflag <- G.getSessionDynFlags
return $ showPage dflag st sdoc
2011-01-14 11:18:33 +09:00
2012-02-12 21:04:18 +09:00
----------------------------------------------------------------
2013-05-20 14:28:56 +09:00
-- | Obtaining type of a target expression. (GHCi's type:)
types :: IOish m
=> Bool -- ^ Include constraints into type signature
-> FilePath -- ^ A target file.
2014-04-21 14:04:58 +09:00
-> Int -- ^ Line number.
-> Int -- ^ Column number.
-> GhcModT m String
types withConstraints file lineNo colNo =
2015-06-01 17:54:50 +03:00
ghandle handler $
runGmlT' [Left file] deferErrors $
withInteractiveContext $ do
2015-06-01 17:54:50 +03:00
crdl <- cradle
2015-07-03 22:31:52 +03:00
modSum <- fileModSummaryWithMapping (cradleCurrentDir crdl </> file)
srcSpanTypes <- getSrcSpanType withConstraints modSum lineNo colNo
2015-06-01 17:54:50 +03:00
dflag <- G.getSessionDynFlags
st <- getStyle
convert' $ map (toTup dflag st) $ sortBy (cmp `on` fst) srcSpanTypes
where
2015-05-06 16:13:08 +02:00
handler (SomeException ex) = do
2016-12-15 19:16:37 +01:00
gmLog GmException "types" $ showToDoc ex
2015-05-06 16:13:08 +02:00
return []
2012-02-15 14:52:48 +09:00
getSrcSpanType :: (GhcMonad m) => Bool -> G.ModSummary -> Int -> Int -> m [(SrcSpan, G.Type)]
getSrcSpanType withConstraints modSum lineNo colNo =
G.parseModule modSum
>>= G.typecheckModule
>>= flip (collectSpansTypes withConstraints) (lineNo, colNo)