Skip to content

Conversation

@MichaReiser
Copy link
Member

@MichaReiser MichaReiser commented Oct 27, 2025

Summary

While prototyping notebook support in ty's LSP, I realized that I'm having a hard time understanding DocumentKey, DocumentQuery, Document (which is referred to as controller?). I then decided to take a closer look at those types and ended with a few refactors that I think are worthwhile, getting in independently of notebook support.

  • Make AnySystemPath::key_from_url non-failable (and in turn, key_from_url) by using a virtual path for file URLs that don't represent a valid path.
  • Make snapshot_document return a Result that Errs when the document wasn't found (instead of storing the document as Result). This was introduced in [ty] Make sure to always respond to client requests #19277 but I don't see how it is necessary to ensure we always send a response. The fix added in [ty] Make sure to always respond to client requests #19277, which sends a response if the URL isn't valid, can also be applied when the document wasn't found.
  • Delete DocumentQuery: ty uses salsa to access the file content (e.g. source_text) which then reads the latest document version using the LSPSystem. That eliminates the need to snapshot most document fields. The only exception to this is the notebook metadata which we now expose directly in DocumentSnapshot. I added new document methods that return a &Document for the LSPSystem.
  • I reduced DocumentKey to two variants roughly resembling AnySystemPath. Its purpose is to uniquely identify a document within Index. We could probably use AnySystemPath for that for now, but I wanted a distinct type to make it clear that a cell doesn't map to a ty-path (there's no corresponding file). Instead, cells should use the notebook file.
  • I introduced a new DocumentHandle type, which is a read-only handle to a document in Index. It takes DocumentKey's place in notification handlers because it now exposes the to_file_path and url methods that were previously on DocumentKey. The motivation for the new type were:
    • I wanted DocumentKey to only be used to identify a document within Index, nothing more
    • I wanted a more object-oriented API for document, so that we can add document.close and document.update_text_document to it (rather than having to deal with keys everywhere).
  • Most Session operations now take &Url as an argument, hiding DocumentKey.

My plan for notebooks is to store the cells as regular text documents. This is different from Ruff where the notebook cells are stored as part of the Notebook. We'll see how well this goes but my thinking is that a model closer to how notebooks are handled within VS Code/the LSP specification, the less likely it is that we run into weird edge cases (e.g. where the notebook gets removed before the cells and we then can't find the cell anymore).

Test plan

Tried different LSP operations

@MichaReiser MichaReiser added the server Related to the LSP server label Oct 27, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Oct 27, 2025

Diagnostic diff on typing conformance tests

No changes detected when running ty on typing conformance tests ✅

Comment on lines +856 to +860
impl Borrow<SystemVirtualPath> for SystemVirtualPathBuf {
fn borrow(&self) -> &SystemVirtualPath {
self.as_path()
}
}
Copy link
Member Author

Choose a reason for hiding this comment

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

This is so that we can use SystemVirtualPath as key when calling dict.get

@MichaReiser MichaReiser force-pushed the micha/lsp-notebooks-phase1 branch from 7103813 to 6273b56 Compare October 27, 2025 12:31
@github-actions
Copy link
Contributor

github-actions bot commented Oct 27, 2025

mypy_primer results

No ecosystem changes detected ✅
No memory usage changes detected ✅

@github-actions
Copy link
Contributor

github-actions bot commented Oct 27, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@MichaReiser MichaReiser marked this pull request as ready for review October 27, 2025 12:57
@MichaReiser MichaReiser changed the title [ty] Smaller refactors to server in prep for notebook support [ty] Smaller refactors to server API in prep for notebook support Oct 27, 2025
@MichaReiser MichaReiser added the internal An internal refactor or improvement label Oct 27, 2025
@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Oct 27, 2025
Copy link
Member

@dcreager dcreager left a comment

Choose a reason for hiding this comment

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

This looks good to me, and lets me learn more about the LSP internals. Though it probably deserves at least a cursory look from someone who's already more familiar with the LSP!

params.notebook_document.version,
params.notebook_document.cells,
params.notebook_document.metadata.unwrap_or_default(),
params.notebook_document.uri,
Copy link
Member

Choose a reason for hiding this comment

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

nit: Can you pull out uri above too to be consistent with the other fields? Or is there an ownership issue?

@MichaReiser MichaReiser merged commit 6337e22 into main Oct 31, 2025
40 checks passed
@MichaReiser MichaReiser deleted the micha/lsp-notebooks-phase1 branch October 31, 2025 20:00
@dhruvmanila
Copy link
Member

My plan for notebooks is to store the cells as regular text documents. This is different from Ruff where the notebook cells are stored as part of the Notebook. We'll see how well this goes but my thinking is that a model closer to how notebooks are handled within VS Code/the LSP specification, the less likely it is that we run into weird edge cases (e.g. where the notebook gets removed before the cells and we then can't find the cell anymore).

I agree that this is great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

internal An internal refactor or improvement server Related to the LSP server ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants