Skip to content

Commit cbe1b37

Browse files
committed
Addressed review comments
Signed-off-by: Thomas Mäder <t.s.maeder@gmail.com>
1 parent d557c1e commit cbe1b37

File tree

5 files changed

+37
-14
lines changed

5 files changed

+37
-14
lines changed

package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -930,9 +930,6 @@
930930
"name": {
931931
"type": "string",
932932
"enum": [
933-
"J2SE-1.5",
934-
"JavaSE-1.6",
935-
"JavaSE-1.7",
936933
"JavaSE-1.8",
937934
"JavaSE-9",
938935
"JavaSE-10",

src/commands.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,11 @@ export namespace Commands {
380380
* Open Java Dashboard
381381
*/
382382
export const OPEN_JAVA_DASHBOARD = 'java.dashboard.open';
383+
384+
/**
385+
* Add Java Runtime
386+
*/
387+
export const ADD_JAVA_RUNTIME = 'java.runtimes.add';
383388
}
384389

385390
/**

src/javaRuntimes.ts

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,57 @@
1-
import { getRuntime } from "jdk-utils";
1+
import { getRuntime, IJavaRuntime } from "jdk-utils";
22
import * as vscode from "vscode";
33
import { getSupportedJreNames } from "./jdkUtils";
4+
import { Commands } from "./commands";
5+
import * as path from "path";
46

57

68
export namespace JavaRuntimes {
9+
function compatible(runtime: IJavaRuntime, jreName: string): boolean {
10+
if (!runtime.version) {
11+
return true;
12+
}
13+
const majorVersion = runtime.version.major;
14+
if (majorVersion === 8) {
15+
return jreName === 'JavaSE-1.8';
16+
}
17+
const versionStrings = /[0-9]+/g.exec(jreName);
18+
if (versionStrings && versionStrings.length > 0) {
19+
return majorVersion >= parseInt(versionStrings[0]);
20+
}
21+
return false;
22+
}
23+
724
export async function initialize(context: vscode.ExtensionContext): Promise<void> {
8-
context.subscriptions.push(vscode.commands.registerCommand('java.runtimes.add', async () => {
25+
context.subscriptions.push(vscode.commands.registerCommand(Commands.ADD_JAVA_RUNTIME, async () => {
26+
const lastSelectedDirectory: vscode.Uri | undefined = context.workspaceState.get('java.runtimes.lastSelectedDirectory');
927
const directory = await vscode.window.showOpenDialog({
1028
canSelectFiles: false,
1129
canSelectFolders: true,
1230
canSelectMany: false,
1331
title: 'Select JDK Directory',
32+
defaultUri: lastSelectedDirectory,
1433
});
1534
if (directory) {
16-
const runtime = await getRuntime(directory[0].fsPath);
35+
context.workspaceState.update('java.runtimes.lastSelectedDirectory', vscode.Uri.file(path.dirname(directory[0].fsPath)));
36+
const runtime = await getRuntime(directory[0].fsPath, {withVersion: true});
1737
if (runtime) {
1838
const config = vscode.workspace.getConfiguration('java.configuration').get('runtimes');
1939
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(), {
40+
const candidates = getSupportedJreNames().filter(name => !config.some(r => r.name === name) && compatible(runtime, name));
41+
if (candidates.length > 0) {
42+
const name = await vscode.window.showQuickPick(candidates, {
2443
title: 'Select Java Runtime',
2544
});
2645
if (name) {
2746
config.push({
2847
name: name,
2948
path: directory[0].fsPath,
3049
});
50+
vscode.workspace.getConfiguration('java.configuration').update('runtimes', config, vscode.ConfigurationTarget.Global);
51+
vscode.window.showInformationMessage(`JDK Directory ${directory[0].fsPath} added`);
3152
}
32-
vscode.workspace.getConfiguration('java.configuration').update('runtimes', config, vscode.ConfigurationTarget.Global);
33-
vscode.window.showInformationMessage(`JDK Directory ${directory[0].fsPath} added`);
53+
} else {
54+
vscode.window.showErrorMessage('No compatible environment available');
3455
}
3556
}
3657
} else {

test/lightweight-mode-suite/extension.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ suite('Java Language Extension - LightWeight', () => {
3030
Commands.FILESEXPLORER_ONPASTE,
3131
Commands.CHANGE_JAVA_SEARCH_SCOPE,
3232
Commands.OPEN_JAVA_DASHBOARD,
33-
"java.runtimes.add"
33+
Commands.ADD_JAVA_RUNTIME
3434
].sort();
3535
const foundJavaCommands = commands.filter((value) => {
3636
return JAVA_COMMANDS.indexOf(value)>=0 || value.startsWith('java.');

test/standard-mode-suite/extension.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ suite('Java Language Extension - Standard', () => {
129129
Commands.RESOLVE_PASTED_TEXT,
130130
Commands.CHANGE_JAVA_SEARCH_SCOPE,
131131
Commands.OPEN_JAVA_DASHBOARD,
132-
"java.runtimes.add"
132+
Commands.ADD_JAVA_RUNTIME
133133
].sort();
134134
const foundJavaCommands = commands.filter((value) => {
135135
return JAVA_COMMANDS.indexOf(value)>=0 || value.startsWith('java.');

0 commit comments

Comments
 (0)