Skip to content

Commit f24e569

Browse files
committed
don't run debug server as separate process
This way, we can debug the debug server the same way we debug the rest of the extension
1 parent de41239 commit f24e569

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

src/debugServer.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ export interface LaunchRequestArguments extends DebugProtocol.LaunchRequestArgum
1313
interactiveTerminal: boolean;
1414
}
1515

16-
17-
class Ev3devBrowserDebugSession extends DebugSession {
16+
export class Ev3devBrowserDebugSession extends DebugSession {
1817
protected initializeRequest(response: DebugProtocol.InitializeResponse,
1918
args: DebugProtocol.InitializeRequestArguments): void {
2019
if (response.body) {
@@ -44,4 +43,6 @@ class Ev3devBrowserDebugSession extends DebugSession {
4443
}
4544
}
4645

47-
DebugSession.run(Ev3devBrowserDebugSession);
46+
if (require.main === module) {
47+
DebugSession.run(Ev3devBrowserDebugSession);
48+
}

src/extension.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import * as fs from 'fs';
2+
import * as net from 'net';
23
import * as os from 'os';
34
import * as path from 'path';
45
import * as ssh2Streams from 'ssh2-streams';
56
import * as temp from 'temp';
67

78
import * as vscode from 'vscode';
89

9-
import { LaunchRequestArguments } from './debugServer';
10+
import { Ev3devBrowserDebugSession, LaunchRequestArguments } from './debugServer';
1011
import { Brickd } from './brickd';
1112
import { Device } from './device';
1213
import {
@@ -34,6 +35,7 @@ export function activate(context: vscode.ExtensionContext): void {
3435
resourceDir = context.asAbsolutePath('resources');
3536

3637
ev3devBrowserProvider = new Ev3devBrowserProvider();
38+
const factory = new Ev3devDebugAdapterDescriptorFactory();
3739
context.subscriptions.push(
3840
output, ev3devBrowserProvider,
3941
vscode.window.registerTreeDataProvider('ev3devBrowser', ev3devBrowserProvider),
@@ -52,10 +54,33 @@ export function activate(context: vscode.ExtensionContext): void {
5254
vscode.commands.registerCommand('ev3devBrowser.action.pickDevice', () => pickDevice()),
5355
vscode.commands.registerCommand('ev3devBrowser.action.download', () => downloadAll()),
5456
vscode.commands.registerCommand('ev3devBrowser.action.refresh', () => refresh()),
55-
vscode.debug.onDidReceiveDebugSessionCustomEvent(e => handleCustomDebugEvent(e))
57+
vscode.debug.onDidReceiveDebugSessionCustomEvent(e => handleCustomDebugEvent(e)),
58+
vscode.debug.registerDebugAdapterDescriptorFactory('ev3devBrowser', factory),
5659
);
5760
}
5861

62+
class Ev3devDebugAdapterDescriptorFactory implements vscode.DebugAdapterDescriptorFactory {
63+
private server?: net.Server;
64+
65+
createDebugAdapterDescriptor(session: vscode.DebugSession, executable: vscode.DebugAdapterExecutable | undefined): vscode.ProviderResult<vscode.DebugAdapterDescriptor> {
66+
if (!this.server) {
67+
// start listening on a random port
68+
this.server = net.createServer(socket => {
69+
const session = new Ev3devBrowserDebugSession();
70+
session.setRunAsServer(true);
71+
session.start(<NodeJS.ReadableStream>socket, socket);
72+
}).listen(0);
73+
}
74+
75+
// make VS Code connect to debug server
76+
return new vscode.DebugAdapterServer(this.server.address().port);
77+
}
78+
79+
dispose() {
80+
this.server?.close();
81+
}
82+
}
83+
5984
// this method is called when your extension is deactivated
6085
export function deactivate(): void {
6186
// The "temp" module should clean up automatically, but do this just in case.

0 commit comments

Comments
 (0)