Skip to content

Update the method for determining kernel language #354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ export class JupyterlabNotebookCodeFormatter extends JupyterlabCodeFormatter {
return codeCells;
}

private getNotebookType() {
private getNotebookType(): string | null {
if (!this.notebookTracker.currentWidget) {
return null;
}

const metadata =
this.notebookTracker.currentWidget.content.model!.sharedModel.metadata;
this.notebookTracker.currentWidget.content.model?.sharedModel?.metadata;

if (!metadata) {
return null;
Expand All @@ -123,6 +123,16 @@ export class JupyterlabNotebookCodeFormatter extends JupyterlabCodeFormatter {
}
}

// finally, try to get the language from the current session's kernel spec
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it intended that this block will not be reached when metadata are not defined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great question. I see the problem you're asking about. I think I wanted to change as few lines as possible, but I realized that's just not the right way to do this. I've pushed another update. Thanks for all your effort in reviewing this.

const sessionContext = this.notebookTracker.currentWidget?.sessionContext;
const kernelName = sessionContext?.session?.kernel?.name;
if (kernelName) {
const specs = sessionContext.specsManager.specs?.kernelspecs;
if (specs && kernelName in specs) {
return specs[kernelName]!.language;
}
}

return null;
}

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class JupyterLabCodeFormatter
state: DocumentRegistry.SaveState
) {
if (state === 'started' && this.config.formatOnSave) {
await context.sessionContext.ready;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 thanks!

await this.notebookCodeFormatter.formatAllCodeCells(
this.config,
{ saving: true },
Expand Down
Loading