Skip to content
This repository was archived by the owner on Oct 7, 2020. It is now read-only.

Commit 7ca1293

Browse files
committed
CommandName -> CommandId, make that and PluginId newtypes
We definitely do not want to get those mixed up with plain old texts
1 parent 0a347bb commit 7ca1293

File tree

5 files changed

+38
-33
lines changed

5 files changed

+38
-33
lines changed

hie-plugin-api/Haskell/Ide/Engine/PluginsIdeMonads.hs

+26-22
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ module Haskell.Ide.Engine.PluginsIdeMonads
2424
, allLspCmdIds
2525
, mkLspCmdId
2626
-- * Plugins
27-
, PluginId
28-
, CommandName
27+
, PluginId(..)
28+
, CommandId(..)
2929
, PluginDescriptor(..)
3030
, pluginDescToIdePlugins
3131
, PluginCommand(..)
@@ -105,6 +105,7 @@ import UnliftIO
105105
import Control.Applicative
106106

107107
import Data.Aeson hiding (defaultOptions)
108+
import Data.Coerce
108109
import qualified Data.ConstrainedDynamic as CD
109110
import Data.Default
110111
import qualified Data.List as List
@@ -113,6 +114,7 @@ import qualified Data.Map as Map
113114
import Data.Maybe
114115
import Data.Monoid ( (<>) )
115116
import qualified Data.Set as S
117+
import Data.String
116118
import qualified Data.Text as T
117119
import Data.Typeable ( TypeRep
118120
, Typeable
@@ -175,7 +177,7 @@ instance HasPidCache IO where
175177
instance HasPidCache m => HasPidCache (IdeResultT m) where
176178
getPidCache = lift getPidCache
177179

178-
mkLspCommand :: HasPidCache m => PluginId -> CommandName -> T.Text -> Maybe [Value] -> m Command
180+
mkLspCommand :: HasPidCache m => PluginId -> CommandId -> T.Text -> Maybe [Value] -> m Command
179181
mkLspCommand plid cn title args' = do
180182
cmdId <- mkLspCmdId plid cn
181183
let args = List <$> args'
@@ -184,12 +186,12 @@ mkLspCommand plid cn title args' = do
184186
allLspCmdIds :: HasPidCache m => IdePlugins -> m [T.Text]
185187
allLspCmdIds (IdePlugins m) = concat <$> mapM go (Map.toList (pluginCommands <$> m))
186188
where
187-
go (plid, cmds) = mapM (mkLspCmdId plid . commandName) cmds
189+
go (plid, cmds) = mapM (mkLspCmdId plid . commandId) cmds
188190

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
191193
pid <- T.pack . show <$> getPidCache
192-
return $ pid <> ":" <> plid <> ":" <> cn
194+
return $ pid <> ":" <> coerce plid <> ":" <> coerce cid
193195

194196
-- ---------------------------------------------------------------------
195197
-- Plugins
@@ -260,6 +262,11 @@ type FormattingProvider = T.Text -- ^ Text to format
260262
-> FormattingOptions -- ^ Options for the formatter
261263
-> IdeM (IdeResult [TextEdit]) -- ^ Result of the formatting or the unchanged text.
262264

265+
newtype PluginId = PluginId T.Text
266+
deriving (Show, Read, Eq, Ord)
267+
instance IsString PluginId where
268+
fromString = PluginId . T.pack
269+
263270
data PluginDescriptor =
264271
PluginDescriptor { pluginId :: PluginId
265272
, pluginCommands :: [PluginCommand]
@@ -271,13 +278,15 @@ data PluginDescriptor =
271278
} deriving (Generic)
272279

273280
instance Show PluginCommand where
274-
show (PluginCommand name _) = "PluginCommand { name = " ++ T.unpack name ++ " }"
281+
show (PluginCommand i _) = "PluginCommand { name = " ++ show i ++ " }"
275282

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
278287

279288
data PluginCommand = forall a b. (FromJSON a, ToJSON b, Typeable b) =>
280-
PluginCommand { commandName :: CommandName
289+
PluginCommand { commandId :: CommandId
281290
, commandFunc :: a -> IdeGhcM (IdeResult b)
282291
}
283292

@@ -295,21 +304,21 @@ fromDynJSON = CD.fromDynamic
295304
toDynJSON :: (Typeable a, ToJSON a) => a -> DynamicJSON
296305
toDynJSON = CD.toDyn
297306

298-
-- | Runs a plugin command given a PluginId, CommandName and
307+
-- | Runs a plugin command given a PluginId, CommandId and
299308
-- arguments in the form of a JSON object.
300-
runPluginCommand :: PluginId -> CommandName -> Value
309+
runPluginCommand :: PluginId -> CommandId -> Value
301310
-> IdeGhcM (IdeResult DynamicJSON)
302311
runPluginCommand p com arg = do
303312
IdePlugins m <- getPlugins
304313
case Map.lookup p m of
305314
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
308317
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
310319
Just (PluginCommand _ f) -> case fromJSON arg of
311320
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
313322
Success a -> do
314323
res <- f a
315324
return $ fmap toDynJSON res
@@ -319,11 +328,6 @@ newtype IdePlugins = IdePlugins
319328
{ ipMap :: Map.Map PluginId PluginDescriptor
320329
} deriving (Generic)
321330

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-
327331
-- | For the diagnostic providers in the config, return a map of
328332
-- current enabled state, indexed by the plugin id.
329333
getDiagnosticProvidersConfig :: Config -> Map.Map PluginId Bool

src/Haskell/Ide/Engine/Plugin/Package.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import Distribution.Types.CondTree
5050
import qualified Distribution.PackageDescription.PrettyPrint as PP
5151
import qualified Data.Yaml as Y
5252

53-
packageDescriptor :: T.Text -> PluginDescriptor
53+
packageDescriptor :: PluginId -> PluginDescriptor
5454
packageDescriptor plId = PluginDescriptor
5555
{ pluginId = plId
5656
, pluginCommands = [PluginCommand "add" addCmd]

src/Haskell/Ide/Engine/Server.hs

+7-6
Original file line numberDiff line numberDiff line change
@@ -558,10 +558,10 @@ reactor inp diagIn = do
558558

559559
let params = req ^. J.params
560560

561-
parseCmdId :: T.Text -> Maybe (T.Text, T.Text)
561+
parseCmdId :: T.Text -> Maybe (PluginId, CommandId)
562562
parseCmdId x = case T.splitOn ":" x of
563-
[plugin, command] -> Just (plugin, command)
564-
[_, plugin, command] -> Just (plugin, command)
563+
[plugin, command] -> Just (PluginId plugin, CommandId command)
564+
[_, plugin, command] -> Just (PluginId plugin, CommandId command)
565565
_ -> Nothing
566566

567567
callback obj = do
@@ -854,22 +854,23 @@ requestDiagnostics DiagnosticsRequest{trigger, file, trackingNumber, documentVer
854854
forM_ dss $ \(pid,ds) -> do
855855
debugm $ "requestDiagnostics: calling diagFunc for plugin:" ++ show pid
856856
let
857+
pid' = coerce pid
857858
enabled = Map.findWithDefault True pid dpsEnabled
858859
publishDiagnosticsIO = Core.publishDiagnosticsFunc lf
859860
maxToSend = maxNumberOfProblems clientConfig
860861
sendOne (fileUri,ds') = do
861862
debugm $ "LspStdio.sendone:(fileUri,ds')=" ++ show(fileUri,ds')
862-
publishDiagnosticsIO maxToSend (J.toNormalizedUri fileUri) Nothing (Map.fromList [(Just pid,SL.toSortedList ds')])
863+
publishDiagnosticsIO maxToSend (J.toNormalizedUri fileUri) Nothing (Map.fromList [(Just pid',SL.toSortedList ds')])
863864

864865
sendEmpty = do
865866
debugm "LspStdio.sendempty"
866-
publishDiagnosticsIO maxToSend (J.toNormalizedUri file) Nothing (Map.fromList [(Just pid,SL.toSortedList [])])
867+
publishDiagnosticsIO maxToSend (J.toNormalizedUri file) Nothing (Map.fromList [(Just pid',SL.toSortedList [])])
867868

868869
-- fv = case documentVersion of
869870
-- Nothing -> Nothing
870871
-- Just v -> Just (file,v)
871872
-- let fakeId = J.IdString "fake,remove" -- TODO:AZ: IReq should take a Maybe LspId
872-
let fakeId = J.IdString ("fake,remove:pid=" <> pid) -- TODO:AZ: IReq should take a Maybe LspId
873+
let fakeId = J.IdString ("fake,remove:pid=" <> pid') -- TODO:AZ: IReq should take a Maybe LspId
873874
let reql = case ds of
874875
DiagnosticProviderSync dps ->
875876
IReq trackingNumber "diagnostics" fakeId callbackl

src/Haskell/Ide/Engine/Support/HieExtras.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ splitCaseCmd' uri newPos =
436436
getFormattingPlugin :: Config -> IdePlugins -> Maybe (PluginDescriptor, FormattingProvider)
437437
getFormattingPlugin config plugins = do
438438
let providerName = formattingProvider config
439-
fmtPlugin <- Map.lookup providerName (ipMap plugins)
439+
fmtPlugin <- Map.lookup (PluginId providerName) (ipMap plugins)
440440
fmtProvider <- pluginFormattingProvider fmtPlugin
441441
return (fmtPlugin, fmtProvider)
442442

test/utils/TestUtils.hs

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ testOptions = HIE.defaultOptions { cradleOptsVerbosity = Verbose }
5151

5252

5353
testCommand :: (ToJSON a, Typeable b, ToJSON b, Show b, Eq b)
54-
=> IdePlugins -> IdeGhcM (IdeResult b) -> PluginId -> CommandName -> a -> IdeResult b -> IO ()
54+
=> IdePlugins -> IdeGhcM (IdeResult b) -> PluginId -> CommandId -> a -> IdeResult b -> IO ()
5555
testCommand testPlugins act plugin cmd arg res = do
5656
flushStackEnvironment
5757
(newApiRes, oldApiRes) <- runIGM testPlugins $ do
@@ -65,10 +65,10 @@ runSingle :: IdePlugins -> IdeGhcM (IdeResult b) -> IO (IdeResult b)
6565
runSingle testPlugins act = runIGM testPlugins act
6666

6767
runSingleReq :: ToJSON a
68-
=> IdePlugins -> PluginId -> CommandName -> a -> IO (IdeResult DynamicJSON)
68+
=> IdePlugins -> PluginId -> CommandId -> a -> IO (IdeResult DynamicJSON)
6969
runSingleReq testPlugins plugin com arg = runIGM testPlugins (makeRequest plugin com arg)
7070

71-
makeRequest :: ToJSON a => PluginId -> CommandName -> a -> IdeGhcM (IdeResult DynamicJSON)
71+
makeRequest :: ToJSON a => PluginId -> CommandId -> a -> IdeGhcM (IdeResult DynamicJSON)
7272
makeRequest plugin com arg = runPluginCommand plugin com (toJSON arg)
7373

7474
runIGM :: IdePlugins -> IdeGhcM a -> IO a

0 commit comments

Comments
 (0)