on a410abc help sign in
127 lines 4.1 kB history raw
1-- Copyright (c) 2019 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
2-- SPDX-License-Identifier: Apache-2.0
4{-# LANGUAGE TypeFamilies #-}
5{-# LANGUAGE OverloadedStrings #-}
6{-# LANGUAGE FlexibleInstances #-}
8-- | A Shake implementation of the compiler service, built
9-- using the "Shaker" abstraction layer for in-memory use.
10--
11module Development.IDE.State.Service(
12 Env(..),
13 getServiceEnv,
14 IdeState, initialise, shutdown,
15 runAction, runActions,
16 setFilesOfInterest,
17 writeProfile,
18 getDiagnostics, unsafeClearDiagnostics,
19 logDebug, logInfo, logWarning, logError
20 ) where
22import Control.Concurrent.Extra
23import Control.Monad.Except
24import Development.IDE.Functions.Compile (CompileOpts(..))
25import Development.IDE.State.FileStore
26import qualified Development.IDE.Logger as Logger
27import Data.Maybe
28import Data.Set (Set)
29import qualified Data.Set as Set
30import qualified Data.Text as T
31import Development.IDE.Functions.GHCError
32import Development.Shake hiding (Diagnostic, Env, newCache)
33import Development.IDE.Types.LSP as Compiler
35import "ghc-lib-parser" UniqSupply
37import Development.IDE.State.Shake
40-- | Environment threaded through the Shake actions.
41data Env = Env
42 { envOptions :: CompileOpts
43 -- ^ Compiler options.
44 , envOfInterestVar :: Var (Set FilePath)
45 -- ^ The files of interest.
46 , envUniqSupplyVar :: Var UniqSupply
47 -- ^ The unique supply of names used by the compiler.
48 }
49instance IsIdeGlobal Env
52mkEnv :: CompileOpts -> IO Env
53mkEnv options = do
54 ofInterestVar <- newVar Set.empty
55 uniqSupplyVar <- mkSplitUniqSupply 'a' >>= newVar
56 return Env
57 { envOptions = options
58 , envOfInterestVar = ofInterestVar
59 , envUniqSupplyVar = uniqSupplyVar
60 }
62getDiagnostics :: IdeState -> IO [Diagnostic]
63getDiagnostics = getAllDiagnostics
65unsafeClearDiagnostics :: IdeState -> IO ()
66unsafeClearDiagnostics = unsafeClearAllDiagnostics
69------------------------------------------------------------
70-- Exposed API
72-- | Initialise the Compiler Service.
73initialise :: Rules ()
74 -> Maybe (Event -> IO ())
75 -> Logger.Handle IO
76 -> CompileOpts
77 -> IO IdeState
78initialise mainRule toDiags logger options =
79 shakeOpen
80 (fromMaybe (const $ pure ()) toDiags)
81 logger
82 (setProfiling options $
83 shakeOptions { shakeThreads = optThreads options
84 , shakeFiles = "/dev/null"
85 }) $ do
86 addIdeGlobal =<< liftIO (mkEnv options)
87 fileStoreRules
88 mainRule
90writeProfile :: IdeState -> FilePath -> IO ()
91writeProfile = shakeProfile
93setProfiling :: CompileOpts -> ShakeOptions -> ShakeOptions
94setProfiling opts shakeOpts =
95 maybe shakeOpts (\p -> shakeOpts { shakeReport = [p], shakeTimings = True }) (optShakeProfiling opts)
97-- | Shutdown the Compiler Service.
98shutdown :: IdeState -> IO ()
99shutdown = shakeShut
101-- | Run a single action using the supplied service.
102runAction :: IdeState -> Action a -> IO a
103runAction service action = head <$> runActions service [action]
105-- | Run a list of actions in parallel using the supplied service.
106runActions :: IdeState -> [Action a] -> IO [a]
107runActions x = join . shakeRun x
110-- | Set the files-of-interest which will be built and kept-up-to-date.
111setFilesOfInterest :: IdeState -> Set FilePath -> IO ()
112setFilesOfInterest state files = do
113 Env{..} <- getIdeGlobalState state
114 -- update vars synchronously
115 modifyVar_ envOfInterestVar $ const $ return files
117 -- run shake to update results regarding the files of interest
118 void $ shakeRun state []
120getServiceEnv :: Action Env
121getServiceEnv = getIdeGlobalAction
123logDebug, logInfo, logWarning, logError :: IdeState -> T.Text -> IO ()
124logDebug = shakeLogDebug
125logInfo = shakeLogInfo
126logWarning = shakeLogWarning
127logError = shakeLogError