Skip to content

Commit 14dd6db

Browse files
Adopt DebugAdapterDescriptorFactor interface (#827)
Signed-off-by: Jinbo Wang <[email protected]>
1 parent 4930fce commit 14dd6db

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

src/configurationProvider.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -257,16 +257,7 @@ export class JavaDebugConfigurationProvider implements vscode.DebugConfiguration
257257
});
258258
}
259259

260-
const debugServerPort = await lsPlugin.startDebugSession();
261-
if (debugServerPort) {
262-
config.debugServer = debugServerPort;
263-
return config;
264-
} else {
265-
// Information for diagnostic:
266-
// tslint:disable-next-line:no-console
267-
console.log("Cannot find a port for debugging session");
268-
throw new Error("Failed to start debug server.");
269-
}
260+
return config;
270261
} catch (ex) {
271262
if (ex instanceof utility.JavaExtensionNotEnabledError) {
272263
utility.guideToInstallJavaExtension();

src/extension.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { JavaDebugConfigurationProvider } from "./configurationProvider";
1111
import { HCR_EVENT, JAVA_LANGID, USER_NOTIFICATION_EVENT } from "./constants";
1212
import { initializeCodeLensProvider, startDebugging } from "./debugCodeLensProvider";
1313
import { handleHotCodeReplaceCustomEvent, initializeHotCodeReplace, NO_BUTTON, YES_BUTTON } from "./hotCodeReplace";
14+
import { JavaDebugAdapterDescriptorFactory } from "./javaDebugAdapterDescriptorFactory";
1415
import { IMainMethod, resolveMainMethod } from "./languageServerPlugin";
1516
import { logger, Type } from "./logger";
1617
import { initializeThreadOperations } from "./threadOperations";
@@ -33,6 +34,7 @@ function initializeExtension(operationId: string, context: vscode.ExtensionConte
3334
registerDebugEventListener(context);
3435
context.subscriptions.push(logger);
3536
context.subscriptions.push(vscode.debug.registerDebugConfigurationProvider("java", new JavaDebugConfigurationProvider()));
37+
context.subscriptions.push(vscode.debug.registerDebugAdapterDescriptorFactory("java", new JavaDebugAdapterDescriptorFactory()));
3638
context.subscriptions.push(instrumentOperationAsVsCodeCommand("JavaDebug.SpecifyProgramArgs", async () => {
3739
return specifyProgramArguments(context);
3840
}));
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
import { DebugAdapterDescriptor, DebugAdapterDescriptorFactory, DebugAdapterExecutable, DebugAdapterServer, DebugSession } from "vscode";
5+
6+
import { startDebugSession } from "./languageServerPlugin";
7+
import { Type } from "./logger";
8+
import { formatErrorProperties, showErrorMessageWithTroubleshooting } from "./utility";
9+
10+
export class JavaDebugAdapterDescriptorFactory implements DebugAdapterDescriptorFactory {
11+
public async createDebugAdapterDescriptor(session: DebugSession, executable: DebugAdapterExecutable): Promise<DebugAdapterDescriptor> {
12+
let error;
13+
try {
14+
const debugServerPort = <number> (await startDebugSession());
15+
if (debugServerPort) {
16+
return new DebugAdapterServer(debugServerPort);
17+
} else {
18+
// Information for diagnostic:
19+
// tslint:disable-next-line:no-console
20+
console.log("Cannot find a port for debugging session");
21+
}
22+
} catch (err) {
23+
error = err;
24+
}
25+
26+
let errorMessage = "Failed to start debug server.";
27+
let details = {};
28+
if (error) {
29+
errorMessage = error.message || String(error);
30+
details = formatErrorProperties(error);
31+
}
32+
33+
showErrorMessageWithTroubleshooting({
34+
message: errorMessage,
35+
type: Type.EXCEPTION,
36+
details,
37+
});
38+
}
39+
}

0 commit comments

Comments
 (0)