77{-# LANGUAGE TypeInType #-}
88{-# LANGUAGE ScopedTypeVariables #-}
99{-# LANGUAGE ExistentialQuantification #-}
10+ {-# LANGUAGE DuplicateRecordFields #-}
1011
1112{-|
1213Module : Language.LSP.Test
@@ -501,7 +502,7 @@ getDocumentSymbols doc = do
501502-- | Returns the code actions in the specified range.
502503getCodeActions :: TextDocumentIdentifier -> Range -> Session [Command |? CodeAction ]
503504getCodeActions 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+
529550getCodeActionContext :: TextDocumentIdentifier -> Session CodeActionContext
530551getCodeActionContext doc = do
531552 curDiags <- getCurrentDiagnostics doc
0 commit comments