Skip to content

Commit 6eac56f

Browse files
committed
Add action to query active diagnostics for a given Range
Implement 'rangesOverlap' function which checks whether two 'Range's overlap in any way. Implement two new plugin utility functions which allow to conveniently get all currently displayed diagnostics for a given 'Range'.
1 parent 4c9e7a3 commit 6eac56f

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

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

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{-# LANGUAGE GADTs #-}
22
module Development.IDE.Core.PluginUtils
3-
(-- Wrapped Action functions
3+
(-- * Wrapped Action functions
44
runActionE
55
, runActionMT
66
, useE
@@ -9,13 +9,13 @@ module Development.IDE.Core.PluginUtils
99
, usesMT
1010
, useWithStaleE
1111
, useWithStaleMT
12-
-- Wrapped IdeAction functions
12+
-- * Wrapped IdeAction functions
1313
, runIdeActionE
1414
, runIdeActionMT
1515
, useWithStaleFastE
1616
, useWithStaleFastMT
1717
, uriToFilePathE
18-
-- Wrapped PositionMapping functions
18+
-- * Wrapped PositionMapping functions
1919
, toCurrentPositionE
2020
, toCurrentPositionMT
2121
, fromCurrentPositionE
@@ -24,9 +24,13 @@ module Development.IDE.Core.PluginUtils
2424
, toCurrentRangeMT
2525
, fromCurrentRangeE
2626
, fromCurrentRangeMT
27-
-- Formatting handlers
27+
-- * Diagnostics
28+
, activeDiagnosticsInRange
29+
, activeDiagnosticsInRangeMT
30+
-- * Formatting handlers
2831
, mkFormattingHandlers) where
2932

33+
import Control.Concurrent.STM
3034
import Control.Lens ((^.))
3135
import Control.Monad.Error.Class (MonadError (throwError))
3236
import Control.Monad.Extra
@@ -47,14 +51,17 @@ import Development.IDE.Core.Shake (IdeAction, IdeRule,
4751
import qualified Development.IDE.Core.Shake as Shake
4852
import Development.IDE.GHC.Orphans ()
4953
import Development.IDE.Graph hiding (ShakeValue)
54+
import Development.IDE.Types.Diagnostics
5055
import Development.IDE.Types.Location (NormalizedFilePath)
5156
import qualified Development.IDE.Types.Location as Location
5257
import qualified Ide.Logger as Logger
5358
import Ide.Plugin.Error
59+
import Ide.PluginUtils (rangesOverlap)
5460
import Ide.Types
5561
import qualified Language.LSP.Protocol.Lens as LSP
5662
import Language.LSP.Protocol.Message (SMethod (..))
5763
import qualified Language.LSP.Protocol.Types as LSP
64+
import qualified StmContainers.Map as STM
5865

5966
-- ----------------------------------------------------------------------------
6067
-- Action wrappers
@@ -173,6 +180,25 @@ fromCurrentRangeE mapping = maybeToExceptT (PluginInvalidUserState "fromCurrentR
173180
fromCurrentRangeMT :: Monad m => PositionMapping -> LSP.Range -> MaybeT m LSP.Range
174181
fromCurrentRangeMT mapping = MaybeT . pure . fromCurrentRange mapping
175182

183+
-- ----------------------------------------------------------------------------
184+
-- Diagnostics
185+
-- ----------------------------------------------------------------------------
186+
187+
activeDiagnosticsInRangeMT :: MonadIO m => Shake.ShakeExtras -> NormalizedFilePath -> LSP.Range -> MaybeT m [FileDiagnostic]
188+
activeDiagnosticsInRangeMT ide nfp range = do
189+
MaybeT $ liftIO $ atomically $ do
190+
mDiags <- STM.lookup (LSP.normalizedFilePathToUri nfp) (Shake.publishedDiagnostics ide)
191+
case mDiags of
192+
Nothing -> pure Nothing
193+
Just fileDiags -> do
194+
pure $ Just $ filter diagRangeOverlaps fileDiags
195+
where
196+
diagRangeOverlaps = \fileDiag ->
197+
rangesOverlap range (fileDiag ^. fdLspDiagnosticL . LSP.range)
198+
199+
activeDiagnosticsInRange :: MonadIO m => Shake.ShakeExtras -> NormalizedFilePath -> LSP.Range -> m (Maybe [FileDiagnostic])
200+
activeDiagnosticsInRange ide nfp range = runMaybeT (activeDiagnosticsInRangeMT ide nfp range)
201+
176202
-- ----------------------------------------------------------------------------
177203
-- Formatting handlers
178204
-- ----------------------------------------------------------------------------

hls-plugin-api/src/Ide/PluginUtils.hs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ module Ide.PluginUtils
2828
allLspCmdIds',
2929
installSigUsr1Handler,
3030
subRange,
31+
rangesOverlap,
3132
positionInRange,
3233
usePropertyLsp,
3334
-- * Escape
@@ -277,6 +278,21 @@ fullRange s = Range startPos endPos
277278
subRange :: Range -> Range -> Bool
278279
subRange = isSubrangeOf
279280

281+
282+
-- | Check whether the two 'Range's overlap in any way.
283+
--
284+
-- >>> rangesOverlap (mkRange 1 0 1 4) (mkRange 1 2 1 5)
285+
-- True
286+
-- >>> rangesOverlap (mkRange 1 2 1 5) (mkRange 1 0 1 4)
287+
-- True
288+
-- >>> rangesOverlap (mkRange 1 0 1 6) (mkRange 1 2 1 4)
289+
-- True
290+
-- >>> rangesOverlap (mkRange 1 2 1 4) (mkRange 1 0 1 6)
291+
-- True
292+
rangesOverlap :: Range -> Range -> Bool
293+
rangesOverlap r1 r2 =
294+
r1 ^. L.start <= r2 ^. L.end && r2 ^. L.start <= r1 ^. L.end
295+
280296
-- ---------------------------------------------------------------------
281297

282298
allLspCmdIds' :: T.Text -> IdePlugins ideState -> [T.Text]

0 commit comments

Comments
 (0)