From 1d29e946e3c3d8e2b3016ef665082b12dbd77cd2 Mon Sep 17 00:00:00 2001 From: Akhilesh Singh Date: Tue, 30 Jun 2026 12:01:41 +0530 Subject: [PATCH] JAVAVSCODE-863: Notebook project-context selection does not appear in the current-project-context text box. --- .../nbcode/java/notebook/NotebookConfigs.java | 29 ++++++++++++++----- .../nbcode/java/project/ProjectContext.java | 2 +- vscode/src/commands/notebook.ts | 23 +++++++++++---- 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/nbcode/notebooks/src/org/netbeans/modules/nbcode/java/notebook/NotebookConfigs.java b/nbcode/notebooks/src/org/netbeans/modules/nbcode/java/notebook/NotebookConfigs.java index 54db1b89..1e95efec 100644 --- a/nbcode/notebooks/src/org/netbeans/modules/nbcode/java/notebook/NotebookConfigs.java +++ b/nbcode/notebooks/src/org/netbeans/modules/nbcode/java/notebook/NotebookConfigs.java @@ -144,28 +144,28 @@ public void notebookConfigsChangeListener(JsonObject settings) { return; } - JsonElement classPathConfig = settings.get(CONFIG_CLASSPATH); + JsonElement classPathConfig = getConfig(settings, CONFIG_CLASSPATH); if (classPathConfig != null && classPathConfig.isJsonArray()) { classPath = String.join(File.pathSeparator, classPathConfig.getAsJsonArray().asList().stream().map((elem) -> elem.getAsString()).toList()); } else { classPath = null; } - JsonElement modulePathConfig = settings.get(CONFIG_MODULEPATH); + JsonElement modulePathConfig = getConfig(settings, CONFIG_MODULEPATH); if (modulePathConfig != null && modulePathConfig.isJsonArray()) { modulePath = String.join(File.pathSeparator, modulePathConfig.getAsJsonArray().asList().stream().map((elem) -> elem.getAsString()).toList()); } else { modulePath = null; } - JsonElement addModulesConfig = settings.get(CONFIG_ADDMODULES); + JsonElement addModulesConfig = getConfig(settings, CONFIG_ADDMODULES); if (addModulesConfig != null && addModulesConfig.isJsonArray()) { addModules = String.join(",", addModulesConfig.getAsJsonArray().asList().stream().map((elem) -> elem.getAsString()).toList()); } else { addModules = null; } - JsonElement enablePreviewConfig = settings.get(CONFIG_ENABLE_PREVIEW); + JsonElement enablePreviewConfig = getConfig(settings, CONFIG_ENABLE_PREVIEW); if (enablePreviewConfig != null && enablePreviewConfig.isJsonPrimitive()) { JsonPrimitive primitive = enablePreviewConfig.getAsJsonPrimitive(); enablePreview = primitive.isBoolean() && primitive.getAsBoolean(); @@ -173,25 +173,40 @@ public void notebookConfigsChangeListener(JsonObject settings) { enablePreview = false; } - JsonElement implicitImportsConfig = settings.get(CONFIG_IMPLICIT_IMPORTS); + JsonElement implicitImportsConfig = getConfig(settings, CONFIG_IMPLICIT_IMPORTS); if (implicitImportsConfig != null && implicitImportsConfig.isJsonArray()) { implicitImports = implicitImportsConfig.getAsJsonArray().asList().stream().map((elem) -> elem.getAsString()).toList(); } else { implicitImports = null; } - JsonElement notebookProjectMappingConfig = settings.get(CONFIG_PROJECTS_MAPPING); + JsonElement notebookProjectMappingConfig = getConfig(settings, CONFIG_PROJECTS_MAPPING); if (notebookProjectMappingConfig != null && notebookProjectMappingConfig.isJsonObject()) { notebookProjectMapping = notebookProjectMappingConfig.getAsJsonObject(); } else { notebookProjectMapping = new JsonObject(); } - JsonElement notebookVmOptionsConfig = settings.get(CONFIG_VM_OPTIONS); + JsonElement notebookVmOptionsConfig = getConfig(settings, CONFIG_VM_OPTIONS); if (notebookVmOptionsConfig != null && notebookVmOptionsConfig.isJsonArray()) { notebookVmOptions = notebookVmOptionsConfig.getAsJsonArray().asList().stream().map(el -> el.getAsString()).toList(); } else { notebookVmOptions = Collections.emptyList(); } } + + private static JsonElement getConfig(JsonObject settings, String key) { + if (!key.contains(".")) { + return settings.get(key); + } + + JsonElement current = settings; + for (String part : key.split("\\.")) { + if (current == null || !current.isJsonObject()) { + return null; + } + current = current.getAsJsonObject().get(part); + } + return current; + } } diff --git a/nbcode/notebooks/src/org/netbeans/modules/nbcode/java/project/ProjectContext.java b/nbcode/notebooks/src/org/netbeans/modules/nbcode/java/project/ProjectContext.java index 5dbb70a6..931471b4 100644 --- a/nbcode/notebooks/src/org/netbeans/modules/nbcode/java/project/ProjectContext.java +++ b/nbcode/notebooks/src/org/netbeans/modules/nbcode/java/project/ProjectContext.java @@ -105,7 +105,7 @@ private static CompletableFuture> selectFromMultipleProjects(Proje items.add(item); } String placeholder = defaultPrjSelected != null ? Bundle.LBL_CurrentProjectContext(defaultPrjSelected.getName()) - : items.isEmpty() ? Bundle.MSG_NoProjectFound() : Bundle.MSG_NoProjectFound(); + : items.isEmpty() ? Bundle.MSG_NoProjectFound() : Bundle.MSG_NoProjectContextFound(); ShowQuickPickParams params = new ShowQuickPickParams(title, placeholder, false, items); return client.showQuickPick(params).thenApply(selected -> { diff --git a/vscode/src/commands/notebook.ts b/vscode/src/commands/notebook.ts index f57fe45c..55a5252c 100644 --- a/vscode/src/commands/notebook.ts +++ b/vscode/src/commands/notebook.ts @@ -155,10 +155,21 @@ const notebookChangeProjectContextHandler = async (ctx: INotebookToolbar) => { if (!res) { return; } - const oldValue = getConfigurationValue(configKeys.notebookProjectMapping, {}); - updateConfigurationValue(configKeys.notebookProjectMapping, - { ...oldValue, [uri.fsPath]: res }, - ConfigurationTarget.Workspace); + const oldValue = getConfigurationValue>(configKeys.notebookProjectMapping, {}); + const newValue = { ...oldValue, [uri.fsPath]: res }; + + if (oldValue[uri.fsPath] !== newValue[uri.fsPath]) { + updateConfigurationValue(configKeys.notebookProjectMapping, newValue, ConfigurationTarget.Workspace); + + const yes = l10n.value("jdk.extension.cache.label.confirmation.yes") + const cancel = l10n.value("jdk.extension.cache.label.confirmation.cancel") + const confirmation = await window.showWarningMessage("Project context changed. Do you want to restart the notebook kernel?", + yes, cancel); + + if (confirmation === yes) { + await restartKernel(ctx, true); + } + } } else { throw l10n.value("jdk.extension.error_msg.doesntSupportNotebookCellExecution", { client: client?.name }); } @@ -168,13 +179,13 @@ const notebookChangeProjectContextHandler = async (ctx: INotebookToolbar) => { } } -const restartKernel = async (ctx: INotebookToolbar) => { +const restartKernel = async (ctx: INotebookToolbar, skipConfirmation = false) => { try { const uri: Uri = ctx.notebookEditor.notebookUri; const yes = l10n.value("jdk.extension.cache.label.confirmation.yes") const cancel = l10n.value("jdk.extension.cache.label.confirmation.cancel") - const confirmation = await window.showWarningMessage(l10n.value("jdk.notebook.restart.kernel.msg.consent"), + const confirmation = skipConfirmation ? yes : await window.showWarningMessage(l10n.value("jdk.notebook.restart.kernel.msg.consent"), yes, cancel); if (confirmation === yes) {