Skip to content

Commit 6cfbaf9

Browse files
Add python config with args (#56)
* Add ne config type * Add command to show input * Fix code affected by the changes * run prettier * Add tests * Update text * remove unnecessary description * Update name
1 parent c9ab19a commit 6cfbaf9

File tree

12 files changed

+115
-15
lines changed

12 files changed

+115
-15
lines changed

package.json

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,19 @@
210210
"items": {
211211
"type": "string"
212212
},
213-
"type": [
214-
"array",
215-
"string"
213+
"anyOf": [
214+
{
215+
"default": "${command:pickArgs}",
216+
"enum": [
217+
"${command:pickArgs}"
218+
]
219+
},
220+
{
221+
"type": [
222+
"array",
223+
"string"
224+
]
225+
}
216226
]
217227
},
218228
"autoReload": {
@@ -415,7 +425,8 @@
415425
],
416426
"type": "debugpy",
417427
"variables": {
418-
"pickProcess": "debugpy.pickLocalProcess"
428+
"pickProcess": "debugpy.pickLocalProcess",
429+
"pickArgs": "debugpy.pickArgs"
419430
},
420431
"when": "!virtualWorkspace && shellExecutionSupported"
421432
}

src/extension/common/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export namespace Commands {
3232
export const Debug_In_Terminal = 'debugpy.debugInTerminal';
3333
export const TriggerEnvironmentSelection = 'debugpy.triggerEnvSelection';
3434
export const PickLocalProcess = 'debugpy.pickLocalProcess';
35+
export const PickArguments = 'debugpy.pickArgs';
3536
export const ViewOutput = 'debugpy.viewOutput';
3637
export const ClearStorage = 'debugpy.clearCacheAndReload';
3738
export const Enable_SourceMap_Support = 'debugpy.enableSourceMapSupport';

src/extension/common/utils/localize.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ export namespace DebugConfigStrings {
3030
description: l10n.t('Debug the currently active Python file'),
3131
};
3232
}
33+
export namespace fileWithArgs {
34+
export const snippet = {
35+
name: l10n.t('Debugpy: Current File with Arguments'),
36+
};
37+
export const selectConfiguration = {
38+
label: l10n.t('Python File with Arguments'),
39+
description: l10n.t('Debug the currently active Python file with arguments'),
40+
};
41+
}
3342
export namespace module {
3443
export const snippet = {
3544
name: l10n.t('Debugpy: Module'),
@@ -193,3 +202,8 @@ export namespace OutdatedDebugger {
193202
export namespace Logging {
194203
export const currentWorkingDirectory = l10n.t('cwd:');
195204
}
205+
206+
export namespace pickArgsInput {
207+
export const title = l10n.t('Command Line Arguments');
208+
export const prompt = l10n.t('Enter the command line arguments you want to pass to the program');
209+
}

src/extension/debugger/attachQuickPick/types.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@ export interface IAttachProcessProvider {
1717
getAttachItems(): Promise<IAttachItem[]>;
1818
}
1919

20-
// eslint-disable-next-line @typescript-eslint/naming-convention
21-
// export const IAttachProcessProviderFactory = Symbol('IAttachProcessProviderFactory');
22-
// export interface IAttachProcessProviderFactory {
23-
// registerCommands(): void;
24-
// }
25-
2620
export interface IAttachPicker {
2721
showQuickPick(): Promise<string>;
2822
}

src/extension/debugger/configuration/debugConfigurationService.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { buildPidAttachConfiguration } from './providers/pidAttach';
1919
import { buildPyramidLaunchConfiguration } from './providers/pyramidLaunch';
2020
import { buildRemoteAttachConfiguration } from './providers/remoteAttach';
2121
import { IDebugConfigurationResolver } from './types';
22+
import { buildFileWithArgsLaunchDebugConfiguration } from './providers/fileLaunchWithArgs';
2223

2324
@injectable()
2425
export class PythonDebugConfigurationService implements IDebugConfigurationService {
@@ -116,6 +117,11 @@ export class PythonDebugConfigurationService implements IDebugConfigurationServi
116117
type: DebugConfigurationType.launchFile,
117118
description: DebugConfigStrings.file.selectConfiguration.description,
118119
},
120+
{
121+
label: DebugConfigStrings.fileWithArgs.selectConfiguration.label,
122+
type: DebugConfigurationType.launchFileWithArgs,
123+
description: DebugConfigStrings.fileWithArgs.selectConfiguration.description,
124+
},
119125
{
120126
label: DebugConfigStrings.module.selectConfiguration.label,
121127
type: DebugConfigurationType.launchModule,
@@ -162,6 +168,7 @@ export class PythonDebugConfigurationService implements IDebugConfigurationServi
162168
debugConfigurations.set(DebugConfigurationType.launchDjango, buildDjangoLaunchDebugConfiguration);
163169
debugConfigurations.set(DebugConfigurationType.launchFastAPI, buildFastAPILaunchDebugConfiguration);
164170
debugConfigurations.set(DebugConfigurationType.launchFile, buildFileLaunchDebugConfiguration);
171+
debugConfigurations.set(DebugConfigurationType.launchFileWithArgs, buildFileWithArgsLaunchDebugConfiguration);
165172
debugConfigurations.set(DebugConfigurationType.launchFlask, buildFlaskLaunchDebugConfiguration);
166173
debugConfigurations.set(DebugConfigurationType.launchModule, buildModuleLaunchConfiguration);
167174
debugConfigurations.set(DebugConfigurationType.pidAttach, buildPidAttachConfiguration);

src/extension/debugger/configuration/providers/fastapiLaunch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export async function buildFastAPILaunchDebugConfiguration(
3030
justMyCode: true,
3131
};
3232

33-
if (!application && config.args) {
33+
if (!application) {
3434
const selectedPath = await input.showInputBox({
3535
title: DebugConfigStrings.fastapi.enterAppPathOrNamePath.title,
3636
value: 'main.py',
@@ -44,7 +44,7 @@ export async function buildFastAPILaunchDebugConfiguration(
4444
});
4545
if (selectedPath) {
4646
manuallyEnteredAValue = true;
47-
config.args[0] = `${path.basename(selectedPath, '.py').replace('/', '.')}:app`;
47+
config.args = [`${path.basename(selectedPath, '.py').replace('/', '.')}:app`, '--reload'];
4848
}
4949
}
5050

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
'use strict';
5+
6+
import { DebugConfigStrings } from '../../../common/utils/localize';
7+
import { MultiStepInput } from '../../../common/multiStepInput';
8+
import { sendTelemetryEvent } from '../../../telemetry';
9+
import { EventName } from '../../../telemetry/constants';
10+
import { DebuggerTypeName } from '../../../constants';
11+
import { LaunchRequestArguments } from '../../../types';
12+
import { DebugConfigurationState, DebugConfigurationType } from '../../types';
13+
14+
export async function buildFileWithArgsLaunchDebugConfiguration(
15+
_input: MultiStepInput<DebugConfigurationState>,
16+
state: DebugConfigurationState,
17+
): Promise<void> {
18+
const config: Partial<LaunchRequestArguments> = {
19+
name: DebugConfigStrings.fileWithArgs.snippet.name,
20+
type: DebuggerTypeName,
21+
request: 'launch',
22+
program: '${file}',
23+
console: 'integratedTerminal',
24+
args: '${command:pickArgs}',
25+
justMyCode: true,
26+
};
27+
sendTelemetryEvent(EventName.DEBUGGER_CONFIGURATION_PROMPTS, undefined, {
28+
configurationType: DebugConfigurationType.launchFileWithArgs,
29+
});
30+
Object.assign(state.config, config);
31+
}

src/extension/debugger/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export type DebugConfigurationState = {
3030

3131
export enum DebugConfigurationType {
3232
launchFile = 'launchFile',
33+
launchFileWithArgs = 'launchFileWithArgs',
3334
remoteAttach = 'remoteAttach',
3435
launchDjango = 'launchDjango',
3536
launchFastAPI = 'launchFastAPI',

src/extension/extensionInit.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
'use strict';
55

6-
import { debug, DebugConfigurationProviderTriggerKind, languages, Uri } from 'vscode';
6+
import { debug, DebugConfigurationProviderTriggerKind, languages, Uri, window } from 'vscode';
77
import { executeCommand, getConfiguration, registerCommand, startDebugging } from './common/vscodeapi';
88
import { DebuggerTypeName } from './constants';
99
import { DynamicPythonDebugConfigurationService } from './debugger/configuration/dynamicdebugConfigurationService';
@@ -31,6 +31,7 @@ import { JsonLanguages, LaunchJsonCompletionProvider } from './debugger/configur
3131
import { InterpreterPathCommand } from './debugger/configuration/launch.json/interpreterPathCommand';
3232
import { LaunchJsonUpdaterServiceHelper } from './debugger/configuration/launch.json/updaterServiceHelper';
3333
import { ignoreErrors } from './common/promiseUtils';
34+
import { pickArgsInput } from './common/utils/localize';
3435

3536
export async function registerDebugger(context: IExtensionContext): Promise<void> {
3637
const childProcessAttachService = new ChildProcessAttachService();
@@ -80,6 +81,11 @@ export async function registerDebugger(context: IExtensionContext): Promise<void
8081
const attachProcessProvider = new AttachProcessProvider();
8182
const attachPicker = new AttachPicker(attachProcessProvider);
8283
context.subscriptions.push(registerCommand(Commands.PickLocalProcess, () => attachPicker.showQuickPick()));
84+
context.subscriptions.push(
85+
registerCommand(Commands.PickArguments, () => {
86+
return window.showInputBox({ title: pickArgsInput.title, prompt: pickArgsInput.prompt });
87+
}),
88+
);
8389

8490
const debugAdapterDescriptorFactory = new DebugAdapterDescriptorFactory(persistantState);
8591
const debugSessionLoggingFactory = new DebugSessionLoggingFactory();

src/extension/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ interface IKnownLaunchRequestArguments extends ICommonDebugArguments {
9797
python?: string;
9898
// Automatically stop target after launch. If not specified, target does not stop.
9999
stopOnEntry?: boolean;
100-
args?: string[];
100+
args?: string[] | String;
101101
cwd?: string;
102102
debugOptions?: DebugOptions[];
103103
env?: Record<string, string | undefined>;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
'use strict';
5+
6+
import { expect } from 'chai';
7+
import * as path from 'path';
8+
import { Uri } from 'vscode';
9+
import { MultiStepInput } from '../../../../extension/common/multiStepInput';
10+
import { DebugConfigStrings } from '../../../../extension/common/utils/localize';
11+
import { DebuggerTypeName } from '../../../../extension/constants';
12+
import { buildFileWithArgsLaunchDebugConfiguration } from '../../../../extension/debugger/configuration/providers/fileLaunchWithArgs';
13+
import { DebugConfigurationState } from '../../../../extension/debugger/types';
14+
15+
suite('Debugging - Configuration Provider File with Arguments', () => {
16+
test('Launch JSON with default config', async () => {
17+
const folder = { uri: Uri.parse(path.join('one', 'two')), name: '1', index: 0 };
18+
const state = { config: {}, folder };
19+
20+
await buildFileWithArgsLaunchDebugConfiguration(undefined as unknown as MultiStepInput<DebugConfigurationState>, state);
21+
22+
const config = {
23+
name: DebugConfigStrings.fileWithArgs.snippet.name,
24+
type: DebuggerTypeName,
25+
request: 'launch',
26+
program: '${file}',
27+
console: 'integratedTerminal',
28+
args: '${command:pickArgs}',
29+
justMyCode: true,
30+
};
31+
32+
expect(state.config).to.be.deep.equal(config);
33+
});
34+
});

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

6262
sinon.assert.calledWithExactly(registerCommandStub, Commands.Debug_In_Terminal, sinon.match.any);
6363
sinon.assert.calledWithExactly(registerCommandStub, Commands.PickLocalProcess, sinon.match.any);
64+
sinon.assert.calledWithExactly(registerCommandStub, Commands.PickArguments, sinon.match.any);
6465
sinon.assert.calledWithExactly(
6566
registerCommandStub,
6667
Commands.SelectDebugConfig,
@@ -69,7 +70,7 @@ suite('Debugging - register Debugging', () => {
6970
);
7071
sinon.assert.calledWithExactly(registerCommandStub, Commands.GetSelectedInterpreterPath, sinon.match.any);
7172
sinon.assert.calledWithExactly(registerCommandStub, Commands.ClearStorage, sinon.match.any);
72-
expect(registerCommandStub.callCount).to.be.equal(5);
73+
expect(registerCommandStub.callCount).to.be.equal(6);
7374
});
7475
test('Activation will register the Debug adapter factories', async () => {
7576
registerDebugger(context.object);

0 commit comments

Comments
 (0)