Skip to content

Commit

Permalink
Fall back to session check if metadata is missing
Browse files Browse the repository at this point in the history
`getNotebookType()` has two ways to determine the language. If the
metadata check fails, it falls through to the current session check.
Previously, if metadata was `null`, the method would return early.
  • Loading branch information
shreve committed Aug 12, 2024
1 parent 92c805d commit afad37b
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions src/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,38 +93,41 @@ export class JupyterlabNotebookCodeFormatter extends JupyterlabCodeFormatter {
}

private getNotebookType(): string | null {
// If there is no current notebook, there is nothing to do
if (!this.notebookTracker.currentWidget) {
return null;
}

// first, check the notebook's metadata for language info
const metadata =
this.notebookTracker.currentWidget.content.model?.sharedModel?.metadata;

if (!metadata) {
return null;
}

// prefer kernelspec language
if (
metadata.kernelspec &&
metadata.kernelspec.language &&
typeof metadata.kernelspec.language === 'string'
) {
return metadata.kernelspec.language.toLowerCase();
}
if (metadata) {
// prefer kernelspec language
if (
metadata.kernelspec &&
metadata.kernelspec.language &&
typeof metadata.kernelspec.language === 'string'
) {
return metadata.kernelspec.language.toLowerCase();
}

// otherwise, check language info code mirror mode
if (metadata.language_info && metadata.language_info.codemirror_mode) {
const mode = metadata.language_info.codemirror_mode;
if (typeof mode === 'string') {
return mode.toLowerCase();
} else if (typeof mode.name === 'string') {
return mode.name.toLowerCase();
// otherwise, check language info code mirror mode
if (
metadata.language_info &&
metadata.language_info.codemirror_mode
) {
const mode = metadata.language_info.codemirror_mode;
if (typeof mode === 'string') {
return mode.toLowerCase();
} else if (typeof mode.name === 'string') {
return mode.name.toLowerCase();
}
}
}

// finally, try to get the language from the current session's kernel spec
const sessionContext = this.notebookTracker.currentWidget?.sessionContext;
// in the absence of metadata, look in the current session's kernel spec
const sessionContext = this.notebookTracker.currentWidget.sessionContext;
const kernelName = sessionContext?.session?.kernel?.name;
if (kernelName) {
const specs = sessionContext.specsManager.specs?.kernelspecs;
Expand Down

0 comments on commit afad37b

Please sign in to comment.