|
| 1 | +{-# LANGUAGE CPP #-} |
| 2 | +{-# LANGUAGE DeriveGeneric #-} |
| 3 | +{-# LANGUAGE FlexibleContexts #-} |
| 4 | +{-# LANGUAGE OverloadedStrings #-} |
| 5 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 6 | +{-# LANGUAGE TupleSections #-} |
| 7 | +{-# LANGUAGE TypeFamilies #-} |
| 8 | +-- | This module provides the interface to GHC, mainly for loading |
| 9 | +-- modules while updating the module cache. |
| 10 | + |
| 11 | +module Haskell.Ide.Engine.Ghc |
| 12 | + ( |
| 13 | + setTypecheckedModule |
| 14 | + , Diagnostics |
| 15 | + , AdditionalErrs |
| 16 | + , cabalModuleGraphs |
| 17 | + , makeRevRedirMapFunc |
| 18 | + ) where |
| 19 | + |
| 20 | +import Bag |
| 21 | +import Control.Monad.IO.Class |
| 22 | +import Data.IORef |
| 23 | +import qualified Data.Map.Strict as Map |
| 24 | +import qualified Data.Set as Set |
| 25 | +import qualified Data.Text as T |
| 26 | +import ErrUtils |
| 27 | +import qualified GhcMod.DynFlags as GM |
| 28 | +import qualified GhcMod.Error as GM |
| 29 | +import qualified GhcMod.Gap as GM |
| 30 | +import qualified GhcMod.ModuleLoader as GM |
| 31 | +import qualified GhcMod.Monad as GM |
| 32 | +import Data.Monoid ((<>)) |
| 33 | +import qualified GhcMod.Target as GM |
| 34 | +import qualified GhcMod.Types as GM |
| 35 | +import qualified GhcMod.Utils as GM |
| 36 | +import Haskell.Ide.Engine.MonadFunctions |
| 37 | +import Haskell.Ide.Engine.MonadTypes |
| 38 | +import Haskell.Ide.Engine.PluginUtils |
| 39 | +import System.FilePath |
| 40 | + |
| 41 | +import DynFlags |
| 42 | +import GHC |
| 43 | +import IOEnv as G |
| 44 | +import HscTypes |
| 45 | +import Outputable (renderWithStyle) |
| 46 | + |
| 47 | +-- --------------------------------------------------------------------- |
| 48 | + |
| 49 | +type Diagnostics = Map.Map Uri (Set.Set Diagnostic) |
| 50 | +type AdditionalErrs = [T.Text] |
| 51 | + |
| 52 | +-- --------------------------------------------------------------------- |
| 53 | + |
| 54 | +lspSev :: Severity -> DiagnosticSeverity |
| 55 | +lspSev SevWarning = DsWarning |
| 56 | +lspSev SevError = DsError |
| 57 | +lspSev SevFatal = DsError |
| 58 | +lspSev SevInfo = DsInfo |
| 59 | +lspSev _ = DsInfo |
| 60 | + |
| 61 | +-- --------------------------------------------------------------------- |
| 62 | +-- type LogAction = DynFlags -> WarnReason -> Severity -> SrcSpan -> PprStyle -> MsgDoc -> IO () |
| 63 | +logDiag :: (FilePath -> FilePath) -> IORef AdditionalErrs -> IORef Diagnostics -> LogAction |
| 64 | +logDiag rfm eref dref df _reason sev spn style msg = do |
| 65 | + eloc <- srcSpan2Loc rfm spn |
| 66 | + let msgTxt = T.pack $ renderWithStyle df msg style |
| 67 | + case eloc of |
| 68 | + Right (Location uri range) -> do |
| 69 | + let update = Map.insertWith Set.union uri l |
| 70 | + where l = Set.singleton diag |
| 71 | + diag = Diagnostic range (Just $ lspSev sev) Nothing (Just "ghcmod") msgTxt Nothing |
| 72 | + modifyIORef' dref update |
| 73 | + Left _ -> do |
| 74 | + modifyIORef' eref (msgTxt:) |
| 75 | + return () |
| 76 | + |
| 77 | +-- --------------------------------------------------------------------- |
| 78 | + |
| 79 | +-- unhelpfulSrcSpanErr :: T.Text -> IdeError |
| 80 | +-- unhelpfulSrcSpanErr err = |
| 81 | +-- IdeError PluginError |
| 82 | +-- ("Unhelpful SrcSpan" <> ": \"" <> err <> "\"") |
| 83 | +-- Null |
| 84 | + |
| 85 | +-- --------------------------------------------------------------------- |
| 86 | + |
| 87 | +srcErrToDiag :: MonadIO m |
| 88 | + => DynFlags |
| 89 | + -> (FilePath -> FilePath) |
| 90 | + -> SourceError -> m (Diagnostics, AdditionalErrs) |
| 91 | +srcErrToDiag df rfm se = do |
| 92 | + debugm "in srcErrToDiag" |
| 93 | + let errMsgs = bagToList $ srcErrorMessages se |
| 94 | + processMsg err = do |
| 95 | + let sev = Just DsError |
| 96 | + unqual = errMsgContext err |
| 97 | + st = GM.mkErrStyle' df unqual |
| 98 | + msgTxt = T.pack $ renderWithStyle df (pprLocErrMsg err) st |
| 99 | + eloc <- srcSpan2Loc rfm $ errMsgSpan err |
| 100 | + case eloc of |
| 101 | + Right (Location uri range) -> |
| 102 | + return $ Right (uri, Diagnostic range sev Nothing (Just "ghcmod") msgTxt Nothing) |
| 103 | + Left _ -> return $ Left msgTxt |
| 104 | + processMsgs [] = return (Map.empty,[]) |
| 105 | + processMsgs (x:xs) = do |
| 106 | + res <- processMsg x |
| 107 | + (m,es) <- processMsgs xs |
| 108 | + case res of |
| 109 | + Right (uri, diag) -> |
| 110 | + return (Map.insertWith Set.union uri (Set.singleton diag) m, es) |
| 111 | + Left e -> return (m, e:es) |
| 112 | + processMsgs errMsgs |
| 113 | + |
| 114 | +-- --------------------------------------------------------------------- |
| 115 | + |
| 116 | +myWrapper :: GM.IOish m |
| 117 | + => (FilePath -> FilePath) |
| 118 | + -> GM.GmlT m () |
| 119 | + -> GM.GmlT m (Diagnostics, AdditionalErrs) |
| 120 | +myWrapper rfm action = do |
| 121 | + env <- getSession |
| 122 | + diagRef <- liftIO $ newIORef Map.empty |
| 123 | + errRef <- liftIO $ newIORef [] |
| 124 | + let setLogger df = df { log_action = logDiag rfm errRef diagRef } |
| 125 | + setDeferTypedHoles = setGeneralFlag' Opt_DeferTypedHoles |
| 126 | + ghcErrRes msg = (Map.empty, [T.pack msg]) |
| 127 | + handlers = errorHandlers ghcErrRes (srcErrToDiag (hsc_dflags env) rfm ) |
| 128 | + action' = do |
| 129 | + GM.withDynFlags (setLogger . setDeferTypedHoles) action |
| 130 | + diags <- liftIO $ readIORef diagRef |
| 131 | + errs <- liftIO $ readIORef errRef |
| 132 | + return (diags,errs) |
| 133 | + GM.gcatches action' handlers |
| 134 | + |
| 135 | +-- --------------------------------------------------------------------- |
| 136 | + |
| 137 | +errorHandlers :: (Monad m) => (String -> a) -> (SourceError -> m a) -> [GM.GHandler m a] |
| 138 | +errorHandlers ghcErrRes renderSourceError = handlers |
| 139 | + where |
| 140 | + -- ghc throws GhcException, SourceError, GhcApiError and |
| 141 | + -- IOEnvFailure. ghc-mod-core throws GhcModError. |
| 142 | + handlers = |
| 143 | + [ GM.GHandler $ \(ex :: GM.GhcModError) -> |
| 144 | + return $ ghcErrRes (show ex) |
| 145 | + , GM.GHandler $ \(ex :: IOEnvFailure) -> |
| 146 | + return $ ghcErrRes (show ex) |
| 147 | + , GM.GHandler $ \(ex :: GhcApiError) -> |
| 148 | + return $ ghcErrRes (show ex) |
| 149 | + , GM.GHandler $ \(ex :: SourceError) -> |
| 150 | + renderSourceError ex |
| 151 | + , GM.GHandler $ \(ex :: GhcException) -> |
| 152 | + return $ ghcErrRes $ GM.renderGm $ GM.ghcExceptionDoc ex |
| 153 | + , GM.GHandler $ \(ex :: IOError) -> |
| 154 | + return $ ghcErrRes (show ex) |
| 155 | + -- , GM.GHandler $ \(ex :: GM.SomeException) -> |
| 156 | + -- return $ ghcErrRes (show ex) |
| 157 | + ] |
| 158 | + |
| 159 | +-- --------------------------------------------------------------------- |
| 160 | + |
| 161 | +setTypecheckedModule :: Uri -> IdeGhcM (IdeResult (Diagnostics, AdditionalErrs)) |
| 162 | +setTypecheckedModule uri = |
| 163 | + pluginGetFile "setTypecheckedModule: " uri $ \fp -> do |
| 164 | + fileMap <- GM.getMMappedFiles |
| 165 | + debugm $ "setTypecheckedModule: file mapping state is: " ++ show fileMap |
| 166 | + rfm <- GM.mkRevRedirMapFunc |
| 167 | + let |
| 168 | + ghcErrRes msg = ((Map.empty, [T.pack msg]),Nothing,Nothing) |
| 169 | + progTitle = "Typechecking " <> T.pack (takeFileName fp) |
| 170 | + debugm "setTypecheckedModule: before ghc-mod" |
| 171 | + -- TODO:AZ: loading this one module may/should trigger loads of any |
| 172 | + -- other modules which currently have a VFS entry. Need to make |
| 173 | + -- sure that their diagnostics are reported, and their module |
| 174 | + -- cache entries are updated. |
| 175 | + -- TODO: Are there any hooks we can use to report back on the progress? |
| 176 | + ((diags', errs), mtm, mpm) <- withIndefiniteProgress progTitle NotCancellable $ GM.gcatches |
| 177 | + (GM.getModulesGhc' (myWrapper rfm) fp) |
| 178 | + (errorHandlers ghcErrRes (return . ghcErrRes . show)) |
| 179 | + debugm "setTypecheckedModule: after ghc-mod" |
| 180 | + |
| 181 | + canonUri <- canonicalizeUri uri |
| 182 | + let diags = Map.insertWith Set.union canonUri Set.empty diags' |
| 183 | + diags2 <- case (mpm,mtm) of |
| 184 | + (Just pm, Nothing) -> do |
| 185 | + debugm $ "setTypecheckedModule: Did get parsed module for: " ++ show fp |
| 186 | + cacheModule fp (Left pm) |
| 187 | + debugm "setTypecheckedModule: done" |
| 188 | + return diags |
| 189 | + |
| 190 | + (_, Just tm) -> do |
| 191 | + debugm $ "setTypecheckedModule: Did get typechecked module for: " ++ show fp |
| 192 | + sess <- fmap GM.gmgsSession . GM.gmGhcSession <$> GM.gmsGet |
| 193 | + |
| 194 | + -- set the session before we cache the module, so that deferred |
| 195 | + -- responses triggered by cacheModule can access it |
| 196 | + modifyMTS (\s -> s {ghcSession = sess}) |
| 197 | + cacheModule fp (Right tm) |
| 198 | + debugm "setTypecheckedModule: done" |
| 199 | + return diags |
| 200 | + |
| 201 | + _ -> do |
| 202 | + debugm $ "setTypecheckedModule: Didn't get typechecked or parsed module for: " ++ show fp |
| 203 | + debugm $ "setTypecheckedModule: errs: " ++ show errs |
| 204 | + |
| 205 | + failModule fp |
| 206 | + |
| 207 | + let sev = Just DsError |
| 208 | + range = Range (Position 0 0) (Position 1 0) |
| 209 | + msgTxt = T.unlines errs |
| 210 | + let d = Diagnostic range sev Nothing (Just "ghcmod") msgTxt Nothing |
| 211 | + return $ Map.insertWith Set.union canonUri (Set.singleton d) diags |
| 212 | + |
| 213 | + return $ IdeResultOk (diags2,errs) |
| 214 | + |
| 215 | +-- --------------------------------------------------------------------- |
| 216 | + |
| 217 | +cabalModuleGraphs :: IdeGhcM [GM.GmModuleGraph] |
| 218 | +cabalModuleGraphs = doCabalModuleGraphs |
| 219 | + where |
| 220 | + doCabalModuleGraphs :: (GM.IOish m) => GM.GhcModT m [GM.GmModuleGraph] |
| 221 | + doCabalModuleGraphs = do |
| 222 | + crdl <- GM.cradle |
| 223 | + case GM.cradleCabalFile crdl of |
| 224 | + Just _ -> do |
| 225 | + mcs <- GM.cabalResolvedComponents |
| 226 | + let graph = map GM.gmcHomeModuleGraph $ Map.elems mcs |
| 227 | + return graph |
| 228 | + Nothing -> return [] |
| 229 | + |
| 230 | +-- --------------------------------------------------------------------- |
| 231 | + |
| 232 | +makeRevRedirMapFunc :: IdeGhcM (FilePath -> FilePath) |
| 233 | +makeRevRedirMapFunc = make |
| 234 | + where |
| 235 | + make :: (GM.IOish m) => GM.GhcModT m (FilePath -> FilePath) |
| 236 | + make = GM.mkRevRedirMapFunc |
| 237 | + |
| 238 | +-- --------------------------------------------------------------------- |
0 commit comments