Skip to content

Commit a9936f1

Browse files
committed
Debug refactor merge
1 parent ed7b018 commit a9936f1

File tree

7 files changed

+382
-326
lines changed

7 files changed

+382
-326
lines changed

vscode/src/commands/commands.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ export const extCommands = {
4848
editNodeProps: appendPrefixToCommand('node.properties.edit'),
4949
selectEditorProjs: appendPrefixToCommand('select.editor.projects'),
5050
attachDebugger: appendPrefixToCommand("java.attachDebugger.connector"),
51-
startDebug: 'workbench.action.debug.start',
5251
}
5352

5453
export const builtInCommands = {
@@ -59,7 +58,8 @@ export const builtInCommands = {
5958
goToEditorLocations: 'editor.action.goToLocations',
6059
renameSymbol: 'editor.action.rename',
6160
quickAccess: 'workbench.action.quickOpen',
62-
openSettings: 'workbench.action.openSettings'
61+
openSettings: 'workbench.action.openSettings',
62+
startDebug: 'workbench.action.debug.start',
6363
}
6464

6565
export const nbCommands = {

vscode/src/commands/debug.ts

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
Copyright (c) 2023-2024, Oracle and/or its affiliates.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import * as vscode from 'vscode';
18+
import { builtInCommands, extCommands } from "./commands";
19+
import { ICommand } from "./types";
20+
import { extConstants } from '../constants';
21+
import { getContextUri } from './utils';
22+
23+
const runTest = async (uri: any, methodName? : string, launchConfiguration?: string) => {
24+
await runDebug(true, true, uri, methodName, launchConfiguration);
25+
}
26+
const debugTest = async (uri: any, methodName? : string, launchConfiguration?: string) => {
27+
await runDebug(false, true, uri, methodName, launchConfiguration);
28+
}
29+
const runSingle = async (uri: any, methodName? : string, launchConfiguration?: string) => {
30+
await runDebug(true, false, uri, methodName, launchConfiguration);
31+
}
32+
const debugSingle = async (uri: any, methodName? : string, launchConfiguration?: string) => {
33+
await runDebug(false, false, uri, methodName, launchConfiguration);
34+
}
35+
const projectRun = async (node: any, launchConfiguration? : string) => {
36+
return runDebug(true, false, getContextUri(node)?.toString() || '', undefined, launchConfiguration, true);
37+
}
38+
const projectDebug = async (node: any, launchConfiguration? : string) => {
39+
return runDebug(false, false, getContextUri(node)?.toString() || '', undefined, launchConfiguration, true);
40+
}
41+
const projectTest = async (node: any, launchConfiguration? : string) => {
42+
return runDebug(true, true, getContextUri(node)?.toString() || '', undefined, launchConfiguration, true);
43+
}
44+
const packageTest = async (uri: any, launchConfiguration? : string) => {
45+
await runDebug(true, true, uri, undefined, launchConfiguration);
46+
}
47+
48+
const runDebug = async (noDebug: boolean, testRun: boolean, uri: any, methodName?: string, launchConfiguration?: string, project : boolean = false, ) => {
49+
const docUri = getContextUri(uri);
50+
if (docUri) {
51+
// attempt to find the active configuration in the vsode launch settings; undefined if no config is there.
52+
let debugConfig : vscode.DebugConfiguration = await findRunConfiguration(docUri) || {
53+
type: extConstants.COMMAND_PREFIX,
54+
name: "Java Single Debug",
55+
request: "launch"
56+
};
57+
if (methodName) {
58+
debugConfig['methodName'] = methodName;
59+
}
60+
if (launchConfiguration == '') {
61+
if (debugConfig['launchConfiguration']) {
62+
delete debugConfig['launchConfiguration'];
63+
}
64+
} else {
65+
debugConfig['launchConfiguration'] = launchConfiguration;
66+
}
67+
debugConfig['testRun'] = testRun;
68+
const workspaceFolder = vscode.workspace.getWorkspaceFolder(docUri);
69+
if (project) {
70+
debugConfig['projectFile'] = docUri.toString();
71+
debugConfig['project'] = true;
72+
} else {
73+
debugConfig['mainClass'] = docUri.toString();
74+
}
75+
const debugOptions : vscode.DebugSessionOptions = {
76+
noDebug: noDebug,
77+
}
78+
79+
80+
const ret = await vscode.debug.startDebugging(workspaceFolder, debugConfig, debugOptions);
81+
return ret ? new Promise((resolve) => {
82+
const listener = vscode.debug.onDidTerminateDebugSession(() => {
83+
listener.dispose();
84+
resolve(true);
85+
});
86+
}) : ret;
87+
}
88+
};
89+
90+
91+
async function findRunConfiguration(uri : vscode.Uri) : Promise<vscode.DebugConfiguration|undefined> {
92+
// do not invoke debug start with no (jdk) configurations, as it would probably create an user prompt
93+
let cfg = vscode.workspace.getConfiguration("launch");
94+
let c = cfg.get('configurations');
95+
if (!Array.isArray(c)) {
96+
return undefined;
97+
}
98+
let f = c.filter((v) => v['type'] === extConstants.COMMAND_PREFIX);
99+
if (!f.length) {
100+
return undefined;
101+
}
102+
class P implements vscode.DebugConfigurationProvider {
103+
config : vscode.DebugConfiguration | undefined;
104+
105+
resolveDebugConfigurationWithSubstitutedVariables(folder: vscode.WorkspaceFolder | undefined, debugConfiguration: vscode.DebugConfiguration, token?: vscode.CancellationToken): vscode.ProviderResult<vscode.DebugConfiguration> {
106+
this.config = debugConfiguration;
107+
return undefined;
108+
}
109+
}
110+
let provider = new P();
111+
let d = vscode.debug.registerDebugConfigurationProvider(extConstants.COMMAND_PREFIX, provider);
112+
// let vscode to select a debug config
113+
return await vscode.commands.executeCommand(builtInCommands.startDebug, { config: {
114+
type: extConstants.COMMAND_PREFIX,
115+
mainClass: uri.toString()
116+
}, noDebug: true}).then((v) => {
117+
d.dispose();
118+
return provider.config;
119+
}, (err) => {
120+
d.dispose();
121+
return undefined;
122+
});
123+
}
124+
125+
export const registerDebugCommands: ICommand[] = [
126+
{
127+
command: extCommands.runTest,
128+
handler: runTest
129+
}, {
130+
command: extCommands.debugTest,
131+
handler: debugTest
132+
}, {
133+
command: extCommands.runSingle,
134+
handler: runSingle
135+
}, {
136+
command: extCommands.debugSingle,
137+
handler: debugSingle
138+
}, {
139+
command: extCommands.projectRun,
140+
handler: projectRun
141+
}, {
142+
command: extCommands.projectDebug,
143+
handler: projectDebug
144+
}, {
145+
command: extCommands.projectTest,
146+
handler: projectTest
147+
}, {
148+
command: extCommands.packageTest,
149+
handler: packageTest
150+
}
151+
];

vscode/src/commands/register.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { registerWebviewCommands } from "./webViews";
2222
import { registerBuildOperationCommands } from "./buildOperations";
2323
import { registerRefactorCommands } from "./refactor";
2424
import { registerUtilCommands } from "./utilCommands";
25+
import { registerDebugCommands } from "./debug";
2526

2627
type ICommandModules = Record<string, ICommand[]>;
2728

@@ -32,7 +33,8 @@ const commandModules: ICommandModules = {
3233
webview: registerWebviewCommands,
3334
buildOperations: registerBuildOperationCommands,
3435
refactor: registerRefactorCommands,
35-
util: registerUtilCommands
36+
util: registerUtilCommands,
37+
debug: registerDebugCommands
3638
}
3739

3840
export const subscribeCommands = (context: ExtensionContext) => {

0 commit comments

Comments
 (0)