@@ -24,8 +24,8 @@ module Haskell.Ide.Engine.PluginsIdeMonads
24
24
, allLspCmdIds
25
25
, mkLspCmdId
26
26
-- * Plugins
27
- , PluginId
28
- , CommandName
27
+ , PluginId ( .. )
28
+ , CommandId ( .. )
29
29
, PluginDescriptor (.. )
30
30
, pluginDescToIdePlugins
31
31
, PluginCommand (.. )
@@ -105,6 +105,7 @@ import UnliftIO
105
105
import Control.Applicative
106
106
107
107
import Data.Aeson hiding (defaultOptions )
108
+ import Data.Coerce
108
109
import qualified Data.ConstrainedDynamic as CD
109
110
import Data.Default
110
111
import qualified Data.List as List
@@ -113,6 +114,7 @@ import qualified Data.Map as Map
113
114
import Data.Maybe
114
115
import Data.Monoid ( (<>) )
115
116
import qualified Data.Set as S
117
+ import Data.String
116
118
import qualified Data.Text as T
117
119
import Data.Typeable ( TypeRep
118
120
, Typeable
@@ -175,7 +177,7 @@ instance HasPidCache IO where
175
177
instance HasPidCache m => HasPidCache (IdeResultT m ) where
176
178
getPidCache = lift getPidCache
177
179
178
- mkLspCommand :: HasPidCache m => PluginId -> CommandName -> T. Text -> Maybe [Value ] -> m Command
180
+ mkLspCommand :: HasPidCache m => PluginId -> CommandId -> T. Text -> Maybe [Value ] -> m Command
179
181
mkLspCommand plid cn title args' = do
180
182
cmdId <- mkLspCmdId plid cn
181
183
let args = List <$> args'
@@ -184,12 +186,12 @@ mkLspCommand plid cn title args' = do
184
186
allLspCmdIds :: HasPidCache m => IdePlugins -> m [T. Text ]
185
187
allLspCmdIds (IdePlugins m) = concat <$> mapM go (Map. toList (pluginCommands <$> m))
186
188
where
187
- go (plid, cmds) = mapM (mkLspCmdId plid . commandName ) cmds
189
+ go (plid, cmds) = mapM (mkLspCmdId plid . commandId ) cmds
188
190
189
- mkLspCmdId :: HasPidCache m => PluginId -> CommandName -> m T. Text
190
- mkLspCmdId plid cn = do
191
+ mkLspCmdId :: HasPidCache m => PluginId -> CommandId -> m T. Text
192
+ mkLspCmdId plid cid = do
191
193
pid <- T. pack . show <$> getPidCache
192
- return $ pid <> " :" <> plid <> " :" <> cn
194
+ return $ pid <> " :" <> coerce plid <> " :" <> coerce cid
193
195
194
196
-- ---------------------------------------------------------------------
195
197
-- Plugins
@@ -260,6 +262,11 @@ type FormattingProvider = T.Text -- ^ Text to format
260
262
-> FormattingOptions -- ^ Options for the formatter
261
263
-> IdeM (IdeResult [TextEdit ]) -- ^ Result of the formatting or the unchanged text.
262
264
265
+ newtype PluginId = PluginId T. Text
266
+ deriving (Show , Read , Eq , Ord )
267
+ instance IsString PluginId where
268
+ fromString = PluginId . T. pack
269
+
263
270
data PluginDescriptor =
264
271
PluginDescriptor { pluginId :: PluginId
265
272
, pluginCommands :: [PluginCommand ]
@@ -271,13 +278,15 @@ data PluginDescriptor =
271
278
} deriving (Generic )
272
279
273
280
instance Show PluginCommand where
274
- show (PluginCommand name _) = " PluginCommand { name = " ++ T. unpack name ++ " }"
281
+ show (PluginCommand i _) = " PluginCommand { name = " ++ show i ++ " }"
275
282
276
- type PluginId = T. Text
277
- type CommandName = T. Text
283
+ newtype CommandId = CommandId T. Text
284
+ deriving (Show , Read , Eq , Ord )
285
+ instance IsString CommandId where
286
+ fromString = CommandId . T. pack
278
287
279
288
data PluginCommand = forall a b . (FromJSON a , ToJSON b , Typeable b ) =>
280
- PluginCommand { commandName :: CommandName
289
+ PluginCommand { commandId :: CommandId
281
290
, commandFunc :: a -> IdeGhcM (IdeResult b )
282
291
}
283
292
@@ -295,21 +304,21 @@ fromDynJSON = CD.fromDynamic
295
304
toDynJSON :: (Typeable a , ToJSON a ) => a -> DynamicJSON
296
305
toDynJSON = CD. toDyn
297
306
298
- -- | Runs a plugin command given a PluginId, CommandName and
307
+ -- | Runs a plugin command given a PluginId, CommandId and
299
308
-- arguments in the form of a JSON object.
300
- runPluginCommand :: PluginId -> CommandName -> Value
309
+ runPluginCommand :: PluginId -> CommandId -> Value
301
310
-> IdeGhcM (IdeResult DynamicJSON )
302
311
runPluginCommand p com arg = do
303
312
IdePlugins m <- getPlugins
304
313
case Map. lookup p m of
305
314
Nothing -> return $
306
- IdeResultFail $ IdeError UnknownPlugin (" Plugin " <> p <> " doesn't exist" ) Null
307
- Just PluginDescriptor { pluginCommands = xs } -> case List. find ((com == ) . commandName ) xs of
315
+ IdeResultFail $ IdeError UnknownPlugin (" Plugin " <> coerce p <> " doesn't exist" ) Null
316
+ Just PluginDescriptor { pluginCommands = xs } -> case List. find ((com == ) . commandId ) xs of
308
317
Nothing -> return $ IdeResultFail $
309
- IdeError UnknownCommand (" Command " <> com <> " isn't defined for plugin " <> p <> " . Legal commands are: " <> T. pack(show $ map commandName xs)) Null
318
+ IdeError UnknownCommand (" Command " <> coerce com <> " isn't defined for plugin " <> coerce p <> " . Legal commands are: " <> T. pack(show $ map commandId xs)) Null
310
319
Just (PluginCommand _ f) -> case fromJSON arg of
311
320
Error err -> return $ IdeResultFail $
312
- IdeError ParameterError (" error while parsing args for " <> com <> " in plugin " <> p <> " : " <> T. pack err) Null
321
+ IdeError ParameterError (" error while parsing args for " <> coerce com <> " in plugin " <> coerce p <> " : " <> T. pack err) Null
313
322
Success a -> do
314
323
res <- f a
315
324
return $ fmap toDynJSON res
@@ -319,11 +328,6 @@ newtype IdePlugins = IdePlugins
319
328
{ ipMap :: Map. Map PluginId PluginDescriptor
320
329
} deriving (Generic )
321
330
322
- -- TODO:AZ this is a defective instance, do we actually need it?
323
- -- Perhaps rather make a separate type explicitly for this purpose.
324
- instance ToJSON IdePlugins where
325
- toJSON (IdePlugins m) = toJSON $ fmap commandName <$> fmap pluginCommands m
326
-
327
331
-- | For the diagnostic providers in the config, return a map of
328
332
-- current enabled state, indexed by the plugin id.
329
333
getDiagnosticProvidersConfig :: Config -> Map. Map PluginId Bool
0 commit comments