Skip to content

Commit b7f76ca

Browse files
Generate the launch configuration in memory if no launch.json exists (#733)
* Generate the launch configuration in memory if no launch.json exists Signed-off-by: Jinbo Wang <[email protected]> * Improve launch.json searching logic for multi roots Signed-off-by: Jinbo Wang <[email protected]> * make tslint happy Signed-off-by: Jinbo Wang <[email protected]>
1 parent f9abdc9 commit b7f76ca

File tree

4 files changed

+25
-24
lines changed

4 files changed

+25
-24
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"debugger"
1515
],
1616
"engines": {
17-
"vscode": "^1.32.0"
17+
"vscode": "^1.38.0"
1818
},
1919
"license": "SEE LICENSE IN LICENSE.txt",
2020
"repository": {
@@ -529,7 +529,7 @@
529529
"@types/lodash": "^4.14.137",
530530
"@types/mocha": "^5.2.7",
531531
"@types/node": "^8.10.51",
532-
"@types/vscode": "1.32.0",
532+
"@types/vscode": "1.38.0",
533533
"cross-env": "^5.2.0",
534534
"gulp": "^4.0.2",
535535
"gulp-tslint": "^8.1.4",

src/configurationProvider.ts

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,8 @@ export class JavaDebugConfigurationProvider implements vscode.DebugConfiguration
135135
await updateDebugSettings();
136136
}
137137

138-
/**
139-
* If no launch.json exists in the current workspace folder
140-
* delegate to provideDebugConfigurations api to generate the initial launch.json configurations
141-
*/
142-
if (this.isEmptyConfig(config) && folder) {
143-
// Follow the feature request https://github.com/Microsoft/vscode/issues/54213#issuecomment-420965778,
144-
// in order to generate launch.json, the resolveDebugConfiguration api must return null explicitly.
145-
return null;
146-
}
147-
148-
// If it's the single file case that no workspace folder is opened, generate debug config in memory
149-
if (this.isEmptyConfig(config) && !folder) {
138+
// If no debug configuration is provided, then generate one in memory.
139+
if (this.isEmptyConfig(config)) {
150140
config.type = "java";
151141
config.name = "Java Debug";
152142
config.request = "launch";
@@ -293,13 +283,13 @@ export class JavaDebugConfigurationProvider implements vscode.DebugConfiguration
293283
return mainEntries[0];
294284
} else if (mainEntries.length > 1) {
295285
return this.showMainClassQuickPick(this.formatMainClassOptions(mainEntries),
296-
`Multiple main classes found in the file '${path.basename(currentFile)}', please select one first.`);
286+
`Please select a main class you want to run.`);
297287
}
298288
}
299289

300290
const hintMessage = currentFile ?
301-
`No main class found in the file '${path.basename(currentFile)}', please select main class<project name> again.` :
302-
"Please select main class<project name>.";
291+
`The file '${path.basename(currentFile)}' is not executable, please select a main class you want to run.` :
292+
"Please select a main class you want to run.";
303293
return this.promptMainClass(folder, hintMessage);
304294
}
305295

@@ -389,8 +379,9 @@ export class JavaDebugConfigurationProvider implements vscode.DebugConfiguration
389379
private async promptMainClass(folder: vscode.Uri | undefined, hintMessage?: string): Promise<lsPlugin.IMainClassOption | undefined> {
390380
const res = await lsPlugin.resolveMainClass(folder);
391381
if (res.length === 0) {
382+
const workspaceFolder = folder ? vscode.workspace.getWorkspaceFolder(folder) : undefined;
392383
throw new utility.UserError({
393-
message: "Cannot find a class with the main method.",
384+
message: `Cannot find a class with the main method${ workspaceFolder ? " in the folder '" + workspaceFolder.name + "'" : ""}.`,
394385
type: Type.USAGEERROR,
395386
anchor: anchor.CANNOT_FIND_MAIN_CLASS,
396387
});

src/debugCodeLensProvider.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ async function constructDebugConfig(mainClass: string, projectName: string, work
149149
projectName,
150150
};
151151

152-
// Persist the default debug configuration only if the workspace exists.
153-
if (workspace) {
152+
// Persist the configuration into launch.json only if the launch.json file already exists in the workspace.
153+
if ((rawConfigs && rawConfigs.length) || await launchJsonExists(workspace)) {
154154
try {
155155
// Insert the default debug configuration to the beginning of launch.json.
156156
rawConfigs.splice(0, 0, debugConfig);
@@ -166,6 +166,16 @@ async function constructDebugConfig(mainClass: string, projectName: string, work
166166
return _.cloneDeep(debugConfig);
167167
}
168168

169+
async function launchJsonExists(workspace: vscode.Uri): Promise<boolean> {
170+
if (!workspace) {
171+
return false;
172+
}
173+
174+
const workspaceFolder = vscode.workspace.getWorkspaceFolder(workspace);
175+
const results: vscode.Uri[] = await vscode.workspace.findFiles(".vscode/launch.json");
176+
return !!results.find((launchJson) => vscode.workspace.getWorkspaceFolder(launchJson) === workspaceFolder);
177+
}
178+
169179
export async function startDebugging(mainClass: string, projectName: string, uri: vscode.Uri, noDebug: boolean): Promise<boolean> {
170180
const workspaceFolder: vscode.WorkspaceFolder = vscode.workspace.getWorkspaceFolder(uri);
171181
const workspaceUri: vscode.Uri = workspaceFolder ? workspaceFolder.uri : undefined;

0 commit comments

Comments
 (0)