on master help sign in
Separate diagnostics from rule results (#1423)
commit6cba2e57a47cc934242a291881cd02f8aef68e49 authorMoritz Kiefer <moritz.kiefer@purelyfunctional.org> dateTue 28 May 2019, 14:18 (7 years ago) parent1fa783b
4 files changed+132−107
BUILD.bazel+1 src/Development/IDE/State/FileStore.hs−9 src/Development/IDE/State/Shake.hs+54−56 src/Development/IDE/Types/Diagnostics.hs+77−42
BUILD.bazel+1
@@ -21,6 +21,7 @@ depends = [
2121 "mtl",
2222 "pretty",
2323 "safe-exceptions",
24+ "sorted-list",
2425 "shake",
2526 "stm",
2627 "syb",
src/Development/IDE/State/FileStore.hs−9
@@ -42,10 +42,6 @@ newtype GlobalDirtyFiles = GlobalDirtyFiles (Var DirtyFiles)
4242instance IsIdeGlobal GlobalDirtyFiles
4343
4444
45−
46−-- | Get the modification time of a file.
47−type instance RuleResult GetModificationTime = UTCTime
48−
4945-- | Get the contents of a file, either dirty (if the buffer is modified) or from disk.
5046type instance RuleResult GetFileContents = (UTCTime, StringBuffer)
5147
@@ -58,11 +54,6 @@ data GetFileExists = GetFileExists
5854instance Hashable GetFileExists
5955instance NFData GetFileExists
6056
61−data GetModificationTime = GetModificationTime
62− deriving (Eq, Show, Generic)
63−instance Hashable GetModificationTime
64−instance NFData GetModificationTime
65−
6657data GetFileContents = GetFileContents
6758 deriving (Eq, Show, Generic)
6859instance Hashable GetFileContents
src/Development/IDE/State/Shake.hs+54−56
@@ -24,7 +24,7 @@
2424-- useStale.
2525module Development.IDE.State.Shake(
2626 IdeState,
27− IdeRule, IdeResult,
27+ IdeRule, IdeResult, GetModificationTime(..),
2828 shakeOpen, shakeShut,
2929 shakeRun,
3030 shakeProfile,
@@ -55,19 +55,18 @@ import Data.List.Extra
5555import qualified Data.Text as T
5656import Development.IDE.Logger as Logger
5757import 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
5960import Control.Concurrent.Extra
6061import Control.Exception
6162import Control.DeepSeq
6263import System.Time.Extra
6364import Data.Typeable
64−import Data.Tuple.Extra
65−import System.Directory
66−import System.FilePath
65+import System.FilePath hiding (makeRelative)
6766import qualified Development.Shake as Shake
6867import Control.Monad.Extra
69−import qualified Data.Set as Set
7068import Data.Time
69+import GHC.Generics
7170import System.IO.Unsafe
7271import Numeric.Extra
7372
@@ -79,6 +78,7 @@ data ShakeExtras = ShakeExtras
7978 ,logger :: Logger.Handle
8079 ,globals :: Var (Map.HashMap TypeRep Dynamic)
8180 ,state :: Var Values
81+ ,diagnostics :: Var (ProjectDiagnostics Key)
8282 }
8383
8484getShakeExtras :: Action ShakeExtras
@@ -116,12 +116,7 @@ getIdeGlobalState = getIdeGlobalExtras . shakeExtras
116116
117117
118118-- | The state of the all values - nested so you can easily find all errors at a given file.
119−type Values =
120− Map.HashMap FilePath
121− (Map.HashMap Key
122− (IdeResult Dynamic)
123− )
124−
119+type Values = Map.HashMap (FilePath, Key) (Maybe Dynamic)
125120
126121-- | Key type
127122data Key = forall k . (Typeable k, Hashable k, Eq k, Show k) => Key k
@@ -198,13 +193,10 @@ setValues :: IdeRule k v
198193 => Var Values
199194 -> k
200195 -> FilePath
201− -> IdeResult v
202− -> IO (Maybe [FileDiagnostic], [FileDiagnostic]) -- ^ (before, after)
203−setValues state key file val = modifyVar state $ \inVal -> do
204− let k = Key key
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))
196+ -> Maybe v
197+ -> IO ()
198+setValues state key file val = modifyVar_ state $
199+ pure . Map.insert (file, Key key) (fmap toDyn val)
208200
209201-- | The outer Maybe is Nothing if this function hasn't been computed before
210202-- 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
213205getValues state key file = do
214206 vs <- readVar state
215207 return $ do
216− f <- Map.lookup file vs
217− v <- Map.lookup (Key key) f
218− pure $ fmap (fromJust . fromDynamic @v) $ snd v
208+ v <- Map.lookup (file, Key key) vs
209+ pure $ fmap (fromJust . fromDynamic @v) v
219210
220211-- | Open a 'IdeState', should be shut using 'shakeShut'.
221212shakeOpen :: (Event -> IO ()) -- ^ diagnostic handler
@@ -223,8 +214,12 @@ shakeOpen :: (Event -> IO ()) -- ^ diagnostic handler
223214 -> ShakeOptions
224215 -> Rules ()
225216 -> IO IdeState
226−shakeOpen diags shakeLogger opts rules = do
227− shakeExtras <- ShakeExtras diags shakeLogger <$> newVar Map.empty <*> newVar Map.empty
217+shakeOpen eventer logger opts rules = do
218+ shakeExtras <- do
219+ globals <- newVar Map.empty
220+ state <- newVar Map.empty
221+ diagnostics <- newVar emptyDiagnostics
222+ pure ShakeExtras{..}
228223 (shakeDb, shakeClose) <- shakeOpenDatabase opts{shakeExtra = addShakeExtra shakeExtras $ shakeExtra opts} rules
229224 shakeAbort <- newVar $ return ()
230225 shakeDb <- shakeDb
@@ -263,20 +258,22 @@ useStale IdeState{shakeExtras=ShakeExtras{state}} k fp =
263258
264259
265260getAllDiagnostics :: IdeState -> IO [FileDiagnostic]
266−getAllDiagnostics IdeState{shakeExtras = ShakeExtras{state}} = do
267− val <- readVar state
268− return $ concatMap (concatMap fst . Map.elems) $ Map.elems val
261+getAllDiagnostics IdeState{shakeExtras = ShakeExtras{diagnostics}} = do
262+ val <- readVar diagnostics
263+ return $ D.getAllDiagnostics val
269264
270265-- | FIXME: This function is temporary! Only required because the files of interest doesn't work
271266unsafeClearAllDiagnostics :: IdeState -> IO ()
272−unsafeClearAllDiagnostics IdeState{shakeExtras = ShakeExtras{state}} = modifyVar_ state $
273− return . Map.map (Map.map (\(_, x) -> ([], x)))
267+unsafeClearAllDiagnostics IdeState{shakeExtras = ShakeExtras{diagnostics}} =
268+ writeVar diagnostics emptyDiagnostics
274269
275270-- | Clear the results for all files that do not match the given predicate.
276271garbageCollect :: (FilePath -> Bool) -> Action ()
277272garbageCollect keep = do
278− ShakeExtras{state} <- getShakeExtras
279− liftIO $ modifyVar_ state $ return . Map.filterWithKey (\file _ -> keep file)
273+ ShakeExtras{state, diagnostics} <- getShakeExtras
274+ liftIO $
275+ do modifyVar_ state $ return . Map.filterWithKey (\(file, _) _ -> keep file)
276+ modifyVar_ diagnostics $ return . filterDiagnostics keep
280277
281278define
282279 :: IdeRule k v
@@ -354,7 +351,7 @@ defineEarlyCutoff
354351 => (k -> FilePath -> Action (Maybe BS.ByteString, IdeResult v))
355352 -> Rules ()
356353defineEarlyCutoff op = addBuiltinRule noLint noIdentity $ \(Q (key, file)) old mode -> do
357− ShakeExtras{state} <- getShakeExtras
354+ extras@ShakeExtras{state} <- getShakeExtras
358355 val <- case old of
359356 Just old | mode == RunDependenciesSame -> do
360357 v <- liftIO $ getValues state key file
@@ -365,46 +362,39 @@ defineEarlyCutoff op = addBuiltinRule noLint noIdentity $ \(Q (key, file)) old m
365362 case val of
366363 Just res -> return res
367364 Nothing -> do
368− (bs, res) <- actionCatch
365+ (bs, (diags, res)) <- actionCatch
369366 (do v <- op key file; liftIO $ evaluate $ force v) $
370367 \(e :: SomeException) -> pure (Nothing, ([ideErrorText file $ T.pack $ show e | not $ isBadDependency e],Nothing))
371− res <- return $ first (map $ \(_,d) -> (file,d)) res
372368
373− (before, after) <- liftIO $ setValues state key file res
374− updateFileDiagnostics file before after
369+ liftIO $ setValues state key file res
370+ updateFileDiagnostics file (Key key) extras $ map snd diags
375371 let eq = case (bs, fmap unwrap old) of
376372 (Just a, Just (Just b)) -> a == b
377373 _ -> False
378374 return $ RunResult
379375 (if eq then ChangedRecomputeSame else ChangedRecomputeDiff)
380376 (wrap bs)
381− $ A (snd res) bs
377+ $ A res bs
382378 where
383379 wrap = maybe BS.empty (BS.cons '_')
384380 unwrap x = if BS.null x then Nothing else Just $ BS.tail x
385381
386−
387382updateFileDiagnostics ::
388383 FilePath
389− -> Maybe [FileDiagnostic] -- ^ previous results for this file
390− -> [FileDiagnostic] -- ^ current results
384+ -> Key
385+ -> ShakeExtras
386+ -> [Diagnostic] -- ^ current results
391387 -> Action ()
392−updateFileDiagnostics afp previousAll currentAll = do
393− -- TODO (MK) We canonicalize to make sure that the two files agree on use of
394− -- / and \ and other shenanigans.
395− -- Once we have finished the migration to haskell-lsp we should make sure that
396− -- this is no longer necessary.
397− afp' <- liftIO $ canonicalizePath afp
398− let filtM diags = do
399− diags' <-
400− filterM
401− (\x -> fmap (== afp') (canonicalizePath $ fst x))
402− diags
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)
388+updateFileDiagnostics fp k ShakeExtras{diagnostics, state} current = do
389+ (newDiags, oldDiags) <- liftIO $ do
390+ modTime <- join <$> getValues state GetModificationTime fp
391+ modifyVar diagnostics $ \old -> do
392+ let oldDiags = getFileDiagnostics fp old
393+ let newDiagsStore = setStageDiagnostics fp modTime k current old
394+ let newDiags = getFileDiagnostics fp newDiagsStore
395+ pure (newDiagsStore, (newDiags, oldDiags))
396+ when (newDiags /= oldDiags) $
397+ sendEvent $ EventFileDiagnostics (fp, newDiags)
408398
409399
410400setPriority :: (Enum a) => a -> Action ()
@@ -424,3 +414,11 @@ logDebug, logSeriousError
424414 :: IdeState -> T.Text -> IO ()
425415logDebug = sl Logger.logDebug
426416logSeriousError = sl Logger.logSeriousError
417+
418+data GetModificationTime = GetModificationTime
419+ deriving (Eq, Show, Generic)
420+instance Hashable GetModificationTime
421+instance NFData GetModificationTime
422+
423+-- | Get the modification time of a file.
424+type instance RuleResult GetModificationTime = UTCTime
src/Development/IDE/Types/Diagnostics.hs+77−42
@@ -27,12 +27,16 @@ module Development.IDE.Types.Diagnostics (
2727 ideTryIOException,
2828 showDiagnostics,
2929 showDiagnosticsColored,
30− prettyDiagnosticStore,
3130 defDiagnostic,
32− addDiagnostics,
33− filterSeriousErrors,
3431 filePathToUri,
35− getDiagnosticsFromStore
32+ uriToFilePath',
33+ ProjectDiagnostics,
34+ emptyDiagnostics,
35+ setStageDiagnostics,
36+ getAllDiagnostics,
37+ filterDiagnostics,
38+ getFileDiagnostics,
39+ prettyDiagnostics
3640 ) where
3741
3842import Control.Exception
@@ -40,14 +44,17 @@ import Data.Either.Combinators
4044import Data.Maybe as Maybe
4145import Data.Foldable
4246import qualified Data.Map as Map
47+import Data.Time.Clock
48+import Data.Time.Clock.POSIX
4349import qualified Data.Text as T
4450import Data.Text.Prettyprint.Doc.Syntax
51+import qualified Data.SortedList as SL
4552import qualified Text.PrettyPrint.Annotated.HughesPJClass as Pretty
46−import Language.Haskell.LSP.Types as LSP (
53+import qualified Language.Haskell.LSP.Types as LSP
54+import Language.Haskell.LSP.Types as LSP (
4755 DiagnosticSeverity(..)
4856 , Diagnostic(..)
4957 , filePathToUri
50− , uriToFilePath
5158 , List(..)
5259 , DiagnosticRelatedInformation(..)
5360 , Uri(..)
@@ -56,6 +63,15 @@ import Language.Haskell.LSP.Diagnostics
5663
5764import Development.IDE.Types.Location
5865
66+-- | We use an empty string as a filepath when we don’t have a file.
67+-- However, haskell-lsp doesn’t support that in uriToFilePath and given
68+-- that it is not a valid filepath it does not make sense to upstream a fix.
69+-- So we have our own wrapper here that supports empty filepaths.
70+uriToFilePath' :: Uri -> Maybe FilePath
71+uriToFilePath' uri
72+ | uri == filePathToUri "" = Just ""
73+ | otherwise = LSP.uriToFilePath uri
74+
5975ideErrorText :: FilePath -> T.Text -> FileDiagnostic
6076ideErrorText fp = errorDiag fp "Ide Error"
6177
@@ -96,28 +112,6 @@ defDiagnostic _range _message = LSP.Diagnostic {
96112 , _relatedInformation = Nothing
97113 }
98114
99−filterSeriousErrors ::
100− FilePath ->
101− [LSP.Diagnostic] ->
102− [LSP.Diagnostic]
103−filterSeriousErrors fp =
104− filter (maybe False hasSeriousErrors . LSP._relatedInformation)
105− where
106− hasSeriousErrors :: List DiagnosticRelatedInformation -> Bool
107− hasSeriousErrors (List a) = any ((/=) uri . _uri . _location) a
108− uri = LSP.filePathToUri fp
109−
110−addDiagnostics ::
111− FilePath ->
112− [LSP.Diagnostic] ->
113− DiagnosticStore -> DiagnosticStore
114−addDiagnostics fp diags ds =
115− updateDiagnostics
116− ds
117− (LSP.filePathToUri fp)
118− Nothing $
119− partitionBySource diags
120−
121115ideTryIOException :: FilePath -> IO a -> IO (Either FileDiagnostic a)
122116ideTryIOException fp act =
123117 mapLeft (\(e :: IOException) -> ideErrorText fp $ T.pack $ show e) <$> try act
@@ -167,20 +161,61 @@ prettyDiagnostic (fp, LSP.Diagnostic{..}) =
167161 where
168162 sev = fromMaybe LSP.DsError _severity
169163
170−prettyDiagnosticStore :: DiagnosticStore -> Doc SyntaxClass
171−prettyDiagnosticStore ds =
172− vcat $
173− map (\(uri, diags) -> prettyFileDiagnostics (fromMaybe noFilePath $ uriToFilePath uri, diags)) $
174− Map.assocs $
175− Map.map getDiagnosticsFromStore ds
176−
177−prettyFileDiagnostics :: FileDiagnostics -> Doc SyntaxClass
178−prettyFileDiagnostics (filePath, diags) =
179− slabel_ "Compiler error in" $ vcat
180− [ slabel_ "File:" $ pretty filePath
181− , slabel_ "Errors:" $ vcat $ map (prettyDiagnostic . (filePath,)) diags
182− ]
183−
184164getDiagnosticsFromStore :: StoreItem -> [Diagnostic]
185165getDiagnosticsFromStore (StoreItem _ diags) =
186166 toList =<< Map.elems diags
167+
168+-- | This represents every diagnostic in a LSP project, the stage type variable is
169+-- the type of the compiler stages, in this project that is always the Key data
170+-- type found in Development.IDE.State.Shake
171+newtype ProjectDiagnostics stage = ProjectDiagnostics {getStore :: DiagnosticStore}
172+ deriving Show
173+
174+emptyDiagnostics :: ProjectDiagnostics stage
175+emptyDiagnostics = ProjectDiagnostics mempty
176+
177+-- | Sets the diagnostics for a file and compilation step
178+-- if you want to clear the diagnostics call this with an empty list
179+setStageDiagnostics ::
180+ Show stage =>
181+ FilePath ->
182+ Maybe UTCTime ->
183+ -- ^ the time that the file these diagnostics originate from was last edited
184+ stage ->
185+ [LSP.Diagnostic] ->
186+ ProjectDiagnostics stage ->
187+ ProjectDiagnostics stage
188+setStageDiagnostics fp timeM stage diags (ProjectDiagnostics ds) =
189+ ProjectDiagnostics $ updateDiagnostics ds uri posixTime diagsBySource
190+ where
191+ diagsBySource = Map.singleton (Just $ T.pack $ show stage) (SL.toSortedList diags)
192+ posixTime :: Maybe Int
193+ posixTime = fmap (fromEnum . utcTimeToPOSIXSeconds) timeM
194+ uri = filePathToUri fp
195+
196+fromUri :: LSP.Uri -> FilePath
197+fromUri = fromMaybe noFilePath . uriToFilePath'
198+
199+getAllDiagnostics ::
200+ ProjectDiagnostics stage ->
201+ [FileDiagnostic]
202+getAllDiagnostics =
203+ concatMap (\(k,v) -> map (fromUri k,) $ getDiagnosticsFromStore v) . Map.toList . getStore
204+
205+getFileDiagnostics ::
206+ FilePath ->
207+ ProjectDiagnostics stage ->
208+ [LSP.Diagnostic]
209+getFileDiagnostics fp ds =
210+ maybe [] getDiagnosticsFromStore $
211+ Map.lookup (filePathToUri fp) $
212+ getStore ds
213+
214+filterDiagnostics ::
215+ (FilePath -> Bool) ->
216+ ProjectDiagnostics stage ->
217+ ProjectDiagnostics stage
218+filterDiagnostics keep =
219+ ProjectDiagnostics .
220+ Map.filterWithKey (\uri _ -> maybe True keep $ uriToFilePath' uri) .
221+ getStore