|
| 1 | +{-# LANGUAGE GADTs #-} |
| 2 | +module Development.IDE.Core.PluginUtils where |
| 3 | + |
| 4 | +import Control.Monad.Extra |
| 5 | +import Control.Monad.IO.Class |
| 6 | +import Control.Monad.Reader (runReaderT) |
| 7 | +import Control.Monad.Trans.Except |
| 8 | +import Control.Monad.Trans.Maybe |
| 9 | +import Data.Either.Extra (maybeToEither) |
| 10 | +import Data.Functor.Identity |
| 11 | +import qualified Data.Text as T |
| 12 | +import Development.IDE.Core.PositionMapping |
| 13 | +import Development.IDE.Core.Shake (IdeAction, IdeRule, |
| 14 | + IdeState (shakeExtras), |
| 15 | + mkDelayedAction, |
| 16 | + shakeEnqueue) |
| 17 | +import qualified Development.IDE.Core.Shake as Shake |
| 18 | +import Development.IDE.GHC.Orphans () |
| 19 | +import Development.IDE.Graph hiding (ShakeValue) |
| 20 | +import Development.IDE.Types.Location (NormalizedFilePath) |
| 21 | +import qualified Development.IDE.Types.Location as Location |
| 22 | +import qualified Development.IDE.Types.Logger as Logger |
| 23 | +import qualified Ide.PluginUtils as PluginUtils |
| 24 | +import qualified Language.LSP.Types as LSP |
| 25 | +import Prettyprinter |
| 26 | +import Prettyprinter.Render.Text (renderStrict) |
| 27 | + |
| 28 | +-- ---------------------------------------------------------------------------- |
| 29 | +-- Plugin Error wrapping |
| 30 | +-- ---------------------------------------------------------------------------- |
| 31 | + |
| 32 | +data GhcidePluginError |
| 33 | + = forall a . Show a => FastRuleNotReady a |
| 34 | + | forall a . Show a => RuleFailed a |
| 35 | + | CoreError PluginUtils.PluginError |
| 36 | + |
| 37 | +instance Pretty GhcidePluginError where |
| 38 | + pretty = \case |
| 39 | + FastRuleNotReady rule -> "FastRuleNotReady:" <+> viaShow rule |
| 40 | + RuleFailed rule -> "RuleFailed:" <+> viaShow rule |
| 41 | + CoreError perror -> pretty $ PluginUtils.prettyPluginError perror |
| 42 | + |
| 43 | +pluginResponse :: |
| 44 | + Monad m => |
| 45 | + ExceptT GhcidePluginError m a -> |
| 46 | + m (Either LSP.ResponseError a) |
| 47 | +pluginResponse = PluginUtils.pluginResponse' handlePluginError |
| 48 | + |
| 49 | +withPluginError :: Functor m => ExceptT PluginUtils.PluginError m a -> ExceptT GhcidePluginError m a |
| 50 | +withPluginError = PluginUtils.withError CoreError |
| 51 | + |
| 52 | +mkPluginErrorMessage :: T.Text -> GhcidePluginError |
| 53 | +mkPluginErrorMessage = CoreError . PluginUtils.mkPluginErrorMessage |
| 54 | + |
| 55 | +handlePluginError :: GhcidePluginError -> LSP.ResponseError |
| 56 | +handlePluginError msg = PluginUtils.mkSimpleResponseError $ renderStrict simpleDoc |
| 57 | + where |
| 58 | + simpleDoc = layoutPretty defaultLayoutOptions $ pretty msg |
| 59 | + |
| 60 | +-- ---------------------------------------------------------------------------- |
| 61 | +-- Action wrappers |
| 62 | +-- ---------------------------------------------------------------------------- |
| 63 | + |
| 64 | +runAction :: MonadIO m => String -> IdeState -> ExceptT e Action a -> ExceptT e m a |
| 65 | +runAction herald ide act = |
| 66 | + PluginUtils.hoistExceptT . ExceptT $ |
| 67 | + join $ shakeEnqueue (shakeExtras ide) (mkDelayedAction herald Logger.Debug $ runExceptT act) |
| 68 | + |
| 69 | +-- | Request a Rule result, it not available return the last computed result which may be stale. |
| 70 | +-- Errors out if none available. |
| 71 | +useWithStale_ ::(IdeRule k v) |
| 72 | + => k -> NormalizedFilePath -> ExceptT e Action (v, PositionMapping) |
| 73 | +useWithStale_ key file = ExceptT $ fmap Right $ Shake.useWithStale_ key file |
| 74 | + |
| 75 | +useWithStale :: IdeRule k v |
| 76 | + => k -> NormalizedFilePath -> ExceptT GhcidePluginError Action (v, PositionMapping) |
| 77 | +useWithStale key file = maybeToExceptT (FastRuleNotReady key) $ useWithStaleMaybeT key file |
| 78 | + |
| 79 | +-- | useE is useful to implement functions that aren’t rules but need shortcircuiting |
| 80 | +-- e.g. getDefinition. |
| 81 | +use :: IdeRule k v => k -> NormalizedFilePath -> ExceptT GhcidePluginError Action v |
| 82 | +use k = maybeToExceptT (RuleFailed k) . MaybeT . Shake.use k |
| 83 | + |
| 84 | +useWithStaleMaybeT :: IdeRule k v |
| 85 | + => k -> NormalizedFilePath -> MaybeT Action (v, PositionMapping) |
| 86 | +useWithStaleMaybeT key file = MaybeT $ runIdentity <$> Shake.usesWithStale key (Identity file) |
| 87 | + |
| 88 | +-- ---------------------------------------------------------------------------- |
| 89 | +-- IdeAction wrappers |
| 90 | +-- ---------------------------------------------------------------------------- |
| 91 | + |
| 92 | +runIdeAction :: MonadIO m => String -> Shake.ShakeExtras -> ExceptT e IdeAction a -> ExceptT e m a |
| 93 | +runIdeAction _herald s i = ExceptT $ liftIO $ runReaderT (Shake.runIdeActionT $ runExceptT i) s |
| 94 | + |
| 95 | +-- | useE is useful to implement functions that aren’t rules but need shortcircuiting |
| 96 | +-- e.g. getDefinition. |
| 97 | +useWithStaleFast :: IdeRule k v => k -> NormalizedFilePath -> ExceptT GhcidePluginError IdeAction (v, PositionMapping) |
| 98 | +useWithStaleFast k = maybeToExceptT (RuleFailed k) . MaybeT . Shake.useWithStaleFast k |
| 99 | + |
| 100 | +uriToFilePath' :: Monad m => LSP.Uri -> ExceptT GhcidePluginError m FilePath |
| 101 | +uriToFilePath' uri = ExceptT . pure . maybeToEither (CoreError $ PluginUtils.PluginUriToFilePath uri) $ Location.uriToFilePath' uri |
| 102 | + |
| 103 | +-- ---------------------------------------------------------------------------- |
| 104 | +-- Internal Helper function, not exported |
| 105 | +-- ---------------------------------------------------------------------------- |
| 106 | + |
| 107 | +hoistAction :: Action a -> ExceptT e Action a |
| 108 | +hoistAction = ExceptT . fmap Right |
0 commit comments