Skip to content

Commit c2aad4c

Browse files
committed
Enable formatting java in jupyter notebook
1 parent 86f9253 commit c2aad4c

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/commands.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,11 @@ export namespace Commands {
366366
*/
367367
export const SHOW_EXTEND_OUTLINE = 'java.action.showExtendedOutline';
368368

369+
/**
370+
* Get the content of the currently visible editor.
371+
*/
372+
export const GET_VISIBLE_EDITOR_CONTENT = '_java.getVisibleEditorContent';
373+
369374
}
370375

371376
/**

src/extension.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,8 @@ export async function activate(context: ExtensionContext): Promise<ExtensionAPI>
202202
documentSelector: [
203203
{ scheme: 'file', language: 'java' },
204204
{ scheme: 'jdt', language: 'java' },
205-
{ scheme: 'untitled', language: 'java' }
205+
{ scheme: 'untitled', language: 'java' },
206+
{ scheme: 'vscode-notebook-cell', language: 'java' }
206207
],
207208
synchronize: {
208209
configurationSection: ['java', 'editor.insertSpaces', 'editor.tabSize', "files.associations"],
@@ -233,6 +234,11 @@ export async function activate(context: ExtensionContext): Promise<ExtensionAPI>
233234
advancedUpgradeGradleSupport: true,
234235
executeClientCommandSupport: true,
235236
snippetEditSupport: true,
237+
nonStandardJavaFormatting : {
238+
schemes: ["vscode-notebook-cell"],
239+
extensions: ["jsh", "jshell", "ipynb"],
240+
getContentCallback: Commands.GET_VISIBLE_EDITOR_CONTENT,
241+
}
236242
},
237243
triggerFiles,
238244
},
@@ -511,6 +517,20 @@ export async function activate(context: ExtensionContext): Promise<ExtensionAPI>
511517

512518
context.subscriptions.push(onConfigurationChange(workspacePath, context));
513519

520+
context.subscriptions.push(commands.registerCommand(Commands.GET_VISIBLE_EDITOR_CONTENT, (uri: string) => {
521+
for (const editor of window.visibleTextEditors) {
522+
if (editor.document.uri.toString() === uri) {
523+
return editor.document.getText();
524+
}
525+
}
526+
const editor = window.activeTextEditor;
527+
if (editor) {
528+
return editor.document.getText();
529+
} else {
530+
return null;
531+
}
532+
}));
533+
514534
registerRestartJavaLanguageServerCommand(context);
515535

516536
/**

0 commit comments

Comments
 (0)