-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathJsonAPI.hs
219 lines (194 loc) · 6.3 KB
/
JsonAPI.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ExtendedDefaultRules #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE OverloadedStrings #-}
-- The JSON api that we expose
--
-- Some general design principles:
--
-- * We have a single Request type for requests sent by the editor to the client
-- and a single Response type for responses sent back from the client to the
-- editor.
--
-- * We try to roughly have a one-to-one mapping between the functions exported
-- by IdeSession; for instance, ide-backend's `updateSourceFileFromFile`
-- corresponds to the `RequestUpdateSourceFileFromFile` request and the
-- `ResponseUpdateSourceFileFromFile` response.
--
-- Hopefully with these principles in place the API should be predictable,
-- stable, and well documented.
module Stack.Ide.JsonAPI (
-- * Requests
Request(..)
, RequestSessionUpdate(..)
, Response(..)
, ResponseSpanInfo(..)
, ResponseExpType(..)
, ResponseAnnExpType(..)
, Ann(..)
, CodeAnn(..)
, VersionInfo(..)
, Identifier
, Targets(..)
, Sequenced(..), unsequenced, withSameSeqAs
, ByteString64(..)
, sliceSpans
) where
import Control.Applicative
import Data.Aeson
import Data.Aeson.TH
import qualified Data.HashMap.Strict as H
import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
import IdeSession.Types.Public hiding (idProp, Value)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Base64 as B64
{-------------------------------------------------------------------------------
Types
-------------------------------------------------------------------------------}
-- | Messages sent from the editor to the client
data Request =
-- Update
RequestUpdateSession [RequestSessionUpdate]
-- Query
| RequestGetSourceErrors
| RequestGetLoadedModules
| RequestGetSpanInfo SourceSpan
| RequestGetExpTypes SourceSpan
| RequestGetAnnExpTypes SourceSpan
| RequestGetAutocompletion FilePath String
-- Run
| RequestRun Bool ModuleName Identifier
| RequestProcessInput String
| RequestProcessInterrupt
| RequestProcessKill
-- Misc
| RequestShutdownSession
deriving (Show, Eq)
-- | Session updates
data RequestSessionUpdate
= RequestSessionUpdate -- Simply calls updateSession with no changes to trigger a recompile.
| RequestUpdateTargets Targets
| RequestUpdateSourceFile FilePath ByteString64
| RequestUpdateSourceFileDelete FilePath
| RequestUpdateDataFile FilePath ByteString64
| RequestUpdateDataFileDelete FilePath
| RequestUpdateGhcOpts [String]
| RequestUpdateRtsOpts [String]
| RequestUpdateRelativeIncludes [FilePath]
| RequestUpdateCodeGeneration Bool
| RequestUpdateEnv [(String, Maybe String)]
| RequestUpdateArgs [String]
deriving (Show, Eq)
-- TODO:
-- RequestUpdateStdoutBufferMode
-- RequestUpdateStderrBufferMode
-- | Messages sent back from the client to the editor
data Response =
-- | Sent on session initialization
ResponseWelcome VersionInfo
-- | Nothing indicates the update completed
| ResponseUpdateSession UpdateStatus
| ResponseGetSourceErrors [SourceError]
| ResponseGetLoadedModules [ModuleName]
| ResponseGetSpanInfo [ResponseSpanInfo]
| ResponseGetExpTypes [ResponseExpType]
| ResponseGetAnnExpTypes [ResponseAnnExpType]
| ResponseGetAutocompletion [IdInfo]
-- Run
| ResponseProcessOutput String
| ResponseProcessDone RunResult
| ResponseNoProcessError
-- Misc
| ResponseLog Text
| ResponseInvalidRequest String
| ResponseFatalError String
| ResponseShutdownSession
deriving (Show, Eq)
data ResponseSpanInfo =
ResponseSpanInfo SpanInfo SourceSpan
deriving (Show, Eq)
data ResponseExpType =
ResponseExpType Text SourceSpan
deriving (Show, Eq)
data ResponseAnnExpType =
ResponseAnnExpType (Ann CodeAnn) SourceSpan
deriving (Show, Eq)
data Ann a =
Ann a (Ann a)
| AnnGroup [Ann a]
| AnnLeaf Text
deriving (Show, Eq, Functor)
data CodeAnn =
CodeIdInfo IdInfo
deriving (Show, Eq)
-- | Client version
--
-- Standard versioning applies (major, minor, patch)
data VersionInfo =
VersionInfo Int Int Int
deriving (Show, Eq)
type Identifier = Text
-- | An extension of messages with an optional sequence code,
-- an uninterpreted JSON value. See (#39) for motivation.
data Sequenced a =
NoSeq a
| HasSeq Value a
deriving (Show, Eq, Functor)
unsequenced :: Sequenced a -> a
unsequenced (NoSeq a) = a
unsequenced (HasSeq _ a) = a
withSameSeqAs :: Sequenced a -> b -> Sequenced b
withSameSeqAs sa b = b <$ sa
instance ToJSON a => ToJSON (Sequenced a) where
toJSON (NoSeq a) = toJSON a
toJSON (HasSeq s a) =
case toJSON a of
Object o -> Object (H.insert "seq" s o)
json_a -> json_a
instance FromJSON a => FromJSON (Sequenced a) where
parseJSON x
= case x of
Object o | Just s <- H.lookup "seq" o ->
HasSeq s <$> parseJSON (Object $ H.delete "seq" o)
_ ->
NoSeq <$> parseJSON x
newtype ByteString64 = ByteString64 { unByteString64 :: ByteString }
deriving (Eq, Read, Show, Ord)
instance ToJSON ByteString64 where
toJSON (ByteString64 bs) = toJSON (Text.decodeUtf8 $ B64.encode bs)
instance FromJSON ByteString64 where
parseJSON o =
parseJSON o >>= either fail (return . ByteString64) . B64.decode . Text.encodeUtf8
--------------------------------------------------------------------------------
-- Misc utilities
-- | Useful for slicing up annotated text. Exported for client-side and
-- server-side code.
sliceSpans :: Int -> Text -> [(Int, Int, a)] -> [(Text, Maybe a)]
sliceSpans _ txt _ | Text.null txt = []
sliceSpans _ txt [] = [(txt, Nothing)]
sliceSpans ix txt ((fr, to, x) : xs) =
appendNonNull before Nothing $
appendNonNull chunk (Just x) $
sliceSpans to rest' xs
where
appendNonNull t mx = if Text.null t then id else ((t, mx) :)
(chunk, rest') = Text.splitAt ((to - ix) - fr') rest
(before, rest) = Text.splitAt fr' txt
fr' = max 0 (fr - ix)
--------------------------------------------------------------------------------
-- For the moment we use Aeson's built-in instance deriver
$(concat <$> mapM (deriveJSON defaultOptions)
[ ''Ann
, ''CodeAnn
, ''Request
, ''RequestSessionUpdate
, ''Response
, ''ResponseAnnExpType
, ''ResponseExpType
, ''ResponseSpanInfo
, ''Targets
, ''VersionInfo
])