Skip to content

Commit 7d9e078

Browse files
committed
Add more resolve helpers
1 parent 6854485 commit 7d9e078

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

lsp-test/ChangeLog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## HEAD
44

5-
* Adds `resolveCodeAction`, `resolveCodeLens` and `executeAndResolveCodeAction` functions to resolve code lens and code actions
5+
* Adds helper functions to resolve code lens, code actions, and completion items.
66

77
## 0.15.0.0
88

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,14 @@ module Language.LSP.Test
6969
, executeCommand
7070
-- ** Code Actions
7171
, getCodeActions
72+
, getAndResolveCodeActions
7273
, getAllCodeActions
7374
, executeCodeAction
7475
, resolveCodeAction
7576
, resolveAndExecuteCodeAction
7677
-- ** Completions
7778
, getCompletions
79+
, getAndResolveCompletions
7880
-- ** References
7981
, getReferences
8082
-- ** Definitions
@@ -95,6 +97,7 @@ module Language.LSP.Test
9597
, applyEdit
9698
-- ** Code lenses
9799
, getCodeLenses
100+
, getAndResolveCodeLenses
98101
, resolveCodeLens
99102
-- ** Call hierarchy
100103
, prepareCallHierarchy
@@ -533,6 +536,17 @@ getCodeActions doc range = do
533536
Right (InR _) -> return []
534537
Left error -> throw (UnexpectedResponseError (SomeLspId $ fromJust $ rsp ^. L.id) error)
535538

539+
-- | Returns the Returns the code actions in the specified range, resolving any with
540+
-- a non empty _data_ field.
541+
getAndResolveCodeActions :: TextDocumentIdentifier -> Range -> Session [Command |? CodeAction]
542+
getAndResolveCodeActions doc range = do
543+
items <- getCodeActions doc range
544+
forM items leaveCommandResolveCodeAction
545+
where leaveCommandResolveCodeAction l@(InL _) = pure l
546+
leaveCommandResolveCodeAction (InR r) | isJust (r ^. L.data_) =
547+
InR <$> resolveCodeAction r
548+
leaveCommandResolveCodeAction r@(InR _) = pure r
549+
536550
-- | Returns all the code actions in a document by
537551
-- querying the code actions at each of the current
538552
-- diagnostics' positions.
@@ -667,6 +681,21 @@ getCompletions doc pos = do
667681
InR (InL c) -> return $ c ^. L.items
668682
InR (InR _) -> return []
669683

684+
-- | Returns the completions for the position in the document, resolving any with
685+
-- a non empty _data_ field.
686+
getAndResolveCompletions :: TextDocumentIdentifier -> Position -> Session [CompletionItem]
687+
getAndResolveCompletions doc pos = do
688+
items <- getCompletions doc pos
689+
forM items (\item -> if isJust (item ^. L.data_) then resolveCompletion item else pure item)
690+
691+
-- |Resolves the provided completion item.
692+
resolveCompletion :: CompletionItem -> Session CompletionItem
693+
resolveCompletion ci = do
694+
rsp <- request SMethod_CompletionItemResolve ci
695+
case rsp ^. L.result of
696+
Right ci -> return ci
697+
Left error -> throw (UnexpectedResponseError (SomeLspId $ fromJust $ rsp ^. L.id) error)
698+
670699
-- | Returns the references for the position in the document.
671700
getReferences :: TextDocumentIdentifier -- ^ The document to lookup in.
672701
-> Position -- ^ The position to lookup.
@@ -768,6 +797,13 @@ getCodeLenses tId = do
768797
rsp <- request SMethod_TextDocumentCodeLens (CodeLensParams Nothing Nothing tId)
769798
pure $ absorbNull $ getResponseResult rsp
770799

800+
-- | RReturns the code lenses for the specified document, resolving any with
801+
-- a non empty _data_ field.
802+
getAndResolveCodeLenses :: TextDocumentIdentifier -> Session [CodeLens]
803+
getAndResolveCodeLenses tId = do
804+
codeLenses <- getCodeLenses tId
805+
forM codeLenses (\codeLens -> if isJust (codeLens ^. L.data_) then resolveCodeLens codeLens else pure codeLens)
806+
771807
-- |Resolves the provided code lens.
772808
resolveCodeLens :: CodeLens -> Session CodeLens
773809
resolveCodeLens cl = do

0 commit comments

Comments
 (0)