@@ -46,6 +46,18 @@ function formatPropertyValue(property: xdebug.BaseProperty): string {
46
46
return displayValue
47
47
}
48
48
49
+ class NewDbgpConnectionEvent extends vscode . Event {
50
+ body : {
51
+ connId : number
52
+ }
53
+ constructor ( connId : number ) {
54
+ super ( 'newDbgpConnection' ) ;
55
+ this . body = {
56
+ connId : connId
57
+ }
58
+ }
59
+ }
60
+
49
61
/**
50
62
* This interface should always match the schema found in the mock-debug extension manifest.
51
63
*/
@@ -85,9 +97,11 @@ interface LaunchRequestArguments extends VSCodeDebugProtocol.LaunchRequestArgume
85
97
env ?: { [ key : string ] : string }
86
98
/** If true launch the target in an external console. */
87
99
externalConsole ?: boolean
100
+ /** Internal variable for passing connections between adapter instances */
101
+ connId ?: number
88
102
}
89
103
90
- class PhpDebugSession extends vscode . DebugSession {
104
+ export class PhpDebugSession extends vscode . DebugSession {
91
105
/** The arguments that were given to launchRequest */
92
106
private _args : LaunchRequestArguments
93
107
@@ -143,6 +157,9 @@ class PhpDebugSession extends vscode.DebugSession {
143
157
/** A flag to indicate that the adapter has already processed the stopOnEntry step request */
144
158
private _hasStoppedOnEntry = false
145
159
160
+ /** Static store of all active connections used to pass them betweeen adapter instances */
161
+ private static _allConnections = new Map < number , xdebug . Connection > ( )
162
+
146
163
public constructor ( ) {
147
164
super ( )
148
165
this . setDebuggerColumnsStartAt1 ( true )
@@ -186,12 +203,69 @@ class PhpDebugSession extends vscode.DebugSession {
186
203
this . sendResponse ( response )
187
204
}
188
205
189
- protected attachRequest (
206
+ protected async attachRequest (
190
207
response : VSCodeDebugProtocol . AttachResponse ,
191
- args : VSCodeDebugProtocol . AttachRequestArguments
208
+ args2 : VSCodeDebugProtocol . AttachRequestArguments
192
209
) {
193
- this . sendErrorResponse ( response , new Error ( 'Attach requests are not supported' ) )
194
- this . shutdown ( )
210
+ const args = args2 as LaunchRequestArguments
211
+ if ( ! args . connId || ! PhpDebugSession . _allConnections . has ( args . connId ) ) {
212
+ this . sendErrorResponse ( response , new Error ( 'Cant find connection' ) )
213
+ this . shutdown ( )
214
+ return
215
+ }
216
+ this . sendResponse ( response )
217
+
218
+ this . _args = args
219
+ const connection = PhpDebugSession . _allConnections . get ( args . connId ! ) !
220
+
221
+ this . _connections . set ( connection . id , connection )
222
+ this . _waitingConnections . add ( connection )
223
+ const disposeConnection = ( error ?: Error ) => {
224
+ if ( this . _connections . has ( connection . id ) ) {
225
+ if ( args . log ) {
226
+ this . sendEvent ( new vscode . OutputEvent ( 'connection ' + connection . id + ' closed\n' ) )
227
+ }
228
+ if ( error ) {
229
+ this . sendEvent (
230
+ new vscode . OutputEvent (
231
+ 'connection ' + connection . id + ': ' + error . message + '\n'
232
+ )
233
+ )
234
+ }
235
+ this . sendEvent ( new vscode . ContinuedEvent ( connection . id , false ) )
236
+ this . sendEvent ( new vscode . ThreadEvent ( 'exited' , connection . id ) )
237
+ connection . close ( )
238
+ this . _connections . delete ( connection . id )
239
+ this . _waitingConnections . delete ( connection )
240
+ }
241
+ PhpDebugSession . _allConnections . delete ( connection . id )
242
+ this . sendEvent ( new vscode . TerminatedEvent ( ) )
243
+ }
244
+ connection . on ( 'warning' , ( warning : string ) => {
245
+ this . sendEvent ( new vscode . OutputEvent ( warning + '\n' ) )
246
+ } )
247
+ connection . on ( 'error' , disposeConnection )
248
+ connection . on ( 'close' , disposeConnection )
249
+ await connection . waitForInitPacket ( )
250
+
251
+ // override features from launch.json
252
+ try {
253
+ const xdebugSettings = args . xdebugSettings || { }
254
+ await Promise . all (
255
+ Object . keys ( xdebugSettings ) . map ( setting =>
256
+ connection . sendFeatureSetCommand ( setting , xdebugSettings [ setting ] )
257
+ )
258
+ )
259
+ } catch ( error ) {
260
+ throw new Error (
261
+ 'Error applying xdebugSettings: ' + ( error instanceof Error ? error . message : error )
262
+ )
263
+ }
264
+
265
+ this . sendEvent ( new vscode . ThreadEvent ( 'started' , connection . id ) )
266
+
267
+ // request breakpoints from VS Code
268
+ this . sendEvent ( new vscode . InitializedEvent ( ) )
195
269
}
196
270
197
271
protected async launchRequest ( response : VSCodeDebugProtocol . LaunchResponse , args : LaunchRequestArguments ) {
@@ -263,6 +337,9 @@ class PhpDebugSession extends vscode.DebugSession {
263
337
if ( args . log ) {
264
338
this . sendEvent ( new vscode . OutputEvent ( 'new connection ' + connection . id + '\n' ) , true )
265
339
}
340
+ PhpDebugSession . _allConnections . set ( connection . id , connection )
341
+ this . sendEvent ( new NewDbgpConnectionEvent ( connection . id ) )
342
+ /*
266
343
this._connections.set(connection.id, connection)
267
344
this._waitingConnections.add(connection)
268
345
const disposeConnection = (error?: Error) => {
@@ -282,6 +359,7 @@ class PhpDebugSession extends vscode.DebugSession {
282
359
connection.close()
283
360
this._connections.delete(connection.id)
284
361
this._waitingConnections.delete(connection)
362
+ PhpDebugSession._allConnections.delete(connection.id)
285
363
}
286
364
}
287
365
connection.on('warning', (warning: string) => {
@@ -309,6 +387,7 @@ class PhpDebugSession extends vscode.DebugSession {
309
387
310
388
// request breakpoints from VS Code
311
389
await this.sendEvent(new vscode.InitializedEvent())
390
+ */
312
391
} catch ( error ) {
313
392
this . sendEvent (
314
393
new vscode . OutputEvent ( ( error instanceof Error ? error . message : error ) + '\n' , 'stderr' )
@@ -1076,6 +1155,10 @@ class PhpDebugSession extends vscode.DebugSession {
1076
1155
this . sendErrorResponse ( response , error )
1077
1156
}
1078
1157
}
1158
+
1159
+ protected customRequest ( command : string , response : VSCodeDebugProtocol . Response , args : any ) : void {
1160
+ console . log ( 'test' , args )
1161
+ }
1079
1162
}
1080
1163
1081
1164
vscode . DebugSession . run ( PhpDebugSession )
0 commit comments