Skip to content

Commit b2998e1

Browse files
committed
Add test for #374
1 parent 48d8e7b commit b2998e1

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ updateState (FromServerMess SWorkspaceApplyEdit r) = do
425425
vers <- textDocumentVersions uri
426426
pure $ map (\(v, e) -> TextDocumentEdit v (List [InL e])) $ zip vers edits
427427

428-
getChangeParams uri (List edits) = do
428+
getChangeParams uri (List edits) = do
429429
map <$> pure getParamsFromTextDocumentEdit <*> textDocumentEdits uri (reverse edits)
430430

431431
mergeParams :: [DidChangeTextDocumentParams] -> DidChangeTextDocumentParams

lsp-test/test/DummyServer.hs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,33 @@ handlers =
139139
do
140140
Just token <- runInIO $ asks absRegToken >>= tryReadMVar
141141
runInIO $ unregisterCapability token
142+
, notificationHandler STextDocumentDidChange $ \noti -> do
143+
let NotificationMessage _ _ params = noti
144+
void $ sendNotification (SCustomMethod "custom/textDocument/didChange") (toJSON params)
145+
142146
, requestHandler SWorkspaceExecuteCommand $ \req resp -> do
143-
let RequestMessage _ _ _ (ExecuteCommandParams Nothing "doAnEdit" (Just (List [val]))) = req
147+
case req of
148+
RequestMessage _ _ _ (ExecuteCommandParams Nothing "doAnEdit" (Just (List [val]))) -> do
149+
let
144150
Success docUri = fromJSON val
145151
edit = List [TextEdit (mkRange 0 0 0 5) "howdy"]
146152
params =
147153
ApplyWorkspaceEditParams (Just "Howdy edit") $
148154
WorkspaceEdit (Just (HM.singleton docUri edit)) Nothing Nothing
149-
resp $ Right Null
150-
void $ sendRequest SWorkspaceApplyEdit params (const (pure ()))
155+
resp $ Right Null
156+
void $ sendRequest SWorkspaceApplyEdit params (const (pure ()))
157+
RequestMessage _ _ _ (ExecuteCommandParams Nothing "doAVersionedEdit" (Just (List [val]))) -> do
158+
let
159+
Success versionedDocUri = fromJSON val
160+
edit = List [InL (TextEdit (mkRange 0 0 0 5) "howdy")]
161+
documentEdit = TextDocumentEdit versionedDocUri edit
162+
params =
163+
ApplyWorkspaceEditParams (Just "Howdy edit") $
164+
WorkspaceEdit Nothing (Just (List [InL documentEdit])) Nothing
165+
resp $ Right Null
166+
void $ sendRequest SWorkspaceApplyEdit params (const (pure ()))
167+
RequestMessage _ _ _ (ExecuteCommandParams _ name _) ->
168+
error $ "unsupported command: " <> show name
151169
, requestHandler STextDocumentCodeAction $ \req resp -> do
152170
let RequestMessage _ _ _ params = req
153171
CodeActionParams _ _ _ _ cactx = params

lsp-test/test/Test.hs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{-# LANGUAGE GADTs #-}
33
{-# LANGUAGE DuplicateRecordFields #-}
44
{-# LANGUAGE OverloadedStrings #-}
5+
{-# LANGUAGE ScopedTypeVariables #-}
56
{-# LANGUAGE DeriveGeneric #-}
67
{-# LANGUAGE DeriveAnyClass #-}
78

@@ -132,7 +133,37 @@ main = hspec $ around withDummyServer $ do
132133
in runSessionWithHandles hin hout def fullCaps "." sesh
133134
`shouldThrow` selector
134135

135-
describe "text document VFS" $
136+
describe "text document VFS" $ do
137+
it "sends back didChange notifications (documentChanges)" $ \(hin, hout) ->
138+
runSessionWithHandles hin hout def fullCaps "." $ do
139+
doc <- openDoc "test/data/refactor/Main.hs" "haskell"
140+
VersionedTextDocumentIdentifier _ beforeVersion <- getVersionedDoc doc
141+
142+
let args = toJSON (VersionedTextDocumentIdentifier (doc ^. uri) beforeVersion)
143+
reqParams = ExecuteCommandParams Nothing "doAVersionedEdit" (Just (List [args]))
144+
145+
request_ SWorkspaceExecuteCommand reqParams
146+
147+
editReq <- message SWorkspaceApplyEdit
148+
liftIO $ do
149+
let Just (List [InL(TextDocumentEdit vdoc (List [InL edit_]))]) =
150+
editReq ^. params . edit . documentChanges
151+
vdoc `shouldBe` VersionedTextDocumentIdentifier (doc ^. uri) beforeVersion
152+
edit_ `shouldBe` TextEdit (Range (Position 0 0) (Position 0 5)) "howdy"
153+
154+
change <- customNotification "custom/textDocument/didChange"
155+
let NotMess (NotificationMessage _ _ (c::Value)) = change
156+
Success (DidChangeTextDocumentParams reportedVDoc _edit) = fromJSON c
157+
VersionedTextDocumentIdentifier _ reportedVersion = reportedVDoc
158+
159+
contents <- documentContents doc
160+
161+
liftIO $ contents `shouldBe` "howdy:: IO Int\nmain = return (42)\n"
162+
VersionedTextDocumentIdentifier _ afterVersion <- getVersionedDoc doc
163+
liftIO $ afterVersion `shouldNotBe` beforeVersion
164+
165+
liftIO $ reportedVersion `shouldNotBe` beforeVersion
166+
136167
it "sends back didChange notifications" $ \(hin, hout) ->
137168
runSessionWithHandles hin hout def fullCaps "." $ do
138169
doc <- openDoc "test/data/refactor/Main.hs" "haskell"

0 commit comments

Comments
 (0)