Skip to content

Commit ad7bc0e

Browse files
authored
Merge pull request #384 from Bodigrim/master
Eliminate dependent-sum and dependent-map dependencies
2 parents f14a077 + c6bde6a commit ad7bc0e

File tree

6 files changed

+85
-24
lines changed

6 files changed

+85
-24
lines changed

.github/workflows/haskell.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
strategy:
1010
fail-fast: false
1111
matrix:
12-
ghc: ['9.0.1', '8.10.7', '8.8.4', '8.6.5']
12+
ghc: ['9.2.1', '9.0.1', '8.10.7', '8.8.4', '8.6.5']
1313
os: [ubuntu-latest, macOS-latest, windows-latest]
1414

1515
steps:

lsp-types/lsp-types.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ library
2020
exposed-modules: Language.LSP.Types
2121
, Language.LSP.Types.Capabilities
2222
, Language.LSP.Types.Lens
23+
, Language.LSP.Types.SMethodMap
2324
, Language.LSP.VFS
2425
, Data.IxMap
2526
other-modules: Language.LSP.Types.CallHierarchy
@@ -90,7 +91,6 @@ library
9091
, rope-utf16-splay >= 0.3.1.0
9192
, scientific
9293
, some
93-
, dependent-sum >= 0.7.1.0
9494
, text
9595
, template-haskell
9696
, temporary
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{-# LANGUAGE DataKinds #-}
2+
{-# LANGUAGE GADTs #-}
3+
{-# LANGUAGE MagicHash #-}
4+
{-# LANGUAGE PolyKinds #-}
5+
{-# LANGUAGE RankNTypes #-}
6+
7+
module Language.LSP.Types.SMethodMap
8+
( SMethodMap
9+
, singleton
10+
, insert
11+
, delete
12+
, member
13+
, lookup
14+
, map
15+
) where
16+
17+
import Prelude hiding (lookup, map)
18+
import Data.IntMap (IntMap)
19+
import qualified Data.IntMap.Strict as IntMap
20+
import Data.Kind (Type)
21+
import Data.Map (Map)
22+
import qualified Data.Map.Strict as Map
23+
import Data.Text (Text)
24+
import GHC.Exts (Int(..), dataToTag#)
25+
import Unsafe.Coerce (unsafeCoerce)
26+
27+
import Language.LSP.Types.Method (Method(..), SMethod(..))
28+
29+
data SMethodMap (v :: Method f t -> Type) =
30+
SMethodMap !(IntMap (v 'CustomMethod)) !(Map Text (v 'CustomMethod))
31+
32+
toIx :: SMethod a -> Int
33+
toIx k = I# (dataToTag# k)
34+
35+
singleton :: SMethod a -> v a -> SMethodMap v
36+
singleton (SCustomMethod t) v = SMethodMap mempty (Map.singleton t v)
37+
singleton k v = SMethodMap (IntMap.singleton (toIx k) (unsafeCoerce v)) mempty
38+
39+
insert :: SMethod a -> v a -> SMethodMap v -> SMethodMap v
40+
insert (SCustomMethod t) v (SMethodMap xs ys) = SMethodMap xs (Map.insert t v ys)
41+
insert k v (SMethodMap xs ys) = SMethodMap (IntMap.insert (toIx k) (unsafeCoerce v) xs) ys
42+
43+
delete :: SMethod a -> SMethodMap v -> SMethodMap v
44+
delete (SCustomMethod t) (SMethodMap xs ys) = SMethodMap xs (Map.delete t ys)
45+
delete k (SMethodMap xs ys) = SMethodMap (IntMap.delete (toIx k) xs) ys
46+
47+
member :: SMethod a -> SMethodMap v -> Bool
48+
member (SCustomMethod t) (SMethodMap _ ys) = Map.member t ys
49+
member k (SMethodMap xs _) = IntMap.member (toIx k) xs
50+
51+
lookup :: SMethod a -> SMethodMap v -> Maybe (v a)
52+
lookup (SCustomMethod t) (SMethodMap _ ys) = Map.lookup t ys
53+
lookup k (SMethodMap xs _) = unsafeCoerce (IntMap.lookup (toIx k) xs)
54+
55+
map :: (forall a. u a -> v a) -> SMethodMap u -> SMethodMap v
56+
map f (SMethodMap xs ys) = SMethodMap (IntMap.map f xs) (Map.map f ys)
57+
58+
instance Semigroup (SMethodMap v) where
59+
SMethodMap xs ys <> SMethodMap xs' ys' = SMethodMap (xs <> xs') (ys <> ys')
60+
61+
instance Monoid (SMethodMap v) where
62+
mempty = SMethodMap mempty mempty

lsp/lsp.cabal

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ library
4242
, hslogger
4343
, hashable
4444
, lsp-types == 1.4.*
45-
, dependent-map
4645
, lens >= 4.15.2
4746
, mtl
4847
, network-uri

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ import qualified Data.Aeson as J
4242
import Data.Default
4343
import Data.Functor.Product
4444
import Data.IxMap
45-
import qualified Data.Dependent.Map as DMap
46-
import Data.Dependent.Map (DMap)
4745
import qualified Data.HashMap.Strict as HM
4846
import Data.Kind
4947
import qualified Data.List as L
@@ -56,6 +54,8 @@ import Data.Text ( Text )
5654
import qualified Data.UUID as UUID
5755
import qualified Language.LSP.Types.Capabilities as J
5856
import Language.LSP.Types as J
57+
import Language.LSP.Types.SMethodMap (SMethodMap)
58+
import qualified Language.LSP.Types.SMethodMap as SMethodMap
5959
import qualified Language.LSP.Types.Lens as J
6060
import Language.LSP.VFS
6161
import Language.LSP.Diagnostics
@@ -131,19 +131,19 @@ data LanguageContextEnv config =
131131
-- @
132132
data Handlers m
133133
= Handlers
134-
{ reqHandlers :: !(DMap SMethod (ClientMessageHandler m Request))
135-
, notHandlers :: !(DMap SMethod (ClientMessageHandler m Notification))
134+
{ reqHandlers :: !(SMethodMap (ClientMessageHandler m Request))
135+
, notHandlers :: !(SMethodMap (ClientMessageHandler m Notification))
136136
}
137137
instance Semigroup (Handlers config) where
138138
Handlers r1 n1 <> Handlers r2 n2 = Handlers (r1 <> r2) (n1 <> n2)
139139
instance Monoid (Handlers config) where
140140
mempty = Handlers mempty mempty
141141

142142
notificationHandler :: forall (m :: Method FromClient Notification) f. SMethod m -> Handler f m -> Handlers f
143-
notificationHandler m h = Handlers mempty (DMap.singleton m (ClientMessageHandler h))
143+
notificationHandler m h = Handlers mempty (SMethodMap.singleton m (ClientMessageHandler h))
144144

145145
requestHandler :: forall (m :: Method FromClient Request) f. SMethod m -> Handler f m -> Handlers f
146-
requestHandler m h = Handlers (DMap.singleton m (ClientMessageHandler h)) mempty
146+
requestHandler m h = Handlers (SMethodMap.singleton m (ClientMessageHandler h)) mempty
147147

148148
-- | Wrapper to restrict 'Handler's to 'FromClient' 'Method's
149149
newtype ClientMessageHandler f (t :: MethodType) (m :: Method FromClient t) = ClientMessageHandler (Handler f m)
@@ -170,8 +170,8 @@ mapHandlers
170170
-> Handlers m -> Handlers n
171171
mapHandlers mapReq mapNot (Handlers reqs nots) = Handlers reqs' nots'
172172
where
173-
reqs' = DMap.map (\(ClientMessageHandler i) -> ClientMessageHandler $ mapReq i) reqs
174-
nots' = DMap.map (\(ClientMessageHandler i) -> ClientMessageHandler $ mapNot i) nots
173+
reqs' = SMethodMap.map (\(ClientMessageHandler i) -> ClientMessageHandler $ mapReq i) reqs
174+
nots' = SMethodMap.map (\(ClientMessageHandler i) -> ClientMessageHandler $ mapNot i) nots
175175

176176
-- | state used by the LSP dispatcher to manage the message loop
177177
data LanguageContextState config =
@@ -189,7 +189,7 @@ data LanguageContextState config =
189189

190190
type ResponseMap = IxMap LspId (Product SMethod ServerResponseCallback)
191191

192-
type RegistrationMap (t :: MethodType) = DMap SMethod (Product RegistrationId (ClientMessageHandler IO t))
192+
type RegistrationMap (t :: MethodType) = SMethodMap (Product RegistrationId (ClientMessageHandler IO t))
193193

194194
data RegistrationToken (m :: Method FromClient t) = RegistrationToken (SMethod m) (RegistrationId m)
195195
newtype RegistrationId (m :: Method FromClient t) = RegistrationId Text
@@ -496,8 +496,8 @@ registerCapability method regOpts f = do
496496
clientCaps <- resClientCapabilities <$> getLspEnv
497497
handlers <- resHandlers <$> getLspEnv
498498
let alreadyStaticallyRegistered = case splitClientMethod method of
499-
IsClientNot -> DMap.member method $ notHandlers handlers
500-
IsClientReq -> DMap.member method $ reqHandlers handlers
499+
IsClientNot -> SMethodMap.member method $ notHandlers handlers
500+
IsClientReq -> SMethodMap.member method $ reqHandlers handlers
501501
IsClientEither -> error "Cannot register capability for custom methods"
502502
go clientCaps alreadyStaticallyRegistered
503503
where
@@ -515,10 +515,10 @@ registerCapability method regOpts f = do
515515
~() <- case splitClientMethod method of
516516
IsClientNot -> modifyState resRegistrationsNot $ \oldRegs ->
517517
let pair = Pair regId (ClientMessageHandler (unliftIO rio . f))
518-
in DMap.insert method pair oldRegs
518+
in SMethodMap.insert method pair oldRegs
519519
IsClientReq -> modifyState resRegistrationsReq $ \oldRegs ->
520520
let pair = Pair regId (ClientMessageHandler (\msg k -> unliftIO rio $ f msg (liftIO . k)))
521-
in DMap.insert method pair oldRegs
521+
in SMethodMap.insert method pair oldRegs
522522
IsClientEither -> error "Cannot register capability for custom methods"
523523

524524
-- TODO: handle the scenario where this returns an error
@@ -572,8 +572,8 @@ registerCapability method regOpts f = do
572572
unregisterCapability :: MonadLsp config f => RegistrationToken m -> f ()
573573
unregisterCapability (RegistrationToken m (RegistrationId uuid)) = do
574574
~() <- case splitClientMethod m of
575-
IsClientReq -> modifyState resRegistrationsReq $ DMap.delete m
576-
IsClientNot -> modifyState resRegistrationsNot $ DMap.delete m
575+
IsClientReq -> modifyState resRegistrationsReq $ SMethodMap.delete m
576+
IsClientNot -> modifyState resRegistrationsNot $ SMethodMap.delete m
577577
IsClientEither -> error "Cannot unregister capability for custom methods"
578578

579579
let unregistration = J.Unregistration uuid (J.SomeClientMethod m)

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import qualified Data.Text.Lazy.Encoding as TL
2222
import Language.LSP.Types
2323
import Language.LSP.Types.Capabilities
2424
import qualified Language.LSP.Types.Lens as LSP
25+
import Language.LSP.Types.SMethodMap (SMethodMap)
26+
import qualified Language.LSP.Types.SMethodMap as SMethodMap
2527
import Language.LSP.Server.Core
2628
import Language.LSP.VFS
2729
import Data.Functor.Product
@@ -34,9 +36,7 @@ import Control.Monad.Trans.Except
3436
import Control.Monad.Reader
3537
import Data.IxMap
3638
import System.Log.Logger
37-
import qualified Data.Dependent.Map as DMap
3839
import Data.Maybe
39-
import Data.Dependent.Map (DMap)
4040
import qualified Data.Map.Strict as Map
4141
import System.Exit
4242
import Data.Default (def)
@@ -185,8 +185,8 @@ inferServerCapabilities clientCaps o h =
185185

186186
supported_b :: forall m. SClientMethod m -> Bool
187187
supported_b m = case splitClientMethod m of
188-
IsClientNot -> DMap.member m $ notHandlers h
189-
IsClientReq -> DMap.member m $ reqHandlers h
188+
IsClientNot -> SMethodMap.member m $ notHandlers h
189+
IsClientReq -> SMethodMap.member m $ reqHandlers h
190190
IsClientEither -> error "capabilities depend on custom method"
191191

192192
singleton :: a -> [a]
@@ -335,8 +335,8 @@ handle' mAction m msg = do
335335
where
336336
-- | Checks to see if there's a dynamic handler, and uses it in favour of the
337337
-- static handler, if it exists.
338-
pickHandler :: RegistrationMap t -> DMap SMethod (ClientMessageHandler IO t) -> Maybe (Handler IO m)
339-
pickHandler dynHandlerMap staticHandler = case (DMap.lookup m dynHandlerMap, DMap.lookup m staticHandler) of
338+
pickHandler :: RegistrationMap t -> SMethodMap (ClientMessageHandler IO t) -> Maybe (Handler IO m)
339+
pickHandler dynHandlerMap staticHandler = case (SMethodMap.lookup m dynHandlerMap, SMethodMap.lookup m staticHandler) of
340340
(Just (Pair _ (ClientMessageHandler h)), _) -> Just h
341341
(Nothing, Just (ClientMessageHandler h)) -> Just h
342342
(Nothing, Nothing) -> Nothing

0 commit comments

Comments
 (0)