Skip to content

Commit 14a6ec7

Browse files
committed
Add command to add JDK to settings
Signed-off-by: Thomas Mäder <t.s.maeder@gmail.com>
1 parent 1bb0a99 commit 14a6ec7

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,6 +1752,11 @@
17521752
"command": "java.action.showExtendedOutline",
17531753
"title": "%java.action.showExtendedOutline%",
17541754
"category": "Java"
1755+
},
1756+
{
1757+
"command": "java.runtimes.add",
1758+
"title": "%java.runtimes.add%",
1759+
"category": "Java"
17551760
}
17561761
],
17571762
"keybindings": [

package.nls.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@
2929
"java.action.filesExplorerPasteAction": "Paste Clipboard Text Into a File",
3030
"java.action.doCleanup": "Performs Cleanup Actions",
3131
"java.change.searchScope": "Change Search Scope",
32-
"java.action.showExtendedOutline": "Open Extended Outline"
32+
"java.action.showExtendedOutline": "Open Extended Outline",
33+
"java.runtimes.add": "Add Java Runtime"
3334
}

src/extension.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import { BuildFileSelector, PICKED_BUILD_FILES, cleanupWorkspaceState } from './
3939
import { pasteFile } from './pasteAction';
4040
import { ServerStatusKind } from './serverStatus';
4141
import { TelemetryService } from '@redhat-developer/vscode-redhat-telemetry/lib/node';
42+
import { JavaRuntimes } from './javaRuntimes';
4243

4344
const syntaxClient: SyntaxLanguageClient = new SyntaxLanguageClient();
4445
const standardClient: StandardLanguageClient = new StandardLanguageClient();
@@ -114,6 +115,7 @@ export function fixJdtLinksInDocumentation(oldDocumentation: MarkdownString): Ma
114115

115116
export async function activate(context: ExtensionContext): Promise<ExtensionAPI> {
116117
await loadSupportedJreNames(context);
118+
await JavaRuntimes.initialize(context);
117119
context.subscriptions.push(commands.registerCommand(Commands.FILESEXPLORER_ONPASTE, async () => {
118120
const originalClipboard = await env.clipboard.readText();
119121
// Hack in order to get path to selected folder if applicable (see https://github.com/microsoft/vscode/issues/3553#issuecomment-1098562676)

src/javaRuntimes.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { getRuntime } from "jdk-utils";
2+
import * as vscode from "vscode";
3+
import { getSupportedJreNames } from "./jdkUtils";
4+
5+
6+
export namespace JavaRuntimes {
7+
export async function initialize(context: vscode.ExtensionContext): Promise<void> {
8+
context.subscriptions.push(vscode.commands.registerCommand('java.runtimes.add', async () => {
9+
const directory = await vscode.window.showOpenDialog({
10+
canSelectFiles: false,
11+
canSelectFolders: true,
12+
canSelectMany: false,
13+
title: 'Select JDK Directory',
14+
});
15+
if (directory) {
16+
const runtime = await getRuntime(directory[0].fsPath);
17+
if (runtime) {
18+
const config = vscode.workspace.getConfiguration('java.configuration').get('runtimes');
19+
if (Array.isArray(config)) {
20+
if (config.some(r => r.path === directory[0].fsPath)) {
21+
vscode.window.showErrorMessage(`JDK Directory ${directory[0].fsPath} already configured`);
22+
} else {
23+
const name = await vscode.window.showQuickPick(getSupportedJreNames(), {
24+
title: 'Select Java Runtime',
25+
});
26+
if (name) {
27+
config.push({
28+
name: name,
29+
path: directory[0].fsPath,
30+
});
31+
}
32+
vscode.workspace.getConfiguration('java.configuration').update('runtimes', config, vscode.ConfigurationTarget.Global);
33+
vscode.window.showInformationMessage(`JDK Directory ${directory[0].fsPath} added`);
34+
}
35+
}
36+
} else {
37+
vscode.window.showErrorMessage(`Invalid JDK Directory ${directory[0].fsPath}`);
38+
}
39+
}
40+
}));
41+
}
42+
}

0 commit comments

Comments
 (0)