Skip to content

Commit 2c4243f

Browse files
committed
Separate lens module from the general type file
1 parent 9636cf0 commit 2c4243f

File tree

20 files changed

+151
-135
lines changed

20 files changed

+151
-135
lines changed

lsp-test/bench/SimpleBench.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Main where
66

77
import Language.LSP.Server
88
import qualified Language.LSP.Test as Test
9-
import Language.LSP.Protocol.Types hiding (options, range, start, end)
9+
import Language.LSP.Protocol.Types
1010
import Language.LSP.Protocol.Message
1111
import Control.Monad.IO.Class
1212
import Control.Monad

lsp-test/func-test/FuncTest.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ module Main where
44

55
import Language.LSP.Server
66
import qualified Language.LSP.Test as Test
7-
import Language.LSP.Protocol.Types hiding (options, error)
7+
import Language.LSP.Protocol.Types hiding (error)
8+
import Language.LSP.Protocol.Types.Lens hiding (options)
89
import Language.LSP.Protocol.Message hiding (options, error)
910
import Control.Monad.IO.Class
1011
import System.IO

lsp-test/lsp-test.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ test-suite tests
105105
, lsp ^>=2.0
106106
, lsp-test
107107
, mtl <2.4
108+
, parser-combinators
108109
, process
109110
, text
110111
, unliftio
@@ -122,6 +123,7 @@ test-suite func-test
122123
, lens
123124
, lsp
124125
, lsp-test
126+
, parser-combinators
125127
, process
126128
, unliftio
127129

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

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,8 @@ import Data.Default
118118
import Data.List
119119
import Data.Maybe
120120
import Language.LSP.Protocol.Types
121-
hiding (capabilities, message, executeCommand, applyEdit, rename, to, id)
122-
import Language.LSP.Protocol.Message
123-
import qualified Language.LSP.Protocol.Types as LSP
121+
import Language.LSP.Protocol.Message hiding (id)
122+
import qualified Language.LSP.Protocol.Types.Lens as J
124123
import qualified Language.LSP.Protocol.Message as LSP
125124
import qualified Language.LSP.Protocol.Capabilities as C
126125
import Language.LSP.VFS
@@ -293,7 +292,7 @@ envOverrideConfig cfg = do
293292
documentContents :: TextDocumentIdentifier -> Session T.Text
294293
documentContents doc = do
295294
vfs <- vfs <$> get
296-
let Just file = vfs ^. vfsMap . at (toNormalizedUri (doc ^. uri))
295+
let Just file = vfs ^. vfsMap . at (toNormalizedUri (doc ^. J.uri))
297296
return (virtualFileText file)
298297

299298
-- | Parses an ApplyEditRequest, checks that it is for the passed document
@@ -308,14 +307,14 @@ getDocumentEdit doc = do
308307
documentContents doc
309308
where
310309
checkDocumentChanges req =
311-
let changes = req ^. params . edit . documentChanges
310+
let changes = req ^. params . J.edit . J.documentChanges
312311
maybeDocs = fmap (fmap documentChangeUri) changes
313312
in case maybeDocs of
314-
Just docs -> (doc ^. uri) `elem` docs
313+
Just docs -> (doc ^. J.uri) `elem` docs
315314
Nothing -> False
316315
checkChanges req =
317-
let mMap = req ^. params . edit . changes
318-
in maybe False (Map.member (doc ^. uri)) mMap
316+
let mMap = req ^. params . J.edit . J.changes
317+
in maybe False (Map.member (doc ^. J.uri)) mMap
319318

320319
-- | Sends a request to the server and waits for its response.
321320
-- Will skip any messages in between the request and the response
@@ -434,10 +433,10 @@ createDoc file languageId contents = do
434433
createHits _ = False
435434

436435
regHits :: TRegistration Method_WorkspaceDidChangeWatchedFiles -> Bool
437-
regHits reg = foldl' (\acc w -> acc || watchHits w) False (reg ^. registerOptions . _Just . watchers)
436+
regHits reg = foldl' (\acc w -> acc || watchHits w) False (reg ^. registerOptions . _Just . J.watchers)
438437

439438
clientCapsSupports =
440-
caps ^? workspace . _Just . didChangeWatchedFiles . _Just . dynamicRegistration . _Just
439+
caps ^? J.workspace . _Just . J.didChangeWatchedFiles . _Just . J.dynamicRegistration . _Just
441440
== Just True
442441
shouldSend = clientCapsSupports && foldl' (\acc r -> acc || regHits r) False regs
443442

@@ -469,14 +468,14 @@ openDoc' file languageId contents = do
469468
-- | Closes a text document and sends a textDocument/didOpen notification to the server.
470469
closeDoc :: TextDocumentIdentifier -> Session ()
471470
closeDoc docId = do
472-
let params = DidCloseTextDocumentParams (TextDocumentIdentifier (docId ^. uri))
471+
let params = DidCloseTextDocumentParams (TextDocumentIdentifier (docId ^. J.uri))
473472
sendNotification SMethod_TextDocumentDidClose params
474473

475474
-- | Changes a text document and sends a textDocument/didOpen notification to the server.
476475
changeDoc :: TextDocumentIdentifier -> [TextDocumentContentChangeEvent] -> Session ()
477476
changeDoc docId changes = do
478477
verDoc <- getVersionedDoc docId
479-
let params = DidChangeTextDocumentParams (verDoc & version +~ 1) changes
478+
let params = DidChangeTextDocumentParams (verDoc & J.version +~ 1) changes
480479
sendNotification SMethod_TextDocumentDidChange params
481480

482481
-- | Gets the Uri for the file corrected to the session directory.
@@ -490,7 +489,7 @@ getDocUri file = do
490489
waitForDiagnostics :: Session [Diagnostic]
491490
waitForDiagnostics = do
492491
diagsNot <- skipManyTill anyMessage (message SMethod_TextDocumentPublishDiagnostics)
493-
let diags = diagsNot ^. params . LSP.diagnostics
492+
let diags = diagsNot ^. params . J.diagnostics
494493
return diags
495494

496495
-- | The same as 'waitForDiagnostics', but will only match a specific
@@ -504,15 +503,15 @@ waitForDiagnosticsSource src = do
504503
else return res
505504
where
506505
matches :: Diagnostic -> Bool
507-
matches d = d ^. source == Just (T.pack src)
506+
matches d = d ^. J.source == Just (T.pack src)
508507

509508
-- | Expects a 'PublishDiagnosticsNotification' and throws an
510509
-- 'UnexpectedDiagnostics' exception if there are any diagnostics
511510
-- returned.
512511
noDiagnostics :: Session ()
513512
noDiagnostics = do
514513
diagsNot <- message SMethod_TextDocumentPublishDiagnostics
515-
when (diagsNot ^. params . LSP.diagnostics /= []) $ liftIO $ throw UnexpectedDiagnostics
514+
when (diagsNot ^. params . J.diagnostics /= []) $ liftIO $ throw UnexpectedDiagnostics
516515

517516
-- | Returns the symbols in a document.
518517
getDocumentSymbols :: TextDocumentIdentifier -> Session (Either [SymbolInformation] [DocumentSymbol])
@@ -533,7 +532,7 @@ getCodeActions doc range = do
533532
case rsp ^. result of
534533
Right (InL xs) -> return xs
535534
Right (InR _) -> return []
536-
Left error -> throw (UnexpectedResponseError (SomeLspId $ fromJust $ rsp ^. LSP.id) error)
535+
Left error -> throw (UnexpectedResponseError (SomeLspId $ fromJust $ rsp ^. J.id) error)
537536

538537
-- | Returns all the code actions in a document by
539538
-- querying the code actions at each of the current
@@ -547,7 +546,7 @@ getAllCodeActions doc = do
547546
where
548547
go :: CodeActionContext -> [Command |? CodeAction] -> Diagnostic -> Session [Command |? CodeAction]
549548
go ctx acc diag = do
550-
TResponseMessage _ rspLid res <- request SMethod_TextDocumentCodeAction (CodeActionParams Nothing Nothing doc (diag ^. range) ctx)
549+
TResponseMessage _ rspLid res <- request SMethod_TextDocumentCodeAction (CodeActionParams Nothing Nothing doc (diag ^. J.range) ctx)
551550

552551
case res of
553552
Left e -> throw (UnexpectedResponseError (SomeLspId $ fromJust rspLid) e)
@@ -582,7 +581,7 @@ getCodeActionContext doc = do
582581
-- | Returns the current diagnostics that have been sent to the client.
583582
-- Note that this does not wait for more to come in.
584583
getCurrentDiagnostics :: TextDocumentIdentifier -> Session [Diagnostic]
585-
getCurrentDiagnostics doc = fromMaybe [] . Map.lookup (toNormalizedUri $ doc ^. uri) . curDiagnostics <$> get
584+
getCurrentDiagnostics doc = fromMaybe [] . Map.lookup (toNormalizedUri $ doc ^. J.uri) . curDiagnostics <$> get
586585

587586
-- | Returns the tokens of all progress sessions that have started but not yet ended.
588587
getIncompleteProgressSessions :: Session (Set.Set ProgressToken)
@@ -591,8 +590,8 @@ getIncompleteProgressSessions = curProgressSessions <$> get
591590
-- | Executes a command.
592591
executeCommand :: Command -> Session ()
593592
executeCommand cmd = do
594-
let args = decode $ encode $ fromJust $ cmd ^. arguments
595-
execParams = ExecuteCommandParams Nothing (cmd ^. command) args
593+
let args = decode $ encode $ fromJust $ cmd ^. J.arguments
594+
execParams = ExecuteCommandParams Nothing (cmd ^. J.command) args
596595
void $ sendRequest SMethod_WorkspaceExecuteCommand execParams
597596

598597
-- | Executes a code action.
@@ -601,8 +600,8 @@ executeCommand cmd = do
601600
-- be applied first.
602601
executeCodeAction :: CodeAction -> Session ()
603602
executeCodeAction action = do
604-
maybe (return ()) handleEdit $ action ^. edit
605-
maybe (return ()) executeCommand $ action ^. command
603+
maybe (return ()) handleEdit $ action ^. J.edit
604+
maybe (return ()) executeCommand $ action ^. J.command
606605

607606
where handleEdit :: WorkspaceEdit -> Session ()
608607
handleEdit e =
@@ -627,14 +626,14 @@ applyEdit doc edit = do
627626

628627
caps <- asks sessionCapabilities
629628

630-
let supportsDocChanges = fromMaybe False $ caps ^? LSP.workspace . _Just . LSP.workspaceEdit . _Just . documentChanges . _Just
629+
let supportsDocChanges = fromMaybe False $ caps ^? J.workspace . _Just . J.workspaceEdit . _Just . J.documentChanges . _Just
631630

632631
let wEdit = if supportsDocChanges
633632
then
634633
let docEdit = TextDocumentEdit (review _versionedTextDocumentIdentifier verDoc) [InL edit]
635634
in WorkspaceEdit Nothing (Just [InL docEdit]) Nothing
636635
else
637-
let changes = Map.singleton (doc ^. uri) [edit]
636+
let changes = Map.singleton (doc ^. J.uri) [edit]
638637
in WorkspaceEdit (Just changes) Nothing Nothing
639638

640639
let req = TRequestMessage "" (IdInt 0) SMethod_WorkspaceApplyEdit (ApplyWorkspaceEditParams Nothing wEdit)
@@ -650,7 +649,7 @@ getCompletions doc pos = do
650649

651650
case getResponseResult rsp of
652651
InL items -> return items
653-
InR (InL c) -> return $ c ^. LSP.items
652+
InR (InL c) -> return $ c ^. J.items
654653
InR (InR _) -> return []
655654

656655
-- | Returns the references for the position in the document.
@@ -725,7 +724,7 @@ getResponseResult :: (ToJSON (ErrorData m)) => TResponseMessage m -> MessageResu
725724
getResponseResult rsp =
726725
case rsp ^. result of
727726
Right x -> x
728-
Left err -> throw $ UnexpectedResponseError (SomeLspId $ fromJust $ rsp ^. LSP.id) err
727+
Left err -> throw $ UnexpectedResponseError (SomeLspId $ fromJust $ rsp ^. J.id) err
729728

730729
-- | Applies formatting to the specified document.
731730
formatDoc :: TextDocumentIdentifier -> FormattingOptions -> Session ()
@@ -743,7 +742,7 @@ formatRange doc opts range = do
743742

744743
applyTextEdits :: TextDocumentIdentifier -> [TextEdit] -> Session ()
745744
applyTextEdits doc edits =
746-
let wEdit = WorkspaceEdit (Just (Map.singleton (doc ^. uri) edits)) Nothing Nothing
745+
let wEdit = WorkspaceEdit (Just (Map.singleton (doc ^. J.uri) edits)) Nothing Nothing
747746
-- Send a dummy message to updateState so it can do bookkeeping
748747
req = TRequestMessage "" (IdInt 0) SMethod_WorkspaceApplyEdit (ApplyWorkspaceEditParams Nothing wEdit)
749748
in updateState (FromServerMess SMethod_WorkspaceApplyEdit req)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import Data.Maybe
1616
import System.IO
1717
import System.IO.Error
1818
import Language.LSP.Protocol.Message hiding (error)
19-
import Language.LSP.Protocol.Types
19+
import qualified Language.LSP.Protocol.Types.Lens as J
2020
import Language.LSP.Test.Exceptions
2121

2222
import Data.IxMap
@@ -64,10 +64,10 @@ getRequestMap = foldl' helper emptyIxMap
6464
helper acc msg = case msg of
6565
FromClientMess m mess -> case splitClientMethod m of
6666
IsClientNot -> acc
67-
IsClientReq -> fromJust $ updateRequestMap acc (mess ^. id) m
67+
IsClientReq -> fromJust $ updateRequestMap acc (mess ^. J.id) m
6868
IsClientEither -> case mess of
6969
NotMess _ -> acc
70-
ReqMess msg -> fromJust $ updateRequestMap acc (msg ^. id) m
70+
ReqMess msg -> fromJust $ updateRequestMap acc (msg ^. J.id) m
7171
_ -> acc
7272

7373
decodeFromServerMsg :: RequestMap -> B.ByteString -> (RequestMap, FromServerMessage)

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ module Language.LSP.Test.Files
1010
)
1111
where
1212

13-
import Language.LSP.Protocol.Message hiding (error)
14-
import Language.LSP.Protocol.Types hiding (id)
13+
import Language.LSP.Protocol.Message hiding (error, id)
14+
import Language.LSP.Protocol.Types
15+
import Language.LSP.Protocol.Types.Lens hiding (id)
1516
import Control.Lens
1617
import qualified Data.Map.Strict as M
1718
import qualified Data.Text as T

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

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ import qualified Data.Text as T
6464
import qualified Data.Text.IO as T
6565
import Data.Maybe
6666
import Data.Function
67-
import Language.LSP.Protocol.Types as LSP hiding (to)
67+
import Language.LSP.Protocol.Types as LSP
68+
import qualified Language.LSP.Protocol.Types.Lens as J
6869
import Language.LSP.Protocol.Message as LSP hiding (error)
6970
import Language.LSP.VFS
7071
import Language.LSP.Test.Compat
@@ -305,63 +306,63 @@ updateStateC = awaitForever $ \msg -> do
305306
where
306307
respond :: (MonadIO m, HasReader SessionContext m) => FromServerMessage -> m ()
307308
respond (FromServerMess SMethod_WindowWorkDoneProgressCreate req) =
308-
sendMessage $ TResponseMessage "2.0" (Just $ req ^. LSP.id) (Right Null)
309+
sendMessage $ TResponseMessage "2.0" (Just $ req ^. J.id) (Right Null)
309310
respond (FromServerMess SMethod_WorkspaceApplyEdit r) = do
310-
sendMessage $ TResponseMessage "2.0" (Just $ r ^. LSP.id) (Right $ ApplyWorkspaceEditResult True Nothing Nothing)
311+
sendMessage $ TResponseMessage "2.0" (Just $ r ^. J.id) (Right $ ApplyWorkspaceEditResult True Nothing Nothing)
311312
respond _ = pure ()
312313

313314

314315
-- extract Uri out from DocumentChange
315316
-- didn't put this in `lsp-types` because TH was getting in the way
316317
documentChangeUri :: DocumentChange -> Uri
317-
documentChangeUri (InL x) = x ^. textDocument . uri
318-
documentChangeUri (InR (InL x)) = x ^. uri
319-
documentChangeUri (InR (InR (InL x))) = x ^. oldUri
320-
documentChangeUri (InR (InR (InR x))) = x ^. uri
318+
documentChangeUri (InL x) = x ^. J.textDocument . J.uri
319+
documentChangeUri (InR (InL x)) = x ^. J.uri
320+
documentChangeUri (InR (InR (InL x))) = x ^. J.oldUri
321+
documentChangeUri (InR (InR (InR x))) = x ^. J.uri
321322

322323
updateState :: (MonadIO m, HasReader SessionContext m, HasState SessionState m)
323324
=> FromServerMessage -> m ()
324-
updateState (FromServerMess SMethod_Progress req) = case req ^. params . value of
325+
updateState (FromServerMess SMethod_Progress req) = case req ^. params . J.value of
325326
v | Just _ <- v ^? _workDoneProgressBegin ->
326-
modify $ \s -> s { curProgressSessions = Set.insert (req ^. params . token) $ curProgressSessions s }
327+
modify $ \s -> s { curProgressSessions = Set.insert (req ^. params . J.token) $ curProgressSessions s }
327328
v | Just _ <- v ^? _workDoneProgressEnd ->
328-
modify $ \s -> s { curProgressSessions = Set.delete (req ^. params . token) $ curProgressSessions s }
329+
modify $ \s -> s { curProgressSessions = Set.delete (req ^. params . J.token) $ curProgressSessions s }
329330
_ -> pure ()
330331

331332
-- Keep track of dynamic capability registration
332333
updateState (FromServerMess SMethod_ClientRegisterCapability req) = do
333334
let
334335
regs :: [SomeRegistration]
335-
regs = req ^.. params . registrations . traversed . to toSomeRegistration . _Just
336+
regs = req ^.. params . J.registrations . traversed . to toSomeRegistration . _Just
336337
let newRegs = (\sr@(SomeRegistration r) -> (r ^. LSP.id, sr)) <$> regs
337338
modify $ \s ->
338339
s { curDynCaps = Map.union (Map.fromList newRegs) (curDynCaps s) }
339340

340341
updateState (FromServerMess SMethod_ClientUnregisterCapability req) = do
341-
let unRegs = (^. LSP.id) <$> req ^. params . unregisterations
342+
let unRegs = (^. J.id) <$> req ^. params . J.unregisterations
342343
modify $ \s ->
343344
let newCurDynCaps = foldr' Map.delete (curDynCaps s) unRegs
344345
in s { curDynCaps = newCurDynCaps }
345346

346347
updateState (FromServerMess SMethod_TextDocumentPublishDiagnostics n) = do
347-
let diags = n ^. params . diagnostics
348-
doc = n ^. params . uri
348+
let diags = n ^. params . J.diagnostics
349+
doc = n ^. params . J.uri
349350
modify $ \s ->
350351
let newDiags = Map.insert (toNormalizedUri doc) diags (curDiagnostics s)
351352
in s { curDiagnostics = newDiags }
352353

353354
updateState (FromServerMess SMethod_WorkspaceApplyEdit r) = do
354355

355356
-- First, prefer the versioned documentChanges field
356-
allChangeParams <- case r ^. params . edit . documentChanges of
357+
allChangeParams <- case r ^. params . J.edit . J.documentChanges of
357358
Just (cs) -> do
358359
mapM_ (checkIfNeedsOpened . documentChangeUri) cs
359360
-- replace the user provided version numbers with the VFS ones + 1
360361
-- (technically we should check that the user versions match the VFS ones)
361-
cs' <- traverseOf (traverse . _L . textDocument . _versionedTextDocumentIdentifier) bumpNewestVersion cs
362+
cs' <- traverseOf (traverse . _L . J.textDocument . _versionedTextDocumentIdentifier) bumpNewestVersion cs
362363
return $ mapMaybe getParamsFromDocumentChange cs'
363364
-- Then fall back to the changes field
364-
Nothing -> case r ^. params . edit . changes of
365+
Nothing -> case r ^. params . J.edit . J.changes of
365366
Just cs -> do
366367
mapM_ checkIfNeedsOpened (Map.keys cs)
367368
concat <$> mapM (uncurry getChangeParams) (Map.toList cs)
@@ -372,15 +373,15 @@ updateState (FromServerMess SMethod_WorkspaceApplyEdit r) = do
372373
let newVFS = flip execState (vfs s) $ changeFromServerVFS logger r
373374
return $ s { vfs = newVFS }
374375

375-
let groupedParams = groupBy (\a b -> a ^. textDocument == b ^. textDocument) allChangeParams
376+
let groupedParams = groupBy (\a b -> a ^. J.textDocument == b ^. J.textDocument) allChangeParams
376377
mergedParams = map mergeParams groupedParams
377378

378379
-- TODO: Don't do this when replaying a session
379380
forM_ mergedParams (sendMessage . TNotificationMessage "2.0" SMethod_TextDocumentDidChange)
380381

381382
-- Update VFS to new document versions
382-
let sortedVersions = map (sortBy (compare `on` (^. textDocument . version))) groupedParams
383-
latestVersions = map ((^. textDocument) . last) sortedVersions
383+
let sortedVersions = map (sortBy (compare `on` (^. J.textDocument . J.version))) groupedParams
384+
latestVersions = map ((^. J.textDocument) . last) sortedVersions
384385

385386
forM_ latestVersions $ \(VersionedTextDocumentIdentifier uri v) ->
386387
modify $ \s ->
@@ -412,8 +413,8 @@ updateState (FromServerMess SMethod_WorkspaceApplyEdit r) = do
412413

413414
-- TODO: move somewhere reusable
414415
editToChangeEvent :: TextEdit |? AnnotatedTextEdit -> TextDocumentContentChangeEvent
415-
editToChangeEvent (InR e) = TextDocumentContentChangeEvent $ InL $ #range .== (e ^. range) .+ #rangeLength .== Nothing .+ #text .== (e ^. newText)
416-
editToChangeEvent (InL e) = TextDocumentContentChangeEvent $ InL $ #range .== (e ^. range) .+ #rangeLength .== Nothing .+ #text .== (e ^. newText)
416+
editToChangeEvent (InR e) = TextDocumentContentChangeEvent $ InL $ #range .== (e ^. J.range) .+ #rangeLength .== Nothing .+ #text .== (e ^. J.newText)
417+
editToChangeEvent (InL e) = TextDocumentContentChangeEvent $ InL $ #range .== (e ^. J.range) .+ #rangeLength .== Nothing .+ #text .== (e ^. J.newText)
417418

418419
getParamsFromDocumentChange :: DocumentChange -> Maybe DidChangeTextDocumentParams
419420
getParamsFromDocumentChange (InL textDocumentEdit) = getParamsFromTextDocumentEdit textDocumentEdit
@@ -438,8 +439,8 @@ updateState (FromServerMess SMethod_WorkspaceApplyEdit r) = do
438439
pure $ catMaybes $ map getParamsFromTextDocumentEdit edits
439440

440441
mergeParams :: [DidChangeTextDocumentParams] -> DidChangeTextDocumentParams
441-
mergeParams params = let events = concat (toList (map (toList . (^. contentChanges)) params))
442-
in DidChangeTextDocumentParams (head params ^. textDocument) events
442+
mergeParams params = let events = concat (toList (map (toList . (^. J.contentChanges)) params))
443+
in DidChangeTextDocumentParams (head params ^. J.textDocument) events
443444
updateState _ = return ()
444445

445446
sendMessage :: (MonadIO m, HasReader SessionContext m, ToJSON a) => a -> m ()

0 commit comments

Comments
 (0)