Skip to content

Commit e6d5c5d

Browse files
jetjinsermichaelpj
andauthored
add inlay hints lsp-test support (#575)
* add inlay hints lsp-test support * format with fourmolu --------- Co-authored-by: Michael Peyton Jones <[email protected]>
1 parent 6327e0f commit e6d5c5d

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ module Language.LSP.Test (
124124
getAndResolveCodeLenses,
125125
resolveCodeLens,
126126

127+
-- ** Inlay Hints
128+
getInlayHints,
129+
getAndResolveInlayHints,
130+
resolveInlayHint,
131+
127132
-- ** Call hierarchy
128133
prepareCallHierarchy,
129134
incomingCalls,
@@ -981,6 +986,28 @@ resolveCodeLens cl = do
981986
Right cl -> return cl
982987
Left error -> throw (UnexpectedResponseError (SomeLspId $ fromJust $ rsp ^. L.id) error)
983988

989+
-- | Returns the inlay hints in the specified range.
990+
getInlayHints :: TextDocumentIdentifier -> Range -> Session [InlayHint]
991+
getInlayHints tId range = do
992+
rsp <- request SMethod_TextDocumentInlayHint (InlayHintParams Nothing tId range)
993+
pure $ absorbNull $ getResponseResult rsp
994+
995+
{- | Returns the inlay hints in the specified range, resolving any with
996+
a non empty _data_ field.
997+
-}
998+
getAndResolveInlayHints :: TextDocumentIdentifier -> Range -> Session [InlayHint]
999+
getAndResolveInlayHints tId range = do
1000+
inlayHints <- getInlayHints tId range
1001+
for inlayHints $ \inlayHint -> if isJust (inlayHint ^. L.data_) then resolveInlayHint inlayHint else pure inlayHint
1002+
1003+
-- | Resolves the provided inlay hint.
1004+
resolveInlayHint :: InlayHint -> Session InlayHint
1005+
resolveInlayHint ih = do
1006+
rsp <- request SMethod_InlayHintResolve ih
1007+
case rsp ^. L.result of
1008+
Right ih -> return ih
1009+
Left error -> throw (UnexpectedResponseError (SomeLspId $ fromJust $ rsp ^. L.id) error)
1010+
9841011
-- | Pass a param and return the response from `prepareCallHierarchy`
9851012
prepareCallHierarchy :: CallHierarchyPrepareParams -> Session [CallHierarchyItem]
9861013
prepareCallHierarchy = resolveRequestWithListResp SMethod_TextDocumentPrepareCallHierarchy

lsp-test/test/DummyServer.hs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{-# LANGUAGE DataKinds #-}
22
{-# LANGUAGE DuplicateRecordFields #-}
33
{-# LANGUAGE OverloadedStrings #-}
4+
{-# LANGUAGE RecordWildCards #-}
45

56
module DummyServer where
67

@@ -256,4 +257,25 @@ handlers =
256257
case tokens of
257258
Left t -> resp $ Left $ ResponseError (InR ErrorCodes_InternalError) t Nothing
258259
Right tokens -> resp $ Right $ InL tokens
260+
, requestHandler SMethod_TextDocumentInlayHint $ \req resp -> do
261+
let TRequestMessage _ _ _ params = req
262+
InlayHintParams _ _ (Range start end) = params
263+
ih =
264+
InlayHint
265+
end
266+
(InL ":: Text")
267+
Nothing
268+
Nothing
269+
Nothing
270+
Nothing
271+
Nothing
272+
(Just $ toJSON start)
273+
resp $ Right $ InL [ih]
274+
, requestHandler SMethod_InlayHintResolve $ \req resp -> do
275+
let TRequestMessage _ _ _ params = req
276+
(InlayHint{_data_ = Just data_, ..}) = params
277+
start :: Position
278+
Success start = fromJSON data_
279+
ih = InlayHint{_data_ = Nothing, _tooltip = Just $ InL $ "start at " <> T.pack (show start), ..}
280+
resp $ Right ih
259281
]

lsp-test/test/Test.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,3 +454,13 @@ main = hspec $ around withDummyServer $ do
454454
let doc = TextDocumentIdentifier (Uri "")
455455
InL toks <- getSemanticTokens doc
456456
liftIO $ toks ^. L.data_ `shouldBe` [0, 1, 2, 1, 0]
457+
458+
describe "inlay hints" $ do
459+
it "get works" $ \(hin, hout) -> runSessionWithHandles hin hout def fullCaps "." $ do
460+
doc <- openDoc "test/data/renamePass/Desktop/simple.hs" "haskell"
461+
inlayHints <- getInlayHints doc (Range (Position 1 2) (Position 3 4))
462+
liftIO $ head inlayHints ^. L.label `shouldBe` InL ":: Text"
463+
it "resolve tooltip works" $ \(hin, hout) -> runSessionWithHandles hin hout def fullCaps "." $ do
464+
doc <- openDoc "test/data/renamePass/Desktop/simple.hs" "haskell"
465+
inlayHints <- getAndResolveInlayHints doc (Range (Position 1 2) (Position 3 4))
466+
liftIO $ head inlayHints ^. L.tooltip `shouldBe` Just (InL $ "start at " <> T.pack (show (Position 1 2)))

0 commit comments

Comments
 (0)