-- Copyright (c) 2019 The DAML Authors. All rights reserved.
-- SPDX-License-Identifier: Apache-2.0

{-# LANGUAGE TypeFamilies               #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE RankNTypes #-}

-- | A Shake implementation of the compiler service, built
--   using the "Shaker" abstraction layer for in-memory use.
--
module Development.IDE.Core.Service(
    getIdeOptions, getIdeOptionsIO,
    IdeState, initialise, shutdown,
    runAction,
    writeProfile,
    getDiagnostics,
    ideLogger,
    updatePositionMapping,
    ) where

import Development.IDE.Types.Options (IdeOptions(..))
import Development.IDE.Core.Debouncer
import           Development.IDE.Core.FileStore  (fileStoreRules)
import           Development.IDE.Core.FileExists (fileExistsRules)
import           Development.IDE.Core.OfInterest
import Development.IDE.Types.Logger as Logger
import           Development.Shake
import qualified Language.LSP.Server as LSP
import qualified Language.LSP.Types as LSP
import           Ide.Plugin.Config

import           Development.IDE.Core.Shake
import Control.Monad


------------------------------------------------------------
-- Exposed API

-- | Initialise the Compiler Service.
initialise :: Rules ()
           -> Maybe (LSP.LanguageContextEnv Config)
           -> Logger
           -> Debouncer LSP.NormalizedUri
           -> IdeOptions
           -> VFSHandle
           -> HieDb
           -> IndexQueue
           -> IO IdeState
initialise :: Rules ()
-> Maybe (LanguageContextEnv Config)
-> Logger
-> Debouncer NormalizedUri
-> IdeOptions
-> VFSHandle
-> HieDb
-> IndexQueue
-> IO IdeState
initialise Rules ()
mainRule Maybe (LanguageContextEnv Config)
lspEnv Logger
logger Debouncer NormalizedUri
debouncer IdeOptions
options VFSHandle
vfs HieDb
hiedb IndexQueue
hiedbChan =
    Maybe (LanguageContextEnv Config)
-> Logger
-> Debouncer NormalizedUri
-> Maybe FilePath
-> IdeReportProgress
-> IdeTesting
-> HieDb
-> IndexQueue
-> VFSHandle
-> ShakeOptions
-> Rules ()
-> IO IdeState
shakeOpen
        Maybe (LanguageContextEnv Config)
lspEnv
        Logger
logger
        Debouncer NormalizedUri
debouncer
        (IdeOptions -> Maybe FilePath
optShakeProfiling IdeOptions
options)
        (IdeOptions -> IdeReportProgress
optReportProgress IdeOptions
options)
        (IdeOptions -> IdeTesting
optTesting IdeOptions
options)
        HieDb
hiedb
        IndexQueue
hiedbChan
        VFSHandle
vfs
        (IdeOptions -> ShakeOptions
optShakeOptions IdeOptions
options)
          (Rules () -> IO IdeState) -> Rules () -> IO IdeState
forall a b. (a -> b) -> a -> b
$ do
            GlobalIdeOptions -> Rules ()
forall a. IsIdeGlobal a => a -> Rules ()
addIdeGlobal (GlobalIdeOptions -> Rules ()) -> GlobalIdeOptions -> Rules ()
forall a b. (a -> b) -> a -> b
$ IdeOptions -> GlobalIdeOptions
GlobalIdeOptions IdeOptions
options
            VFSHandle -> Rules ()
fileStoreRules VFSHandle
vfs
            Rules ()
ofInterestRules
            Maybe (LanguageContextEnv Config) -> VFSHandle -> Rules ()
forall c. Maybe (LanguageContextEnv c) -> VFSHandle -> Rules ()
fileExistsRules Maybe (LanguageContextEnv Config)
lspEnv VFSHandle
vfs
            Rules ()
mainRule

writeProfile :: IdeState -> FilePath -> IO ()
writeProfile :: IdeState -> FilePath -> IO ()
writeProfile = IdeState -> FilePath -> IO ()
shakeProfile

-- | Shutdown the Compiler Service.
shutdown :: IdeState -> IO ()
shutdown :: IdeState -> IO ()
shutdown = IdeState -> IO ()
shakeShut

-- This will return as soon as the result of the action is
-- available.  There might still be other rules running at this point,
-- e.g., the ofInterestRule.
runAction :: String -> IdeState -> Action a -> IO a
runAction :: FilePath -> IdeState -> Action a -> IO a
runAction FilePath
herald IdeState
ide Action a
act =
  IO (IO a) -> IO a
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (IO (IO a) -> IO a) -> IO (IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ ShakeExtras -> DelayedAction a -> IO (IO a)
forall a. ShakeExtras -> DelayedAction a -> IO (IO a)
shakeEnqueue (IdeState -> ShakeExtras
shakeExtras IdeState
ide) (FilePath -> Priority -> Action a -> DelayedAction a
forall a. FilePath -> Priority -> Action a -> DelayedAction a
mkDelayedAction FilePath
herald Priority
Logger.Info Action a
act)