2017-02-28 02:01:14 +00:00
|
|
|
module GhcModExe.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 Data.Function (on)
|
2014-06-21 09:38:44 +00:00
|
|
|
import Data.List (sortBy)
|
2015-03-03 20:12:43 +00:00
|
|
|
import System.FilePath
|
2014-04-11 02:51:25 +00:00
|
|
|
import Exception (ghandle, SomeException(..))
|
2016-01-19 19:50:14 +00:00
|
|
|
import GHC (GhcMonad, SrcSpan)
|
2015-08-03 01:09:56 +00:00
|
|
|
import Prelude
|
2014-03-27 06:56:14 +00:00
|
|
|
import qualified GHC as G
|
2015-03-03 20:12:43 +00:00
|
|
|
|
2017-01-12 15:36:47 +00:00
|
|
|
import qualified Language.Haskell.GhcMod.Gap as Gap
|
2015-03-03 20:12:43 +00:00
|
|
|
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
|
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
|
2015-07-04 14:49:48 +00:00
|
|
|
import Language.Haskell.GhcMod.Utils (mkRevRedirMapFunc)
|
2015-07-03 19:31:52 +00:00
|
|
|
import Language.Haskell.GhcMod.FileMapping (fileModSummaryWithMapping)
|
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
|
2015-03-09 21:04:04 +00:00
|
|
|
info file expr =
|
2015-06-01 14:54:50 +00:00
|
|
|
ghandle handler $
|
|
|
|
runGmlT' [Left file] deferErrors $
|
2015-09-01 08:27:12 +00:00
|
|
|
withInteractiveContext $ do
|
|
|
|
convert' =<< body
|
2014-04-23 07:37:24 +00:00
|
|
|
where
|
2015-03-03 20:12:43 +00:00
|
|
|
handler (SomeException ex) = do
|
2016-12-15 18:16:37 +00:00
|
|
|
gmLog GmException "info" $ text "" $$ nest 4 (showToDoc ex)
|
2015-06-01 14:54:50 +00:00
|
|
|
convert' "Cannot show info"
|
2015-03-03 20:12:43 +00:00
|
|
|
|
2015-07-04 14:49:48 +00:00
|
|
|
body :: (GhcMonad m, GmState m, GmEnv m) => m String
|
2015-03-03 20:12:43 +00:00
|
|
|
body = do
|
2015-07-04 14:49:48 +00:00
|
|
|
m <- mkRevRedirMapFunc
|
|
|
|
sdoc <- Gap.infoThing m expr
|
2015-06-01 14:54:50 +00:00
|
|
|
st <- getStyle
|
|
|
|
dflag <- G.getSessionDynFlags
|
|
|
|
return $ showPage dflag st sdoc
|
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
|
2016-01-19 22:29:33 +00:00
|
|
|
=> Bool -- ^ Include constraints into type signature
|
|
|
|
-> 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
|
2016-01-19 22:29:33 +00:00
|
|
|
types withConstraints file lineNo colNo =
|
2015-06-01 14:54:50 +00:00
|
|
|
ghandle handler $
|
|
|
|
runGmlT' [Left file] deferErrors $
|
2015-08-21 02:12:53 +00:00
|
|
|
withInteractiveContext $ do
|
2015-06-01 14:54:50 +00:00
|
|
|
crdl <- cradle
|
2015-07-03 19:31:52 +00:00
|
|
|
modSum <- fileModSummaryWithMapping (cradleCurrentDir crdl </> file)
|
2016-01-19 22:29:33 +00:00
|
|
|
srcSpanTypes <- getSrcSpanType withConstraints modSum lineNo colNo
|
2015-06-01 14:54:50 +00:00
|
|
|
dflag <- G.getSessionDynFlags
|
|
|
|
st <- getStyle
|
2015-03-03 20:12:43 +00:00
|
|
|
convert' $ map (toTup dflag st) $ sortBy (cmp `on` fst) srcSpanTypes
|
|
|
|
where
|
2015-05-06 14:13:08 +00:00
|
|
|
handler (SomeException ex) = do
|
2016-12-15 18:16:37 +00:00
|
|
|
gmLog GmException "types" $ showToDoc ex
|
2015-05-06 14:13:08 +00:00
|
|
|
return []
|
2012-02-15 05:52:48 +00:00
|
|
|
|
2016-01-19 22:29:33 +00:00
|
|
|
getSrcSpanType :: (GhcMonad m) => Bool -> G.ModSummary -> Int -> Int -> m [(SrcSpan, G.Type)]
|
|
|
|
getSrcSpanType withConstraints modSum lineNo colNo =
|
2016-01-19 19:50:14 +00:00
|
|
|
G.parseModule modSum
|
|
|
|
>>= G.typecheckModule
|
2016-01-19 22:29:33 +00:00
|
|
|
>>= flip (collectSpansTypes withConstraints) (lineNo, colNo)
|