@@ -68,62 +68,48 @@ impl LSPSystem {
68
68
self . index . as_ref ( ) . unwrap ( )
69
69
}
70
70
71
- fn make_document_ref ( & self , url : Url ) -> Result < DocumentQuery > {
71
+ fn make_document_ref ( & self , url : Url ) -> Option < DocumentQuery > {
72
72
let index = self . index ( ) ;
73
73
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)
80
75
}
81
76
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 > > {
83
78
let url = Url :: from_file_path ( path. as_std_path ( ) ) . map_err ( |( ) | {
84
79
std:: io:: Error :: new (
85
80
std:: io:: ErrorKind :: InvalidInput ,
86
81
format ! ( "Failed to convert system path to URL: {path:?}" ) ,
87
82
)
88
83
} ) ?;
89
- self . make_document_ref ( url)
84
+ Ok ( self . make_document_ref ( url) )
90
85
}
91
86
92
87
fn system_virtual_path_to_document_ref (
93
88
& self ,
94
89
path : & SystemVirtualPath ,
95
- ) -> Result < DocumentQuery > {
90
+ ) -> Result < Option < DocumentQuery > > {
96
91
let url = Url :: parse ( path. as_str ( ) ) . map_err ( |_| {
97
92
std:: io:: Error :: new (
98
93
std:: io:: ErrorKind :: InvalidInput ,
99
94
format ! ( "Failed to convert virtual path to URL: {path:?}" ) ,
100
95
)
101
96
} ) ?;
102
- self . make_document_ref ( url)
97
+ Ok ( self . make_document_ref ( url) )
103
98
}
104
99
}
105
100
106
101
impl System for LSPSystem {
107
102
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)
127
113
}
128
114
}
129
115
@@ -132,57 +118,44 @@ impl System for LSPSystem {
132
118
}
133
119
134
120
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) ? ;
136
122
137
123
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) ,
149
127
}
150
128
}
151
129
152
130
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) ? ;
154
132
155
133
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 ( ) )
162
136
}
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) ,
167
139
}
168
140
}
169
141
170
142
fn virtual_path_metadata ( & self , path : & SystemVirtualPath ) -> Result < Metadata > {
171
143
// 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) ) ?;
173
147
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) ]
177
148
Ok ( Metadata :: new (
178
- FileRevision :: new ( document. version ( ) as u128 ) ,
149
+ document_revision ( & document) ,
179
150
None ,
180
151
FileType :: File ,
181
152
) )
182
153
}
183
154
184
155
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) ) ?;
186
159
187
160
if let DocumentQuery :: Text { document, .. } = & document {
188
161
Ok ( document. contents ( ) . to_string ( ) )
@@ -195,12 +168,13 @@ impl System for LSPSystem {
195
168
& self ,
196
169
path : & SystemVirtualPath ,
197
170
) -> 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) ) ?;
199
174
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 ( ) ) ,
204
178
}
205
179
}
206
180
@@ -241,3 +215,18 @@ fn not_a_notebook(path: impl Display) -> NotebookError {
241
215
format ! ( "Input is not a notebook: {path}" ) ,
242
216
) )
243
217
}
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