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

[red-knot] Support untitled files in the server #13044

Merged
merged 2 commits into from
Aug 23, 2024

Conversation

dhruvmanila
Copy link
Member

@dhruvmanila dhruvmanila commented Aug 22, 2024

Summary

This PR adds support for untitled files in the red knot server.

Test Plan

Screen.Recording.2024-08-22.at.17.11.51.mov

@dhruvmanila dhruvmanila added the red-knot Multi-file analysis & type inference label Aug 22, 2024
@dhruvmanila dhruvmanila force-pushed the dhruv/virtual-file branch 3 times, most recently from 7f18134 to b7c8fe2 Compare August 22, 2024 10:59
Comment on lines 74 to 76
let (range, message) = match words.as_slice() {
[_filename, line, column, message] => {
[_, _, line, column, message] | [_, line, column, message] => {
let line = line.parse::<u32>().unwrap_or_default().saturating_sub(1);
Copy link
Member Author

Choose a reason for hiding this comment

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

I think this should be temporary. This is because the virtual path would contain the entire URL (untitled:Untitled-1) which means the message would have an extra :

Comment on lines 266 to 278
pub(crate) fn file(&self, db: &RootDatabase) -> Option<File> {
let path = url_to_system_path(self.document_ref.file_url()).ok()?;
system_path_to_file(db, path).ok()
match url_to_any_system_path(self.document_ref.file_url()).ok()? {
AnySystemPath::System(path) => system_path_to_file(db, path).ok(),
AnySystemPath::SystemVirtual(virtual_path) => db
.files()
.try_virtual_file(&virtual_path)
.map(|virtual_file| virtual_file.file()),
}
}
Copy link
Member Author

Choose a reason for hiding this comment

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

I thought about adding File to the DocumentController but my concern is about notebook cells which also uses TextDocument to represent them. I'm not exactly sure how to represent them. I do want to follow-up on this when adding support for notebooks.

Copy link
Member

Choose a reason for hiding this comment

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

Ah okay :)

Comment on lines +138 to +143
watch::ChangeEvent::DeletedVirtual(path) => {
if let Some(virtual_file) = self.files().try_virtual_file(&path) {
virtual_file.close(self);
}
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Or, should we expose a close_virtual_file on Files instead?

Copy link
Member

Choose a reason for hiding this comment

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

I think this is fine for now

@dhruvmanila dhruvmanila marked this pull request as ready for review August 22, 2024 11:45
Copy link
Contributor

github-actions bot commented Aug 22, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Comment on lines +28 to +30
Ok(AnySystemPath::System(
SystemPathBuf::from_path_buf(url.to_file_path()?).map_err(|_| ())?,
))
Copy link
Member

Choose a reason for hiding this comment

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

What are the cases where url.to_file_path() can fail if the schema is file?

It would be nice if this method wouldn't have to return a Result.

Copy link
Member Author

Choose a reason for hiding this comment

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

From the docs (https://docs.rs/url/2.5.2/url/struct.Url.html#method.to_file_path):

Returns Err if the host is neither empty nor "localhost" (except on Windows, where file: URLs may have a non-local host), or if Path::new_opt() returns None. (That is, if the percent-decoded path contains a NUL byte or, for a Windows path, is not UTF-8.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure what Path::new_opt is but the code doesn't seem to be doing that.

Copy link
Member Author

Choose a reason for hiding this comment

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

Going to merge the PR, will look at this in a follow-up.

@@ -75,7 +84,7 @@ impl ChangeEvent {
ChangeEvent::Created { path, .. }
| ChangeEvent::Changed { path, .. }
| ChangeEvent::Deleted { path, .. } => Some(path),
ChangeEvent::Rescan => None,
_ => None,
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Rename the method to system_path

crates/red_knot_server/src/server/api.rs Show resolved Hide resolved
Comment on lines +46 to +47
let db = session.default_workspace_db_mut();
db.apply_changes(vec![ChangeEvent::ChangedVirtual(virtual_path)], None);
Copy link
Member

Choose a reason for hiding this comment

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

This is more a note to myself. It's unfortunate that we need to go through apply_changes here which requires going from file -> path but this seems necessary when they e.g. open a pyproject.toml (and we support linting it in the future)

Comment on lines 266 to 278
pub(crate) fn file(&self, db: &RootDatabase) -> Option<File> {
let path = url_to_system_path(self.document_ref.file_url()).ok()?;
system_path_to_file(db, path).ok()
match url_to_any_system_path(self.document_ref.file_url()).ok()? {
AnySystemPath::System(path) => system_path_to_file(db, path).ok(),
AnySystemPath::SystemVirtual(virtual_path) => db
.files()
.try_virtual_file(&virtual_path)
.map(|virtual_file| virtual_file.file()),
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Ah okay :)

Comment on lines +138 to +143
watch::ChangeEvent::DeletedVirtual(path) => {
if let Some(virtual_file) = self.files().try_virtual_file(&path) {
virtual_file.close(self);
}
}
Copy link
Member

Choose a reason for hiding this comment

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

I think this is fine for now

Base automatically changed from dhruv/virtual-file to main August 23, 2024 07:04
@dhruvmanila dhruvmanila merged commit cfe25ab into main Aug 23, 2024
20 checks passed
@dhruvmanila dhruvmanila deleted the dhruv/lsp-virtual-file branch August 23, 2024 07:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
red-knot Multi-file analysis & type inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants