|
| 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 | +]; |
0 commit comments