Add notebook cell support for isort#565
Merged
edvilme merged 13 commits intomicrosoft:mainfrom Mar 4, 2026
Merged
Conversation
- Add notebook document sync options for jupyter-notebook and interactive - Add LSP handlers for notebook did_open, did_change, did_save, did_close - Remove notebook cell guard in server-less code action provider - Use uri.toString() instead of fsPath for code action data
- Add missing PEP 8 blank lines between top-level functions in lsp_server.py
- Restore notebook cell guard in server-less mode code action provider
- Revert unrelated package-lock.json peer changes
- Fix prettier formatting (catch {}, empty arrow functions)
Lint newly added cells and clear diagnostics for removed cells, not just cells with text content changes.
The function resolves notebook cell URIs to the notebook's .ipynb path (stripping the cell fragment), not to a .py path.
- Add notebook notification methods to LspSession test client - Add tests for notebookDocument/didOpen, didChange, didSave, didClose - Test cell structure changes (adding new cells with unsorted imports) - Test diagnostics are cleared on notebook close
The mock lsprotocol.types module was missing notebook-related constants and types needed by the module-level NOTEBOOK_SYNC_OPTIONS in lsp_server.py, causing import failures in CI.
Contributor
Author
|
Fixes #303 |
rchiodo
reviewed
Mar 3, 2026
rchiodo
reviewed
Mar 3, 2026
rchiodo
reviewed
Mar 3, 2026
Use cell.kind check to skip markdown cells, matching the pattern used in notebook_did_save.
Consistent with the other .fsPath to .toString() changes in this file — fsPath loses scheme information for non-file URIs.
Add getDocumentPath helper to runner.ts that resolves notebook cell URIs to filesystem paths (mirroring _get_document_path in Python). Remove the isNotebookCell guard in sortImports.ts since the runner can now handle notebook URIs.
The notebook document URI should use the file: scheme (it represents the .ipynb file on disk). Only individual cell URIs use the vscode-notebook-cell: scheme.
rchiodo
previously approved these changes
Mar 3, 2026
Contributor
rchiodo
left a comment
There was a problem hiding this comment.
Approved via Review Center.
Remove is_python, strip_magic_commands, and MAGIC_COMMAND_REGEX from lsp_server.py. The is_python check used ast.parse which rejected valid-but-incomplete Python code, preventing organize imports from working on files being actively edited. Closes microsoft#243 Also removes now-unused ast and re imports. Adds tests for both regular documents and notebook cells with incomplete Python code (missing function body) to verify isort correctly sorts imports without the is_python guard. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
rchiodo
approved these changes
Mar 4, 2026
18 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
With the upgrade to pygls 2, the extension can now leverage LSP 3.17's notebook document sync protocol. This PR adds Jupyter notebook cell support to the isort extension, enabling import sorting diagnostics and code actions for Python cells within notebooks in both server and server-less modes. Closes #303.
Also removes the
is_pythonguard that usedast.parseto validate Python code before running isort. This check rejected valid-but-incomplete Python code, preventing organize imports from working on files being actively edited. Closes #243.Changes
LSP Server (
bundled/tool/lsp_server.py)NotebookDocumentSyncOptionsforjupyter-notebookandinteractivenotebook types with Python cell filteringnotebookDocument/didOpen,didChange,didSave, anddidClosehandlers that run diagnostics on notebook cellsNotebookCellKind.CodechecksdidChangehandler processes text content edits, cell additions (structure.did_open), and cell removals (structure.did_close)is_pythonguard: Removedis_python,strip_magic_commands, andMAGIC_COMMAND_REGEX— isort handles magic commands and syntax errors gracefully on its own, and theast.parsecheck was incorrectly rejecting incomplete code ("no organize imports action available" error on uncompleted code #243)_get_document_pathexample to show.ipynboutput instead of.pyServer-less mode (
src/common/runner.ts,src/common/sortImports.ts)getDocumentPathhelper torunner.tsthat resolvesvscode-notebook-cell://URIs to filesystem paths, mirroring_get_document_pathon the Python sideisNotebookCellguard — server-less mode now fully supports notebook cellsuri.fsPathtouri.toString()so notebook cell URIs round-trip correctly through code action resolutionTests
notify_notebook_did_open/did_change/did_save/did_close) to the LSP test clienttest_notebook.py): 5 tests covering all notebook handlers:test_notebook_did_open— diagnostics published on notebook opentest_notebook_did_change_text_content— diagnostics update on cell edittest_notebook_did_change_structure_add_cell— new cells are lintedtest_notebook_did_save— all cells re-linted on savetest_notebook_did_close— diagnostics cleared on closeis_pythonguardlsprotocolmock intest_get_cwd.pyto fix CI import failures