Skip to content

Commit 4718b3b

Browse files
Update debug python button (#138)
* Add debug python file for python debugger * Add command that run config in launch json * Update function to get config from workspace too * fix tests
1 parent 6d573f4 commit 4718b3b

File tree

8 files changed

+67
-6
lines changed

8 files changed

+67
-6
lines changed

package.json

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@
4444
"icon": "$(debug-alt)",
4545
"title": "%debugpy.command.debugInTerminal.title%"
4646
},
47+
{
48+
"category": "Python Debugger",
49+
"command": "debugpy.debugUsingLaunchConfig",
50+
"icon": "$(debug-alt)",
51+
"title": "%debugpy.command.debugUsingLaunchConfig.title%"
52+
},
4753
{
4854
"category": "Python Debugger",
4955
"command": "debugpy.clearCacheAndReload",
@@ -70,13 +76,33 @@
7076
"category": "Python Debugger",
7177
"command": "debugpy.debugInTerminal",
7278
"icon": "$(debug-alt)",
73-
"title": "%debugpy.command.debugInTerminal.title%"
79+
"title": "%debugpy.command.debugInTerminal.title%",
80+
"when": "!virtualWorkspace && shellExecutionSupported && editorLangId == python"
81+
},
82+
{
83+
"category": "Python Debugger",
84+
"command": "debugpy.debugUsingLaunchConfig",
85+
"icon": "$(debug-alt)",
86+
"title": "%debugpy.command.debugUsingLaunchConfig.title%",
87+
"when": "!virtualWorkspace && shellExecutionSupported && editorLangId == python"
7488
},
7589
{
7690
"category": "Python Debugger",
7791
"command": "debugpy.viewOutput",
7892
"title": "%debugpy.command.viewOutput.title%"
7993
}
94+
],
95+
"editor/title/run": [
96+
{
97+
"command": "debugpy.debugInTerminal",
98+
"title": "%debugpy.command.debugInTerminal.title%",
99+
"when": "resourceLangId == python && !isInDiffEditor && !virtualWorkspace && shellExecutionSupported"
100+
},
101+
{
102+
"command": "debugpy.debugUsingLaunchConfig",
103+
"title": "%debugpy.command.debugUsingLaunchConfig.title%",
104+
"when": "resourceLangId == python && !isInDiffEditor && !virtualWorkspace && shellExecutionSupported"
105+
}
80106
]
81107
},
82108
"configuration": {

package.nls.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
2-
"debugpy.command.debugInTerminal.title": "Debug Python File",
2+
"debugpy.command.debugInTerminal.title": "Python Debugger: Debug Python File",
3+
"debugpy.command.debugUsingLaunchConfig.title": "Python Debugger: Debug using launch.json",
34
"debugpy.command.clearCacheAndReload.title": "Clear Cache and Reload Window",
45
"debugpy.command.viewOutput.title": "Show Output",
56
"debugpy.debugJustMyCode": "When debugging only step through user-written code. Disable this to allow stepping into library code."

src/extension/common/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export function isUnitTestExecution(): boolean {
3030

3131
export namespace Commands {
3232
export const Debug_In_Terminal = 'debugpy.debugInTerminal';
33+
export const Debug_Using_Launch_Config = 'debugpy.debugUsingLaunchConfig';
3334
export const TriggerEnvironmentSelection = 'debugpy.triggerEnvSelection';
3435
export const PickLocalProcess = 'debugpy.pickLocalProcess';
3536
export const PickArguments = 'debugpy.pickArgs';

src/extension/debugger/configuration/launch.json/launchJsonReader.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ import * as path from 'path';
55
import * as fs from 'fs-extra';
66
import { parse } from 'jsonc-parser';
77
import { DebugConfiguration, Uri, WorkspaceFolder } from 'vscode';
8-
import { getWorkspaceFolder } from '../../../common/vscodeapi';
8+
import { getConfiguration, getWorkspaceFolder } from '../../../common/vscodeapi';
99

1010
export async function getConfigurationsForWorkspace(workspace: WorkspaceFolder): Promise<DebugConfiguration[]> {
1111
const filename = path.join(workspace.uri.fsPath, '.vscode', 'launch.json');
12-
1312
if (!(await fs.pathExists(filename))) {
14-
return [];
13+
// Check launch config in the workspace file
14+
const codeWorkspaceConfig = getConfiguration('launch');
15+
if (!codeWorkspaceConfig.configurations || !Array.isArray(codeWorkspaceConfig.configurations)) {
16+
return [];
17+
}
18+
return codeWorkspaceConfig.configurations;
1519
}
1620

1721
const text = await fs.readFile(filename, 'utf-8');

src/extension/extensionInit.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { LaunchJsonUpdaterServiceHelper } from './debugger/configuration/launch.
3232
import { ignoreErrors } from './common/promiseUtils';
3333
import { pickArgsInput } from './common/utils/localize';
3434
import { DebugPortAttributesProvider } from './debugger/debugPort/portAttributesProvider';
35+
import { getConfigurationsByUri } from './debugger/configuration/launch.json/launchJsonReader';
3536

3637
export async function registerDebugger(context: IExtensionContext): Promise<void> {
3738
const childProcessAttachService = new ChildProcessAttachService();
@@ -74,6 +75,25 @@ export async function registerDebugger(context: IExtensionContext): Promise<void
7475
}),
7576
);
7677

78+
context.subscriptions.push(
79+
registerCommand(Commands.Debug_Using_Launch_Config, async (file?: Uri) => {
80+
sendTelemetryEvent(EventName.DEBUG_USING_LAUNCH_CONFIG_BUTTON);
81+
const interpreter = await getInterpreterDetails(file);
82+
83+
if (!interpreter.path) {
84+
runPythonExtensionCommand(Commands.TriggerEnvironmentSelection, file).then(noop, noop);
85+
return;
86+
}
87+
const configs = await getConfigurationsByUri(file);
88+
if (configs.length > 0) {
89+
executeCommand('workbench.action.debug.selectandstart');
90+
} else {
91+
await executeCommand('debug.addConfiguration');
92+
executeCommand('workbench.action.debug.start');
93+
}
94+
}),
95+
);
96+
7797
//PersistentStateFactory
7898
const persistantState = new PersistentStateFactory(context.globalState, context.workspaceState);
7999
persistantState.activate();

src/extension/telemetry/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
export enum EventName {
88
DEBUG_SUCCESS_ACTIVATION = 'DEBUG.SUCCESS_ACTIVATION',
99
DEBUG_IN_TERMINAL_BUTTON = 'DEBUG.IN_TERMINAL',
10+
DEBUG_USING_LAUNCH_CONFIG_BUTTON = 'DEBUG.USING_LAUNCH_CONFIG',
1011
DEBUG_ADAPTER_USING_WHEELS_PATH = 'DEBUG_ADAPTER.USING_WHEELS_PATH',
1112
DEBUG_SESSION_ERROR = 'DEBUG_SESSION.ERROR',
1213
DEBUG_SESSION_START = 'DEBUG_SESSION.START',

src/extension/telemetry/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,13 @@ export interface IEventNamePropertyMapping {
276276
"debug_in_terminal_button" : { "owner": "paulacamargo25" }
277277
*/
278278
[EventName.DEBUG_IN_TERMINAL_BUTTON]: never | undefined;
279+
/**
280+
* Telemetry event sent when debug using launch.json button was used to debug.
281+
*/
282+
/* __GDPR__
283+
"debug_using_launch_config_button" : { "owner": "paulacamargo25" }
284+
*/
285+
[EventName.DEBUG_USING_LAUNCH_CONFIG_BUTTON]: never | undefined;
279286
/**
280287
* Telemetry event captured when debug adapter executable is created
281288
*/

src/test/unittest/extensionInit.unit.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ suite('Debugging - register Debugging', () => {
6161
registerDebugger(context.object);
6262

6363
sinon.assert.calledWithExactly(registerCommandStub, Commands.Debug_In_Terminal, sinon.match.any);
64+
sinon.assert.calledWithExactly(registerCommandStub, Commands.Debug_Using_Launch_Config, sinon.match.any);
6465
sinon.assert.calledWithExactly(registerCommandStub, Commands.PickLocalProcess, sinon.match.any);
6566
sinon.assert.calledWithExactly(registerCommandStub, Commands.PickArguments, sinon.match.any);
6667
sinon.assert.calledWithExactly(
@@ -70,7 +71,7 @@ suite('Debugging - register Debugging', () => {
7071
sinon.match.any,
7172
);
7273
sinon.assert.calledWithExactly(registerCommandStub, Commands.ClearStorage, sinon.match.any);
73-
expect(registerCommandStub.callCount).to.be.equal(5);
74+
expect(registerCommandStub.callCount).to.be.equal(6);
7475
});
7576

7677
test('Activation will register the Debug adapter factories', async () => {

0 commit comments

Comments
 (0)