Skip to content

Commit 9457d40

Browse files
committed
feature: Add Reverse Requests support
Exposes 'sendRunInTerminalReverseRequest' helper too.
1 parent 967022f commit 9457d40

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

src/DAP/Adaptor.hs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ module DAP.Adaptor
2626
, sendErrorResponse
2727
-- * Events
2828
, sendSuccesfulEvent
29+
-- * Reverse Requests
30+
, sendReverseRequest
31+
, sendRunInTerminalReverseRequest
2932
-- * Server
3033
, getServerCapabilities
3134
, withConnectionLock
@@ -293,6 +296,21 @@ sendEvent action = do
293296
writeToHandle address handle payload
294297
resetAdaptorStatePayload
295298
----------------------------------------------------------------------------
299+
-- | Write reverse request to Handle
300+
sendReverseRequest
301+
:: ReverseCommand
302+
-> Adaptor app Request ()
303+
sendReverseRequest rcmd = send $ do
304+
setField "type" MessageTypeRequest
305+
setField "command" rcmd
306+
----------------------------------------------------------------------------
307+
-- | Send runInTerminal reverse request
308+
sendRunInTerminalReverseRequest :: RunInTerminalRequestArguments -> Adaptor app Request ()
309+
sendRunInTerminalReverseRequest args = do
310+
setField "arguments" args
311+
sendReverseRequest ReverseCommandRunInTerminal
312+
313+
----------------------------------------------------------------------------
296314
-- | Writes payload to the given 'Handle' using the local connection lock
297315
----------------------------------------------------------------------------
298316
writeToHandle

src/DAP/Types.hs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ module DAP.Types
7272
, PathFormat (..)
7373
-- * Command
7474
, Command (..)
75+
-- * Reverse Command
76+
, ReverseCommand (..)
7577
-- * Event
7678
, EventType (..)
7779
-- ** Events
@@ -121,6 +123,7 @@ module DAP.Types
121123
, LoadedSourcesResponse (..)
122124
, ModulesResponse (..)
123125
, ReadMemoryResponse (..)
126+
, RunInTerminalResponse (..)
124127
, ScopesResponse (..)
125128
, SetExpressionResponse (..)
126129
, SetVariableResponse (..)
@@ -153,6 +156,8 @@ module DAP.Types
153156
, RestartArguments (..)
154157
, RestartFrameArguments (..)
155158
, ReverseContinueArguments (..)
159+
, RunInTerminalRequestArguments (..)
160+
, RunInTerminalRequestArgumentsKind (..)
156161
, ScopesArguments (..)
157162
, SetBreakpointsArguments (..)
158163
, SetDataBreakpointsArguments (..)
@@ -172,7 +177,6 @@ module DAP.Types
172177
, ThreadsArguments (..)
173178
, VariablesArguments (..)
174179
, WriteMemoryArguments (..)
175-
, RunInTerminalResponse (..)
176180
-- * defaults
177181
, defaultBreakpoint
178182
, defaultBreakpointLocation
@@ -894,8 +898,6 @@ instance ToJSON EventType where
894898
----------------------------------------------------------------------------
895899
data Command
896900
= CommandCancel
897-
| CommandRunInTerminal
898-
| CommandStartDebugging
899901
| CommandInitialize
900902
| CommandConfigurationDone
901903
| CommandLaunch
@@ -954,6 +956,24 @@ instance ToJSON Command where
954956
toJSON (CustomCommand x) = toJSON x
955957
toJSON cmd = genericToJSONWithModifier cmd
956958
----------------------------------------------------------------------------
959+
data ReverseCommand
960+
= ReverseCommandRunInTerminal
961+
| ReverseCommandStartDebugging
962+
deriving stock (Show, Eq, Read, Generic)
963+
----------------------------------------------------------------------------
964+
instance FromJSON ReverseCommand where
965+
parseJSON = withText name $ \command ->
966+
case readMaybe (name <> capitalize (T.unpack command)) of
967+
Just cmd ->
968+
pure cmd
969+
Nothing ->
970+
fail $ "Unknown reverse command: " ++ show command
971+
where
972+
name = show (typeRep (Proxy @ReverseCommand))
973+
----------------------------------------------------------------------------
974+
instance ToJSON ReverseCommand where
975+
toJSON cmd = genericToJSONWithModifier cmd
976+
----------------------------------------------------------------------------
957977
data ErrorMessage
958978
= ErrorMessageCancelled
959979
| ErrorMessageNotStopped
@@ -2688,6 +2708,9 @@ data RunInTerminalRequestArgumentsKind
26882708
| RunInTerminalRequestArgumentsKindExternal
26892709
deriving stock (Show, Eq, Generic)
26902710
----------------------------------------------------------------------------
2711+
instance ToJSON RunInTerminalRequestArgumentsKind where
2712+
toJSON = genericToJSONWithModifier
2713+
----------------------------------------------------------------------------
26912714
instance FromJSON RunInTerminalRequestArgumentsKind where
26922715
parseJSON = genericParseJSONWithModifier
26932716
----------------------------------------------------------------------------
@@ -2728,6 +2751,9 @@ data RunInTerminalRequestArguments
27282751
--
27292752
} deriving stock (Show, Eq, Generic)
27302753
----------------------------------------------------------------------------
2754+
instance ToJSON RunInTerminalRequestArguments where
2755+
toJSON = genericToJSONWithModifier
2756+
----------------------------------------------------------------------------
27312757
instance FromJSON RunInTerminalRequestArguments where
27322758
parseJSON = genericParseJSONWithModifier
27332759
----------------------------------------------------------------------------

0 commit comments

Comments
 (0)