Skip to content

Commit f90b002

Browse files
aufarglukel97
authored andcommitted
Limit diagnostics by range in getCodeActions
1 parent 19d2882 commit f90b002

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

lsp-test/src/Language/LSP/Test.hs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
{-# LANGUAGE TypeInType #-}
88
{-# LANGUAGE ScopedTypeVariables #-}
99
{-# LANGUAGE ExistentialQuantification #-}
10+
{-# LANGUAGE DuplicateRecordFields #-}
1011

1112
{-|
1213
Module : Language.LSP.Test
@@ -501,7 +502,7 @@ getDocumentSymbols doc = do
501502
-- | Returns the code actions in the specified range.
502503
getCodeActions :: TextDocumentIdentifier -> Range -> Session [Command |? CodeAction]
503504
getCodeActions doc range = do
504-
ctx <- getCodeActionContext doc
505+
ctx <- getCodeActionContextInRange doc range
505506
rsp <- request STextDocumentCodeAction (CodeActionParams Nothing Nothing doc range ctx)
506507

507508
case rsp ^. result of
@@ -526,6 +527,26 @@ getAllCodeActions doc = do
526527
Left e -> throw (UnexpectedResponseError (SomeLspId $ fromJust rspLid) e)
527528
Right (List cmdOrCAs) -> pure (acc ++ cmdOrCAs)
528529

530+
getCodeActionContextInRange :: TextDocumentIdentifier -> Range -> Session CodeActionContext
531+
getCodeActionContextInRange doc caRange = do
532+
curDiags <- getCurrentDiagnostics doc
533+
let diags = [ d | d@Diagnostic{_range=range} <- curDiags
534+
, overlappingRange caRange range
535+
]
536+
return $ CodeActionContext (List diags) Nothing
537+
where
538+
overlappingRange :: Range -> Range -> Bool
539+
overlappingRange (Range s e) range =
540+
positionInRange s range
541+
|| positionInRange e range
542+
543+
positionInRange :: Position -> Range -> Bool
544+
positionInRange (Position pl po) (Range (Position sl so) (Position el eo)) =
545+
pl > sl && pl < el
546+
|| pl == sl && pl == el && po >= so && po <= eo
547+
|| pl == sl && po >= so
548+
|| pl == el && po <= eo
549+
529550
getCodeActionContext :: TextDocumentIdentifier -> Session CodeActionContext
530551
getCodeActionContext doc = do
531552
curDiags <- getCurrentDiagnostics doc

lsp-test/test/Test.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,10 @@ main = findServer >>= \serverExe -> hspec $ do
161161
it "works" $ runSession serverExe fullCaps "test/data/refactor" $ do
162162
doc <- openDoc "Main.hs" "haskell"
163163
waitForDiagnostics
164-
[InR action] <- getCodeActions doc (Range (Position 1 14) (Position 1 18))
164+
[InR action] <- getCodeActions doc (Range (Position 0 0) (Position 0 2))
165+
actions <- getCodeActions doc (Range (Position 1 14) (Position 1 18))
165166
liftIO $ action ^. title `shouldBe` "Delete this"
167+
liftIO $ actions `shouldSatisfy` null
166168

167169
describe "getAllCodeActions" $
168170
it "works" $ runSession serverExe fullCaps "test/data/refactor" $ do

0 commit comments

Comments
 (0)