Skip to content

Commit e799210

Browse files
committed
reply to reviewers
1 parent 513df2b commit e799210

File tree

4 files changed

+56
-32
lines changed

4 files changed

+56
-32
lines changed

Extension/src/Debugger/configurationProvider.ts

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class DebugConfigurationProvider implements vscode.DebugConfigurationProv
4848
* Returns a list of initial debug configurations based on contextual information, e.g. package.json or folder.
4949
*/
5050
async provideDebugConfigurations(folder?: vscode.WorkspaceFolder, token?: vscode.CancellationToken): Promise<vscode.DebugConfiguration[]> {
51-
let configs: vscode.DebugConfiguration[] | null | undefined = await this.provideDebugConfigurationsTypeSpecific(this.type, folder, token);
51+
let configs: vscode.DebugConfiguration[] | null | undefined = await this.provideDebugConfigurationsForType(this.type, folder, token);
5252
if (!configs) {
5353
configs = [];
5454
}
@@ -80,29 +80,34 @@ export class DebugConfigurationProvider implements vscode.DebugConfigurationProv
8080

8181
const selection: MenuItem | undefined = await vscode.window.showQuickPick(items, {placeHolder: localize("select.configuration", "Select a configuration")});
8282
if (!selection) {
83-
// throw Error(localize("debug.configuration.selection.canceled", "Debug configuration selection canceled")); // User canceled it.
84-
Telemetry.logDebuggerEvent(DebuggerEvent.debugPanel, { "debugType": "debug", "folderMode": folder ? "folder" : "singleMode", "cancelled": "true" });
83+
Telemetry.logDebuggerEvent(DebuggerEvent.debugPanel, { "debugType": "debug", "folderMode": folder ? "folder" : "singleFile", "cancelled": "true" });
8584
return []; // User canceled it.
8685
}
87-
if (!this.isClAvailable(selection.label)) {
88-
return [selection.configuration];
86+
87+
if (this.isClConfiguration(selection.label)) {
88+
this.showErrorClNotAvailable(selection.label);
8989
}
9090

9191
return [selection.configuration];
9292
}
9393

9494
/**
9595
* Error checks the provided 'config' without any variables substituted.
96+
* If return "undefined", the debugging will be aborted silently.
97+
* If return "null", the debugging will be aborted and launch.json will be opened.
9698
*/
9799
resolveDebugConfiguration(folder: vscode.WorkspaceFolder | undefined, config: vscode.DebugConfiguration, token?: vscode.CancellationToken): vscode.ProviderResult<vscode.DebugConfiguration> {
98100
if (!config || !config.type) {
99101
// When DebugConfigurationProviderTriggerKind is Dynamic, this function will be called with an empty config.
100102
// Providing debug configs, and debugging should be called manually.
103+
// Currently, we expect only one debug config to be selected.
101104
this.provideDebugConfigurations(folder).then(async configs => {
102105
if (!configs || configs.length === 0) {
103-
Telemetry.logDebuggerEvent(DebuggerEvent.debugPanel, { "debugType": "debug", "folderMode": folder ? "folder" : "singleMode", "cancelled": "true" });
106+
Telemetry.logDebuggerEvent(DebuggerEvent.debugPanel, { "debugType": "debug", "folderMode": folder ? "folder" : "singleFile", "cancelled": "true" });
104107
return undefined; // aborts debugging silently
105108
} else {
109+
console.assert(configs.length === 1, "More than one debug config is selected.");
110+
await this.resolvePreLaunchTask(folder, configs[0], DebuggerEvent.debugPanel);
106111
await this.startDebugging(folder, configs[0], DebuggerEvent.debugPanel);
107112
return configs[0];
108113
}
@@ -221,7 +226,7 @@ export class DebugConfigurationProvider implements vscode.DebugConfigurationProv
221226
return config;
222227
}
223228

224-
async provideDebugConfigurationsTypeSpecific(type: DebuggerType, folder?: vscode.WorkspaceFolder, token?: vscode.CancellationToken): Promise<vscode.DebugConfiguration[]> {
229+
async provideDebugConfigurationsForType(type: DebuggerType, folder?: vscode.WorkspaceFolder, token?: vscode.CancellationToken): Promise<vscode.DebugConfiguration[]> {
225230
const defaultConfig: vscode.DebugConfiguration = this.assetProvider.getInitialConfigurations(type).find((config: any) =>
226231
isDebugLaunchStr(config.name) && config.request === "launch");
227232
console.assert(defaultConfig, "Could not find default debug configuration.");
@@ -369,17 +374,22 @@ export class DebugConfigurationProvider implements vscode.DebugConfigurationProv
369374
}
370375

371376
private buildAndDebugActiveFileStr(): string {
372-
return `${localize("build.and.debug.active.file", 'Build and debug active file')}`;
377+
return `${localize("build.and.debug.active.file", 'build and debug active file')}`;
378+
}
379+
380+
private isClConfiguration(configurationLabel: string) : boolean{
381+
return configurationLabel.startsWith("C/C++: cl.exe")? true : false;
373382
}
374383

375-
private isClAvailable(configurationLabel: string): boolean {
376-
if (configurationLabel.startsWith("C/C++: cl.exe")) {
384+
private showErrorClNotAvailable(configurationLabel: string): boolean {
385+
if (this.isClConfiguration(configurationLabel)) {
377386
if (!process.env.DevEnvDir || process.env.DevEnvDir.length === 0) {
378387
vscode.window.showErrorMessage(localize("cl.exe.not.available", "{0} build and debug is only usable when VS Code is run from the Developer Command Prompt for VS.", "cl.exe"));
379-
return false;
388+
return true;
380389
}
390+
return false;
381391
}
382-
return true;
392+
throw new Error("Config is not a cl.exe config.");
383393
}
384394

385395
private getLLDBFrameworkPath(): string | undefined {
@@ -504,6 +514,11 @@ export class DebugConfigurationProvider implements vscode.DebugConfigurationProv
504514
}
505515
}
506516

517+
public async buildAndRun(textEditor: vscode.TextEditor): Promise<void> {
518+
// Turn off the debug mode.
519+
return this.buildAndDebug(textEditor, false);
520+
}
521+
507522
public async buildAndDebug(textEditor: vscode.TextEditor, debugModeOn: boolean = true): Promise<void> {
508523

509524
const folder: vscode.WorkspaceFolder | undefined = vscode.workspace.getWorkspaceFolder(textEditor.document.uri);
@@ -513,11 +528,10 @@ export class DebugConfigurationProvider implements vscode.DebugConfigurationProv
513528
}
514529

515530
// Get debug configurations for all debugger types.
516-
let configs: vscode.DebugConfiguration[] = [];
531+
let configs: vscode.DebugConfiguration[] = await this.provideDebugConfigurationsForType(DebuggerType.cppdbg, folder);
517532
if (os.platform() === 'win32') {
518-
configs = await this.provideDebugConfigurationsTypeSpecific(DebuggerType.cppvsdbg, folder);
533+
configs = configs.concat(await this.provideDebugConfigurationsForType(DebuggerType.cppvsdbg, folder));
519534
}
520-
configs = configs.concat(await this.provideDebugConfigurationsTypeSpecific(DebuggerType.cppdbg, folder));
521535

522536
const defaultConfig: vscode.DebugConfiguration[] = configs.filter((config: vscode.DebugConfiguration) => (config.hasOwnProperty("isDefault") && config.isDefault));
523537
interface MenuItem extends vscode.QuickPickItem {
@@ -547,12 +561,15 @@ export class DebugConfigurationProvider implements vscode.DebugConfigurationProv
547561

548562
const debuggerEvent: string = DebuggerEvent.launchPlayButton;
549563
if (!selection) {
550-
Telemetry.logDebuggerEvent(debuggerEvent, { "debugType": debugModeOn ? "debug" : "run", "folderMode": folder ? "folder" : "singleMode", "cancelled": "true" });
564+
Telemetry.logDebuggerEvent(debuggerEvent, { "debugType": debugModeOn ? "debug" : "run", "folderMode": folder ? "folder" : "singleFile", "cancelled": "true" });
551565
return; // User canceled it.
552566
}
553-
if (!this.isClAvailable(selection.label)) {
567+
568+
if (this.isClConfiguration(selection.label) && this.showErrorClNotAvailable(selection.label)) {
554569
return;
555570
}
571+
572+
// Resolve config before start debugging.
556573
let resolvedConfig: vscode.DebugConfiguration | undefined | null;
557574
if (selection.configuration && selection.configuration.type) {
558575
resolvedConfig = await this.resolveDebugConfiguration(folder, selection.configuration);
@@ -561,24 +578,24 @@ export class DebugConfigurationProvider implements vscode.DebugConfigurationProv
561578
}
562579
}
563580
if (resolvedConfig) {
581+
await this.resolvePreLaunchTask(folder, resolvedConfig, debuggerEvent, debugModeOn);
564582
await this.startDebugging(folder, resolvedConfig, debuggerEvent, debugModeOn);
565583
}
566584
}
567585

568-
private async startDebugging(folder: vscode.WorkspaceFolder | undefined, configuration: vscode.DebugConfiguration, debuggerEvent: string, debugModeOn: boolean = true): Promise<void> {
569-
586+
private async resolvePreLaunchTask(folder: vscode.WorkspaceFolder | undefined, configuration: vscode.DebugConfiguration, debuggerEvent: string, debugModeOn: boolean = true): Promise<void> {
570587
const debugType: string = debugModeOn ? "debug" : "run";
571-
const folderMode: string = folder ? "folder" : "singleMode";
588+
const folderMode: string = folder ? "folder" : "singleFile";
572589
if (configuration.preLaunchTask) {
573590
try {
574591
if (folder) {
575592
await cppBuildTaskProvider.checkBuildTaskExists(configuration.preLaunchTask);
576593
DebugConfigurationProvider.recentBuildTaskLableStr = configuration.preLaunchTask;
577594
} else {
578-
// In case of single mode file, remove the preLaunch task from the debug configuration and run it here instead.
595+
// In case of singleFile, remove the preLaunch task from the debug configuration and run it here instead.
579596
await cppBuildTaskProvider.runBuildTask(configuration.preLaunchTask);
580597
DebugConfigurationProvider.recentBuildTaskLableStr = configuration.preLaunchTask;
581-
configuration.preLaunchTask = undefined;
598+
582599
}
583600
} catch (errJS) {
584601
const e: Error = errJS as Error;
@@ -588,6 +605,15 @@ export class DebugConfigurationProvider implements vscode.DebugConfigurationProv
588605
Telemetry.logDebuggerEvent(debuggerEvent, { "debugType": debugType, "folderMode": folderMode, "config": "noBuildConfig", "success": "false" });
589606
}
590607
}
608+
}
609+
610+
private async startDebugging(folder: vscode.WorkspaceFolder | undefined, configuration: vscode.DebugConfiguration, debuggerEvent: string, debugModeOn: boolean = true): Promise<void> {
611+
const debugType: string = debugModeOn ? "debug" : "run";
612+
const folderMode: string = folder ? "folder" : "singleFile";
613+
if (!folder) {
614+
// In case of singleFile, remove the preLaunch task.
615+
configuration.preLaunchTask = undefined;
616+
}
591617
try {
592618
// Check if the debug configuration exists in launch.json.
593619
await cppBuildTaskProvider.checkDebugConfigExists(configuration.name);

Extension/src/Debugger/configurations.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFo
1010
const localize: nls.LocalizeFunc = nls.loadMessageBundle();
1111

1212
export enum DebuggerType {
13-
cppvsdbg,
14-
cppdbg,
15-
all
13+
cppvsdbg = "cppvsdbg",
14+
cppdbg = "cppdbg",
15+
all = "all"
1616
}
1717

1818
export enum DebuggerEvent {

Extension/src/Debugger/extension.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ export async function initialize(context: vscode.ExtensionContext): Promise<void
3030
let cppVsDebugProvider: DebugConfigurationProvider | null = null;
3131
if (os.platform() === 'win32') {
3232
cppVsDebugProvider = new DebugConfigurationProvider(assetProvider, DebuggerType.cppvsdbg);
33-
disposables.push(vscode.debug.registerDebugConfigurationProvider('cppvsdbg', cppVsDebugProvider, vscode.DebugConfigurationProviderTriggerKind.Dynamic));
33+
disposables.push(vscode.debug.registerDebugConfigurationProvider(DebuggerType.cppvsdbg, cppVsDebugProvider, vscode.DebugConfigurationProviderTriggerKind.Dynamic));
3434
}
3535
const cppDebugProvider: DebugConfigurationProvider = new DebugConfigurationProvider(assetProvider, DebuggerType.cppdbg);
36-
disposables.push(vscode.debug.registerDebugConfigurationProvider('cppdbg', cppDebugProvider, vscode.DebugConfigurationProviderTriggerKind.Dynamic));
36+
disposables.push(vscode.debug.registerDebugConfigurationProvider(DebuggerType.cppdbg, cppDebugProvider, vscode.DebugConfigurationProviderTriggerKind.Dynamic));
3737

3838
// Register DebugConfigurationProviders for "Run and Debug" play button.
3939
const debugProvider: DebugConfigurationProvider = new DebugConfigurationProvider(assetProvider, DebuggerType.all);
4040
disposables.push(vscode.commands.registerTextEditorCommand("C_Cpp.BuildAndDebugFile", async (textEditor: vscode.TextEditor, edit: vscode.TextEditorEdit, ...args: any[]) => { await debugProvider.buildAndDebug(textEditor); }));
41-
disposables.push(vscode.commands.registerTextEditorCommand("C_Cpp.BuildAndRunFile", async (textEditor: vscode.TextEditor, edit: vscode.TextEditorEdit, ...args: any[]) => { await debugProvider.buildAndDebug(textEditor, false); }));
41+
disposables.push(vscode.commands.registerTextEditorCommand("C_Cpp.BuildAndRunFile", async (textEditor: vscode.TextEditor, edit: vscode.TextEditorEdit, ...args: any[]) => { await debugProvider.buildAndRun(textEditor); }));
4242

4343
assetProvider.getConfigurationSnippets();
4444

@@ -52,8 +52,8 @@ export async function initialize(context: vscode.ExtensionContext): Promise<void
5252
disposables.push(vscode.languages.registerCompletionItemProvider(launchJsonDocumentSelector, new ConfigurationSnippetProvider(assetProvider)));
5353

5454
// Register Debug Adapters
55-
disposables.push(vscode.debug.registerDebugAdapterDescriptorFactory('cppvsdbg' , new CppvsdbgDebugAdapterDescriptorFactory(context)));
56-
disposables.push(vscode.debug.registerDebugAdapterDescriptorFactory('cppdbg', new CppdbgDebugAdapterDescriptorFactory(context)));
55+
disposables.push(vscode.debug.registerDebugAdapterDescriptorFactory(DebuggerType.cppvsdbg , new CppvsdbgDebugAdapterDescriptorFactory(context)));
56+
disposables.push(vscode.debug.registerDebugAdapterDescriptorFactory(DebuggerType.cppdbg, new CppdbgDebugAdapterDescriptorFactory(context)));
5757

5858
vscode.Disposable.from(...disposables);
5959
}

Extension/tools/OptionsSchema.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@
195195
},
196196
"CppdbgLaunchOptions": {
197197
"type": "object",
198-
"default": {},
199198
"required": [
200199
"program"
201200
],
@@ -519,7 +518,6 @@
519518
},
520519
"CppvsdbgLaunchOptions": {
521520
"type": "object",
522-
"default": {},
523521
"required": [
524522
"program",
525523
"cwd"

0 commit comments

Comments
 (0)