module Development.IDE.Plugin.CodeAction.Args
  ( CodeActionTitle,
    CodeActionPreferred,
    GhcideCodeActionResult,
    GhcideCodeAction,
    mkGhcideCAPlugin,
    mkGhcideCAsPlugin,
    ToTextEdit (..),
    ToCodeAction (..),
    wrap,
    mkCA,
  )
where

import           Control.Concurrent.STM.Stats                 (readTVarIO)
import           Control.Monad.Except                         (ExceptT (..),
                                                               runExceptT)
import           Control.Monad.Reader
import           Control.Monad.Trans.Maybe
import           Data.Either                                  (fromRight,
                                                               partitionEithers)
import           Data.Functor                                 ((<&>))
import           Data.IORef.Extra
import qualified Data.Map                                     as Map
import           Data.Maybe                                   (fromMaybe)
import qualified Data.Text                                    as T
import           Development.IDE                              hiding
                                                              (pluginHandlers)
import           Development.IDE.Core.Shake
import           Development.IDE.GHC.Compat
import           Development.IDE.GHC.ExactPrint
import           Development.IDE.Plugin.CodeAction.ExactPrint (Rewrite,
                                                               rewriteToEdit)
import           Development.IDE.Plugin.TypeLenses            (GetGlobalBindingTypeSigs (GetGlobalBindingTypeSigs),
                                                               GlobalBindingTypeSigsResult)
import           Development.IDE.Spans.LocalBindings          (Bindings)
import           Development.IDE.Types.Exports                (ExportsMap)
import           Development.IDE.Types.Options                (IdeOptions)
import           Ide.Plugin.Error                             (PluginError)
import           Ide.Types
import           Language.LSP.Protocol.Message
import           Language.LSP.Protocol.Types

type CodeActionTitle = T.Text

type CodeActionPreferred = Bool

type GhcideCodeActionResult = [(CodeActionTitle, Maybe CodeActionKind, Maybe CodeActionPreferred, [TextEdit])]

type GhcideCodeAction = ExceptT PluginError (ReaderT CodeActionArgs IO) GhcideCodeActionResult

-------------------------------------------------------------------------------------------------

runGhcideCodeAction :: IdeState -> MessageParams Method_TextDocumentCodeAction -> GhcideCodeAction -> HandlerM Config GhcideCodeActionResult
runGhcideCodeAction :: IdeState
-> MessageParams 'Method_TextDocumentCodeAction
-> GhcideCodeAction
-> HandlerM Config GhcideCodeActionResult
runGhcideCodeAction IdeState
state (CodeActionParams Maybe ProgressToken
_ Maybe ProgressToken
_ (TextDocumentIdentifier Uri
uri) Range
_range CodeActionContext {$sel:_diagnostics:CodeActionContext :: CodeActionContext -> [Diagnostic]
_diagnostics = [Diagnostic]
diags}) GhcideCodeAction
codeAction = do
  let mbFile :: Maybe NormalizedFilePath
mbFile = FilePath -> NormalizedFilePath
toNormalizedFilePath' (FilePath -> NormalizedFilePath)
-> Maybe FilePath -> Maybe NormalizedFilePath
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Uri -> Maybe FilePath
uriToFilePath Uri
uri
      runRule :: k -> IO (Maybe (RuleResult k))
runRule k
key = FilePath
-> IdeState
-> Action (Maybe (RuleResult k))
-> IO (Maybe (RuleResult k))
forall a. FilePath -> IdeState -> Action a -> IO a
runAction (FilePath
"GhcideCodeActions." FilePath -> FilePath -> FilePath
forall a. Semigroup a => a -> a -> a
<> k -> FilePath
forall a. Show a => a -> FilePath
show k
key) IdeState
state (Action (Maybe (RuleResult k)) -> IO (Maybe (RuleResult k)))
-> Action (Maybe (RuleResult k)) -> IO (Maybe (RuleResult k))
forall a b. (a -> b) -> a -> b
$ MaybeT Action (RuleResult k) -> Action (Maybe (RuleResult k))
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT (MaybeT Action (RuleResult k) -> Action (Maybe (RuleResult k)))
-> MaybeT Action (RuleResult k) -> Action (Maybe (RuleResult k))
forall a b. (a -> b) -> a -> b
$ Action (Maybe NormalizedFilePath)
-> MaybeT Action NormalizedFilePath
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (Maybe NormalizedFilePath -> Action (Maybe NormalizedFilePath)
forall a. a -> Action a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe NormalizedFilePath
mbFile) MaybeT Action NormalizedFilePath
-> (NormalizedFilePath -> MaybeT Action (RuleResult k))
-> MaybeT Action (RuleResult k)
forall a b.
MaybeT Action a -> (a -> MaybeT Action b) -> MaybeT Action b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Action (Maybe (RuleResult k)) -> MaybeT Action (RuleResult k)
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (Action (Maybe (RuleResult k)) -> MaybeT Action (RuleResult k))
-> (NormalizedFilePath -> Action (Maybe (RuleResult k)))
-> NormalizedFilePath
-> MaybeT Action (RuleResult k)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. k -> NormalizedFilePath -> Action (Maybe (RuleResult k))
forall k v.
IdeRule k v =>
k -> NormalizedFilePath -> Action (Maybe v)
use k
key
  IO (Maybe HscEnvEq)
caaGhcSession <- IO (Maybe HscEnvEq) -> HandlerM Config (IO (Maybe HscEnvEq))
forall (m :: * -> *) a. MonadIO m => IO a -> m (IO a)
onceIO (IO (Maybe HscEnvEq) -> HandlerM Config (IO (Maybe HscEnvEq)))
-> IO (Maybe HscEnvEq) -> HandlerM Config (IO (Maybe HscEnvEq))
forall a b. (a -> b) -> a -> b
$ GhcSession -> IO (Maybe (RuleResult GhcSession))
forall {k}.
(Hashable k, Show k, Show (RuleResult k), Typeable k,
 Typeable (RuleResult k), NFData k, NFData (RuleResult k)) =>
k -> IO (Maybe (RuleResult k))
runRule GhcSession
GhcSession
  IO ExportsMap
caaExportsMap <-
    IO ExportsMap -> HandlerM Config (IO ExportsMap)
forall (m :: * -> *) a. MonadIO m => IO a -> m (IO a)
onceIO (IO ExportsMap -> HandlerM Config (IO ExportsMap))
-> IO ExportsMap -> HandlerM Config (IO ExportsMap)
forall a b. (a -> b) -> a -> b
$
      IO (Maybe HscEnvEq)
caaGhcSession IO (Maybe HscEnvEq)
-> (Maybe HscEnvEq -> IO ExportsMap) -> IO ExportsMap
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Just HscEnvEq
env -> do
          ExportsMap
pkgExports <- HscEnvEq -> IO ExportsMap
envPackageExports HscEnvEq
env
          ExportsMap
localExports <- TVar ExportsMap -> IO ExportsMap
forall a. TVar a -> IO a
readTVarIO (ShakeExtras -> TVar ExportsMap
exportsMap (ShakeExtras -> TVar ExportsMap) -> ShakeExtras -> TVar ExportsMap
forall a b. (a -> b) -> a -> b
$ IdeState -> ShakeExtras
shakeExtras IdeState
state)
          ExportsMap -> IO ExportsMap
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ExportsMap -> IO ExportsMap) -> ExportsMap -> IO ExportsMap
forall a b. (a -> b) -> a -> b
$ ExportsMap
localExports ExportsMap -> ExportsMap -> ExportsMap
forall a. Semigroup a => a -> a -> a
<> ExportsMap
pkgExports
        Maybe HscEnvEq
_ -> ExportsMap -> IO ExportsMap
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ExportsMap
forall a. Monoid a => a
mempty
  IO IdeOptions
caaIdeOptions <- IO IdeOptions -> HandlerM Config (IO IdeOptions)
forall (m :: * -> *) a. MonadIO m => IO a -> m (IO a)
onceIO (IO IdeOptions -> HandlerM Config (IO IdeOptions))
-> IO IdeOptions -> HandlerM Config (IO IdeOptions)
forall a b. (a -> b) -> a -> b
$ FilePath -> IdeState -> Action IdeOptions -> IO IdeOptions
forall a. FilePath -> IdeState -> Action a -> IO a
runAction FilePath
"GhcideCodeActions.getIdeOptions" IdeState
state Action IdeOptions
getIdeOptions
  IO (Maybe ParsedModule)
caaParsedModule <- IO (Maybe ParsedModule)
-> HandlerM Config (IO (Maybe ParsedModule))
forall (m :: * -> *) a. MonadIO m => IO a -> m (IO a)
onceIO (IO (Maybe ParsedModule)
 -> HandlerM Config (IO (Maybe ParsedModule)))
-> IO (Maybe ParsedModule)
-> HandlerM Config (IO (Maybe ParsedModule))
forall a b. (a -> b) -> a -> b
$ GetParsedModuleWithComments
-> IO (Maybe (RuleResult GetParsedModuleWithComments))
forall {k}.
(Hashable k, Show k, Show (RuleResult k), Typeable k,
 Typeable (RuleResult k), NFData k, NFData (RuleResult k)) =>
k -> IO (Maybe (RuleResult k))
runRule GetParsedModuleWithComments
GetParsedModuleWithComments
  IO (Maybe Text)
caaContents <-
    IO (Maybe Text) -> HandlerM Config (IO (Maybe Text))
forall (m :: * -> *) a. MonadIO m => IO a -> m (IO a)
onceIO (IO (Maybe Text) -> HandlerM Config (IO (Maybe Text)))
-> IO (Maybe Text) -> HandlerM Config (IO (Maybe Text))
forall a b. (a -> b) -> a -> b
$
      GetFileContents -> IO (Maybe (RuleResult GetFileContents))
forall {k}.
(Hashable k, Show k, Show (RuleResult k), Typeable k,
 Typeable (RuleResult k), NFData k, NFData (RuleResult k)) =>
k -> IO (Maybe (RuleResult k))
runRule GetFileContents
GetFileContents IO (Maybe (FileVersion, Maybe Text))
-> (Maybe (FileVersion, Maybe Text) -> Maybe Text)
-> IO (Maybe Text)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
        Just (FileVersion
_, Maybe Text
txt) -> Maybe Text
txt
        Maybe (FileVersion, Maybe Text)
Nothing       -> Maybe Text
forall a. Maybe a
Nothing
  IO (Maybe DynFlags)
caaDf <- IO (Maybe DynFlags) -> HandlerM Config (IO (Maybe DynFlags))
forall (m :: * -> *) a. MonadIO m => IO a -> m (IO a)
onceIO (IO (Maybe DynFlags) -> HandlerM Config (IO (Maybe DynFlags)))
-> IO (Maybe DynFlags) -> HandlerM Config (IO (Maybe DynFlags))
forall a b. (a -> b) -> a -> b
$ (ParsedModule -> DynFlags) -> Maybe ParsedModule -> Maybe DynFlags
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (ModSummary -> DynFlags
ms_hspp_opts (ModSummary -> DynFlags)
-> (ParsedModule -> ModSummary) -> ParsedModule -> DynFlags
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParsedModule -> ModSummary
pm_mod_summary) (Maybe ParsedModule -> Maybe DynFlags)
-> IO (Maybe ParsedModule) -> IO (Maybe DynFlags)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO (Maybe ParsedModule)
caaParsedModule
  IO (Maybe ParsedSource)
caaAnnSource <- IO (Maybe ParsedSource)
-> HandlerM Config (IO (Maybe ParsedSource))
forall (m :: * -> *) a. MonadIO m => IO a -> m (IO a)
onceIO (IO (Maybe ParsedSource)
 -> HandlerM Config (IO (Maybe ParsedSource)))
-> IO (Maybe ParsedSource)
-> HandlerM Config (IO (Maybe ParsedSource))
forall a b. (a -> b) -> a -> b
$ GetAnnotatedParsedSource
-> IO (Maybe (RuleResult GetAnnotatedParsedSource))
forall {k}.
(Hashable k, Show k, Show (RuleResult k), Typeable k,
 Typeable (RuleResult k), NFData k, NFData (RuleResult k)) =>
k -> IO (Maybe (RuleResult k))
runRule GetAnnotatedParsedSource
GetAnnotatedParsedSource
  IO (Maybe TcModuleResult)
caaTmr <- IO (Maybe TcModuleResult)
-> HandlerM Config (IO (Maybe TcModuleResult))
forall (m :: * -> *) a. MonadIO m => IO a -> m (IO a)
onceIO (IO (Maybe TcModuleResult)
 -> HandlerM Config (IO (Maybe TcModuleResult)))
-> IO (Maybe TcModuleResult)
-> HandlerM Config (IO (Maybe TcModuleResult))
forall a b. (a -> b) -> a -> b
$ TypeCheck -> IO (Maybe (RuleResult TypeCheck))
forall {k}.
(Hashable k, Show k, Show (RuleResult k), Typeable k,
 Typeable (RuleResult k), NFData k, NFData (RuleResult k)) =>
k -> IO (Maybe (RuleResult k))
runRule TypeCheck
TypeCheck
  IO (Maybe HieAstResult)
caaHar <- IO (Maybe HieAstResult)
-> HandlerM Config (IO (Maybe HieAstResult))
forall (m :: * -> *) a. MonadIO m => IO a -> m (IO a)
onceIO (IO (Maybe HieAstResult)
 -> HandlerM Config (IO (Maybe HieAstResult)))
-> IO (Maybe HieAstResult)
-> HandlerM Config (IO (Maybe HieAstResult))
forall a b. (a -> b) -> a -> b
$ GetHieAst -> IO (Maybe (RuleResult GetHieAst))
forall {k}.
(Hashable k, Show k, Show (RuleResult k), Typeable k,
 Typeable (RuleResult k), NFData k, NFData (RuleResult k)) =>
k -> IO (Maybe (RuleResult k))
runRule GetHieAst
GetHieAst
  IO (Maybe Bindings)
caaBindings <- IO (Maybe Bindings) -> HandlerM Config (IO (Maybe Bindings))
forall (m :: * -> *) a. MonadIO m => IO a -> m (IO a)
onceIO (IO (Maybe Bindings) -> HandlerM Config (IO (Maybe Bindings)))
-> IO (Maybe Bindings) -> HandlerM Config (IO (Maybe Bindings))
forall a b. (a -> b) -> a -> b
$ GetBindings -> IO (Maybe (RuleResult GetBindings))
forall {k}.
(Hashable k, Show k, Show (RuleResult k), Typeable k,
 Typeable (RuleResult k), NFData k, NFData (RuleResult k)) =>
k -> IO (Maybe (RuleResult k))
runRule GetBindings
GetBindings
  IO (Maybe GlobalBindingTypeSigsResult)
caaGblSigs <- IO (Maybe GlobalBindingTypeSigsResult)
-> HandlerM Config (IO (Maybe GlobalBindingTypeSigsResult))
forall (m :: * -> *) a. MonadIO m => IO a -> m (IO a)
onceIO (IO (Maybe GlobalBindingTypeSigsResult)
 -> HandlerM Config (IO (Maybe GlobalBindingTypeSigsResult)))
-> IO (Maybe GlobalBindingTypeSigsResult)
-> HandlerM Config (IO (Maybe GlobalBindingTypeSigsResult))
forall a b. (a -> b) -> a -> b
$ GetGlobalBindingTypeSigs
-> IO (Maybe (RuleResult GetGlobalBindingTypeSigs))
forall {k}.
(Hashable k, Show k, Show (RuleResult k), Typeable k,
 Typeable (RuleResult k), NFData k, NFData (RuleResult k)) =>
k -> IO (Maybe (RuleResult k))
runRule GetGlobalBindingTypeSigs
GetGlobalBindingTypeSigs
  [Either PluginError GhcideCodeActionResult]
results <- IO [Either PluginError GhcideCodeActionResult]
-> HandlerM Config [Either PluginError GhcideCodeActionResult]
forall a. IO a -> HandlerM Config a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO [Either PluginError GhcideCodeActionResult]
 -> HandlerM Config [Either PluginError GhcideCodeActionResult])
-> IO [Either PluginError GhcideCodeActionResult]
-> HandlerM Config [Either PluginError GhcideCodeActionResult]
forall a b. (a -> b) -> a -> b
$
      [IO (Either PluginError GhcideCodeActionResult)]
-> IO [Either PluginError GhcideCodeActionResult]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
forall (m :: * -> *) a. Monad m => [m a] -> m [a]
sequence
        [ ReaderT
  CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
-> CodeActionArgs -> IO (Either PluginError GhcideCodeActionResult)
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT (GhcideCodeAction
-> ReaderT
     CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT GhcideCodeAction
codeAction) CodeActionArgs {IO (Maybe Text)
IO (Maybe ParsedSource)
IO (Maybe DynFlags)
IO (Maybe ParsedModule)
IO (Maybe HscEnvEq)
IO (Maybe Bindings)
IO (Maybe HieAstResult)
IO (Maybe TcModuleResult)
IO (Maybe GlobalBindingTypeSigsResult)
IO ExportsMap
IO IdeOptions
Diagnostic
caaGhcSession :: IO (Maybe HscEnvEq)
caaExportsMap :: IO ExportsMap
caaIdeOptions :: IO IdeOptions
caaParsedModule :: IO (Maybe ParsedModule)
caaContents :: IO (Maybe Text)
caaDf :: IO (Maybe DynFlags)
caaAnnSource :: IO (Maybe ParsedSource)
caaTmr :: IO (Maybe TcModuleResult)
caaHar :: IO (Maybe HieAstResult)
caaBindings :: IO (Maybe Bindings)
caaGblSigs :: IO (Maybe GlobalBindingTypeSigsResult)
caaDiagnostic :: Diagnostic
$sel:caaExportsMap:CodeActionArgs :: IO ExportsMap
$sel:caaGhcSession:CodeActionArgs :: IO (Maybe HscEnvEq)
$sel:caaIdeOptions:CodeActionArgs :: IO IdeOptions
$sel:caaParsedModule:CodeActionArgs :: IO (Maybe ParsedModule)
$sel:caaContents:CodeActionArgs :: IO (Maybe Text)
$sel:caaDf:CodeActionArgs :: IO (Maybe DynFlags)
$sel:caaAnnSource:CodeActionArgs :: IO (Maybe ParsedSource)
$sel:caaTmr:CodeActionArgs :: IO (Maybe TcModuleResult)
$sel:caaHar:CodeActionArgs :: IO (Maybe HieAstResult)
$sel:caaBindings:CodeActionArgs :: IO (Maybe Bindings)
$sel:caaGblSigs:CodeActionArgs :: IO (Maybe GlobalBindingTypeSigsResult)
$sel:caaDiagnostic:CodeActionArgs :: Diagnostic
..}
          | Diagnostic
caaDiagnostic <- [Diagnostic]
diags
        ]
  let ([PluginError]
_errs, [GhcideCodeActionResult]
successes) = [Either PluginError GhcideCodeActionResult]
-> ([PluginError], [GhcideCodeActionResult])
forall a b. [Either a b] -> ([a], [b])
partitionEithers [Either PluginError GhcideCodeActionResult]
results
  GhcideCodeActionResult -> HandlerM Config GhcideCodeActionResult
forall a. a -> HandlerM Config a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (GhcideCodeActionResult -> HandlerM Config GhcideCodeActionResult)
-> GhcideCodeActionResult -> HandlerM Config GhcideCodeActionResult
forall a b. (a -> b) -> a -> b
$ [GhcideCodeActionResult] -> GhcideCodeActionResult
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [GhcideCodeActionResult]
successes

mkCA :: T.Text -> Maybe CodeActionKind -> Maybe Bool -> [Diagnostic] -> WorkspaceEdit -> (Command |? CodeAction)
mkCA :: Text
-> Maybe CodeActionKind
-> Maybe Bool
-> [Diagnostic]
-> WorkspaceEdit
-> Command |? CodeAction
mkCA Text
title Maybe CodeActionKind
kind Maybe Bool
isPreferred [Diagnostic]
diags WorkspaceEdit
edit =
  CodeAction -> Command |? CodeAction
forall a b. b -> a |? b
InR (CodeAction -> Command |? CodeAction)
-> CodeAction -> Command |? CodeAction
forall a b. (a -> b) -> a -> b
$ Text
-> Maybe CodeActionKind
-> Maybe [Diagnostic]
-> Maybe Bool
-> Maybe CodeActionDisabled
-> Maybe WorkspaceEdit
-> Maybe Command
-> Maybe Value
-> CodeAction
CodeAction Text
title Maybe CodeActionKind
kind ([Diagnostic] -> Maybe [Diagnostic]
forall a. a -> Maybe a
Just [Diagnostic]
diags) Maybe Bool
isPreferred Maybe CodeActionDisabled
forall a. Maybe a
Nothing (WorkspaceEdit -> Maybe WorkspaceEdit
forall a. a -> Maybe a
Just WorkspaceEdit
edit) Maybe Command
forall a. Maybe a
Nothing Maybe Value
forall a. Maybe a
Nothing

mkGhcideCAPlugin :: GhcideCodeAction -> PluginId -> T.Text -> PluginDescriptor IdeState
mkGhcideCAPlugin :: GhcideCodeAction -> PluginId -> Text -> PluginDescriptor IdeState
mkGhcideCAPlugin GhcideCodeAction
codeAction PluginId
plId Text
desc =
  (PluginId -> Text -> PluginDescriptor IdeState
forall ideState. PluginId -> Text -> PluginDescriptor ideState
defaultPluginDescriptor PluginId
plId Text
desc)
    { pluginHandlers = mkPluginHandler SMethod_TextDocumentCodeAction $
        \IdeState
state PluginId
_ params :: MessageParams 'Method_TextDocumentCodeAction
params@(CodeActionParams Maybe ProgressToken
_ Maybe ProgressToken
_ (TextDocumentIdentifier Uri
uri) Range
_ CodeActionContext {$sel:_diagnostics:CodeActionContext :: CodeActionContext -> [Diagnostic]
_diagnostics = [Diagnostic]
diags}) -> do
          GhcideCodeActionResult
results <- HandlerM Config GhcideCodeActionResult
-> ExceptT PluginError (HandlerM Config) GhcideCodeActionResult
forall (m :: * -> *) a. Monad m => m a -> ExceptT PluginError m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (HandlerM Config GhcideCodeActionResult
 -> ExceptT PluginError (HandlerM Config) GhcideCodeActionResult)
-> HandlerM Config GhcideCodeActionResult
-> ExceptT PluginError (HandlerM Config) GhcideCodeActionResult
forall a b. (a -> b) -> a -> b
$ IdeState
-> MessageParams 'Method_TextDocumentCodeAction
-> GhcideCodeAction
-> HandlerM Config GhcideCodeActionResult
runGhcideCodeAction IdeState
state MessageParams 'Method_TextDocumentCodeAction
params GhcideCodeAction
codeAction
          ([Command |? CodeAction] |? Null)
-> ExceptT
     PluginError (HandlerM Config) ([Command |? CodeAction] |? Null)
forall a. a -> ExceptT PluginError (HandlerM Config) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (([Command |? CodeAction] |? Null)
 -> ExceptT
      PluginError (HandlerM Config) ([Command |? CodeAction] |? Null))
-> ([Command |? CodeAction] |? Null)
-> ExceptT
     PluginError (HandlerM Config) ([Command |? CodeAction] |? Null)
forall a b. (a -> b) -> a -> b
$
              [Command |? CodeAction] -> [Command |? CodeAction] |? Null
forall a b. a -> a |? b
InL
                [ Text
-> Maybe CodeActionKind
-> Maybe Bool
-> [Diagnostic]
-> WorkspaceEdit
-> Command |? CodeAction
mkCA Text
title Maybe CodeActionKind
kind Maybe Bool
isPreferred [Diagnostic]
diags WorkspaceEdit
edit
                  | (Text
title, Maybe CodeActionKind
kind, Maybe Bool
isPreferred, [TextEdit]
tedit) <- GhcideCodeActionResult
results,
                    let edit :: WorkspaceEdit
edit = Maybe (Map Uri [TextEdit])
-> Maybe
     [TextDocumentEdit |? (CreateFile |? (RenameFile |? DeleteFile))]
-> Maybe (Map ChangeAnnotationIdentifier ChangeAnnotation)
-> WorkspaceEdit
WorkspaceEdit (Map Uri [TextEdit] -> Maybe (Map Uri [TextEdit])
forall a. a -> Maybe a
Just (Map Uri [TextEdit] -> Maybe (Map Uri [TextEdit]))
-> Map Uri [TextEdit] -> Maybe (Map Uri [TextEdit])
forall a b. (a -> b) -> a -> b
$ Uri -> [TextEdit] -> Map Uri [TextEdit]
forall k a. k -> a -> Map k a
Map.singleton Uri
uri [TextEdit]
tedit) Maybe
  [TextDocumentEdit |? (CreateFile |? (RenameFile |? DeleteFile))]
forall a. Maybe a
Nothing Maybe (Map ChangeAnnotationIdentifier ChangeAnnotation)
forall a. Maybe a
Nothing
                ]
    }

mkGhcideCAsPlugin :: [GhcideCodeAction] -> PluginId -> T.Text -> PluginDescriptor IdeState
mkGhcideCAsPlugin :: [GhcideCodeAction] -> PluginId -> Text -> PluginDescriptor IdeState
mkGhcideCAsPlugin [GhcideCodeAction]
codeActions = GhcideCodeAction -> PluginId -> Text -> PluginDescriptor IdeState
mkGhcideCAPlugin (GhcideCodeAction -> PluginId -> Text -> PluginDescriptor IdeState)
-> GhcideCodeAction
-> PluginId
-> Text
-> PluginDescriptor IdeState
forall a b. (a -> b) -> a -> b
$ [GhcideCodeAction] -> GhcideCodeAction
forall a. Monoid a => [a] -> a
mconcat [GhcideCodeAction]
codeActions

-------------------------------------------------------------------------------------------------

class ToTextEdit a where
  toTextEdit :: CodeActionArgs -> a -> IO [TextEdit]

instance ToTextEdit TextEdit where
  toTextEdit :: CodeActionArgs -> TextEdit -> IO [TextEdit]
toTextEdit CodeActionArgs
_ = [TextEdit] -> IO [TextEdit]
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([TextEdit] -> IO [TextEdit])
-> (TextEdit -> [TextEdit]) -> TextEdit -> IO [TextEdit]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TextEdit -> [TextEdit]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure

instance ToTextEdit Rewrite where
  toTextEdit :: CodeActionArgs -> Rewrite -> IO [TextEdit]
toTextEdit CodeActionArgs {IO (Maybe Text)
IO (Maybe ParsedSource)
IO (Maybe DynFlags)
IO (Maybe ParsedModule)
IO (Maybe HscEnvEq)
IO (Maybe Bindings)
IO (Maybe HieAstResult)
IO (Maybe TcModuleResult)
IO (Maybe GlobalBindingTypeSigsResult)
IO ExportsMap
IO IdeOptions
Diagnostic
$sel:caaExportsMap:CodeActionArgs :: CodeActionArgs -> IO ExportsMap
$sel:caaGhcSession:CodeActionArgs :: CodeActionArgs -> IO (Maybe HscEnvEq)
$sel:caaIdeOptions:CodeActionArgs :: CodeActionArgs -> IO IdeOptions
$sel:caaParsedModule:CodeActionArgs :: CodeActionArgs -> IO (Maybe ParsedModule)
$sel:caaContents:CodeActionArgs :: CodeActionArgs -> IO (Maybe Text)
$sel:caaDf:CodeActionArgs :: CodeActionArgs -> IO (Maybe DynFlags)
$sel:caaAnnSource:CodeActionArgs :: CodeActionArgs -> IO (Maybe ParsedSource)
$sel:caaTmr:CodeActionArgs :: CodeActionArgs -> IO (Maybe TcModuleResult)
$sel:caaHar:CodeActionArgs :: CodeActionArgs -> IO (Maybe HieAstResult)
$sel:caaBindings:CodeActionArgs :: CodeActionArgs -> IO (Maybe Bindings)
$sel:caaGblSigs:CodeActionArgs :: CodeActionArgs -> IO (Maybe GlobalBindingTypeSigsResult)
$sel:caaDiagnostic:CodeActionArgs :: CodeActionArgs -> Diagnostic
caaExportsMap :: IO ExportsMap
caaGhcSession :: IO (Maybe HscEnvEq)
caaIdeOptions :: IO IdeOptions
caaParsedModule :: IO (Maybe ParsedModule)
caaContents :: IO (Maybe Text)
caaDf :: IO (Maybe DynFlags)
caaAnnSource :: IO (Maybe ParsedSource)
caaTmr :: IO (Maybe TcModuleResult)
caaHar :: IO (Maybe HieAstResult)
caaBindings :: IO (Maybe Bindings)
caaGblSigs :: IO (Maybe GlobalBindingTypeSigsResult)
caaDiagnostic :: Diagnostic
..} Rewrite
rw = (Maybe [TextEdit] -> [TextEdit])
-> IO (Maybe [TextEdit]) -> IO [TextEdit]
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ([TextEdit] -> Maybe [TextEdit] -> [TextEdit]
forall a. a -> Maybe a -> a
fromMaybe []) (IO (Maybe [TextEdit]) -> IO [TextEdit])
-> IO (Maybe [TextEdit]) -> IO [TextEdit]
forall a b. (a -> b) -> a -> b
$
    MaybeT IO [TextEdit] -> IO (Maybe [TextEdit])
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT (MaybeT IO [TextEdit] -> IO (Maybe [TextEdit]))
-> MaybeT IO [TextEdit] -> IO (Maybe [TextEdit])
forall a b. (a -> b) -> a -> b
$ do
      DynFlags
df <- IO (Maybe DynFlags) -> MaybeT IO DynFlags
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT IO (Maybe DynFlags)
caaDf
      let r :: Either FilePath [TextEdit]
r = HasCallStack => DynFlags -> Rewrite -> Either FilePath [TextEdit]
DynFlags -> Rewrite -> Either FilePath [TextEdit]
rewriteToEdit DynFlags
df Rewrite
rw
      [TextEdit] -> MaybeT IO [TextEdit]
forall a. a -> MaybeT IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([TextEdit] -> MaybeT IO [TextEdit])
-> [TextEdit] -> MaybeT IO [TextEdit]
forall a b. (a -> b) -> a -> b
$ [TextEdit] -> Either FilePath [TextEdit] -> [TextEdit]
forall b a. b -> Either a b -> b
fromRight [] Either FilePath [TextEdit]
r

instance ToTextEdit a => ToTextEdit [a] where
  toTextEdit :: CodeActionArgs -> [a] -> IO [TextEdit]
toTextEdit CodeActionArgs
caa = (a -> IO [TextEdit]) -> [a] -> IO [TextEdit]
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (CodeActionArgs -> a -> IO [TextEdit]
forall a. ToTextEdit a => CodeActionArgs -> a -> IO [TextEdit]
toTextEdit CodeActionArgs
caa)

instance ToTextEdit a => ToTextEdit (Maybe a) where
  toTextEdit :: CodeActionArgs -> Maybe a -> IO [TextEdit]
toTextEdit CodeActionArgs
caa = IO [TextEdit] -> (a -> IO [TextEdit]) -> Maybe a -> IO [TextEdit]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ([TextEdit] -> IO [TextEdit]
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []) (CodeActionArgs -> a -> IO [TextEdit]
forall a. ToTextEdit a => CodeActionArgs -> a -> IO [TextEdit]
toTextEdit CodeActionArgs
caa)

instance (ToTextEdit a, ToTextEdit b) => ToTextEdit (Either a b) where
  toTextEdit :: CodeActionArgs -> Either a b -> IO [TextEdit]
toTextEdit CodeActionArgs
caa = (a -> IO [TextEdit])
-> (b -> IO [TextEdit]) -> Either a b -> IO [TextEdit]
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (CodeActionArgs -> a -> IO [TextEdit]
forall a. ToTextEdit a => CodeActionArgs -> a -> IO [TextEdit]
toTextEdit CodeActionArgs
caa) (CodeActionArgs -> b -> IO [TextEdit]
forall a. ToTextEdit a => CodeActionArgs -> a -> IO [TextEdit]
toTextEdit CodeActionArgs
caa)

-------------------------------------------------------------------------------------------------

data CodeActionArgs = CodeActionArgs
  { CodeActionArgs -> IO ExportsMap
caaExportsMap   :: IO ExportsMap,
    CodeActionArgs -> IO (Maybe HscEnvEq)
caaGhcSession   :: IO (Maybe HscEnvEq),
    CodeActionArgs -> IO IdeOptions
caaIdeOptions   :: IO IdeOptions,
    CodeActionArgs -> IO (Maybe ParsedModule)
caaParsedModule :: IO (Maybe ParsedModule),
    CodeActionArgs -> IO (Maybe Text)
caaContents     :: IO (Maybe T.Text),
    CodeActionArgs -> IO (Maybe DynFlags)
caaDf           :: IO (Maybe DynFlags),
    CodeActionArgs -> IO (Maybe ParsedSource)
caaAnnSource    :: IO (Maybe ParsedSource),
    CodeActionArgs -> IO (Maybe TcModuleResult)
caaTmr          :: IO (Maybe TcModuleResult),
    CodeActionArgs -> IO (Maybe HieAstResult)
caaHar          :: IO (Maybe HieAstResult),
    CodeActionArgs -> IO (Maybe Bindings)
caaBindings     :: IO (Maybe Bindings),
    CodeActionArgs -> IO (Maybe GlobalBindingTypeSigsResult)
caaGblSigs      :: IO (Maybe GlobalBindingTypeSigsResult),
    CodeActionArgs -> Diagnostic
caaDiagnostic   :: Diagnostic
  }

-- | There's no concurrency in each provider,
-- so we don't need to be thread-safe here
onceIO :: MonadIO m => IO a -> m (IO a)
onceIO :: forall (m :: * -> *) a. MonadIO m => IO a -> m (IO a)
onceIO IO a
io = do
  IORef (Maybe a)
var <- IO (IORef (Maybe a)) -> m (IORef (Maybe a))
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (IORef (Maybe a)) -> m (IORef (Maybe a)))
-> IO (IORef (Maybe a)) -> m (IORef (Maybe a))
forall a b. (a -> b) -> a -> b
$ Maybe a -> IO (IORef (Maybe a))
forall a. a -> IO (IORef a)
newIORef Maybe a
forall a. Maybe a
Nothing
  IO a -> m (IO a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (IO a -> m (IO a)) -> IO a -> m (IO a)
forall a b. (a -> b) -> a -> b
$
    IORef (Maybe a) -> IO (Maybe a)
forall a. IORef a -> IO a
readIORef IORef (Maybe a)
var IO (Maybe a) -> (Maybe a -> IO a) -> IO a
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
      Just a
x -> a -> IO a
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x
      Maybe a
_      -> IO a
io IO a -> (a -> IO a) -> IO a
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \a
x -> IORef (Maybe a) -> Maybe a -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef' IORef (Maybe a)
var (a -> Maybe a
forall a. a -> Maybe a
Just a
x) IO () -> IO a -> IO a
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> a -> IO a
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x

-------------------------------------------------------------------------------------------------

wrap :: (ToCodeAction a) => a -> GhcideCodeAction
wrap :: forall a. ToCodeAction a => a -> GhcideCodeAction
wrap = a -> GhcideCodeAction
forall a. ToCodeAction a => a -> GhcideCodeAction
toCodeAction

class ToCodeAction a where
  toCodeAction :: a -> GhcideCodeAction

instance ToCodeAction GhcideCodeAction where
  toCodeAction :: GhcideCodeAction -> GhcideCodeAction
toCodeAction = GhcideCodeAction -> GhcideCodeAction
forall a. a -> a
id

instance Semigroup GhcideCodeAction where
  GhcideCodeAction
a <> :: GhcideCodeAction -> GhcideCodeAction -> GhcideCodeAction
<> GhcideCodeAction
b = [GhcideCodeAction] -> GhcideCodeAction
forall a. ToCodeAction a => a -> GhcideCodeAction
toCodeAction [GhcideCodeAction
a, GhcideCodeAction
b]

instance Monoid GhcideCodeAction where
  mempty :: GhcideCodeAction
mempty = GhcideCodeActionResult -> GhcideCodeAction
forall a. a -> ExceptT PluginError (ReaderT CodeActionArgs IO) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []

instance ToCodeAction a => ToCodeAction [a] where
  toCodeAction :: [a] -> GhcideCodeAction
toCodeAction = ([GhcideCodeActionResult] -> GhcideCodeActionResult)
-> ExceptT
     PluginError (ReaderT CodeActionArgs IO) [GhcideCodeActionResult]
-> GhcideCodeAction
forall a b.
(a -> b)
-> ExceptT PluginError (ReaderT CodeActionArgs IO) a
-> ExceptT PluginError (ReaderT CodeActionArgs IO) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [GhcideCodeActionResult] -> GhcideCodeActionResult
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat (ExceptT
   PluginError (ReaderT CodeActionArgs IO) [GhcideCodeActionResult]
 -> GhcideCodeAction)
-> ([a]
    -> ExceptT
         PluginError (ReaderT CodeActionArgs IO) [GhcideCodeActionResult])
-> [a]
-> GhcideCodeAction
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> GhcideCodeAction)
-> [a]
-> ExceptT
     PluginError (ReaderT CodeActionArgs IO) [GhcideCodeActionResult]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM a -> GhcideCodeAction
forall a. ToCodeAction a => a -> GhcideCodeAction
toCodeAction

instance ToCodeAction a => ToCodeAction (Maybe a) where
  toCodeAction :: Maybe a -> GhcideCodeAction
toCodeAction = GhcideCodeAction
-> (a -> GhcideCodeAction) -> Maybe a -> GhcideCodeAction
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (GhcideCodeActionResult -> GhcideCodeAction
forall a. a -> ExceptT PluginError (ReaderT CodeActionArgs IO) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []) a -> GhcideCodeAction
forall a. ToCodeAction a => a -> GhcideCodeAction
toCodeAction

instance ToCodeAction a => ToCodeAction (Either PluginError a) where
  toCodeAction :: Either PluginError a -> GhcideCodeAction
toCodeAction = (PluginError -> GhcideCodeAction)
-> (a -> GhcideCodeAction)
-> Either PluginError a
-> GhcideCodeAction
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (\PluginError
err -> ReaderT
  CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
-> GhcideCodeAction
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (ReaderT
   CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
 -> GhcideCodeAction)
-> ReaderT
     CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
-> GhcideCodeAction
forall a b. (a -> b) -> a -> b
$ (CodeActionArgs -> IO (Either PluginError GhcideCodeActionResult))
-> ReaderT
     CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((CodeActionArgs -> IO (Either PluginError GhcideCodeActionResult))
 -> ReaderT
      CodeActionArgs IO (Either PluginError GhcideCodeActionResult))
-> (CodeActionArgs
    -> IO (Either PluginError GhcideCodeActionResult))
-> ReaderT
     CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
forall a b. (a -> b) -> a -> b
$ \CodeActionArgs
_ -> Either PluginError GhcideCodeActionResult
-> IO (Either PluginError GhcideCodeActionResult)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either PluginError GhcideCodeActionResult
 -> IO (Either PluginError GhcideCodeActionResult))
-> Either PluginError GhcideCodeActionResult
-> IO (Either PluginError GhcideCodeActionResult)
forall a b. (a -> b) -> a -> b
$ PluginError -> Either PluginError GhcideCodeActionResult
forall a b. a -> Either a b
Left PluginError
err) a -> GhcideCodeAction
forall a. ToCodeAction a => a -> GhcideCodeAction
toCodeAction

instance ToTextEdit a => ToCodeAction (CodeActionTitle, a) where
  toCodeAction :: (Text, a) -> GhcideCodeAction
toCodeAction (Text
title, a
te) = ReaderT
  CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
-> GhcideCodeAction
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (ReaderT
   CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
 -> GhcideCodeAction)
-> ReaderT
     CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
-> GhcideCodeAction
forall a b. (a -> b) -> a -> b
$ (CodeActionArgs -> IO (Either PluginError GhcideCodeActionResult))
-> ReaderT
     CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((CodeActionArgs -> IO (Either PluginError GhcideCodeActionResult))
 -> ReaderT
      CodeActionArgs IO (Either PluginError GhcideCodeActionResult))
-> (CodeActionArgs
    -> IO (Either PluginError GhcideCodeActionResult))
-> ReaderT
     CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
forall a b. (a -> b) -> a -> b
$ \CodeActionArgs
caa -> GhcideCodeActionResult -> Either PluginError GhcideCodeActionResult
forall a b. b -> Either a b
Right (GhcideCodeActionResult
 -> Either PluginError GhcideCodeActionResult)
-> ([TextEdit] -> GhcideCodeActionResult)
-> [TextEdit]
-> Either PluginError GhcideCodeActionResult
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text, Maybe CodeActionKind, Maybe Bool, [TextEdit])
-> GhcideCodeActionResult
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((Text, Maybe CodeActionKind, Maybe Bool, [TextEdit])
 -> GhcideCodeActionResult)
-> ([TextEdit]
    -> (Text, Maybe CodeActionKind, Maybe Bool, [TextEdit]))
-> [TextEdit]
-> GhcideCodeActionResult
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text
title,CodeActionKind -> Maybe CodeActionKind
forall a. a -> Maybe a
Just CodeActionKind
CodeActionKind_QuickFix,Maybe Bool
forall a. Maybe a
Nothing,) ([TextEdit] -> Either PluginError GhcideCodeActionResult)
-> IO [TextEdit] -> IO (Either PluginError GhcideCodeActionResult)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CodeActionArgs -> a -> IO [TextEdit]
forall a. ToTextEdit a => CodeActionArgs -> a -> IO [TextEdit]
toTextEdit CodeActionArgs
caa a
te

instance ToTextEdit a => ToCodeAction (CodeActionTitle, CodeActionKind, a) where
  toCodeAction :: (Text, CodeActionKind, a) -> GhcideCodeAction
toCodeAction (Text
title, CodeActionKind
kind, a
te) = ReaderT
  CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
-> GhcideCodeAction
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (ReaderT
   CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
 -> GhcideCodeAction)
-> ReaderT
     CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
-> GhcideCodeAction
forall a b. (a -> b) -> a -> b
$ (CodeActionArgs -> IO (Either PluginError GhcideCodeActionResult))
-> ReaderT
     CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((CodeActionArgs -> IO (Either PluginError GhcideCodeActionResult))
 -> ReaderT
      CodeActionArgs IO (Either PluginError GhcideCodeActionResult))
-> (CodeActionArgs
    -> IO (Either PluginError GhcideCodeActionResult))
-> ReaderT
     CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
forall a b. (a -> b) -> a -> b
$ \CodeActionArgs
caa -> GhcideCodeActionResult -> Either PluginError GhcideCodeActionResult
forall a b. b -> Either a b
Right (GhcideCodeActionResult
 -> Either PluginError GhcideCodeActionResult)
-> ([TextEdit] -> GhcideCodeActionResult)
-> [TextEdit]
-> Either PluginError GhcideCodeActionResult
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text, Maybe CodeActionKind, Maybe Bool, [TextEdit])
-> GhcideCodeActionResult
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((Text, Maybe CodeActionKind, Maybe Bool, [TextEdit])
 -> GhcideCodeActionResult)
-> ([TextEdit]
    -> (Text, Maybe CodeActionKind, Maybe Bool, [TextEdit]))
-> [TextEdit]
-> GhcideCodeActionResult
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text
title,CodeActionKind -> Maybe CodeActionKind
forall a. a -> Maybe a
Just CodeActionKind
kind,Maybe Bool
forall a. Maybe a
Nothing,) ([TextEdit] -> Either PluginError GhcideCodeActionResult)
-> IO [TextEdit] -> IO (Either PluginError GhcideCodeActionResult)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CodeActionArgs -> a -> IO [TextEdit]
forall a. ToTextEdit a => CodeActionArgs -> a -> IO [TextEdit]
toTextEdit CodeActionArgs
caa a
te

instance ToTextEdit a => ToCodeAction (CodeActionTitle, CodeActionPreferred, a) where
  toCodeAction :: (Text, Bool, a) -> GhcideCodeAction
toCodeAction (Text
title, Bool
isPreferred, a
te) = ReaderT
  CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
-> GhcideCodeAction
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (ReaderT
   CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
 -> GhcideCodeAction)
-> ReaderT
     CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
-> GhcideCodeAction
forall a b. (a -> b) -> a -> b
$ (CodeActionArgs -> IO (Either PluginError GhcideCodeActionResult))
-> ReaderT
     CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((CodeActionArgs -> IO (Either PluginError GhcideCodeActionResult))
 -> ReaderT
      CodeActionArgs IO (Either PluginError GhcideCodeActionResult))
-> (CodeActionArgs
    -> IO (Either PluginError GhcideCodeActionResult))
-> ReaderT
     CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
forall a b. (a -> b) -> a -> b
$ \CodeActionArgs
caa -> GhcideCodeActionResult -> Either PluginError GhcideCodeActionResult
forall a b. b -> Either a b
Right (GhcideCodeActionResult
 -> Either PluginError GhcideCodeActionResult)
-> ([TextEdit] -> GhcideCodeActionResult)
-> [TextEdit]
-> Either PluginError GhcideCodeActionResult
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text, Maybe CodeActionKind, Maybe Bool, [TextEdit])
-> GhcideCodeActionResult
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((Text, Maybe CodeActionKind, Maybe Bool, [TextEdit])
 -> GhcideCodeActionResult)
-> ([TextEdit]
    -> (Text, Maybe CodeActionKind, Maybe Bool, [TextEdit]))
-> [TextEdit]
-> GhcideCodeActionResult
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text
title,CodeActionKind -> Maybe CodeActionKind
forall a. a -> Maybe a
Just CodeActionKind
CodeActionKind_QuickFix,Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
isPreferred,) ([TextEdit] -> Either PluginError GhcideCodeActionResult)
-> IO [TextEdit] -> IO (Either PluginError GhcideCodeActionResult)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CodeActionArgs -> a -> IO [TextEdit]
forall a. ToTextEdit a => CodeActionArgs -> a -> IO [TextEdit]
toTextEdit CodeActionArgs
caa a
te

instance ToTextEdit a => ToCodeAction (CodeActionTitle, CodeActionKind, CodeActionPreferred, a) where
  toCodeAction :: (Text, CodeActionKind, Bool, a) -> GhcideCodeAction
toCodeAction (Text
title, CodeActionKind
kind, Bool
isPreferred, a
te) = ReaderT
  CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
-> GhcideCodeAction
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (ReaderT
   CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
 -> GhcideCodeAction)
-> ReaderT
     CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
-> GhcideCodeAction
forall a b. (a -> b) -> a -> b
$ (CodeActionArgs -> IO (Either PluginError GhcideCodeActionResult))
-> ReaderT
     CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((CodeActionArgs -> IO (Either PluginError GhcideCodeActionResult))
 -> ReaderT
      CodeActionArgs IO (Either PluginError GhcideCodeActionResult))
-> (CodeActionArgs
    -> IO (Either PluginError GhcideCodeActionResult))
-> ReaderT
     CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
forall a b. (a -> b) -> a -> b
$ \CodeActionArgs
caa -> GhcideCodeActionResult -> Either PluginError GhcideCodeActionResult
forall a b. b -> Either a b
Right (GhcideCodeActionResult
 -> Either PluginError GhcideCodeActionResult)
-> ([TextEdit] -> GhcideCodeActionResult)
-> [TextEdit]
-> Either PluginError GhcideCodeActionResult
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text, Maybe CodeActionKind, Maybe Bool, [TextEdit])
-> GhcideCodeActionResult
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((Text, Maybe CodeActionKind, Maybe Bool, [TextEdit])
 -> GhcideCodeActionResult)
-> ([TextEdit]
    -> (Text, Maybe CodeActionKind, Maybe Bool, [TextEdit]))
-> [TextEdit]
-> GhcideCodeActionResult
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text
title,CodeActionKind -> Maybe CodeActionKind
forall a. a -> Maybe a
Just CodeActionKind
kind,Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
isPreferred,) ([TextEdit] -> Either PluginError GhcideCodeActionResult)
-> IO [TextEdit] -> IO (Either PluginError GhcideCodeActionResult)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CodeActionArgs -> a -> IO [TextEdit]
forall a. ToTextEdit a => CodeActionArgs -> a -> IO [TextEdit]
toTextEdit CodeActionArgs
caa a
te

-------------------------------------------------------------------------------------------------

toCodeAction1 :: (ToCodeAction r) => (CodeActionArgs -> IO (Maybe a)) -> (Maybe a -> r) -> GhcideCodeAction
toCodeAction1 :: forall r a.
ToCodeAction r =>
(CodeActionArgs -> IO (Maybe a))
-> (Maybe a -> r) -> GhcideCodeAction
toCodeAction1 CodeActionArgs -> IO (Maybe a)
get Maybe a -> r
f = ReaderT
  CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
-> GhcideCodeAction
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (ReaderT
   CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
 -> GhcideCodeAction)
-> ((CodeActionArgs
     -> IO (Either PluginError GhcideCodeActionResult))
    -> ReaderT
         CodeActionArgs IO (Either PluginError GhcideCodeActionResult))
-> (CodeActionArgs
    -> IO (Either PluginError GhcideCodeActionResult))
-> GhcideCodeAction
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CodeActionArgs -> IO (Either PluginError GhcideCodeActionResult))
-> ReaderT
     CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((CodeActionArgs -> IO (Either PluginError GhcideCodeActionResult))
 -> GhcideCodeAction)
-> (CodeActionArgs
    -> IO (Either PluginError GhcideCodeActionResult))
-> GhcideCodeAction
forall a b. (a -> b) -> a -> b
$ \CodeActionArgs
caa -> do
                          Maybe a
caaMay <- CodeActionArgs -> IO (Maybe a)
get CodeActionArgs
caa
                          (ReaderT
   CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
 -> CodeActionArgs
 -> IO (Either PluginError GhcideCodeActionResult))
-> CodeActionArgs
-> ReaderT
     CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
-> IO (Either PluginError GhcideCodeActionResult)
forall a b c. (a -> b -> c) -> b -> a -> c
flip ReaderT
  CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
-> CodeActionArgs -> IO (Either PluginError GhcideCodeActionResult)
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT CodeActionArgs
caa (ReaderT
   CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
 -> IO (Either PluginError GhcideCodeActionResult))
-> (Maybe a
    -> ReaderT
         CodeActionArgs IO (Either PluginError GhcideCodeActionResult))
-> Maybe a
-> IO (Either PluginError GhcideCodeActionResult)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GhcideCodeAction
-> ReaderT
     CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (GhcideCodeAction
 -> ReaderT
      CodeActionArgs IO (Either PluginError GhcideCodeActionResult))
-> (Maybe a -> GhcideCodeAction)
-> Maybe a
-> ReaderT
     CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. r -> GhcideCodeAction
forall a. ToCodeAction a => a -> GhcideCodeAction
toCodeAction (r -> GhcideCodeAction)
-> (Maybe a -> r) -> Maybe a -> GhcideCodeAction
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe a -> r
f (Maybe a -> IO (Either PluginError GhcideCodeActionResult))
-> Maybe a -> IO (Either PluginError GhcideCodeActionResult)
forall a b. (a -> b) -> a -> b
$ Maybe a
caaMay

toCodeAction2 :: (ToCodeAction r) => (CodeActionArgs -> IO (Maybe a)) -> (a -> r) -> GhcideCodeAction
toCodeAction2 :: forall r a.
ToCodeAction r =>
(CodeActionArgs -> IO (Maybe a)) -> (a -> r) -> GhcideCodeAction
toCodeAction2 CodeActionArgs -> IO (Maybe a)
get a -> r
f = ReaderT
  CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
-> GhcideCodeAction
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (ReaderT
   CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
 -> GhcideCodeAction)
-> ((CodeActionArgs
     -> IO (Either PluginError GhcideCodeActionResult))
    -> ReaderT
         CodeActionArgs IO (Either PluginError GhcideCodeActionResult))
-> (CodeActionArgs
    -> IO (Either PluginError GhcideCodeActionResult))
-> GhcideCodeAction
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CodeActionArgs -> IO (Either PluginError GhcideCodeActionResult))
-> ReaderT
     CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((CodeActionArgs -> IO (Either PluginError GhcideCodeActionResult))
 -> GhcideCodeAction)
-> (CodeActionArgs
    -> IO (Either PluginError GhcideCodeActionResult))
-> GhcideCodeAction
forall a b. (a -> b) -> a -> b
$ \CodeActionArgs
caa ->
  CodeActionArgs -> IO (Maybe a)
get CodeActionArgs
caa IO (Maybe a)
-> (Maybe a -> IO (Either PluginError GhcideCodeActionResult))
-> IO (Either PluginError GhcideCodeActionResult)
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Just a
x -> (ReaderT
   CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
 -> CodeActionArgs
 -> IO (Either PluginError GhcideCodeActionResult))
-> CodeActionArgs
-> ReaderT
     CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
-> IO (Either PluginError GhcideCodeActionResult)
forall a b c. (a -> b -> c) -> b -> a -> c
flip ReaderT
  CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
-> CodeActionArgs -> IO (Either PluginError GhcideCodeActionResult)
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT CodeActionArgs
caa (ReaderT
   CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
 -> IO (Either PluginError GhcideCodeActionResult))
-> (a
    -> ReaderT
         CodeActionArgs IO (Either PluginError GhcideCodeActionResult))
-> a
-> IO (Either PluginError GhcideCodeActionResult)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GhcideCodeAction
-> ReaderT
     CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (GhcideCodeAction
 -> ReaderT
      CodeActionArgs IO (Either PluginError GhcideCodeActionResult))
-> (a -> GhcideCodeAction)
-> a
-> ReaderT
     CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. r -> GhcideCodeAction
forall a. ToCodeAction a => a -> GhcideCodeAction
toCodeAction (r -> GhcideCodeAction) -> (a -> r) -> a -> GhcideCodeAction
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> r
f (a -> IO (Either PluginError GhcideCodeActionResult))
-> a -> IO (Either PluginError GhcideCodeActionResult)
forall a b. (a -> b) -> a -> b
$ a
x
    Maybe a
_      -> Either PluginError GhcideCodeActionResult
-> IO (Either PluginError GhcideCodeActionResult)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either PluginError GhcideCodeActionResult
 -> IO (Either PluginError GhcideCodeActionResult))
-> Either PluginError GhcideCodeActionResult
-> IO (Either PluginError GhcideCodeActionResult)
forall a b. (a -> b) -> a -> b
$ GhcideCodeActionResult -> Either PluginError GhcideCodeActionResult
forall a b. b -> Either a b
Right []

toCodeAction3 :: (ToCodeAction r) => (CodeActionArgs -> IO a) -> (a -> r) -> GhcideCodeAction
toCodeAction3 :: forall r a.
ToCodeAction r =>
(CodeActionArgs -> IO a) -> (a -> r) -> GhcideCodeAction
toCodeAction3 CodeActionArgs -> IO a
get a -> r
f = ReaderT
  CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
-> GhcideCodeAction
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (ReaderT
   CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
 -> GhcideCodeAction)
-> ((CodeActionArgs
     -> IO (Either PluginError GhcideCodeActionResult))
    -> ReaderT
         CodeActionArgs IO (Either PluginError GhcideCodeActionResult))
-> (CodeActionArgs
    -> IO (Either PluginError GhcideCodeActionResult))
-> GhcideCodeAction
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CodeActionArgs -> IO (Either PluginError GhcideCodeActionResult))
-> ReaderT
     CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((CodeActionArgs -> IO (Either PluginError GhcideCodeActionResult))
 -> GhcideCodeAction)
-> (CodeActionArgs
    -> IO (Either PluginError GhcideCodeActionResult))
-> GhcideCodeAction
forall a b. (a -> b) -> a -> b
$ \CodeActionArgs
caa -> CodeActionArgs -> IO a
get CodeActionArgs
caa IO a
-> (a -> IO (Either PluginError GhcideCodeActionResult))
-> IO (Either PluginError GhcideCodeActionResult)
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (ReaderT
   CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
 -> CodeActionArgs
 -> IO (Either PluginError GhcideCodeActionResult))
-> CodeActionArgs
-> ReaderT
     CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
-> IO (Either PluginError GhcideCodeActionResult)
forall a b c. (a -> b -> c) -> b -> a -> c
flip ReaderT
  CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
-> CodeActionArgs -> IO (Either PluginError GhcideCodeActionResult)
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT CodeActionArgs
caa (ReaderT
   CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
 -> IO (Either PluginError GhcideCodeActionResult))
-> (a
    -> ReaderT
         CodeActionArgs IO (Either PluginError GhcideCodeActionResult))
-> a
-> IO (Either PluginError GhcideCodeActionResult)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GhcideCodeAction
-> ReaderT
     CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (GhcideCodeAction
 -> ReaderT
      CodeActionArgs IO (Either PluginError GhcideCodeActionResult))
-> (a -> GhcideCodeAction)
-> a
-> ReaderT
     CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. r -> GhcideCodeAction
forall a. ToCodeAction a => a -> GhcideCodeAction
toCodeAction (r -> GhcideCodeAction) -> (a -> r) -> a -> GhcideCodeAction
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> r
f

-- | this instance returns a delta AST, useful for exactprint transforms
instance ToCodeAction r => ToCodeAction (ParsedSource -> r) where
  toCodeAction :: (ParsedSource -> r) -> GhcideCodeAction
toCodeAction = (CodeActionArgs -> IO (Maybe ParsedSource))
-> (ParsedSource -> r) -> GhcideCodeAction
forall r a.
ToCodeAction r =>
(CodeActionArgs -> IO (Maybe a)) -> (a -> r) -> GhcideCodeAction
toCodeAction2 CodeActionArgs -> IO (Maybe ParsedSource)
caaAnnSource

instance ToCodeAction r => ToCodeAction (ExportsMap -> r) where
  toCodeAction :: (ExportsMap -> r) -> GhcideCodeAction
toCodeAction = (CodeActionArgs -> IO ExportsMap)
-> (ExportsMap -> r) -> GhcideCodeAction
forall r a.
ToCodeAction r =>
(CodeActionArgs -> IO a) -> (a -> r) -> GhcideCodeAction
toCodeAction3 CodeActionArgs -> IO ExportsMap
caaExportsMap

instance ToCodeAction r => ToCodeAction (IdeOptions -> r) where
  toCodeAction :: (IdeOptions -> r) -> GhcideCodeAction
toCodeAction = (CodeActionArgs -> IO IdeOptions)
-> (IdeOptions -> r) -> GhcideCodeAction
forall r a.
ToCodeAction r =>
(CodeActionArgs -> IO a) -> (a -> r) -> GhcideCodeAction
toCodeAction3 CodeActionArgs -> IO IdeOptions
caaIdeOptions

instance ToCodeAction r => ToCodeAction (Diagnostic -> r) where
  toCodeAction :: (Diagnostic -> r) -> GhcideCodeAction
toCodeAction Diagnostic -> r
f = ReaderT
  CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
-> GhcideCodeAction
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (ReaderT
   CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
 -> GhcideCodeAction)
-> ((CodeActionArgs
     -> IO (Either PluginError GhcideCodeActionResult))
    -> ReaderT
         CodeActionArgs IO (Either PluginError GhcideCodeActionResult))
-> (CodeActionArgs
    -> IO (Either PluginError GhcideCodeActionResult))
-> GhcideCodeAction
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CodeActionArgs -> IO (Either PluginError GhcideCodeActionResult))
-> ReaderT
     CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((CodeActionArgs -> IO (Either PluginError GhcideCodeActionResult))
 -> GhcideCodeAction)
-> (CodeActionArgs
    -> IO (Either PluginError GhcideCodeActionResult))
-> GhcideCodeAction
forall a b. (a -> b) -> a -> b
$ \caa :: CodeActionArgs
caa@CodeActionArgs {$sel:caaDiagnostic:CodeActionArgs :: CodeActionArgs -> Diagnostic
caaDiagnostic = Diagnostic
x} -> (ReaderT
   CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
 -> CodeActionArgs
 -> IO (Either PluginError GhcideCodeActionResult))
-> CodeActionArgs
-> ReaderT
     CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
-> IO (Either PluginError GhcideCodeActionResult)
forall a b c. (a -> b -> c) -> b -> a -> c
flip ReaderT
  CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
-> CodeActionArgs -> IO (Either PluginError GhcideCodeActionResult)
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT CodeActionArgs
caa (ReaderT
   CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
 -> IO (Either PluginError GhcideCodeActionResult))
-> (r
    -> ReaderT
         CodeActionArgs IO (Either PluginError GhcideCodeActionResult))
-> r
-> IO (Either PluginError GhcideCodeActionResult)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GhcideCodeAction
-> ReaderT
     CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (GhcideCodeAction
 -> ReaderT
      CodeActionArgs IO (Either PluginError GhcideCodeActionResult))
-> (r -> GhcideCodeAction)
-> r
-> ReaderT
     CodeActionArgs IO (Either PluginError GhcideCodeActionResult)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. r -> GhcideCodeAction
forall a. ToCodeAction a => a -> GhcideCodeAction
toCodeAction (r -> IO (Either PluginError GhcideCodeActionResult))
-> r -> IO (Either PluginError GhcideCodeActionResult)
forall a b. (a -> b) -> a -> b
$ Diagnostic -> r
f Diagnostic
x

instance ToCodeAction r => ToCodeAction (Maybe ParsedModule -> r) where
  toCodeAction :: (Maybe ParsedModule -> r) -> GhcideCodeAction
toCodeAction = (CodeActionArgs -> IO (Maybe ParsedModule))
-> (Maybe ParsedModule -> r) -> GhcideCodeAction
forall r a.
ToCodeAction r =>
(CodeActionArgs -> IO (Maybe a))
-> (Maybe a -> r) -> GhcideCodeAction
toCodeAction1 CodeActionArgs -> IO (Maybe ParsedModule)
caaParsedModule

instance ToCodeAction r => ToCodeAction (ParsedModule -> r) where
  toCodeAction :: (ParsedModule -> r) -> GhcideCodeAction
toCodeAction = (CodeActionArgs -> IO (Maybe ParsedModule))
-> (ParsedModule -> r) -> GhcideCodeAction
forall r a.
ToCodeAction r =>
(CodeActionArgs -> IO (Maybe a)) -> (a -> r) -> GhcideCodeAction
toCodeAction2 CodeActionArgs -> IO (Maybe ParsedModule)
caaParsedModule

instance ToCodeAction r => ToCodeAction (Maybe T.Text -> r) where
  toCodeAction :: (Maybe Text -> r) -> GhcideCodeAction
toCodeAction = (CodeActionArgs -> IO (Maybe Text))
-> (Maybe Text -> r) -> GhcideCodeAction
forall r a.
ToCodeAction r =>
(CodeActionArgs -> IO (Maybe a))
-> (Maybe a -> r) -> GhcideCodeAction
toCodeAction1 CodeActionArgs -> IO (Maybe Text)
caaContents

instance ToCodeAction r => ToCodeAction (T.Text -> r) where
  toCodeAction :: (Text -> r) -> GhcideCodeAction
toCodeAction = (CodeActionArgs -> IO (Maybe Text))
-> (Text -> r) -> GhcideCodeAction
forall r a.
ToCodeAction r =>
(CodeActionArgs -> IO (Maybe a)) -> (a -> r) -> GhcideCodeAction
toCodeAction2 CodeActionArgs -> IO (Maybe Text)
caaContents

instance ToCodeAction r => ToCodeAction (Maybe DynFlags -> r) where
  toCodeAction :: (Maybe DynFlags -> r) -> GhcideCodeAction
toCodeAction = (CodeActionArgs -> IO (Maybe DynFlags))
-> (Maybe DynFlags -> r) -> GhcideCodeAction
forall r a.
ToCodeAction r =>
(CodeActionArgs -> IO (Maybe a))
-> (Maybe a -> r) -> GhcideCodeAction
toCodeAction1 CodeActionArgs -> IO (Maybe DynFlags)
caaDf

instance ToCodeAction r => ToCodeAction (DynFlags -> r) where
  toCodeAction :: (DynFlags -> r) -> GhcideCodeAction
toCodeAction = (CodeActionArgs -> IO (Maybe DynFlags))
-> (DynFlags -> r) -> GhcideCodeAction
forall r a.
ToCodeAction r =>
(CodeActionArgs -> IO (Maybe a)) -> (a -> r) -> GhcideCodeAction
toCodeAction2 CodeActionArgs -> IO (Maybe DynFlags)
caaDf

instance ToCodeAction r => ToCodeAction (Maybe ParsedSource -> r) where
  toCodeAction :: (Maybe ParsedSource -> r) -> GhcideCodeAction
toCodeAction = (CodeActionArgs -> IO (Maybe ParsedSource))
-> (Maybe ParsedSource -> r) -> GhcideCodeAction
forall r a.
ToCodeAction r =>
(CodeActionArgs -> IO (Maybe a))
-> (Maybe a -> r) -> GhcideCodeAction
toCodeAction1 CodeActionArgs -> IO (Maybe ParsedSource)
caaAnnSource

instance ToCodeAction r => ToCodeAction (Maybe TcModuleResult -> r) where
  toCodeAction :: (Maybe TcModuleResult -> r) -> GhcideCodeAction
toCodeAction = (CodeActionArgs -> IO (Maybe TcModuleResult))
-> (Maybe TcModuleResult -> r) -> GhcideCodeAction
forall r a.
ToCodeAction r =>
(CodeActionArgs -> IO (Maybe a))
-> (Maybe a -> r) -> GhcideCodeAction
toCodeAction1 CodeActionArgs -> IO (Maybe TcModuleResult)
caaTmr

instance ToCodeAction r => ToCodeAction (TcModuleResult -> r) where
  toCodeAction :: (TcModuleResult -> r) -> GhcideCodeAction
toCodeAction = (CodeActionArgs -> IO (Maybe TcModuleResult))
-> (TcModuleResult -> r) -> GhcideCodeAction
forall r a.
ToCodeAction r =>
(CodeActionArgs -> IO (Maybe a)) -> (a -> r) -> GhcideCodeAction
toCodeAction2 CodeActionArgs -> IO (Maybe TcModuleResult)
caaTmr

instance ToCodeAction r => ToCodeAction (Maybe HieAstResult -> r) where
  toCodeAction :: (Maybe HieAstResult -> r) -> GhcideCodeAction
toCodeAction = (CodeActionArgs -> IO (Maybe HieAstResult))
-> (Maybe HieAstResult -> r) -> GhcideCodeAction
forall r a.
ToCodeAction r =>
(CodeActionArgs -> IO (Maybe a))
-> (Maybe a -> r) -> GhcideCodeAction
toCodeAction1 CodeActionArgs -> IO (Maybe HieAstResult)
caaHar

instance ToCodeAction r => ToCodeAction (HieAstResult -> r) where
  toCodeAction :: (HieAstResult -> r) -> GhcideCodeAction
toCodeAction = (CodeActionArgs -> IO (Maybe HieAstResult))
-> (HieAstResult -> r) -> GhcideCodeAction
forall r a.
ToCodeAction r =>
(CodeActionArgs -> IO (Maybe a)) -> (a -> r) -> GhcideCodeAction
toCodeAction2 CodeActionArgs -> IO (Maybe HieAstResult)
caaHar

instance ToCodeAction r => ToCodeAction (Maybe Bindings -> r) where
  toCodeAction :: (Maybe Bindings -> r) -> GhcideCodeAction
toCodeAction = (CodeActionArgs -> IO (Maybe Bindings))
-> (Maybe Bindings -> r) -> GhcideCodeAction
forall r a.
ToCodeAction r =>
(CodeActionArgs -> IO (Maybe a))
-> (Maybe a -> r) -> GhcideCodeAction
toCodeAction1 CodeActionArgs -> IO (Maybe Bindings)
caaBindings

instance ToCodeAction r => ToCodeAction (Bindings -> r) where
  toCodeAction :: (Bindings -> r) -> GhcideCodeAction
toCodeAction = (CodeActionArgs -> IO (Maybe Bindings))
-> (Bindings -> r) -> GhcideCodeAction
forall r a.
ToCodeAction r =>
(CodeActionArgs -> IO (Maybe a)) -> (a -> r) -> GhcideCodeAction
toCodeAction2 CodeActionArgs -> IO (Maybe Bindings)
caaBindings

instance ToCodeAction r => ToCodeAction (Maybe GlobalBindingTypeSigsResult -> r) where
  toCodeAction :: (Maybe GlobalBindingTypeSigsResult -> r) -> GhcideCodeAction
toCodeAction = (CodeActionArgs -> IO (Maybe GlobalBindingTypeSigsResult))
-> (Maybe GlobalBindingTypeSigsResult -> r) -> GhcideCodeAction
forall r a.
ToCodeAction r =>
(CodeActionArgs -> IO (Maybe a))
-> (Maybe a -> r) -> GhcideCodeAction
toCodeAction1 CodeActionArgs -> IO (Maybe GlobalBindingTypeSigsResult)
caaGblSigs

instance ToCodeAction r => ToCodeAction (GlobalBindingTypeSigsResult -> r) where
  toCodeAction :: (GlobalBindingTypeSigsResult -> r) -> GhcideCodeAction
toCodeAction = (CodeActionArgs -> IO (Maybe GlobalBindingTypeSigsResult))
-> (GlobalBindingTypeSigsResult -> r) -> GhcideCodeAction
forall r a.
ToCodeAction r =>
(CodeActionArgs -> IO (Maybe a)) -> (a -> r) -> GhcideCodeAction
toCodeAction2 CodeActionArgs -> IO (Maybe GlobalBindingTypeSigsResult)
caaGblSigs

instance ToCodeAction r => ToCodeAction (Maybe HscEnvEq -> r) where
  toCodeAction :: (Maybe HscEnvEq -> r) -> GhcideCodeAction
toCodeAction = (CodeActionArgs -> IO (Maybe HscEnvEq))
-> (Maybe HscEnvEq -> r) -> GhcideCodeAction
forall r a.
ToCodeAction r =>
(CodeActionArgs -> IO (Maybe a))
-> (Maybe a -> r) -> GhcideCodeAction
toCodeAction1 CodeActionArgs -> IO (Maybe HscEnvEq)
caaGhcSession

instance ToCodeAction r => ToCodeAction (Maybe HscEnv -> r) where
  toCodeAction :: (Maybe HscEnv -> r) -> GhcideCodeAction
toCodeAction = (CodeActionArgs -> IO (Maybe HscEnv))
-> (Maybe HscEnv -> r) -> GhcideCodeAction
forall r a.
ToCodeAction r =>
(CodeActionArgs -> IO (Maybe a))
-> (Maybe a -> r) -> GhcideCodeAction
toCodeAction1 (((IO (Maybe HscEnvEq) -> IO (Maybe HscEnv))
-> (CodeActionArgs -> IO (Maybe HscEnvEq))
-> CodeActionArgs
-> IO (Maybe HscEnv)
forall a b.
(a -> b) -> (CodeActionArgs -> a) -> CodeActionArgs -> b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap((IO (Maybe HscEnvEq) -> IO (Maybe HscEnv))
 -> (CodeActionArgs -> IO (Maybe HscEnvEq))
 -> CodeActionArgs
 -> IO (Maybe HscEnv))
-> ((HscEnvEq -> HscEnv)
    -> IO (Maybe HscEnvEq) -> IO (Maybe HscEnv))
-> (HscEnvEq -> HscEnv)
-> (CodeActionArgs -> IO (Maybe HscEnvEq))
-> CodeActionArgs
-> IO (Maybe HscEnv)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Maybe HscEnvEq -> Maybe HscEnv)
-> IO (Maybe HscEnvEq) -> IO (Maybe HscEnv)
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap((Maybe HscEnvEq -> Maybe HscEnv)
 -> IO (Maybe HscEnvEq) -> IO (Maybe HscEnv))
-> ((HscEnvEq -> HscEnv) -> Maybe HscEnvEq -> Maybe HscEnv)
-> (HscEnvEq -> HscEnv)
-> IO (Maybe HscEnvEq)
-> IO (Maybe HscEnv)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(HscEnvEq -> HscEnv) -> Maybe HscEnvEq -> Maybe HscEnv
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap) HscEnvEq -> HscEnv
hscEnv CodeActionArgs -> IO (Maybe HscEnvEq)
caaGhcSession)