This repository was archived by the owner on Oct 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathBasePlugin.hs
141 lines (128 loc) · 5.55 KB
/
BasePlugin.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE GADTs #-}
module Haskell.Ide.Engine.BasePlugin where
import Control.Monad
import Data.Aeson
import Data.Foldable
import Data.List
import qualified Data.Map as Map
import Data.Monoid
import qualified Data.Text as T
import Data.Vinyl
import Development.GitRev (gitCommitCount)
import Distribution.System (buildArch)
import Distribution.Text (display)
import Haskell.Ide.Engine.PluginDescriptor
import Haskell.Ide.Engine.PluginUtils
import Options.Applicative.Simple (simpleVersion)
import qualified Paths_haskell_ide_engine as Meta
import Prelude hiding (log)
-- ---------------------------------------------------------------------
baseDescriptor :: PluginDescriptor
baseDescriptor = PluginDescriptor
{
pdCommands =
[
Command
{ cmdDesc = CommandDesc
{ cmdName = "version"
, cmdUiDescription = "return HIE version"
, cmdFileExtensions = []
, cmdContexts = [CtxNone]
, cmdAdditionalParams = []
}
, cmdFunc = versionCmd
}
, Command
{ cmdDesc = CommandDesc
{ cmdName = "plugins"
, cmdUiDescription = "list available plugins"
, cmdFileExtensions = []
, cmdContexts = [CtxNone]
, cmdAdditionalParams = []
}
, cmdFunc = pluginsCmd
}
, Command
{ cmdDesc = CommandDesc
{ cmdName = "commands"
, cmdUiDescription = "list available commands for a given plugin"
, cmdFileExtensions = []
, cmdContexts = [CtxNone]
, cmdAdditionalParams = [RP "plugin" "the plugin name" PtText]
}
, cmdFunc = commandsCmd
}
, Command
{ cmdDesc = CommandDesc
{ cmdName = "commandDetail"
, cmdUiDescription = "list parameters required for a given command"
, cmdFileExtensions = []
, cmdContexts = [CtxNone]
, cmdAdditionalParams = [RP "plugin" "the plugin name" PtText
,RP "command" "the command name" PtText]
}
, cmdFunc = commandDetailCmd
}
]
, pdExposedServices = []
, pdUsedServices = []
}
-- ---------------------------------------------------------------------
versionCmd :: CommandFunc
versionCmd _ _ = return (IdeResponseOk (String $ T.pack version))
pluginsCmd :: CommandFunc
pluginsCmd _ _ = do
plugins <- getPlugins
return (IdeResponseOk (toJSON $ Map.keys plugins))
commandsCmd :: CommandFunc
commandsCmd _ req = do
plugins <- getPlugins
-- TODO: Use Maybe Monad. What abut error reporting?
case Map.lookup "plugin" (ideParams req) of
Nothing -> return (missingParameter "plugin")
Just (ParamTextP p) -> case Map.lookup p plugins of
Nothing -> return (IdeResponseFail (IdeError
UnknownPlugin ("Can't find plugin:" <> p )
(Just $ toJSON $ p)))
Just pl -> return (IdeResponseOk (toJSON $ map (cmdName . cmdDesc) $ pdCommands pl))
Just x -> return $ incorrectParameter "plugin" ("ParamText"::String) x
commandDetailCmd :: CommandFunc
commandDetailCmd _ req = do
plugins <- getPlugins
case getParams (IdText "plugin" :& IdText "command" :& RNil) req of
Left err -> return err
Right (ParamText p :& ParamText command :& RNil) -> do
case Map.lookup p plugins of
Nothing -> return (IdeResponseError (IdeError
UnknownPlugin ("Can't find plugin:" <> p )
(Just $ toJSON $ p)))
Just pl -> case find (\cmd -> command == (cmdName $ cmdDesc cmd) ) (pdCommands pl) of
Nothing -> return (IdeResponseError (IdeError
UnknownCommand ("Can't find command:" <> command )
(Just $ toJSON $ command)))
Just detail -> return (IdeResponseOk (toJSON (cmdDesc detail)))
Right _ -> return (IdeResponseError (IdeError
InternalError "commandDetailCmd: ghc’s exhaustiveness checker is broken" Nothing))
-- ---------------------------------------------------------------------
version :: String
version =
let commitCount = $gitCommitCount
in concat $ concat
[ [$(simpleVersion Meta.version)]
-- Leave out number of commits for --depth=1 clone
-- See https://github.com/commercialhaskell/stack/issues/792
, [" (" ++ commitCount ++ " commits)" | commitCount /= ("1"::String) &&
commitCount /= ("UNKNOWN" :: String)]
, [" ", display buildArch]
]
-- ---------------------------------------------------------------------
replPluginInfo :: Plugins -> Map.Map T.Text (T.Text,Command)
replPluginInfo plugins = Map.fromList commands
where
commands = concatMap extractCommands $ Map.toList plugins
extractCommands (pluginName,descriptor) = cmds
where
cmds = map (\cmd -> (pluginName <> ":" <> (cmdName $ cmdDesc cmd),(pluginName,cmd))) $ pdCommands descriptor
-- ---------------------------------------------------------------------