Skip to content

Commit

Permalink
Use vscode's selected launch config for project launch and codelens r…
Browse files Browse the repository at this point in the history
…un/debug
  • Loading branch information
sdedic committed Dec 7, 2023
1 parent ca7b58b commit fe39037
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1220,7 +1220,10 @@ public Void visitMethod(MethodTree tree, Void p) {
int start = (int) cc.getTrees().getSourcePositions().getStartPosition(cc.getCompilationUnit(), tree);
int end = (int) cc.getTrees().getSourcePositions().getEndPosition(cc.getCompilationUnit(), tree);
org.netbeans.api.lsp.Range range = new org.netbeans.api.lsp.Range(start, end);
List<Object> arguments = Collections.singletonList(uri);
List<Object> arguments = new ArrayList<>();
arguments.add(uri);
arguments.add(null);
arguments.add("");
String method = el.getSimpleName().toString();
lens.add(new org.netbeans.api.lsp.CodeLens(range,
new org.netbeans.api.lsp.Command(Bundle.LBL_Run(method), COMMAND_RUN_SINGLE, arguments),
Expand Down
79 changes: 53 additions & 26 deletions java/java.lsp.server/vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,50 +550,76 @@ export function activate(context: ExtensionContext): VSNetBeansAPI {
await commands.executeCommand('nbls:Tools:org.netbeans.modules.cloud.oracle.actions.AddADBAction');
}
}));
const mergeWithLaunchConfig = (dconfig : vscode.DebugConfiguration) => {
const folder = vscode.workspace.workspaceFolders?.[0];
const uri = folder?.uri;
if (uri) {
const launchConfig = workspace.getConfiguration('launch', uri);
// retrieve values
const configurations = launchConfig.get('configurations') as (any[] | undefined);
if (configurations) {
for (let config of configurations) {
if (config["type"] == dconfig.type) {
for (let key in config) {
if (!dconfig[key]) {
dconfig[key] = config[key];
}
}
break;
}
}

async function findRunConfiguraiton(uri : vscode.Uri) : Promise<vscode.DebugConfiguration|undefined> {
// do not invoke debug start with no (java+) configurations, as it would probably create an user prompt
let cfg = vscode.workspace.getConfiguration("launch");
let c = cfg.get('configurations');
if (!Array.isArray(c)) {
return undefined;
}
let f = c.filter((v) => v['type'] === 'java+');
if (!f.length) {
return undefined;
}
class P implements vscode.DebugConfigurationProvider {
config : vscode.DebugConfiguration | undefined;

resolveDebugConfigurationWithSubstitutedVariables(folder: vscode.WorkspaceFolder | undefined, debugConfiguration: vscode.DebugConfiguration, token?: vscode.CancellationToken): vscode.ProviderResult<vscode.DebugConfiguration> {
this.config = debugConfiguration;
return undefined;
}
}
let provider = new P();
let d = vscode.debug.registerDebugConfigurationProvider('java+', provider);
// let vscode to select a debug config
return await vscode.commands.executeCommand('workbench.action.debug.start', { config: {
type: 'java+',
mainClass: uri.toString()
}, noDebug: true}).then((v) => {
d.dispose();
return provider.config;
}, (err) => {
d.dispose();
return undefined;
});
}

const runDebug = async (noDebug: boolean, testRun: boolean, uri: any, methodName?: string, launchConfiguration?: string, project : boolean = false, ) => {
const docUri = contextUri(uri);
const docUri = contextUri(uri);
if (docUri) {
const workspaceFolder = vscode.workspace.getWorkspaceFolder(docUri);
const debugConfig : vscode.DebugConfiguration = {
// attempt to find the active configuration in the vsode launch settings; undefined if no config is there.
let debugConfig : vscode.DebugConfiguration = await findRunConfiguraiton(docUri) || {
type: "java+",
name: "Java Single Debug",
request: "launch",
methodName,
launchConfiguration,
testRun
request: "launch"
};
if (!methodName) {
debugConfig['methodName'] = methodName;
}
if (launchConfiguration == '') {
if (debugConfig['launchConfiguration']) {
delete debugConfig['launchConfiguration'];
}
} else {
debugConfig['launchConfiguration'] = launchConfiguration;
}
debugConfig['testRun'] = testRun;

const workspaceFolder = vscode.workspace.getWorkspaceFolder(docUri);
if (project) {
debugConfig['projectFile'] = docUri.toString();
debugConfig['project'] = true;
} else {
debugConfig['mainClass'] = docUri.toString();
}
mergeWithLaunchConfig(debugConfig);

const debugOptions : vscode.DebugSessionOptions = {
noDebug: noDebug,
}


let d = undefined;
const ret = await vscode.debug.startDebugging(workspaceFolder, debugConfig, debugOptions);
return ret ? new Promise((resolve) => {
const listener = vscode.debug.onDidTerminateDebugSession(() => {
Expand All @@ -603,6 +629,7 @@ export function activate(context: ExtensionContext): VSNetBeansAPI {
}) : ret;
}
};

context.subscriptions.push(commands.registerCommand(COMMAND_PREFIX + '.run.test', async (uri, methodName?, launchConfiguration?) => {
await runDebug(true, true, uri, methodName, launchConfiguration);
}));
Expand Down

0 comments on commit fe39037

Please sign in to comment.