{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE OverloadedStrings     #-}
{-# OPTIONS_GHC -Wwarn #-}
{-# LANGUAGE LambdaCase            #-}

{- |
Eval Plugin entry point.
-}
module Ide.Plugin.Eval (
    descriptor,
    Log(..)
    ) where

import           Development.IDE               (IdeState)
import           Ide.Logger                    (Pretty (pretty), Recorder,
                                                WithPriority, cmapWithPrio)
import qualified Ide.Plugin.Eval.CodeLens      as CL
import           Ide.Plugin.Eval.Config
import           Ide.Plugin.Eval.Rules         (rules)
import qualified Ide.Plugin.Eval.Rules         as EvalRules
import           Ide.Types                     (ConfigDescriptor (..),
                                                PluginDescriptor (..), PluginId,
                                                defaultConfigDescriptor,
                                                defaultPluginDescriptor,
                                                mkCustomConfig, mkPluginHandler)
import           Language.LSP.Protocol.Message

newtype Log = LogEvalRules EvalRules.Log deriving Int -> Log -> ShowS
[Log] -> ShowS
Log -> String
(Int -> Log -> ShowS)
-> (Log -> String) -> ([Log] -> ShowS) -> Show Log
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Log -> ShowS
showsPrec :: Int -> Log -> ShowS
$cshow :: Log -> String
show :: Log -> String
$cshowList :: [Log] -> ShowS
showList :: [Log] -> ShowS
Show

instance Pretty Log where
  pretty :: forall ann. Log -> Doc ann
pretty = \case
    LogEvalRules Log
log -> Log -> Doc ann
forall a ann. Pretty a => a -> Doc ann
forall ann. Log -> Doc ann
pretty Log
log

-- |Plugin descriptor
descriptor :: Recorder (WithPriority Log) -> PluginId -> PluginDescriptor IdeState
descriptor :: Recorder (WithPriority Log)
-> PluginId -> PluginDescriptor IdeState
descriptor Recorder (WithPriority Log)
recorder PluginId
plId =
    (PluginId -> Text -> PluginDescriptor IdeState
forall ideState. PluginId -> Text -> PluginDescriptor ideState
defaultPluginDescriptor PluginId
plId Text
"Provies a code lens to evaluate expressions in doctest comments")
        { pluginHandlers = mkPluginHandler SMethod_TextDocumentCodeLens CL.codeLens
        , pluginCommands = [CL.evalCommand plId]
        , pluginRules = rules (cmapWithPrio LogEvalRules recorder)
        , pluginConfigDescriptor = defaultConfigDescriptor
                                   { configCustomConfig = mkCustomConfig properties
                                   }
        }