on cb7d95e help sign in
124 lines 4.2 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, modifyFilesOfInterest,
17 writeProfile,
18 getDiagnostics, unsafeClearDiagnostics,
19 logDebug, logSeriousError
20 ) where
22import Control.Concurrent.Extra
23import Control.Monad.Except
24import Development.IDE.Types.Options (IdeOptions(..))
25import Development.IDE.State.FileStore
26import qualified Development.IDE.Logger as Logger
27import Data.Set (Set)
28import qualified Data.Set as Set
29import qualified Data.Text as T
30import Data.Tuple.Extra
31import Development.IDE.Functions.GHCError
32import Development.IDE.Types.Diagnostics (NormalizedFilePath)
33import Development.Shake hiding (Diagnostic, Env, newCache)
34import qualified Language.Haskell.LSP.Messages as LSP
36import UniqSupply
38import Development.IDE.State.Shake
41-- | Environment threaded through the Shake actions.
42data Env = Env
43 { envOptions :: IdeOptions
44 -- ^ Compiler options.
45 , envOfInterestVar :: Var (Set NormalizedFilePath)
46 -- ^ The files of interest.
47 , envUniqSupplyVar :: Var UniqSupply
48 -- ^ The unique supply of names used by the compiler.
49 }
50instance IsIdeGlobal Env
53mkEnv :: IdeOptions -> IO Env
54mkEnv options = do
55 ofInterestVar <- newVar Set.empty
56 uniqSupplyVar <- mkSplitUniqSupply 'a' >>= newVar
57 return Env
58 { envOptions = options
59 , envOfInterestVar = ofInterestVar
60 , envUniqSupplyVar = uniqSupplyVar
61 }
63getDiagnostics :: IdeState -> IO [FileDiagnostic]
64getDiagnostics = getAllDiagnostics
66unsafeClearDiagnostics :: IdeState -> IO ()
67unsafeClearDiagnostics = unsafeClearAllDiagnostics
70------------------------------------------------------------
71-- Exposed API
73-- | Initialise the Compiler Service.
74initialise :: Rules ()
75 -> (LSP.FromServerMessage -> IO ())
76 -> Logger.Handle
77 -> IdeOptions
78 -> VFSHandle
79 -> IO IdeState
80initialise mainRule toDiags logger options vfs =
81 shakeOpen
82 toDiags
83 logger
84 (setProfiling options $
85 shakeOptions { shakeThreads = optThreads options
86 , shakeFiles = "/dev/null"
87 }) $ do
88 addIdeGlobal =<< liftIO (mkEnv options)
89 fileStoreRules vfs
90 mainRule
92writeProfile :: IdeState -> FilePath -> IO ()
93writeProfile = shakeProfile
95setProfiling :: IdeOptions -> ShakeOptions -> ShakeOptions
96setProfiling opts shakeOpts =
97 maybe shakeOpts (\p -> shakeOpts { shakeReport = [p], shakeTimings = True }) (optShakeProfiling opts)
99-- | Shutdown the Compiler Service.
100shutdown :: IdeState -> IO ()
101shutdown = shakeShut
103-- | Run a single action using the supplied service.
104runAction :: IdeState -> Action a -> IO a
105runAction service action = head <$> runActions service [action]
107-- | Run a list of actions in parallel using the supplied service.
108runActions :: IdeState -> [Action a] -> IO [a]
109runActions x = join . shakeRun x
112-- | Set the files-of-interest which will be built and kept-up-to-date.
113setFilesOfInterest :: IdeState -> Set NormalizedFilePath -> IO ()
114setFilesOfInterest state files = modifyFilesOfInterest state (const files)
116modifyFilesOfInterest :: IdeState -> (Set NormalizedFilePath -> Set NormalizedFilePath) -> IO ()
117modifyFilesOfInterest state f = do
118 Env{..} <- getIdeGlobalState state
119 files <- modifyVar envOfInterestVar $ pure . dupe . f
120 logDebug state $ "Set files of interest to: " <> T.pack (show $ Set.toList files)
121 void $ shakeRun state []
123getServiceEnv :: Action Env
124getServiceEnv = getIdeGlobalAction