Skip to content

Commit 680d82e

Browse files
committed
Switch lsp to use microlens
1 parent 1167017 commit 680d82e

File tree

3 files changed

+38
-34
lines changed

3 files changed

+38
-34
lines changed

lsp/lsp.cabal

+2-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ library
6666
, filepath >=1.4 && < 1.6
6767
, generic-lens ^>=2.2
6868
, hashable ^>=1.4
69-
, lens >=5.1 && <5.4
69+
, microlens ^>=0.4
70+
, microlens-ghc ^>=0.4
7071
, lens-aeson ^>=1.2
7172
, lsp-types ^>=2.3
7273
, mtl >=2.2 && <2.4

lsp/src/Language/LSP/Server/Processing.hs

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import Colog.Core (
2121
import Control.Concurrent.Extra as C
2222
import Control.Concurrent.STM
2323
import Control.Exception qualified as E
24-
import Control.Lens hiding (Empty)
2524
import Control.Monad
2625
import Control.Monad.Except ()
2726
import Control.Monad.IO.Class
@@ -34,6 +33,7 @@ import Data.Aeson hiding (
3433
Null,
3534
Options,
3635
)
36+
import Data.Aeson.KeyMap qualified as Aeson
3737
import Data.Aeson.Lens ()
3838
import Data.Aeson.Types hiding (
3939
Error,
@@ -42,6 +42,7 @@ import Data.Aeson.Types hiding (
4242
)
4343
import Data.ByteString.Lazy qualified as BSL
4444
import Data.Foldable (traverse_)
45+
import Data.Functor.Const (Const (Const))
4546
import Data.Functor.Product qualified as P
4647
import Data.IxMap
4748
import Data.List
@@ -582,7 +583,7 @@ initialDynamicRegistrations logger = do
582583
See Note [LSP configuration]
583584
-}
584585
lookForConfigSection :: T.Text -> Value -> Value
585-
lookForConfigSection section (Object o) | Just s' <- o ^. at (fromString $ T.unpack section) = s'
586+
lookForConfigSection section (Object o) | Just s' <- Aeson.lookup (fromString $ T.unpack section) o = s'
586587
lookForConfigSection _ o = o
587588

588589
-- | Handle a workspace/didChangeConfiguration request.

lsp/src/Language/LSP/VFS.hs

+33-31
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
{-# LANGUAGE DataKinds #-}
22
{-# LANGUAGE DuplicateRecordFields #-}
3-
{-# LANGUAGE FunctionalDependencies #-}
4-
{-# LANGUAGE MultiWayIf #-}
53
{-# LANGUAGE OverloadedLabels #-}
4+
{-# LANGUAGE OverloadedRecordDot #-}
65
{-# LANGUAGE OverloadedStrings #-}
7-
{-# LANGUAGE TemplateHaskell #-}
86
{-# LANGUAGE ViewPatterns #-}
97

108
{- |
@@ -31,13 +29,9 @@ module Language.LSP.VFS (
3129

3230
-- * Positions and transformations
3331
CodePointPosition (..),
34-
line,
35-
character,
3632
codePointPositionToPosition,
3733
positionToCodePointPosition,
3834
CodePointRange (..),
39-
start,
40-
end,
4135
codePointRangeToRange,
4236
rangeToCodePointRange,
4337

@@ -51,7 +45,6 @@ module Language.LSP.VFS (
5145
) where
5246

5347
import Colog.Core (LogAction (..), Severity (..), WithSeverity (..), (<&))
54-
import Control.Lens hiding (parts, (<.>))
5548
import Control.Monad
5649
import Control.Monad.State
5750
import Data.Foldable (traverse_)
@@ -70,6 +63,9 @@ import Data.Text.Utf16.Rope.Mixed qualified as Rope
7063
import GHC.Generics
7164
import Language.LSP.Protocol.Message qualified as J
7265
import Language.LSP.Protocol.Types qualified as J
66+
import Lens.Micro
67+
import Lens.Micro.Extras
68+
import Lens.Micro.GHC ()
7369
import Prettyprinter hiding (line)
7470
import System.Directory
7571
import System.FilePath
@@ -79,6 +75,8 @@ import System.IO
7975
{-# ANN module ("hlint: ignore Eta reduce" :: String) #-}
8076
{-# ANN module ("hlint: ignore Redundant do" :: String) #-}
8177

78+
infix 4 .=, %=
79+
8280
-- ---------------------------------------------------------------------
8381

8482
data VirtualFile = VirtualFile
@@ -137,8 +135,8 @@ emptyVFS = VFS mempty
137135
openVFS :: (MonadState VFS m) => LogAction m (WithSeverity VfsLog) -> J.TMessage 'J.Method_TextDocumentDidOpen -> m ()
138136
openVFS logger msg = do
139137
let
140-
p = msg ^. #params
141-
J.TextDocumentItem (J.toNormalizedUri -> uri) _ version text = p ^. #textDocument
138+
p = msg.params
139+
J.TextDocumentItem (J.toNormalizedUri -> uri) _ version text = p.textDocument
142140
vfile = VirtualFile version 0 (Rope.fromText text)
143141
logger <& Opening uri `WithSeverity` Debug
144142
#vfsMap . at uri .= Just vfile
@@ -149,11 +147,11 @@ openVFS logger msg = do
149147
changeFromClientVFS :: (MonadState VFS m) => LogAction m (WithSeverity VfsLog) -> J.TMessage 'J.Method_TextDocumentDidChange -> m ()
150148
changeFromClientVFS logger msg = do
151149
let
152-
J.DidChangeTextDocumentParams vid changes = msg ^. #params
150+
J.DidChangeTextDocumentParams vid changes = msg.params
153151
-- the client shouldn't be sending over a null version, only the server, but we just use 0 if that happens
154152
J.VersionedTextDocumentIdentifier (J.toNormalizedUri -> uri) version = vid
155153
vfs <- get
156-
case vfs ^. #vfsMap . at uri of
154+
case vfs ^. #vfsMap . at @(Map.Map J.NormalizedUri VirtualFile) uri of
157155
Just (VirtualFile _ file_ver contents) -> do
158156
contents' <- applyChanges logger contents changes
159157
#vfsMap . at uri .= Just (VirtualFile version (file_ver + 1) contents')
@@ -216,7 +214,8 @@ applyDeleteFile logger (J.DeleteFile _ann _kind (J.toNormalizedUri -> uri) optio
216214
when (options ^? _Just . #recursive . _Just == Just True) $
217215
logger <& CantRecursiveDelete uri `WithSeverity` Warning
218216
-- Remove and get the old value so we can check if it was missing
219-
old <- #vfsMap . at uri <.= Nothing
217+
old <- gets (view $ #vfsMap . at uri)
218+
#vfsMap . at uri .= Nothing
220219
case old of
221220
-- It's not entirely clear what the semantics of 'ignoreIfNotExists' are, but if it
222221
-- doesn't exist and we're not ignoring it, let's at least log it.
@@ -232,18 +231,18 @@ applyTextDocumentEdit logger (J.TextDocumentEdit vid edits) = do
232231
let sortedEdits = sortOn (Down . editRange) edits
233232
changeEvents = map editToChangeEvent sortedEdits
234233
-- TODO: is this right?
235-
vid' = J.VersionedTextDocumentIdentifier (vid ^. #uri) (case vid ^. #version of J.InL v -> v; J.InR _ -> 0)
234+
vid' = J.VersionedTextDocumentIdentifier vid.uri (case vid.version of J.InL v -> v; J.InR _ -> 0)
236235
ps = J.DidChangeTextDocumentParams vid' changeEvents
237236
notif = J.TNotificationMessage "" J.SMethod_TextDocumentDidChange ps
238237
changeFromClientVFS logger notif
239238
where
240239
editRange :: J.TextEdit J.|? J.AnnotatedTextEdit -> J.Range
241-
editRange (J.InR e) = e ^. #range
242-
editRange (J.InL e) = e ^. #range
240+
editRange (J.InR e) = e.range
241+
editRange (J.InL e) = e.range
243242

244243
editToChangeEvent :: J.TextEdit J.|? J.AnnotatedTextEdit -> J.TextDocumentContentChangeEvent
245-
editToChangeEvent (J.InR e) = J.TextDocumentContentChangeEvent $ J.InL $ J.TextDocumentContentChangePartial{range = e ^. #range, rangeLength = Nothing, text = e ^. #newText}
246-
editToChangeEvent (J.InL e) = J.TextDocumentContentChangeEvent $ J.InL $ J.TextDocumentContentChangePartial{range = e ^. #range, rangeLength = Nothing, text = e ^. #newText}
244+
editToChangeEvent (J.InR e) = J.TextDocumentContentChangeEvent $ J.InL $ J.TextDocumentContentChangePartial{range = e.range, rangeLength = Nothing, text = e.newText}
245+
editToChangeEvent (J.InL e) = J.TextDocumentContentChangeEvent $ J.InL $ J.TextDocumentContentChangePartial{range = e.range, rangeLength = Nothing, text = e.newText}
247246

248247
applyDocumentChange :: (MonadState VFS m) => LogAction m (WithSeverity VfsLog) -> J.DocumentChange -> m ()
249248
applyDocumentChange logger (J.InL change) = applyTextDocumentEdit logger change
@@ -254,7 +253,7 @@ applyDocumentChange logger (J.InR (J.InR (J.InR change))) = applyDeleteFile logg
254253
-- | Applies the changes from a 'ApplyWorkspaceEditRequest' to the 'VFS'
255254
changeFromServerVFS :: forall m. MonadState VFS m => LogAction m (WithSeverity VfsLog) -> J.TMessage 'J.Method_WorkspaceApplyEdit -> m ()
256255
changeFromServerVFS logger msg = do
257-
let J.ApplyWorkspaceEditParams _label edit = msg ^. #params
256+
let J.ApplyWorkspaceEditParams _label edit = msg.params
258257
J.WorkspaceEdit mChanges mDocChanges _anns = edit
259258
case mDocChanges of
260259
Just docChanges -> applyDocumentChanges docChanges
@@ -270,7 +269,7 @@ changeFromServerVFS logger msg = do
270269

271270
-- for sorting [DocumentChange]
272271
project :: J.DocumentChange -> Maybe J.Int32
273-
project (J.InL textDocumentEdit) = case textDocumentEdit ^. #textDocument . #version of
272+
project (J.InL textDocumentEdit) = case textDocumentEdit.textDocument.version of
274273
J.InL v -> Just v
275274
_ -> Nothing
276275
project _ = Nothing
@@ -313,7 +312,7 @@ persistFileVFS logger dir vfs uri =
313312

314313
closeVFS :: (MonadState VFS m) => LogAction m (WithSeverity VfsLog) -> J.TMessage 'J.Method_TextDocumentDidClose -> m ()
315314
closeVFS logger msg = do
316-
let J.DidCloseTextDocumentParams (J.TextDocumentIdentifier (J.toNormalizedUri -> uri)) = msg ^. #params
315+
let J.DidCloseTextDocumentParams (J.TextDocumentIdentifier (J.toNormalizedUri -> uri)) = msg.params
317316
logger <& Closing uri `WithSeverity` Debug
318317
#vfsMap . at uri .= Nothing
319318

@@ -330,11 +329,11 @@ applyChanges logger = foldM (applyChange logger)
330329

331330
applyChange :: (Monad m) => LogAction m (WithSeverity VfsLog) -> Rope -> J.TextDocumentContentChangeEvent -> m Rope
332331
applyChange logger str (J.TextDocumentContentChangeEvent (J.InL e))
333-
| J.Range (J.Position sl sc) (J.Position fl fc) <- e ^. #range
334-
, txt <- e ^. #text =
332+
| J.Range (J.Position sl sc) (J.Position fl fc) <- e.range
333+
, txt <- e.text =
335334
changeChars logger str (Utf16.Position (fromIntegral sl) (fromIntegral sc)) (Utf16.Position (fromIntegral fl) (fromIntegral fc)) txt
336335
applyChange _ _ (J.TextDocumentContentChangeEvent (J.InR e)) =
337-
pure $ Rope.fromText $ e ^. #text
336+
pure $ Rope.fromText $ e.text
338337

339338
-- ---------------------------------------------------------------------
340339

@@ -356,9 +355,9 @@ changeChars logger str start finish new = do
356355
Unicode code points instead of UTF-16 code units.
357356
-}
358357
data CodePointPosition = CodePointPosition
359-
{ _line :: J.UInt
358+
{ line :: J.UInt
360359
-- ^ Line position in a document (zero-based).
361-
, _character :: J.UInt
360+
, character :: J.UInt
362361
-- ^ Character offset on a line in a document in *code points* (zero-based).
363362
}
364363
deriving (Show, Read, Eq, Ord)
@@ -367,16 +366,13 @@ data CodePointPosition = CodePointPosition
367366
Unicode code points instead of UTF-16 code units.
368367
-}
369368
data CodePointRange = CodePointRange
370-
{ _start :: CodePointPosition
369+
{ start :: CodePointPosition
371370
-- ^ The range's start position.
372-
, _end :: CodePointPosition
371+
, end :: CodePointPosition
373372
-- ^ The range's end position.
374373
}
375374
deriving (Show, Read, Eq, Ord)
376375

377-
makeFieldsNoPrefix ''CodePointPosition
378-
makeFieldsNoPrefix ''CodePointRange
379-
380376
{- Note [Converting between code points and code units]
381377
This is inherently a somewhat expensive operation, but we take some care to minimize the cost.
382378
In particular, we use the good asymptotics of 'Rope' to our advantage:
@@ -464,3 +460,9 @@ rangeLinesFromVfs (VirtualFile _ _ ropetext) (J.Range (J.Position lf _cf) (J.Pos
464460
(_, s1) = Rope.splitAtLine (fromIntegral lf) ropetext
465461
(s2, _) = Rope.splitAtLine (fromIntegral (lt - lf)) s1
466462
r = Rope.toText s2
463+
464+
(.=) :: MonadState s m => ASetter s s a b -> b -> m ()
465+
l .= b = modify (l .~ b)
466+
467+
(%=) :: MonadState s m => ASetter s s a b -> (a -> b) -> m ()
468+
l %= f = modify (l %~ f)

0 commit comments

Comments
 (0)