Skip to content

Commit e1c9bbf

Browse files
authored
Improve the prompt message (#901)
* Improve the prompt message Signed-off-by: Sheng Chen <[email protected]> * Fix lint error * Do not auto pick when no main method in the file
1 parent 466e2f3 commit e1c9bbf

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

src/extension.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -266,12 +266,14 @@ async function runJavaFile(uri: vscode.Uri, noDebug: boolean) {
266266

267267
const hasMainMethods: boolean = mainMethods.length > 0;
268268
const canRunTests: boolean = await canDelegateToJavaTestRunner(uri);
269+
const defaultPlaceHolder: string = "Select the main class to run";
269270

270271
if (!hasMainMethods && !canRunTests) {
271272
const mainClasses: IMainClassOption[] = await utility.searchMainMethods();
272-
await launchMain(mainClasses, uri, noDebug);
273+
const placeHolder: string = `The file '${path.basename(uri.fsPath)}' is not executable, please select a main class you want to run.`;
274+
await launchMain(mainClasses, uri, noDebug, placeHolder, false /*autoPick*/);
273275
} else if (hasMainMethods && !canRunTests) {
274-
await launchMain(mainMethods, uri, noDebug);
276+
await launchMain(mainMethods, uri, noDebug, defaultPlaceHolder);
275277
} else if (!hasMainMethods && canRunTests) {
276278
await launchTesting(uri, noDebug);
277279
} else {
@@ -282,7 +284,7 @@ async function runJavaFile(uri: vscode.Uri, noDebug: boolean) {
282284
{ placeHolder: "Please select which kind of task you would like to launch" },
283285
);
284286
if (choice === launchMainChoice) {
285-
await launchMain(mainMethods, uri, noDebug);
287+
await launchMain(mainMethods, uri, noDebug, defaultPlaceHolder);
286288
} else if (choice === launchTestChoice) {
287289
await launchTesting(uri, noDebug);
288290
}
@@ -302,14 +304,15 @@ async function launchTesting(uri: vscode.Uri, noDebug: boolean): Promise<void> {
302304
noDebug ? vscode.commands.executeCommand("java.test.editor.run", uri) : vscode.commands.executeCommand("java.test.editor.debug", uri);
303305
}
304306

305-
async function launchMain(mainMethods: IMainClassOption[], uri: vscode.Uri, noDebug: boolean): Promise<void> {
307+
async function launchMain(mainMethods: IMainClassOption[], uri: vscode.Uri, noDebug: boolean, placeHolder: string,
308+
autoPick: boolean = true): Promise<void> {
306309
if (!mainMethods || !mainMethods.length) {
307310
vscode.window.showErrorMessage(
308311
"Error: Main method not found in the file, please define the main method as: public static void main(String[] args)");
309312
return;
310313
}
311314

312-
const pick = await mainClassPicker.showQuickPickWithRecentlyUsed(mainMethods, "Select the main class to run.");
315+
const pick = await mainClassPicker.showQuickPickWithRecentlyUsed(mainMethods, placeHolder, autoPick);
313316
if (!pick) {
314317
return;
315318
}

src/mainClassPicker.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,30 @@ class MainClassPicker {
5555
}
5656
}
5757

58-
// Record the main class picking history in a most recently used cache.
59-
public async showQuickPickWithRecentlyUsed(options: IMainClassOption[], placeHolder: string,
60-
labelFormatter?: (option: IMainClassOption) => string): Promise<IMainClassOption | undefined> {
58+
// tslint:disable-next-line
59+
public async showQuickPickWithRecentlyUsed(options: IMainClassOption[], placeHolder: string): Promise<IMainClassOption | undefined>;
60+
// tslint:disable-next-line
61+
public async showQuickPickWithRecentlyUsed(options: IMainClassOption[], placeHolder: string, labelFormatter: Formatter): Promise<IMainClassOption | undefined>;
62+
// tslint:disable-next-line
63+
public async showQuickPickWithRecentlyUsed(options: IMainClassOption[], placeHolder: string, autoPick: boolean): Promise<IMainClassOption | undefined>;
64+
// tslint:disable-next-line
65+
public async showQuickPickWithRecentlyUsed(options: IMainClassOption[], placeHolder: string, parameter3?: Formatter | boolean,
66+
parameter4?: boolean): Promise<IMainClassOption | undefined> {
67+
// Record the main class picking history in a most recently used cache.
68+
let labelFormatter: Formatter = defaultLabelFormatter;
69+
let autoPick: boolean = true;
70+
if (typeof parameter3 === "function") {
71+
labelFormatter = parameter3;
72+
} else if (typeof parameter3 === "boolean") {
73+
autoPick = parameter3;
74+
}
75+
if (typeof parameter4 === "boolean") {
76+
autoPick = parameter4;
77+
}
78+
6179
if (!options || !options.length) {
6280
return;
63-
} else if (options.length === 1) {
81+
} else if (autoPick && options.length === 1) {
6482
return options[0];
6583
}
6684

0 commit comments

Comments
 (0)