From c2aad4cc6748559b791fda885a6aac5a3c398763 Mon Sep 17 00:00:00 2001 From: Snjezana Peco Date: Sun, 28 Sep 2025 18:01:19 +0200 Subject: [PATCH] Enable formatting java in jupyter notebook --- src/commands.ts | 5 +++++ src/extension.ts | 22 +++++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/commands.ts b/src/commands.ts index 3466633f78..db05507f22 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -366,6 +366,11 @@ export namespace Commands { */ export const SHOW_EXTEND_OUTLINE = 'java.action.showExtendedOutline'; + /** + * Get the content of the currently visible editor. + */ + export const GET_VISIBLE_EDITOR_CONTENT = '_java.getVisibleEditorContent'; + } /** diff --git a/src/extension.ts b/src/extension.ts index 77ad9031b3..7a852563c0 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -202,7 +202,8 @@ export async function activate(context: ExtensionContext): Promise documentSelector: [ { scheme: 'file', language: 'java' }, { scheme: 'jdt', language: 'java' }, - { scheme: 'untitled', language: 'java' } + { scheme: 'untitled', language: 'java' }, + { scheme: 'vscode-notebook-cell', language: 'java' } ], synchronize: { configurationSection: ['java', 'editor.insertSpaces', 'editor.tabSize', "files.associations"], @@ -233,6 +234,11 @@ export async function activate(context: ExtensionContext): Promise advancedUpgradeGradleSupport: true, executeClientCommandSupport: true, snippetEditSupport: true, + nonStandardJavaFormatting : { + schemes: ["vscode-notebook-cell"], + extensions: ["jsh", "jshell", "ipynb"], + getContentCallback: Commands.GET_VISIBLE_EDITOR_CONTENT, + } }, triggerFiles, }, @@ -511,6 +517,20 @@ export async function activate(context: ExtensionContext): Promise context.subscriptions.push(onConfigurationChange(workspacePath, context)); + context.subscriptions.push(commands.registerCommand(Commands.GET_VISIBLE_EDITOR_CONTENT, (uri: string) => { + for (const editor of window.visibleTextEditors) { + if (editor.document.uri.toString() === uri) { + return editor.document.getText(); + } + } + const editor = window.activeTextEditor; + if (editor) { + return editor.document.getText(); + } else { + return null; + } + })); + registerRestartJavaLanguageServerCommand(context); /**