in src/java-runtime/index.ts [39:155]
async function initializeJavaRuntimeView(context: vscode.ExtensionContext, webviewPanel: vscode.WebviewPanel, onDisposeCallback: () => void) {
webviewPanel.iconPath = {
light: vscode.Uri.file(path.join(context.extensionPath, "caption.light.svg")),
dark: vscode.Uri.file(path.join(context.extensionPath, "caption.dark.svg"))
};
webviewPanel.webview.html = getHtmlForWebview(context.asAbsolutePath("./out/assets/java-runtime/index.js"));
context.subscriptions.push(webviewPanel.onDidDispose(onDisposeCallback));
context.subscriptions.push(webviewPanel.webview.onDidReceiveMessage(async (e) => {
switch (e.command) {
case "onWillListRuntimes": {
findJavaRuntimeEntries().then(data => {
showJavaRuntimeEntries(data);
});
break;
}
case "updateJavaHome": {
const { javaHome } = e;
await vscode.workspace.getConfiguration("java").update("home", javaHome, vscode.ConfigurationTarget.Global);
break;
}
case "updateRuntimePath": {
const { sourceLevel, runtimePath } = e;
const runtimes = vscode.workspace.getConfiguration("java").get<any[]>("configuration.runtimes") || [];
const target = runtimes.find(r => r.name === sourceLevel);
if (target) {
target.path = runtimePath;
} else {
runtimes.push({
name: sourceLevel,
path: runtimePath
});
}
await vscode.workspace.getConfiguration("java").update("configuration.runtimes", runtimes, vscode.ConfigurationTarget.Global);
findJavaRuntimeEntries().then(data => {
showJavaRuntimeEntries(data);
});
break;
}
case "setDefaultRuntime": {
const { runtimePath, majorVersion } = e;
const sourceLevel = sourceLevelDisplayName(majorVersion);
const runtimes = vscode.workspace.getConfiguration("java").get<any[]>("configuration.runtimes") || [];
for (const r of runtimes) {
delete r.default;
}
let targetRuntime = runtimes.find(r => r.path === runtimePath);
if (targetRuntime) {
targetRuntime.default = true;
} else {
targetRuntime = runtimes.find(r => r.name === sourceLevel);
if (targetRuntime) {
targetRuntime.path = runtimePath;
targetRuntime.default = true;
} else {
runtimes.push({
name: sourceLevel,
path: runtimePath,
default: true
});
}
}
await vscode.workspace.getConfiguration("java").update("configuration.runtimes", runtimes, vscode.ConfigurationTarget.Global);
findJavaRuntimeEntries().then(data => {
showJavaRuntimeEntries(data);
});
break;
}
case "openBuildScript": {
const { scriptFile, rootUri } = e;
const rootPath = vscode.Uri.parse(rootUri).fsPath;
const fullPath = path.join(rootPath, scriptFile);
vscode.commands.executeCommand("vscode.open", vscode.Uri.file(fullPath));
break;
}
case "onWillBrowseForJDK": {
const javaHomeUri: vscode.Uri[] | undefined = await vscode.window.showOpenDialog({
canSelectFiles: false,
canSelectMany: false,
canSelectFolders: true,
title: "Specify Java runtime to launch Language Server"
});
if (javaHomeUri) {
const javaHome = javaHomeUri[0].fsPath;
if (await getRuntime(javaHome)) {
await vscode.workspace.getConfiguration("java").update("jdt.ls.java.home", javaHome, vscode.ConfigurationTarget.Global);
} else {
await vscode.window.showWarningMessage(`${javaHome} is not a valid Java runtime home directory.`);
}
}
break;
}
default:
break;
}
}));
function showJavaRuntimeEntries(args: any) {
webviewPanel.webview.postMessage({
command: "showJavaRuntimeEntries",
args: args,
});
}
// refresh webview with latest source levels when classpath (project info) changes
const javaExt = vscode.extensions.getExtension("redhat.java");
if (javaExt?.isActive && javaExt?.exports?.onDidClasspathUpdate) {
const onDidClasspathUpdate: vscode.Event<vscode.Uri> = javaExt.exports.onDidClasspathUpdate;
const listener = onDidClasspathUpdate((_e: vscode.Uri) => {
findJavaRuntimeEntries().then(data => {
showJavaRuntimeEntries(data);
});
});
context.subscriptions.push(webviewPanel.onDidDispose(() => listener.dispose()));
}
}