Skip to content

Commit e0e396a

Browse files
committed
Address review feedback on the LSPSystem
1 parent a55ac8a commit e0e396a

File tree

4 files changed

+56
-67
lines changed

4 files changed

+56
-67
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/red_knot/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "red_knot"
3-
version = "0.5.5"
3+
version = "0.0.0"
44
edition.workspace = true
55
rust-version.workspace = true
66
homepage.workspace = true

crates/red_knot_server/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ pub(crate) const DIAGNOSTIC_NAME: &str = "Red Knot";
2121
pub(crate) type Result<T> = anyhow::Result<T>;
2222

2323
pub(crate) fn version() -> &'static str {
24-
ruff_linter::VERSION
24+
env!("CARGO_PKG_VERSION")
2525
}

crates/red_knot_server/src/system.rs

Lines changed: 53 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -68,62 +68,48 @@ impl LSPSystem {
6868
self.index.as_ref().unwrap()
6969
}
7070

71-
fn make_document_ref(&self, url: Url) -> Result<DocumentQuery> {
71+
fn make_document_ref(&self, url: Url) -> Option<DocumentQuery> {
7272
let index = self.index();
7373
let key = index.key_from_url(url);
74-
index.make_document_ref(key).ok_or_else(|| {
75-
std::io::Error::new(
76-
std::io::ErrorKind::NotFound,
77-
"Document not found in the index",
78-
)
79-
})
74+
index.make_document_ref(key)
8075
}
8176

82-
fn system_path_to_document_ref(&self, path: &SystemPath) -> Result<DocumentQuery> {
77+
fn system_path_to_document_ref(&self, path: &SystemPath) -> Result<Option<DocumentQuery>> {
8378
let url = Url::from_file_path(path.as_std_path()).map_err(|()| {
8479
std::io::Error::new(
8580
std::io::ErrorKind::InvalidInput,
8681
format!("Failed to convert system path to URL: {path:?}"),
8782
)
8883
})?;
89-
self.make_document_ref(url)
84+
Ok(self.make_document_ref(url))
9085
}
9186

9287
fn system_virtual_path_to_document_ref(
9388
&self,
9489
path: &SystemVirtualPath,
95-
) -> Result<DocumentQuery> {
90+
) -> Result<Option<DocumentQuery>> {
9691
let url = Url::parse(path.as_str()).map_err(|_| {
9792
std::io::Error::new(
9893
std::io::ErrorKind::InvalidInput,
9994
format!("Failed to convert virtual path to URL: {path:?}"),
10095
)
10196
})?;
102-
self.make_document_ref(url)
97+
Ok(self.make_document_ref(url))
10398
}
10499
}
105100

106101
impl System for LSPSystem {
107102
fn path_metadata(&self, path: &SystemPath) -> Result<Metadata> {
108-
let document = self.system_path_to_document_ref(path);
109-
110-
// First, we need to check if the document is opened in the editor. If it is, we need to
111-
// use the document's version as the file revision. Otherwise, fall back to the OS system.
112-
match document {
113-
Ok(document) => {
114-
// The file revision is just an opaque number which doesn't have any significant
115-
// meaning other than that the file has changed if the revisions are different.
116-
#[allow(clippy::cast_sign_loss)]
117-
Ok(Metadata::new(
118-
FileRevision::new(document.version() as u128),
119-
None,
120-
FileType::File,
121-
))
122-
}
123-
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
124-
self.os_system.path_metadata(path)
125-
}
126-
Err(err) => Err(err),
103+
let document = self.system_path_to_document_ref(path)?;
104+
105+
if let Some(document) = document {
106+
Ok(Metadata::new(
107+
document_revision(&document),
108+
None,
109+
FileType::File,
110+
))
111+
} else {
112+
self.os_system.path_metadata(path)
127113
}
128114
}
129115

@@ -132,57 +118,44 @@ impl System for LSPSystem {
132118
}
133119

134120
fn read_to_string(&self, path: &SystemPath) -> Result<String> {
135-
let document = self.system_path_to_document_ref(path);
121+
let document = self.system_path_to_document_ref(path)?;
136122

137123
match document {
138-
Ok(document) => {
139-
if let DocumentQuery::Text { document, .. } = &document {
140-
Ok(document.contents().to_string())
141-
} else {
142-
Err(not_a_text_document(path))
143-
}
144-
}
145-
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
146-
self.os_system.read_to_string(path)
147-
}
148-
Err(err) => Err(err),
124+
Some(DocumentQuery::Text { document, .. }) => Ok(document.contents().to_string()),
125+
Some(_) => Err(not_a_text_document(path)),
126+
None => self.os_system.read_to_string(path),
149127
}
150128
}
151129

152130
fn read_to_notebook(&self, path: &SystemPath) -> std::result::Result<Notebook, NotebookError> {
153-
let document = self.system_path_to_document_ref(path);
131+
let document = self.system_path_to_document_ref(path)?;
154132

155133
match document {
156-
Ok(document) => {
157-
if let DocumentQuery::Notebook { notebook, .. } = &document {
158-
Ok(notebook.make_ruff_notebook())
159-
} else {
160-
Err(not_a_notebook(path))
161-
}
134+
Some(DocumentQuery::Text { document, .. }) => {
135+
Notebook::from_source_code(document.contents())
162136
}
163-
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
164-
self.os_system.read_to_notebook(path)
165-
}
166-
Err(err) => Err(NotebookError::from(err)),
137+
Some(DocumentQuery::Notebook { notebook, .. }) => Ok(notebook.make_ruff_notebook()),
138+
None => self.os_system.read_to_notebook(path),
167139
}
168140
}
169141

170142
fn virtual_path_metadata(&self, path: &SystemVirtualPath) -> Result<Metadata> {
171143
// Virtual paths only exists in the LSP system, so we don't need to check the OS system.
172-
let document = self.system_virtual_path_to_document_ref(path)?;
144+
let document = self
145+
.system_virtual_path_to_document_ref(path)?
146+
.ok_or_else(|| virtual_path_not_found(path))?;
173147

174-
// The file revision is just an opaque number which doesn't have any significant
175-
// meaning other than that the file has changed if the revisions are different.
176-
#[allow(clippy::cast_sign_loss)]
177148
Ok(Metadata::new(
178-
FileRevision::new(document.version() as u128),
149+
document_revision(&document),
179150
None,
180151
FileType::File,
181152
))
182153
}
183154

184155
fn read_virtual_path_to_string(&self, path: &SystemVirtualPath) -> Result<String> {
185-
let document = self.system_virtual_path_to_document_ref(path)?;
156+
let document = self
157+
.system_virtual_path_to_document_ref(path)?
158+
.ok_or_else(|| virtual_path_not_found(path))?;
186159

187160
if let DocumentQuery::Text { document, .. } = &document {
188161
Ok(document.contents().to_string())
@@ -195,12 +168,13 @@ impl System for LSPSystem {
195168
&self,
196169
path: &SystemVirtualPath,
197170
) -> std::result::Result<Notebook, NotebookError> {
198-
let document = self.system_virtual_path_to_document_ref(path)?;
171+
let document = self
172+
.system_virtual_path_to_document_ref(path)?
173+
.ok_or_else(|| virtual_path_not_found(path))?;
199174

200-
if let DocumentQuery::Notebook { notebook, .. } = &document {
201-
Ok(notebook.make_ruff_notebook())
202-
} else {
203-
Err(not_a_notebook(path))
175+
match document {
176+
DocumentQuery::Text { document, .. } => Notebook::from_source_code(document.contents()),
177+
DocumentQuery::Notebook { notebook, .. } => Ok(notebook.make_ruff_notebook()),
204178
}
205179
}
206180

@@ -241,3 +215,18 @@ fn not_a_notebook(path: impl Display) -> NotebookError {
241215
format!("Input is not a notebook: {path}"),
242216
))
243217
}
218+
219+
fn virtual_path_not_found(path: impl Display) -> std::io::Error {
220+
std::io::Error::new(
221+
std::io::ErrorKind::NotFound,
222+
format!("Virtual path does not exist: {path}"),
223+
)
224+
}
225+
226+
/// Helper function to get the [`FileRevision`] of the given document.
227+
fn document_revision(document: &DocumentQuery) -> FileRevision {
228+
// The file revision is just an opaque number which doesn't have any significant meaning other
229+
// than that the file has changed if the revisions are different.
230+
#[allow(clippy::cast_sign_loss)]
231+
FileRevision::new(document.version() as u128)
232+
}

0 commit comments

Comments
 (0)