Skip to content

Commit 2cd33df

Browse files
committed
Store FileDiagnostic instead of LSP Diagnostic in Shake store
1 parent 2a2bc3d commit 2cd33df

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

ghcide/src/Development/IDE/Core/Shake.hs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@ import qualified Ide.PluginUtils as HLS
159159
import Ide.Types (IdePlugins (IdePlugins),
160160
PluginDescriptor (pluginId),
161161
PluginId)
162-
import Language.LSP.Diagnostics
163162
import qualified Language.LSP.Protocol.Lens as L
164163
import Language.LSP.Protocol.Message
165164
import Language.LSP.Protocol.Types
@@ -290,7 +289,7 @@ data ShakeExtras = ShakeExtras
290289
,state :: Values
291290
,diagnostics :: STMDiagnosticStore
292291
,hiddenDiagnostics :: STMDiagnosticStore
293-
,publishedDiagnostics :: STM.Map NormalizedUri [Diagnostic]
292+
,publishedDiagnostics :: STM.Map NormalizedUri [FileDiagnostic]
294293
-- ^ This represents the set of diagnostics that we have published.
295294
-- Due to debouncing not every change might get published.
296295

@@ -1349,14 +1348,14 @@ updateFileDiagnostics :: MonadIO m
13491348
-> ShakeExtras
13501349
-> [FileDiagnostic] -- ^ current results
13511350
-> m ()
1352-
updateFileDiagnostics recorder fp ver k ShakeExtras{diagnostics, hiddenDiagnostics, publishedDiagnostics, debouncer, lspEnv, ideTesting} current0 =
1351+
updateFileDiagnostics recorder fp ver k ShakeExtras{diagnostics, hiddenDiagnostics, publishedDiagnostics, debouncer, lspEnv, ideTesting} current0 = do
13531352
liftIO $ withTrace ("update diagnostics " <> fromString(fromNormalizedFilePath fp)) $ \ addTag -> do
13541353
addTag "key" (show k)
13551354
let (currentShown, currentHidden) = partition ((== ShowDiag) . fdShouldShowDiagnostic) current
13561355
uri = filePathToUri' fp
13571356
addTagUnsafe :: String -> String -> String -> a -> a
13581357
addTagUnsafe msg t x v = unsafePerformIO(addTag (msg <> t) x) `seq` v
1359-
update :: (forall a. String -> String -> a -> a) -> [Diagnostic] -> STMDiagnosticStore -> STM [Diagnostic]
1358+
update :: (forall a. String -> String -> a -> a) -> [FileDiagnostic] -> STMDiagnosticStore -> STM [FileDiagnostic]
13601359
update addTagUnsafeMethod new store = addTagUnsafeMethod "count" (show $ Prelude.length new) $ setStageDiagnostics addTagUnsafeMethod uri ver (renderKey k) new store
13611360
current = map (modifyFdLspDiagnostic diagsFromRule) current0
13621361
addTag "version" (show ver)
@@ -1365,21 +1364,21 @@ updateFileDiagnostics recorder fp ver k ShakeExtras{diagnostics, hiddenDiagnosti
13651364
-- published. Otherwise, we might never publish certain diagnostics if
13661365
-- an exception strikes between modifyVar but before
13671366
-- publishDiagnosticsNotification.
1368-
newDiags <- liftIO $ atomicallyNamed "diagnostics - update" $ update (addTagUnsafe "shown ") (map fdLspDiagnostic currentShown) diagnostics
1369-
_ <- liftIO $ atomicallyNamed "diagnostics - hidden" $ update (addTagUnsafe "hidden ") (map fdLspDiagnostic currentHidden) hiddenDiagnostics
1367+
newDiags <- liftIO $ atomicallyNamed "diagnostics - update" $ update (addTagUnsafe "shown ") currentShown diagnostics
1368+
_ <- liftIO $ atomicallyNamed "diagnostics - hidden" $ update (addTagUnsafe "hidden ") currentHidden hiddenDiagnostics
13701369
let uri' = filePathToUri' fp
13711370
let delay = if null newDiags then 0.1 else 0
13721371
registerEvent debouncer delay uri' $ withTrace ("report diagnostics " <> fromString (fromNormalizedFilePath fp)) $ \tag -> do
13731372
join $ mask_ $ do
13741373
lastPublish <- atomicallyNamed "diagnostics - publish" $ STM.focus (Focus.lookupWithDefault [] <* Focus.insert newDiags) uri' publishedDiagnostics
13751374
let action = when (lastPublish /= newDiags) $ case lspEnv of
13761375
Nothing -> -- Print an LSP event.
1377-
logWith recorder Info $ LogDiagsDiffButNoLspEnv (map (\lspDiag -> ideErrorFromLspDiag lspDiag fp Nothing) newDiags) -- TODO: Should try to get structured diagnostics plumbed here if possible
1376+
logWith recorder Info $ LogDiagsDiffButNoLspEnv newDiags
13781377
Just env -> LSP.runLspT env $ do
13791378
liftIO $ tag "count" (show $ Prelude.length newDiags)
13801379
liftIO $ tag "key" (show k)
13811380
LSP.sendNotification SMethod_TextDocumentPublishDiagnostics $
1382-
LSP.PublishDiagnosticsParams (fromNormalizedUri uri') (fmap fromIntegral ver) newDiags
1381+
LSP.PublishDiagnosticsParams (fromNormalizedUri uri') (fmap fromIntegral ver) (map fdLspDiagnostic newDiags)
13831382
return action
13841383
where
13851384
diagsFromRule :: Diagnostic -> Diagnostic
@@ -1403,26 +1402,28 @@ actionLogger :: Action (Recorder (WithPriority Log))
14031402
actionLogger = shakeRecorder <$> getShakeExtras
14041403

14051404
--------------------------------------------------------------------------------
1406-
type STMDiagnosticStore = STM.Map NormalizedUri StoreItem
1405+
type STMDiagnosticStore = STM.Map NormalizedUri StoreItem'
1406+
data StoreItem' = StoreItem' (Maybe Int32) FileDiagnosticsBySource
1407+
type FileDiagnosticsBySource = Map.Map (Maybe T.Text) (SL.SortedList FileDiagnostic)
14071408

1408-
getDiagnosticsFromStore :: StoreItem -> [Diagnostic]
1409-
getDiagnosticsFromStore (StoreItem _ diags) = concatMap SL.fromSortedList $ Map.elems diags
1409+
getDiagnosticsFromStore :: StoreItem' -> [FileDiagnostic]
1410+
getDiagnosticsFromStore (StoreItem' _ diags) = concatMap SL.fromSortedList $ Map.elems diags
14101411

14111412
updateSTMDiagnostics ::
14121413
(forall a. String -> String -> a -> a) ->
14131414
STMDiagnosticStore ->
14141415
NormalizedUri ->
14151416
Maybe Int32 ->
1416-
DiagnosticsBySource ->
1417-
STM [LSP.Diagnostic]
1417+
FileDiagnosticsBySource ->
1418+
STM [FileDiagnostic]
14181419
updateSTMDiagnostics addTag store uri mv newDiagsBySource =
14191420
getDiagnosticsFromStore . fromJust <$> STM.focus (Focus.alter update *> Focus.lookup) uri store
14201421
where
1421-
update (Just(StoreItem mvs dbs))
1422+
update (Just(StoreItem' mvs dbs))
14221423
| addTag "previous version" (show mvs) $
14231424
addTag "previous count" (show $ Prelude.length $ filter (not.null) $ Map.elems dbs) False = undefined
1424-
| mvs == mv = Just (StoreItem mv (newDiagsBySource <> dbs))
1425-
update _ = Just (StoreItem mv newDiagsBySource)
1425+
| mvs == mv = Just (StoreItem' mv (newDiagsBySource <> dbs))
1426+
update _ = Just (StoreItem' mv newDiagsBySource)
14261427

14271428
-- | Sets the diagnostics for a file and compilation step
14281429
-- if you want to clear the diagnostics call this with an empty list
@@ -1431,9 +1432,9 @@ setStageDiagnostics
14311432
-> NormalizedUri
14321433
-> Maybe Int32 -- ^ the time that the file these diagnostics originate from was last edited
14331434
-> T.Text
1434-
-> [LSP.Diagnostic]
1435+
-> [FileDiagnostic]
14351436
-> STMDiagnosticStore
1436-
-> STM [LSP.Diagnostic]
1437+
-> STM [FileDiagnostic]
14371438
setStageDiagnostics addTag uri ver stage diags ds = updateSTMDiagnostics addTag ds uri ver updatedDiags
14381439
where
14391440
!updatedDiags = Map.singleton (Just stage) $! SL.toSortedList diags
@@ -1442,7 +1443,7 @@ getAllDiagnostics ::
14421443
STMDiagnosticStore ->
14431444
STM [FileDiagnostic]
14441445
getAllDiagnostics =
1445-
fmap (concatMap (\(k,v) -> map (\diag -> ideErrorFromLspDiag diag (fromUri k) Nothing) $ getDiagnosticsFromStore v)) . ListT.toList . STM.listT -- TODO: Do we need the structured message here?
1446+
fmap (concatMap (\(_,v) -> getDiagnosticsFromStore v)) . ListT.toList . STM.listT
14461447

14471448
updatePositionMapping :: IdeState -> VersionedTextDocumentIdentifier -> [TextDocumentContentChangeEvent] -> STM ()
14481449
updatePositionMapping IdeState{shakeExtras = ShakeExtras{positionMapping}} VersionedTextDocumentIdentifier{..} changes =

0 commit comments

Comments
 (0)