@@ -24,7 +24,7 @@
24-- useStale.
24-- useStale.
25module Development.IDE.State.Shake(
25module Development.IDE.State.Shake(
27− IdeRule, IdeResult,
27+ IdeRule, IdeResult, GetModificationTime(..),
28 shakeOpen, shakeShut,
28 shakeOpen, shakeShut,
30 shakeProfile,
30 shakeProfile,
@@ -55,19 +55,18 @@ import Data.List.Extra
55import qualified Data.Text as T
55import qualified Data.Text as T
56import Development.IDE.Logger as Logger
56import Development.IDE.Logger as Logger
57import Development.IDE.Types.LSP
57import Development.IDE.Types.LSP
58−import Development.IDE.Types.Diagnostics
58+import Development.IDE.Types.Diagnostics hiding (getAllDiagnostics)
59+import qualified Development.IDE.Types.Diagnostics as D
59import Control.Concurrent.Extra
60import Control.Concurrent.Extra
60import Control.Exception
61import Control.Exception
61import Control.DeepSeq
62import Control.DeepSeq
62import System.Time.Extra
63import System.Time.Extra
63import Data.Typeable
64import Data.Typeable
64−import Data.Tuple.Extra
65−import System.Directory
66−import System.FilePath
65+import System.FilePath hiding (makeRelative)
67import qualified Development.Shake as Shake
66import qualified Development.Shake as Shake
68import Control.Monad.Extra
67import Control.Monad.Extra
69−import qualified Data.Set as Set
70import Data.Time
68import Data.Time
71import System.IO.Unsafe
70import System.IO.Unsafe
72import Numeric.Extra
71import Numeric.Extra
@@ -79,6 +78,7 @@ data ShakeExtras = ShakeExtras
79 ,logger :: Logger.Handle
78 ,logger :: Logger.Handle
80 ,globals :: Var (Map.HashMap TypeRep Dynamic)
79 ,globals :: Var (Map.HashMap TypeRep Dynamic)
81 ,state :: Var Values
80 ,state :: Var Values
81+ ,diagnostics :: Var (ProjectDiagnostics Key)
84getShakeExtras :: Action ShakeExtras
84getShakeExtras :: Action ShakeExtras
@@ -116,12 +116,7 @@ getIdeGlobalState = getIdeGlobalExtras . shakeExtras
118-- | The state of the all values - nested so you can easily find all errors at a given file.
118-- | The state of the all values - nested so you can easily find all errors at a given file.
120− Map.HashMap FilePath
119+type Values = Map.HashMap (FilePath, Key) (Maybe Dynamic)
126-- | Key type
121-- | Key type
127data Key = forall k . (Typeable k, Hashable k, Eq k, Show k) => Key k
122data Key = forall k . (Typeable k, Hashable k, Eq k, Show k) => Key k
@@ -198,13 +193,10 @@ setValues :: IdeRule k v
198 => Var Values
193 => Var Values
200 -> FilePath
195 -> FilePath
201− -> IdeResult v
196+ -> Maybe v
202− -> IO (Maybe [FileDiagnostic], [FileDiagnostic]) -- ^ (before, after)
197+ -> IO ()
203−setValues state key file val = modifyVar state $ \inVal -> do
198+setValues state key file val = modifyVar_ state $
204− let k = Key key
199+ pure . Map.insert (file, Key key) (fmap toDyn val)
205− outVal = Map.insertWith Map.union file (Map.singleton k $ second (fmap toDyn) val) inVal
206− f = concatMap fst . Map.elems
207− return (outVal, (f <$> Map.lookup file inVal, f $ outVal Map.! file))
209-- | The outer Maybe is Nothing if this function hasn't been computed before
201-- | The outer Maybe is Nothing if this function hasn't been computed before
210-- the inner Maybe is Nothing if the result of the previous computation failed to produce
202-- the inner Maybe is Nothing if the result of the previous computation failed to produce
@@ -213,9 +205,8 @@ getValues :: forall k v. IdeRule k v => Var Values -> k -> FilePath -> IO (Maybe
213getValues state key file = do
205getValues state key file = do
214 vs <- readVar state
206 vs <- readVar state
215 return $ do
207 return $ do
216− f <- Map.lookup file vs
208+ v <- Map.lookup (file, Key key) vs
217− v <- Map.lookup (Key key) f
218− pure $ fmap (fromJust . fromDynamic @v) $ snd v
209+ pure $ fmap (fromJust . fromDynamic @v) v
220-- | Open a 'IdeState', should be shut using 'shakeShut'.
211-- | Open a 'IdeState', should be shut using 'shakeShut'.
221shakeOpen :: (Event -> IO ()) -- ^ diagnostic handler
212shakeOpen :: (Event -> IO ()) -- ^ diagnostic handler
@@ -223,8 +214,12 @@ shakeOpen :: (Event -> IO ()) -- ^ diagnostic handler
223 -> ShakeOptions
214 -> ShakeOptions
224 -> Rules ()
215 -> Rules ()
225 -> IO IdeState
216 -> IO IdeState
226−shakeOpen diags shakeLogger opts rules = do
217+shakeOpen eventer logger opts rules = do
227− shakeExtras <- ShakeExtras diags shakeLogger <$> newVar Map.empty <*> newVar Map.empty
218+ shakeExtras <- do
219+ globals <- newVar Map.empty
220+ state <- newVar Map.empty
221+ diagnostics <- newVar emptyDiagnostics
222+ pure ShakeExtras{..}
228 (shakeDb, shakeClose) <- shakeOpenDatabase opts{shakeExtra = addShakeExtra shakeExtras $ shakeExtra opts} rules
223 (shakeDb, shakeClose) <- shakeOpenDatabase opts{shakeExtra = addShakeExtra shakeExtras $ shakeExtra opts} rules
229 shakeAbort <- newVar $ return ()
224 shakeAbort <- newVar $ return ()
230 shakeDb <- shakeDb
225 shakeDb <- shakeDb
@@ -263,20 +258,22 @@ useStale IdeState{shakeExtras=ShakeExtras{state}} k fp =
265getAllDiagnostics :: IdeState -> IO [FileDiagnostic]
260getAllDiagnostics :: IdeState -> IO [FileDiagnostic]
266−getAllDiagnostics IdeState{shakeExtras = ShakeExtras{state}} = do
261+getAllDiagnostics IdeState{shakeExtras = ShakeExtras{diagnostics}} = do
267− val <- readVar state
262+ val <- readVar diagnostics
268− return $ concatMap (concatMap fst . Map.elems) $ Map.elems val
263+ return $ D.getAllDiagnostics val
270-- | FIXME: This function is temporary! Only required because the files of interest doesn't work
265-- | FIXME: This function is temporary! Only required because the files of interest doesn't work
271unsafeClearAllDiagnostics :: IdeState -> IO ()
266unsafeClearAllDiagnostics :: IdeState -> IO ()
272−unsafeClearAllDiagnostics IdeState{shakeExtras = ShakeExtras{state}} = modifyVar_ state $
267+unsafeClearAllDiagnostics IdeState{shakeExtras = ShakeExtras{diagnostics}} =
273− return . Map.map (Map.map (\(_, x) -> ([], x)))
268+ writeVar diagnostics emptyDiagnostics
275-- | Clear the results for all files that do not match the given predicate.
270-- | Clear the results for all files that do not match the given predicate.
276garbageCollect :: (FilePath -> Bool) -> Action ()
271garbageCollect :: (FilePath -> Bool) -> Action ()
277garbageCollect keep = do
272garbageCollect keep = do
278− ShakeExtras{state} <- getShakeExtras
273+ ShakeExtras{state, diagnostics} <- getShakeExtras
279− liftIO $ modifyVar_ state $ return . Map.filterWithKey (\file _ -> keep file)
275+ do modifyVar_ state $ return . Map.filterWithKey (\(file, _) _ -> keep file)
276+ modifyVar_ diagnostics $ return . filterDiagnostics keep
282 :: IdeRule k v
279 :: IdeRule k v
@@ -354,7 +351,7 @@ defineEarlyCutoff
354 => (k -> FilePath -> Action (Maybe BS.ByteString, IdeResult v))
351 => (k -> FilePath -> Action (Maybe BS.ByteString, IdeResult v))
355 -> Rules ()
352 -> Rules ()
356defineEarlyCutoff op = addBuiltinRule noLint noIdentity $ \(Q (key, file)) old mode -> do
353defineEarlyCutoff op = addBuiltinRule noLint noIdentity $ \(Q (key, file)) old mode -> do
357− ShakeExtras{state} <- getShakeExtras
354+ extras@ShakeExtras{state} <- getShakeExtras
358 val <- case old of
355 val <- case old of
359 Just old | mode == RunDependenciesSame -> do
356 Just old | mode == RunDependenciesSame -> do
360 v <- liftIO $ getValues state key file
357 v <- liftIO $ getValues state key file
@@ -365,46 +362,39 @@ defineEarlyCutoff op = addBuiltinRule noLint noIdentity $ \(Q (key, file)) old m
365 case val of
362 case val of
366 Just res -> return res
363 Just res -> return res
367 Nothing -> do
364 Nothing -> do
368− (bs, res) <- actionCatch
365+ (bs, (diags, res)) <- actionCatch
369 (do v <- op key file; liftIO $ evaluate $ force v) $
366 (do v <- op key file; liftIO $ evaluate $ force v) $
370 \(e :: SomeException) -> pure (Nothing, ([ideErrorText file $ T.pack $ show e | not $ isBadDependency e],Nothing))
367 \(e :: SomeException) -> pure (Nothing, ([ideErrorText file $ T.pack $ show e | not $ isBadDependency e],Nothing))
371− res <- return $ first (map $ \(_,d) -> (file,d)) res
373− (before, after) <- liftIO $ setValues state key file res
369+ liftIO $ setValues state key file res
374− updateFileDiagnostics file before after
370+ updateFileDiagnostics file (Key key) extras $ map snd diags
375 let eq = case (bs, fmap unwrap old) of
371 let eq = case (bs, fmap unwrap old) of
376 (Just a, Just (Just b)) -> a == b
372 (Just a, Just (Just b)) -> a == b
377 _ -> False
373 _ -> False
378 return $ RunResult
374 return $ RunResult
379 (if eq then ChangedRecomputeSame else ChangedRecomputeDiff)
375 (if eq then ChangedRecomputeSame else ChangedRecomputeDiff)
380 (wrap bs)
376 (wrap bs)
381− $ A (snd res) bs
377+ $ A res bs
383 wrap = maybe BS.empty (BS.cons '_')
379 wrap = maybe BS.empty (BS.cons '_')
384 unwrap x = if BS.null x then Nothing else Just $ BS.tail x
380 unwrap x = if BS.null x then Nothing else Just $ BS.tail x
387updateFileDiagnostics ::
382updateFileDiagnostics ::
389− -> Maybe [FileDiagnostic] -- ^ previous results for this file
384+ -> Key
390− -> [FileDiagnostic] -- ^ current results
386+ -> [Diagnostic] -- ^ current results
391 -> Action ()
387 -> Action ()
392−updateFileDiagnostics afp previousAll currentAll = do
388+updateFileDiagnostics fp k ShakeExtras{diagnostics, state} current = do
393− -- TODO (MK) We canonicalize to make sure that the two files agree on use of
389+ (newDiags, oldDiags) <- liftIO $ do
394− -- / and \ and other shenanigans.
390+ modTime <- join <$> getValues state GetModificationTime fp
395− -- Once we have finished the migration to haskell-lsp we should make sure that
391+ modifyVar diagnostics $ \old -> do
396− -- this is no longer necessary.
392+ let oldDiags = getFileDiagnostics fp old
397− afp' <- liftIO $ canonicalizePath afp
393+ let newDiagsStore = setStageDiagnostics fp modTime k current old
398− let filtM diags = do
394+ let newDiags = getFileDiagnostics fp newDiagsStore
399− diags' <-
395+ pure (newDiagsStore, (newDiags, oldDiags))
400− filterM
396+ when (newDiags /= oldDiags) $
401− (\x -> fmap (== afp') (canonicalizePath $ fst x))
403− pure (Set.fromList diags')
404− previous <- liftIO $ traverse filtM previousAll
405− current <- liftIO $ filtM currentAll
406− when (Just current /= previous) $
407− sendEvent $ EventFileDiagnostics $ (afp, map snd $ Set.toList current)
397+ sendEvent $ EventFileDiagnostics (fp, newDiags)
410setPriority :: (Enum a) => a -> Action ()
400setPriority :: (Enum a) => a -> Action ()
@@ -424,3 +414,11 @@ logDebug, logSeriousError
424 :: IdeState -> T.Text -> IO ()
414 :: IdeState -> T.Text -> IO ()
425logDebug = sl Logger.logDebug
415logDebug = sl Logger.logDebug
426logSeriousError = sl Logger.logSeriousError
416logSeriousError = sl Logger.logSeriousError
418+data GetModificationTime = GetModificationTime
419+ deriving (Eq, Show, Generic)
420+instance Hashable GetModificationTime
421+instance NFData GetModificationTime
423+-- | Get the modification time of a file.
424+type instance RuleResult GetModificationTime = UTCTime