{-# LANGUAGE GADTs #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Ide.Types
    (
      IdePlugins(..)
    , PluginDescriptor(..)
    , defaultPluginDescriptor
    , PluginCommand(..)
    , PluginId(..)
    , CommandId(..)
    , DiagnosticProvider(..)
    , DiagnosticProviderFunc(..)
    , SymbolsProvider
    , FormattingType(..)
    , FormattingProvider
    , HoverProvider
    , CodeActionProvider
    , CodeLensProvider
    , CommandFunction
    , ExecuteCommandProvider
    , CompletionProvider
    , RenameProvider
    , WithSnippets(..)
    ) where

import           Data.Aeson                    hiding (defaultOptions)
import qualified Data.Map  as Map
import qualified Data.Set                      as S
import           Data.String
import qualified Data.Text                     as T
import           Development.IDE
import           Ide.Plugin.Config
import qualified Language.Haskell.LSP.Core as LSP
import           Language.Haskell.LSP.Types
import           Text.Regex.TDFA.Text()

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


newtype IdePlugins = IdePlugins
  { IdePlugins -> Map PluginId PluginDescriptor
ipMap :: Map.Map PluginId PluginDescriptor
  }

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


data PluginDescriptor =
  PluginDescriptor { PluginDescriptor -> PluginId
pluginId                 :: !PluginId
                   , PluginDescriptor -> Rules ()
pluginRules              :: !(Rules ())
                   , PluginDescriptor -> [PluginCommand]
pluginCommands           :: ![PluginCommand]
                   , PluginDescriptor -> Maybe CodeActionProvider
pluginCodeActionProvider :: !(Maybe CodeActionProvider)
                   , PluginDescriptor -> Maybe CodeLensProvider
pluginCodeLensProvider   :: !(Maybe CodeLensProvider)
                   , PluginDescriptor -> Maybe DiagnosticProvider
pluginDiagnosticProvider :: !(Maybe DiagnosticProvider)
                     -- ^ TODO: diagnostics are generally provided via rules,

                     -- this is probably redundant.

                   , PluginDescriptor -> Maybe HoverProvider
pluginHoverProvider      :: !(Maybe HoverProvider)
                   , PluginDescriptor -> Maybe SymbolsProvider
pluginSymbolsProvider    :: !(Maybe SymbolsProvider)
                   , PluginDescriptor -> Maybe (FormattingProvider IO)
pluginFormattingProvider :: !(Maybe (FormattingProvider IO))
                   , PluginDescriptor -> Maybe CompletionProvider
pluginCompletionProvider :: !(Maybe CompletionProvider)
                   , PluginDescriptor -> Maybe RenameProvider
pluginRenameProvider     :: !(Maybe RenameProvider)
                   }

defaultPluginDescriptor :: PluginId -> PluginDescriptor
defaultPluginDescriptor :: PluginId -> PluginDescriptor
defaultPluginDescriptor PluginId
plId =
  PluginId
-> Rules ()
-> [PluginCommand]
-> Maybe CodeActionProvider
-> Maybe CodeLensProvider
-> Maybe DiagnosticProvider
-> Maybe HoverProvider
-> Maybe SymbolsProvider
-> Maybe (FormattingProvider IO)
-> Maybe CompletionProvider
-> Maybe RenameProvider
-> PluginDescriptor
PluginDescriptor
    PluginId
plId
    Rules ()
forall a. Monoid a => a
mempty
    [PluginCommand]
forall a. Monoid a => a
mempty
    Maybe CodeActionProvider
forall a. Maybe a
Nothing
    Maybe CodeLensProvider
forall a. Maybe a
Nothing
    Maybe DiagnosticProvider
forall a. Maybe a
Nothing
    Maybe HoverProvider
forall a. Maybe a
Nothing
    Maybe SymbolsProvider
forall a. Maybe a
Nothing
    Maybe (FormattingProvider IO)
forall a. Maybe a
Nothing
    Maybe CompletionProvider
forall a. Maybe a
Nothing
    Maybe RenameProvider
forall a. Maybe a
Nothing

-- instance Show PluginCommand where

--   show (PluginCommand i _ _) = "PluginCommand { name = " ++ show i ++ " }"


-- newtype CommandId = CommandId T.Text

--   deriving (Show, Read, Eq, Ord)

-- instance IsString CommandId where

--   fromString = CommandId . T.pack


-- data PluginCommand = forall a b. (FromJSON a, ToJSON b, Typeable b) =>

--   PluginCommand { commandId   :: CommandId

--                 , commandDesc :: T.Text

--                 , commandFunc :: a -> IO (Either ResponseError b)

--                 }


newtype CommandId = CommandId T.Text
  deriving (Int -> CommandId -> ShowS
[CommandId] -> ShowS
CommandId -> String
(Int -> CommandId -> ShowS)
-> (CommandId -> String)
-> ([CommandId] -> ShowS)
-> Show CommandId
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CommandId] -> ShowS
$cshowList :: [CommandId] -> ShowS
show :: CommandId -> String
$cshow :: CommandId -> String
showsPrec :: Int -> CommandId -> ShowS
$cshowsPrec :: Int -> CommandId -> ShowS
Show, ReadPrec [CommandId]
ReadPrec CommandId
Int -> ReadS CommandId
ReadS [CommandId]
(Int -> ReadS CommandId)
-> ReadS [CommandId]
-> ReadPrec CommandId
-> ReadPrec [CommandId]
-> Read CommandId
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [CommandId]
$creadListPrec :: ReadPrec [CommandId]
readPrec :: ReadPrec CommandId
$creadPrec :: ReadPrec CommandId
readList :: ReadS [CommandId]
$creadList :: ReadS [CommandId]
readsPrec :: Int -> ReadS CommandId
$creadsPrec :: Int -> ReadS CommandId
Read, CommandId -> CommandId -> Bool
(CommandId -> CommandId -> Bool)
-> (CommandId -> CommandId -> Bool) -> Eq CommandId
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CommandId -> CommandId -> Bool
$c/= :: CommandId -> CommandId -> Bool
== :: CommandId -> CommandId -> Bool
$c== :: CommandId -> CommandId -> Bool
Eq, Eq CommandId
Eq CommandId
-> (CommandId -> CommandId -> Ordering)
-> (CommandId -> CommandId -> Bool)
-> (CommandId -> CommandId -> Bool)
-> (CommandId -> CommandId -> Bool)
-> (CommandId -> CommandId -> Bool)
-> (CommandId -> CommandId -> CommandId)
-> (CommandId -> CommandId -> CommandId)
-> Ord CommandId
CommandId -> CommandId -> Bool
CommandId -> CommandId -> Ordering
CommandId -> CommandId -> CommandId
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: CommandId -> CommandId -> CommandId
$cmin :: CommandId -> CommandId -> CommandId
max :: CommandId -> CommandId -> CommandId
$cmax :: CommandId -> CommandId -> CommandId
>= :: CommandId -> CommandId -> Bool
$c>= :: CommandId -> CommandId -> Bool
> :: CommandId -> CommandId -> Bool
$c> :: CommandId -> CommandId -> Bool
<= :: CommandId -> CommandId -> Bool
$c<= :: CommandId -> CommandId -> Bool
< :: CommandId -> CommandId -> Bool
$c< :: CommandId -> CommandId -> Bool
compare :: CommandId -> CommandId -> Ordering
$ccompare :: CommandId -> CommandId -> Ordering
$cp1Ord :: Eq CommandId
Ord)
instance IsString CommandId where
  fromString :: String -> CommandId
fromString = Text -> CommandId
CommandId (Text -> CommandId) -> (String -> Text) -> String -> CommandId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack

data PluginCommand = forall a. (FromJSON a) =>
  PluginCommand { PluginCommand -> CommandId
commandId   :: CommandId
                , PluginCommand -> Text
commandDesc :: T.Text
                , ()
commandFunc :: CommandFunction a
                }

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


type CommandFunction a = LSP.LspFuncs Config
                       -> IdeState
                       -> a
                       -> IO (Either ResponseError Value, Maybe (ServerMethod, ApplyWorkspaceEditParams))

type CodeActionProvider = LSP.LspFuncs Config
                        -> IdeState
                        -> PluginId
                        -> TextDocumentIdentifier
                        -> Range
                        -> CodeActionContext
                        -> IO (Either ResponseError (List CAResult))

type CompletionProvider = LSP.LspFuncs Config
                        -> IdeState
                        -> CompletionParams
                        -> IO (Either ResponseError CompletionResponseResult)



type CodeLensProvider = LSP.LspFuncs Config
                      -> IdeState
                      -> PluginId
                      -> CodeLensParams
                      -> IO (Either ResponseError (List CodeLens))

type RenameProvider = LSP.LspFuncs Config
                    -> IdeState
                    -> RenameParams
                    -> IO (Either ResponseError WorkspaceEdit)

type DiagnosticProviderFuncSync
  = DiagnosticTrigger -> Uri
  -> IO (Either ResponseError (Map.Map Uri (S.Set Diagnostic)))

type DiagnosticProviderFuncAsync
  = DiagnosticTrigger -> Uri
  -> (Map.Map Uri (S.Set Diagnostic) -> IO ())
  -> IO (Either ResponseError ())

data DiagnosticProviderFunc
  = DiagnosticProviderSync  DiagnosticProviderFuncSync
  | DiagnosticProviderAsync DiagnosticProviderFuncAsync


data DiagnosticProvider = DiagnosticProvider
     { DiagnosticProvider -> Set DiagnosticTrigger
dpTrigger :: S.Set DiagnosticTrigger -- AZ:should this be a NonEmptyList?

     , DiagnosticProvider -> DiagnosticProviderFunc
dpFunc    :: DiagnosticProviderFunc
     }

data DiagnosticTrigger = DiagnosticOnOpen
                       | DiagnosticOnChange
                       | DiagnosticOnSave
                       deriving (Int -> DiagnosticTrigger -> ShowS
[DiagnosticTrigger] -> ShowS
DiagnosticTrigger -> String
(Int -> DiagnosticTrigger -> ShowS)
-> (DiagnosticTrigger -> String)
-> ([DiagnosticTrigger] -> ShowS)
-> Show DiagnosticTrigger
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [DiagnosticTrigger] -> ShowS
$cshowList :: [DiagnosticTrigger] -> ShowS
show :: DiagnosticTrigger -> String
$cshow :: DiagnosticTrigger -> String
showsPrec :: Int -> DiagnosticTrigger -> ShowS
$cshowsPrec :: Int -> DiagnosticTrigger -> ShowS
Show,Eq DiagnosticTrigger
Eq DiagnosticTrigger
-> (DiagnosticTrigger -> DiagnosticTrigger -> Ordering)
-> (DiagnosticTrigger -> DiagnosticTrigger -> Bool)
-> (DiagnosticTrigger -> DiagnosticTrigger -> Bool)
-> (DiagnosticTrigger -> DiagnosticTrigger -> Bool)
-> (DiagnosticTrigger -> DiagnosticTrigger -> Bool)
-> (DiagnosticTrigger -> DiagnosticTrigger -> DiagnosticTrigger)
-> (DiagnosticTrigger -> DiagnosticTrigger -> DiagnosticTrigger)
-> Ord DiagnosticTrigger
DiagnosticTrigger -> DiagnosticTrigger -> Bool
DiagnosticTrigger -> DiagnosticTrigger -> Ordering
DiagnosticTrigger -> DiagnosticTrigger -> DiagnosticTrigger
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: DiagnosticTrigger -> DiagnosticTrigger -> DiagnosticTrigger
$cmin :: DiagnosticTrigger -> DiagnosticTrigger -> DiagnosticTrigger
max :: DiagnosticTrigger -> DiagnosticTrigger -> DiagnosticTrigger
$cmax :: DiagnosticTrigger -> DiagnosticTrigger -> DiagnosticTrigger
>= :: DiagnosticTrigger -> DiagnosticTrigger -> Bool
$c>= :: DiagnosticTrigger -> DiagnosticTrigger -> Bool
> :: DiagnosticTrigger -> DiagnosticTrigger -> Bool
$c> :: DiagnosticTrigger -> DiagnosticTrigger -> Bool
<= :: DiagnosticTrigger -> DiagnosticTrigger -> Bool
$c<= :: DiagnosticTrigger -> DiagnosticTrigger -> Bool
< :: DiagnosticTrigger -> DiagnosticTrigger -> Bool
$c< :: DiagnosticTrigger -> DiagnosticTrigger -> Bool
compare :: DiagnosticTrigger -> DiagnosticTrigger -> Ordering
$ccompare :: DiagnosticTrigger -> DiagnosticTrigger -> Ordering
$cp1Ord :: Eq DiagnosticTrigger
Ord,DiagnosticTrigger -> DiagnosticTrigger -> Bool
(DiagnosticTrigger -> DiagnosticTrigger -> Bool)
-> (DiagnosticTrigger -> DiagnosticTrigger -> Bool)
-> Eq DiagnosticTrigger
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: DiagnosticTrigger -> DiagnosticTrigger -> Bool
$c/= :: DiagnosticTrigger -> DiagnosticTrigger -> Bool
== :: DiagnosticTrigger -> DiagnosticTrigger -> Bool
$c== :: DiagnosticTrigger -> DiagnosticTrigger -> Bool
Eq)

-- type HoverProvider = Uri -> Position -> IO (Either ResponseError [Hover])

type HoverProvider = IdeState -> TextDocumentPositionParams -> IO (Either ResponseError (Maybe Hover))

type SymbolsProvider = LSP.LspFuncs Config
                     -> IdeState
                     -> DocumentSymbolParams
                     -> IO (Either ResponseError [DocumentSymbol])

type ExecuteCommandProvider = IdeState
                            -> ExecuteCommandParams
                            -> IO (Either ResponseError Value, Maybe (ServerMethod, ApplyWorkspaceEditParams))

newtype WithSnippets = WithSnippets Bool

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


newtype PluginId = PluginId T.Text
  deriving (Int -> PluginId -> ShowS
[PluginId] -> ShowS
PluginId -> String
(Int -> PluginId -> ShowS)
-> (PluginId -> String) -> ([PluginId] -> ShowS) -> Show PluginId
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PluginId] -> ShowS
$cshowList :: [PluginId] -> ShowS
show :: PluginId -> String
$cshow :: PluginId -> String
showsPrec :: Int -> PluginId -> ShowS
$cshowsPrec :: Int -> PluginId -> ShowS
Show, ReadPrec [PluginId]
ReadPrec PluginId
Int -> ReadS PluginId
ReadS [PluginId]
(Int -> ReadS PluginId)
-> ReadS [PluginId]
-> ReadPrec PluginId
-> ReadPrec [PluginId]
-> Read PluginId
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [PluginId]
$creadListPrec :: ReadPrec [PluginId]
readPrec :: ReadPrec PluginId
$creadPrec :: ReadPrec PluginId
readList :: ReadS [PluginId]
$creadList :: ReadS [PluginId]
readsPrec :: Int -> ReadS PluginId
$creadsPrec :: Int -> ReadS PluginId
Read, PluginId -> PluginId -> Bool
(PluginId -> PluginId -> Bool)
-> (PluginId -> PluginId -> Bool) -> Eq PluginId
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PluginId -> PluginId -> Bool
$c/= :: PluginId -> PluginId -> Bool
== :: PluginId -> PluginId -> Bool
$c== :: PluginId -> PluginId -> Bool
Eq, Eq PluginId
Eq PluginId
-> (PluginId -> PluginId -> Ordering)
-> (PluginId -> PluginId -> Bool)
-> (PluginId -> PluginId -> Bool)
-> (PluginId -> PluginId -> Bool)
-> (PluginId -> PluginId -> Bool)
-> (PluginId -> PluginId -> PluginId)
-> (PluginId -> PluginId -> PluginId)
-> Ord PluginId
PluginId -> PluginId -> Bool
PluginId -> PluginId -> Ordering
PluginId -> PluginId -> PluginId
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: PluginId -> PluginId -> PluginId
$cmin :: PluginId -> PluginId -> PluginId
max :: PluginId -> PluginId -> PluginId
$cmax :: PluginId -> PluginId -> PluginId
>= :: PluginId -> PluginId -> Bool
$c>= :: PluginId -> PluginId -> Bool
> :: PluginId -> PluginId -> Bool
$c> :: PluginId -> PluginId -> Bool
<= :: PluginId -> PluginId -> Bool
$c<= :: PluginId -> PluginId -> Bool
< :: PluginId -> PluginId -> Bool
$c< :: PluginId -> PluginId -> Bool
compare :: PluginId -> PluginId -> Ordering
$ccompare :: PluginId -> PluginId -> Ordering
$cp1Ord :: Eq PluginId
Ord)
instance IsString PluginId where
  fromString :: String -> PluginId
fromString = Text -> PluginId
PluginId (Text -> PluginId) -> (String -> Text) -> String -> PluginId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack

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



-- | Format the given Text as a whole or only a @Range@ of it.

-- Range must be relative to the text to format.

-- To format the whole document, read the Text from the file and use 'FormatText'

-- as the FormattingType.

data FormattingType = FormatText
                    | FormatRange Range


-- | To format a whole document, the 'FormatText' @FormattingType@ can be used.

-- It is required to pass in the whole Document Text for that to happen, an empty text

-- and file uri, does not suffice.

type FormattingProvider m
        = LSP.LspFuncs Config
        -> IdeState
        -> FormattingType  -- ^ How much to format

        -> T.Text -- ^ Text to format

        -> NormalizedFilePath -- ^ location of the file being formatted

        -> FormattingOptions -- ^ Options for the formatter

        -> m (Either ResponseError (List TextEdit)) -- ^ Result of the formatting


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