Skip to content

Commit a67c7ea

Browse files
committed
[feat] allow flushing diagnostics by source and uri
1 parent bd0217c commit a67c7ea

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

lsp/src/Language/LSP/Diagnostics.hs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module Language.LSP.Diagnostics (
1111
StoreItem (..),
1212
partitionBySource,
1313
flushBySource,
14+
flushBySourceAndUri,
1415
updateDiagnostics,
1516
getDiagnosticParamsFor,
1617

@@ -42,7 +43,10 @@ all prior entries for the Uri.
4243
type DiagnosticStore = HM.HashMap J.NormalizedUri StoreItem
4344

4445
data StoreItem
45-
= StoreItem (Maybe J.Int32) DiagnosticsBySource
46+
= StoreItem
47+
{ documentVersion :: Maybe J.Int32
48+
, diagnostics :: DiagnosticsBySource
49+
}
4650
deriving (Show, Eq)
4751

4852
type DiagnosticsBySource = Map.Map (Maybe Text) (SL.SortedList J.Diagnostic)
@@ -60,6 +64,13 @@ flushBySource store (Just source) = HM.map remove store
6064
where
6165
remove (StoreItem mv diags) = StoreItem mv (Map.delete (Just source) diags)
6266

67+
flushBySourceAndUri :: DiagnosticStore -> Maybe Text -> J.NormalizedUri -> DiagnosticStore
68+
flushBySourceAndUri store msource uri = HM.mapWithKey remove store
69+
where
70+
remove k item
71+
| k == uri = item {diagnostics = Map.delete msource $ diagnostics item}
72+
| otherwise = item
73+
6374
-- ---------------------------------------------------------------------
6475

6576
updateDiagnostics ::

lsp/src/Language/LSP/Server/Core.hs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,29 @@ flushDiagnosticsBySource maxDiagnosticCount msource = join $ stateState resDiagn
655655

656656
-- ---------------------------------------------------------------------
657657

658+
{- | Remove all diagnostics from a particular uri and source, and send the updates to
659+
the client.
660+
-}
661+
flushDiagnosticsBySourceAndUri ::
662+
MonadLsp config m =>
663+
-- | Max number of diagnostics to send
664+
Int ->
665+
Maybe Text ->
666+
NormalizedUri ->
667+
m ()
668+
flushDiagnosticsBySourceAndUri maxDiagnosticCount msource uri = join $ stateState resDiagnostics $ \oldDiags ->
669+
let !newDiags = flushBySourceAndUri oldDiags msource uri
670+
-- Send the updated diagnostics to the client
671+
act = forM_ (HM.keys newDiags) $ \uri' -> do
672+
let mdp = getDiagnosticParamsFor maxDiagnosticCount newDiags uri'
673+
case mdp of
674+
Nothing -> return ()
675+
Just params -> do
676+
sendToClient $ L.fromServerNot $ L.TNotificationMessage "2.0" L.SMethod_TextDocumentPublishDiagnostics params
677+
in (act, newDiags)
678+
679+
-- ---------------------------------------------------------------------
680+
658681
{- | The changes in a workspace edit should be applied from the end of the file
659682
toward the start. Sort them into this order.
660683
-}

0 commit comments

Comments
 (0)