Skip to content

[feat] allow flushing diagnostics by source and uri #617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lsp/src/Language/LSP/Diagnostics.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Language.LSP.Diagnostics (
StoreItem (..),
partitionBySource,
flushBySource,
flushBySourceAndUri,
updateDiagnostics,
getDiagnosticParamsFor,

Expand Down Expand Up @@ -41,8 +42,10 @@ all prior entries for the Uri.

type DiagnosticStore = HM.HashMap J.NormalizedUri StoreItem

data StoreItem
= StoreItem (Maybe J.Int32) DiagnosticsBySource
data StoreItem = StoreItem
{ documentVersion :: Maybe J.Int32
, diagnostics :: DiagnosticsBySource
}
deriving (Show, Eq)

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

flushBySourceAndUri :: DiagnosticStore -> Maybe Text -> J.NormalizedUri -> DiagnosticStore
flushBySourceAndUri store msource uri = HM.mapWithKey remove store
where
remove k item
| k == uri = item{diagnostics = Map.delete msource $ diagnostics item}
| otherwise = item

-- ---------------------------------------------------------------------

updateDiagnostics ::
Expand Down
1 change: 1 addition & 0 deletions lsp/src/Language/LSP/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ module Language.LSP.Server (
-- * Diagnostics
publishDiagnostics,
flushDiagnosticsBySource,
flushDiagnosticsBySourceAndUri,

-- * Progress
withProgress,
Expand Down
23 changes: 23 additions & 0 deletions lsp/src/Language/LSP/Server/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,29 @@ flushDiagnosticsBySource maxDiagnosticCount msource = join $ stateState resDiagn

-- ---------------------------------------------------------------------

{- | Remove all diagnostics from a particular uri and source, and send the updates to
the client.
-}
flushDiagnosticsBySourceAndUri ::
MonadLsp config m =>
-- | Max number of diagnostics to send
Int ->
Maybe Text ->
NormalizedUri ->
m ()
flushDiagnosticsBySourceAndUri maxDiagnosticCount msource uri = join $ stateState resDiagnostics $ \oldDiags ->
let !newDiags = flushBySourceAndUri oldDiags msource uri
-- Send the updated diagnostics to the client
act = forM_ (HM.keys newDiags) $ \uri' -> do
let mdp = getDiagnosticParamsFor maxDiagnosticCount newDiags uri'
case mdp of
Nothing -> return ()
Just params -> do
sendToClient $ L.fromServerNot $ L.TNotificationMessage "2.0" L.SMethod_TextDocumentPublishDiagnostics params
in (act, newDiags)

-- ---------------------------------------------------------------------

{- | The changes in a workspace edit should be applied from the end of the file
toward the start. Sort them into this order.
-}
Expand Down
Loading