Skip to content

Commit 8ff131c

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

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

lsp/src/Language/LSP/Diagnostics.hs

Lines changed: 12 additions & 2 deletions
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

@@ -41,8 +42,10 @@ all prior entries for the Uri.
4142

4243
type DiagnosticStore = HM.HashMap J.NormalizedUri StoreItem
4344

44-
data StoreItem
45-
= StoreItem (Maybe J.Int32) DiagnosticsBySource
45+
data StoreItem = StoreItem
46+
{ documentVersion :: Maybe J.Int32
47+
, diagnostics :: DiagnosticsBySource
48+
}
4649
deriving (Show, Eq)
4750

4851
type DiagnosticsBySource = Map.Map (Maybe Text) (SL.SortedList J.Diagnostic)
@@ -60,6 +63,13 @@ flushBySource store (Just source) = HM.map remove store
6063
where
6164
remove (StoreItem mv diags) = StoreItem mv (Map.delete (Just source) diags)
6265

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

6575
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)