Skip to content

Commit d484cd9

Browse files
authored
Merge pull request bubba/lsp-test/#81 from banacorn/lspbubba/lsp-test/#267
Fix alanz/lspbubba/lsp-test/#267
2 parents 3f978cb + 227214a commit d484cd9

File tree

4 files changed

+43
-10
lines changed

4 files changed

+43
-10
lines changed

lsp-test/cabal.project

+11
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,14 @@ packages: .
33
flags: +DummyServer
44
test-show-details: direct
55
haddock-quickjump: True
6+
7+
source-repository-package
8+
type: git
9+
location: https://github.com/banacorn/lsp.git
10+
tag: 0556d22fc66f24bb526f671666183a86b485837e
11+
subdir: lsp-types
12+
13+
source-repository-package
14+
type: git
15+
location: https://github.com/banacorn/lsp.git
16+
tag: 0556d22fc66f24bb526f671666183a86b485837e

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ getDocumentEdit doc = do
285285
where
286286
checkDocumentChanges req =
287287
let changes = req ^. params . edit . documentChanges
288-
maybeDocs = fmap (fmap (^. textDocument . uri)) changes
288+
maybeDocs = fmap (fmap documentChangeUri) changes
289289
in case maybeDocs of
290290
Just docs -> (doc ^. uri) `elem` docs
291291
Nothing -> False
@@ -583,7 +583,7 @@ applyEdit doc edit = do
583583
let wEdit = if supportsDocChanges
584584
then
585585
let docEdit = TextDocumentEdit verDoc (List [edit])
586-
in WorkspaceEdit Nothing (Just (List [docEdit]))
586+
in WorkspaceEdit Nothing (Just (List [InL docEdit]))
587587
else
588588
let changes = HashMap.singleton (doc ^. uri) (List [edit])
589589
in WorkspaceEdit (Just changes) Nothing

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

+10-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module Language.LSP.Test.Files
1111
where
1212

1313
import Language.LSP.Types
14-
import Language.LSP.Types.Lens
14+
import Language.LSP.Types.Lens hiding (id)
1515
import Control.Lens
1616
import qualified Data.HashMap.Strict as HM
1717
import qualified Data.Text as T
@@ -75,9 +75,16 @@ mapUris f event =
7575

7676
swapWorkspaceEdit :: WorkspaceEdit -> WorkspaceEdit
7777
swapWorkspaceEdit e =
78-
let newDocChanges = fmap (fmap (swapUri textDocument)) $ e ^. documentChanges
78+
let swapDocumentChangeUri :: DocumentChange -> DocumentChange
79+
swapDocumentChangeUri (InL textDocEdit) = InL $ swapUri textDocument textDocEdit
80+
swapDocumentChangeUri (InR (InL createFile)) = InR $ InL $ swapUri id createFile
81+
-- for RenameFile, we swap `newUri`
82+
swapDocumentChangeUri (InR (InR (InL renameFile))) = InR $ InR $ InL $ newUri .~ f (renameFile ^. newUri) $ renameFile
83+
swapDocumentChangeUri (InR (InR (InR deleteFile))) = InR $ InR $ InR $ swapUri id deleteFile
84+
85+
newDocChanges = fmap (fmap swapDocumentChangeUri) $ e ^. documentChanges
7986
newChanges = fmap (swapKeys f) $ e ^. changes
80-
in WorkspaceEdit newChanges newDocChanges
87+
in WorkspaceEdit newChanges newDocChanges
8188

8289
swapKeys :: (Uri -> Uri) -> HM.HashMap Uri b -> HM.HashMap Uri b
8390
swapKeys f = HM.foldlWithKey' (\acc k v -> HM.insert (f k) v acc) HM.empty

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

+20-5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ module Language.LSP.Test.Session
2929
, bumpTimeoutId
3030
, logMsg
3131
, LogMsgType(..)
32+
, documentChangeUri
3233
)
3334

3435
where
@@ -296,6 +297,14 @@ updateStateC = awaitForever $ \msg -> do
296297
updateState msg
297298
yield msg
298299

300+
-- extract Uri out from DocumentChange
301+
-- didn't put this in `lsp-types` because TH was getting in the way
302+
documentChangeUri :: DocumentChange -> Uri
303+
documentChangeUri (InL x) = x ^. textDocument . uri
304+
documentChangeUri (InR (InL x)) = x ^. uri
305+
documentChangeUri (InR (InR (InL x))) = x ^. oldUri
306+
documentChangeUri (InR (InR (InR x))) = x ^. uri
307+
299308
updateState :: (MonadIO m, HasReader SessionContext m, HasState SessionState m)
300309
=> FromServerMessage -> m ()
301310

@@ -323,8 +332,8 @@ updateState (FromServerMess SWorkspaceApplyEdit r) = do
323332
-- First, prefer the versioned documentChanges field
324333
allChangeParams <- case r ^. params . edit . documentChanges of
325334
Just (List cs) -> do
326-
mapM_ (checkIfNeedsOpened . (^. textDocument . uri)) cs
327-
return $ map getParams cs
335+
mapM_ (checkIfNeedsOpened . documentChangeUri) cs
336+
return $ mapMaybe getParamsFromDocumentChange cs
328337
-- Then fall back to the changes field
329338
Nothing -> case r ^. params . edit . changes of
330339
Just cs -> do
@@ -371,10 +380,16 @@ updateState (FromServerMess SWorkspaceApplyEdit r) = do
371380
let (newVFS,_) = openVFS (vfs s) msg
372381
return $ s { vfs = newVFS }
373382

374-
getParams (TextDocumentEdit docId (List edits)) =
383+
getParamsFromTextDocumentEdit :: TextDocumentEdit -> DidChangeTextDocumentParams
384+
getParamsFromTextDocumentEdit (TextDocumentEdit docId (List edits)) =
375385
let changeEvents = map (\e -> TextDocumentContentChangeEvent (Just (e ^. range)) Nothing (e ^. newText)) edits
376386
in DidChangeTextDocumentParams docId (List changeEvents)
377387

388+
getParamsFromDocumentChange :: DocumentChange -> Maybe DidChangeTextDocumentParams
389+
getParamsFromDocumentChange (InL textDocumentEdit) = Just $ getParamsFromTextDocumentEdit textDocumentEdit
390+
getParamsFromDocumentChange _ = Nothing
391+
392+
378393
-- For a uri returns an infinite list of versions [n,n+1,n+2,...]
379394
-- where n is the current version
380395
textDocumentVersions uri = do
@@ -387,8 +402,8 @@ updateState (FromServerMess SWorkspaceApplyEdit r) = do
387402
vers <- textDocumentVersions uri
388403
pure $ map (\(v, e) -> TextDocumentEdit v (List [e])) $ zip vers edits
389404

390-
getChangeParams uri (List edits) =
391-
map <$> pure getParams <*> textDocumentEdits uri (reverse edits)
405+
getChangeParams uri (List edits) = do
406+
map <$> pure getParamsFromTextDocumentEdit <*> textDocumentEdits uri (reverse edits)
392407

393408
mergeParams :: [DidChangeTextDocumentParams] -> DidChangeTextDocumentParams
394409
mergeParams params = let events = concat (toList (map (toList . (^. contentChanges)) params))

0 commit comments

Comments
 (0)