Skip to content
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

Update the method for determining kernel language #354

Merged
merged 3 commits into from
Aug 14, 2024

Conversation

shreve
Copy link
Contributor

@shreve shreve commented Aug 5, 2024

The previous algorithm for getNotebookType() was based on getting the kernel spec out of the current notebook. This is a problem when format is triggered by, for example, jupyterlab-autosave-on-focus-change, immediately after notebook creation. In that case, the language is determined to be null and errors arise.

Now the onSave() handler waits for the sessionContext to be ready, then getNotebookType() gets the same kernel spec info from the session rather than the loaded notebook.

Copy link

github-actions bot commented Aug 5, 2024

Binder 👈 Launch a Binder on branch shreve/jupyterlab_code_formatter/settings-fix

src/formatter.ts Outdated Show resolved Hide resolved
@shreve shreve marked this pull request as draft August 7, 2024 16:14
@shreve shreve changed the title Don't format if the notebook type is undetermined Update the method for determining kernel language Aug 7, 2024
@shreve
Copy link
Contributor Author

shreve commented Aug 7, 2024

I was pursuing some additional changes around languages for both the notebook and editor formatters, but realized it was a lot to contribute without even asking.

@krassowski @ryantam626 Would you welcome larger code changes? In particular, I added some type definitions, extracted some repeated code, and replaced the hard-coded MIME-type map with the CodeMirror MIME service.

@shreve shreve marked this pull request as ready for review August 7, 2024 23:24
src/formatter.ts Outdated Show resolved Hide resolved
@@ -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!

The previous algorithm for `getNotebookType()` was based on getting the
kernel spec out of the current notebook. This is a problem when format
is triggered by, for example, `jupyterlab-autosave-on-focus-change`,
immediately after notebook creation. In that case, the language is
determined to be `null` and errors arise.

Now the `onSave()` handler waits for the `sessionContext` to be ready,
then `getNotebookType()` gets the same kernel spec info from the session
rather than the loaded notebook.
The `!` operator is a type assertion that the attribute is not null, but
does nothing at runtime to ensure this is the case. I haven't yet found
reproduction steps for this failure, but I encountered a case where
accessing `model!.sharedModel` threw an error. This commit adds optional
chaining operators, which do provide protection at runtime.
src/formatter.ts Outdated
@@ -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.

`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.
@krassowski krassowski added the bug Something isn't working label Aug 14, 2024
Copy link
Contributor

@krassowski krassowski left a comment

Choose a reason for hiding this comment

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

LGTM, thank you @shreve!

@krassowski krassowski linked an issue Aug 14, 2024 that may be closed by this pull request
@krassowski krassowski merged commit 1323459 into jupyterlab-contrib:master Aug 14, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to find default formatters to use
2 participants