Skip to content

Commit 1643b7b

Browse files
committed
feat: recommend JBang for single Java file usage
When a user opens a single Java file with no workspace folder, or a workspace folder with just a single Java file, show an information message recommending the JBang extension for running Java scripts. Includes option to install the jbang-vscode extension from the marketplace, and a 'Don't Show Again' option to suppress future notifications. Fixes #3111
1 parent bd4185a commit 1643b7b

4 files changed

Lines changed: 68 additions & 1 deletion

File tree

‎package.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1900,6 +1900,11 @@
19001900
"command": "java.action.copyFullyQualifiedName",
19011901
"title": "%java.action.copyFullyQualifiedName%",
19021902
"category": "Java"
1903+
},
1904+
{
1905+
"command": "java.jbang.recommend",
1906+
"title": "%java.jbang.recommend%",
1907+
"category": "Java"
19031908
}
19041909
],
19051910
"keybindings": [

‎package.nls.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@
2626
"java.change.searchScope": "Change Search Scope",
2727
"java.action.showExtendedOutline": "Open Extended Outline",
2828
"java.runtimes.add": "Add Java Runtime",
29-
"java.action.copyFullyQualifiedName": "Copy Fully Qualified Name"
29+
"java.action.copyFullyQualifiedName": "Copy Fully Qualified Name",
30+
"java.jbang.recommend": "Recommend JBang"
3031
}

‎src/commands.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,11 @@ export namespace Commands {
368368
*/
369369
export const COPY_FULLY_QUALIFIED_NAME = 'java.action.copyFullyQualifiedName';
370370
export const GET_FULLY_QUALIFIED_NAME = 'java.getFullyQualifiedName';
371+
372+
/**
373+
* Recommend JBang for single Java file usage.
374+
*/
375+
export const JBANG_RECOMMEND = 'java.jbang.recommend';
371376
}
372377

373378
/**

‎src/extension.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,15 @@ export async function activate(context: ExtensionContext): Promise<ExtensionAPI>
437437
cleanWorkspaceExists
438438
);
439439

440+
// Recommend JBang when working with a single Java file (issue #3111)
441+
void showJBangRecommendation(context);
442+
443+
context.subscriptions.push(workspace.onDidOpenTextDocument(async (document) => {
444+
if (document.languageId === 'java') {
445+
await showJBangRecommendation(context);
446+
}
447+
}));
448+
440449
return apiManager.getApiInstance();
441450
}
442451

@@ -1412,3 +1421,50 @@ function addRevealCommand(client: LanguageClient, codeAction: CodeAction, lspEdi
14121421
}
14131422
return codeAction;
14141423
}
1424+
1425+
const JBANG_RECOMMENDATION_KEY = 'jbangRecommendationShown';
1426+
const JBANG_EXTENSION_ID = 'jbangdev.jbang-vscode';
1427+
const JBANG_MARKETPLACE_URL = 'https://marketplace.visualstudio.com/items?itemName=jbangdev.jbang-vscode';
1428+
1429+
function shouldRecommendJBang(): boolean {
1430+
// Case 1: No workspace folder (standalone file mode)
1431+
if (!workspace.workspaceFolders) {
1432+
return true;
1433+
}
1434+
// Case 2: Workspace folder with just a single Java file
1435+
if (workspace.workspaceFolders.length === 1) {
1436+
const javaFileCount = workspace.textDocuments.filter(doc => doc.languageId === 'java').length;
1437+
if (javaFileCount <= 1) {
1438+
return true;
1439+
}
1440+
}
1441+
return false;
1442+
}
1443+
1444+
async function showJBangRecommendation(context: ExtensionContext): Promise<void> {
1445+
const alreadyShown = context.globalState.get<boolean>(JBANG_RECOMMENDATION_KEY, false);
1446+
if (alreadyShown) {
1447+
return;
1448+
}
1449+
1450+
if (!shouldRecommendJBang()) {
1451+
return;
1452+
}
1453+
1454+
const install = 'Install JBang';
1455+
const maybeLater = 'Maybe Later';
1456+
const dontShowAgain = "Don't Show Again";
1457+
1458+
const choice = await window.showInformationMessage(
1459+
'You are working with a single Java file. JBang makes it easy to run Java scripts. Would you like to install the JBang extension?',
1460+
install, maybeLater, dontShowAgain
1461+
);
1462+
1463+
if (choice === install) {
1464+
await commands.executeCommand(Commands.OPEN_BROWSER, Uri.parse(JBANG_MARKETPLACE_URL));
1465+
}
1466+
1467+
if (choice === dontShowAgain || choice === install) {
1468+
await context.globalState.update(JBANG_RECOMMENDATION_KEY, true);
1469+
}
1470+
}

0 commit comments

Comments
 (0)