|
| 1 | +{-# LANGUAGE OverloadedStrings #-} |
1 | 2 | {-# LANGUAGE DeriveGeneric #-}
|
2 | 3 | {-# LANGUAGE TypeSynonymInstances #-}
|
3 | 4 | {-# LANGUAGE FlexibleInstances #-}
|
|
21 | 22 |
|
22 | 23 | module Haskell.Ide.Engine.PluginDescriptor where
|
23 | 24 |
|
| 25 | +import Control.Applicative |
24 | 26 | import Control.Monad.IO.Class
|
25 | 27 | import Data.Aeson
|
26 | 28 | import Data.Aeson.Types
|
@@ -134,57 +136,105 @@ type Dispatcher = forall m. (MonadIO m,GHC.GhcMonad m,HasIdeState m)
|
134 | 136 | -- JSON instances
|
135 | 137 |
|
136 | 138 | instance ToJSON Context where
|
137 |
| - toJSON = genericToJSON defaultOptions |
| 139 | + toJSON ctxt = object [ "cabal" .= toJSON (ctxCabal ctxt) |
| 140 | + , "file" .= toJSON (ctxFile ctxt) |
| 141 | + , "start_pos" .= toJSON (ctxStartPos ctxt) |
| 142 | + , "end_pos" .= toJSON (ctxEndPos ctxt) ] |
138 | 143 |
|
139 | 144 | instance FromJSON Context where
|
140 |
| - -- No need to provide a parseJSON implementation. |
| 145 | + parseJSON (Object v) = |
| 146 | + Context <$> v .: "cabal" |
| 147 | + <*> v .: "file" |
| 148 | + <*> v .: "start_pos" |
| 149 | + <*> v .: "end_pos" |
| 150 | + parseJSON _ = empty |
141 | 151 |
|
142 | 152 | -- -------------------------------------
|
143 | 153 |
|
144 | 154 | instance ToJSON CabalSection where
|
145 |
| - toJSON = genericToJSON defaultOptions |
| 155 | + toJSON (CabalSection s) = toJSON s |
146 | 156 |
|
147 | 157 | instance FromJSON CabalSection where
|
148 |
| - -- No need to provide a parseJSON implementation. |
| 158 | + parseJSON (String s) = pure $ CabalSection s |
| 159 | + parseJSON _ = empty |
149 | 160 |
|
150 | 161 | -- -------------------------------------
|
151 | 162 |
|
152 | 163 | instance ToJSON AcceptedContext where
|
153 |
| - toJSON = genericToJSON defaultOptions |
| 164 | + toJSON CtxNone = String "none" |
| 165 | + toJSON CtxPoint = String "point" |
| 166 | + toJSON CtxRegion = String "region" |
| 167 | + toJSON CtxFile = String "file" |
| 168 | + toJSON CtxCabalTarget = String "cabal_target" |
| 169 | + toJSON CtxProject = String "project" |
154 | 170 |
|
155 | 171 | instance FromJSON AcceptedContext where
|
156 |
| - -- No need to provide a parseJSON implementation. |
| 172 | + parseJSON (String "none") = pure CtxNone |
| 173 | + parseJSON (String "point") = pure CtxPoint |
| 174 | + parseJSON (String "region") = pure CtxRegion |
| 175 | + parseJSON (String "file") = pure CtxFile |
| 176 | + parseJSON (String "cabal_target") = pure CtxCabalTarget |
| 177 | + parseJSON (String "project") = pure CtxProject |
| 178 | + parseJSON _ = empty |
157 | 179 |
|
158 | 180 | -- -------------------------------------
|
159 | 181 |
|
160 | 182 | instance ToJSON RequiredParam where
|
161 |
| - toJSON = genericToJSON defaultOptions |
| 183 | + toJSON (RP s) = toJSON s |
162 | 184 |
|
163 | 185 | instance FromJSON RequiredParam where
|
164 |
| - -- No need to provide a parseJSON implementation. |
| 186 | + parseJSON (String s) = pure $ RP s |
| 187 | + parseJSON _ = empty |
165 | 188 |
|
166 | 189 | -- -------------------------------------
|
167 | 190 |
|
168 | 191 | instance ToJSON CommandDescriptor where
|
169 |
| - toJSON = genericToJSON defaultOptions |
| 192 | + toJSON cmdDescriptor = object [ "name" .= cmdName cmdDescriptor |
| 193 | + , "ui_description" .= cmdUiDescription cmdDescriptor |
| 194 | + , "contexts" .= cmdContexts cmdDescriptor |
| 195 | + , "additional_params" .= cmdAdditionalParams cmdDescriptor ] |
170 | 196 |
|
171 | 197 | instance FromJSON CommandDescriptor where
|
172 |
| - -- No need to provide a parseJSON implementation. |
| 198 | + parseJSON (Object v) = |
| 199 | + CommandDesc <$> v .: "name" |
| 200 | + <*> v .: "ui_description" |
| 201 | + <*> v .: "contexts" |
| 202 | + <*> v .: "additional_params" |
| 203 | + parseJSON _ = empty |
173 | 204 |
|
174 | 205 | -- -------------------------------------
|
175 | 206 |
|
176 | 207 | instance ToJSON IdeRequest where
|
177 |
| - toJSON = genericToJSON defaultOptions |
| 208 | + toJSON (IdeRequest{ideCommand = command,ideContext = context,ideParams = params}) = |
| 209 | + object [ "command" .= command |
| 210 | + , "context" .= context |
| 211 | + , "params" .= params] |
178 | 212 |
|
179 | 213 | instance FromJSON IdeRequest where
|
180 |
| - -- No need to provide a parseJSON implementation. |
| 214 | + parseJSON (Object v) = |
| 215 | + IdeRequest <$> v .: "command" |
| 216 | + <*> v .: "context" |
| 217 | + <*> v .: "params" |
| 218 | + parseJSON _ = empty |
181 | 219 |
|
182 | 220 | -- -------------------------------------
|
183 | 221 |
|
184 | 222 | instance ToJSON IdeResponse where
|
185 |
| - toJSON = genericToJSON defaultOptions |
| 223 | + toJSON (IdeResponseOk v) = object [ "tag" .= String "ok" |
| 224 | + , "contents" .= toJSON v ] |
| 225 | + toJSON (IdeResponseFail v) = object [ "tag" .= String "fail" |
| 226 | + , "contents" .= toJSON v ] |
| 227 | + toJSON (IdeResponseError v) = object [ "tag" .= String "error" |
| 228 | + , "contents" .= toJSON v ] |
186 | 229 |
|
187 | 230 | instance FromJSON IdeResponse where
|
188 |
| - -- No need to provide a parseJSON implementation. |
| 231 | + parseJSON (Object v) = do |
| 232 | + tag <- v .: "tag" :: Parser T.Text |
| 233 | + case tag of |
| 234 | + "ok" -> IdeResponseOk <$> v .: "contents" |
| 235 | + "fail" -> IdeResponseFail <$> v .: "contents" |
| 236 | + "error" -> IdeResponseError <$> v .: "contents" |
| 237 | + _ -> empty |
| 238 | + parseJSON _ = empty |
189 | 239 |
|
190 | 240 | -- EOF
|
0 commit comments