diff options
Diffstat (limited to 'crates/ra_vfs/src/lib.rs')
-rw-r--r-- | crates/ra_vfs/src/lib.rs | 86 |
1 files changed, 16 insertions, 70 deletions
diff --git a/crates/ra_vfs/src/lib.rs b/crates/ra_vfs/src/lib.rs index 71a3f807d..6b4eb6842 100644 --- a/crates/ra_vfs/src/lib.rs +++ b/crates/ra_vfs/src/lib.rs | |||
@@ -58,10 +58,7 @@ impl std::ops::Deref for Roots { | |||
58 | 58 | ||
59 | impl RootConfig { | 59 | impl RootConfig { |
60 | fn new(root: PathBuf, excluded_dirs: Vec<PathBuf>) -> RootConfig { | 60 | fn new(root: PathBuf, excluded_dirs: Vec<PathBuf>) -> RootConfig { |
61 | RootConfig { | 61 | RootConfig { root, excluded_dirs } |
62 | root, | ||
63 | excluded_dirs, | ||
64 | } | ||
65 | } | 62 | } |
66 | /// Cheks if root contains a path and returns a root-relative path. | 63 | /// Cheks if root contains a path and returns a root-relative path. |
67 | pub(crate) fn contains(&self, path: &Path) -> Option<RelativePathBuf> { | 64 | pub(crate) fn contains(&self, path: &Path) -> Option<RelativePathBuf> { |
@@ -111,9 +108,7 @@ impl Roots { | |||
111 | Roots { roots } | 108 | Roots { roots } |
112 | } | 109 | } |
113 | pub(crate) fn find(&self, path: &Path) -> Option<(VfsRoot, RelativePathBuf)> { | 110 | pub(crate) fn find(&self, path: &Path) -> Option<(VfsRoot, RelativePathBuf)> { |
114 | self.roots | 111 | self.roots.iter().find_map(|(root, data)| data.contains(path).map(|it| (root, it))) |
115 | .iter() | ||
116 | .find_map(|(root, data)| data.contains(path).map(|it| (root, it))) | ||
117 | } | 112 | } |
118 | } | 113 | } |
119 | 114 | ||
@@ -154,21 +149,10 @@ impl Vfs { | |||
154 | 149 | ||
155 | for (root, config) in roots.iter() { | 150 | for (root, config) in roots.iter() { |
156 | root2files.insert(root, Default::default()); | 151 | root2files.insert(root, Default::default()); |
157 | worker | 152 | worker.sender().send(io::Task::AddRoot { root, config: Arc::clone(config) }).unwrap(); |
158 | .sender() | ||
159 | .send(io::Task::AddRoot { | ||
160 | root, | ||
161 | config: Arc::clone(config), | ||
162 | }) | ||
163 | .unwrap(); | ||
164 | } | 153 | } |
165 | let res = Vfs { | 154 | let res = |
166 | roots, | 155 | Vfs { roots, files: Arena::default(), root2files, worker, pending_changes: Vec::new() }; |
167 | files: Arena::default(), | ||
168 | root2files, | ||
169 | worker, | ||
170 | pending_changes: Vec::new(), | ||
171 | }; | ||
172 | let vfs_roots = res.roots.iter().map(|(id, _)| id).collect(); | 156 | let vfs_roots = res.roots.iter().map(|(id, _)| id).collect(); |
173 | (res, vfs_roots) | 157 | (res, vfs_roots) |
174 | } | 158 | } |
@@ -205,12 +189,7 @@ impl Vfs { | |||
205 | let text = fs::read_to_string(path).unwrap_or_default(); | 189 | let text = fs::read_to_string(path).unwrap_or_default(); |
206 | let text = Arc::new(text); | 190 | let text = Arc::new(text); |
207 | let file = self.add_file(root, rel_path.clone(), Arc::clone(&text), false); | 191 | let file = self.add_file(root, rel_path.clone(), Arc::clone(&text), false); |
208 | let change = VfsChange::AddFile { | 192 | let change = VfsChange::AddFile { file, text, root, path: rel_path }; |
209 | file, | ||
210 | text, | ||
211 | root, | ||
212 | path: rel_path, | ||
213 | }; | ||
214 | self.pending_changes.push(change); | 193 | self.pending_changes.push(change); |
215 | Some(file) | 194 | Some(file) |
216 | }; | 195 | }; |
@@ -243,10 +222,7 @@ impl Vfs { | |||
243 | cur_files.push((file, path, text)); | 222 | cur_files.push((file, path, text)); |
244 | } | 223 | } |
245 | 224 | ||
246 | let change = VfsChange::AddRoot { | 225 | let change = VfsChange::AddRoot { root, files: cur_files }; |
247 | root, | ||
248 | files: cur_files, | ||
249 | }; | ||
250 | self.pending_changes.push(change); | 226 | self.pending_changes.push(change); |
251 | } | 227 | } |
252 | TaskResult::AddSingleFile { root, path, text } => { | 228 | TaskResult::AddSingleFile { root, path, text } => { |
@@ -278,12 +254,7 @@ impl Vfs { | |||
278 | ) -> Option<VfsFile> { | 254 | ) -> Option<VfsFile> { |
279 | let text = Arc::new(text); | 255 | let text = Arc::new(text); |
280 | let file = self.add_file(root, path.clone(), text.clone(), is_overlay); | 256 | let file = self.add_file(root, path.clone(), text.clone(), is_overlay); |
281 | self.pending_changes.push(VfsChange::AddFile { | 257 | self.pending_changes.push(VfsChange::AddFile { file, root, path, text }); |
282 | file, | ||
283 | root, | ||
284 | path, | ||
285 | text, | ||
286 | }); | ||
287 | Some(file) | 258 | Some(file) |
288 | } | 259 | } |
289 | 260 | ||
@@ -293,8 +264,7 @@ impl Vfs { | |||
293 | } | 264 | } |
294 | let text = Arc::new(text); | 265 | let text = Arc::new(text); |
295 | self.change_file(file, text.clone(), is_overlay); | 266 | self.change_file(file, text.clone(), is_overlay); |
296 | self.pending_changes | 267 | self.pending_changes.push(VfsChange::ChangeFile { file, text }); |
297 | .push(VfsChange::ChangeFile { file, text }); | ||
298 | } | 268 | } |
299 | 269 | ||
300 | fn do_remove_file( | 270 | fn do_remove_file( |
@@ -308,8 +278,7 @@ impl Vfs { | |||
308 | return; | 278 | return; |
309 | } | 279 | } |
310 | self.remove_file(file); | 280 | self.remove_file(file); |
311 | self.pending_changes | 281 | self.pending_changes.push(VfsChange::RemoveFile { root, path, file }); |
312 | .push(VfsChange::RemoveFile { root, path, file }); | ||
313 | } | 282 | } |
314 | 283 | ||
315 | pub fn add_file_overlay(&mut self, path: &Path, text: String) -> Option<VfsFile> { | 284 | pub fn add_file_overlay(&mut self, path: &Path, text: String) -> Option<VfsFile> { |
@@ -363,12 +332,7 @@ impl Vfs { | |||
363 | text: Arc<String>, | 332 | text: Arc<String>, |
364 | is_overlayed: bool, | 333 | is_overlayed: bool, |
365 | ) -> VfsFile { | 334 | ) -> VfsFile { |
366 | let data = VfsFileData { | 335 | let data = VfsFileData { root, path, text, is_overlayed }; |
367 | root, | ||
368 | path, | ||
369 | text, | ||
370 | is_overlayed, | ||
371 | }; | ||
372 | let file = self.files.alloc(data); | 336 | let file = self.files.alloc(data); |
373 | self.root2files.get_mut(root).unwrap().insert(file); | 337 | self.root2files.get_mut(root).unwrap().insert(file); |
374 | file | 338 | file |
@@ -396,32 +360,14 @@ impl Vfs { | |||
396 | } | 360 | } |
397 | 361 | ||
398 | fn find_file(&self, root: VfsRoot, path: &RelativePath) -> Option<VfsFile> { | 362 | fn find_file(&self, root: VfsRoot, path: &RelativePath) -> Option<VfsFile> { |
399 | self.root2files[root] | 363 | self.root2files[root].iter().map(|&it| it).find(|&file| self.files[file].path == path) |
400 | .iter() | ||
401 | .map(|&it| it) | ||
402 | .find(|&file| self.files[file].path == path) | ||
403 | } | 364 | } |
404 | } | 365 | } |
405 | 366 | ||
406 | #[derive(Debug, Clone)] | 367 | #[derive(Debug, Clone)] |
407 | pub enum VfsChange { | 368 | pub enum VfsChange { |
408 | AddRoot { | 369 | AddRoot { root: VfsRoot, files: Vec<(VfsFile, RelativePathBuf, Arc<String>)> }, |
409 | root: VfsRoot, | 370 | AddFile { root: VfsRoot, file: VfsFile, path: RelativePathBuf, text: Arc<String> }, |
410 | files: Vec<(VfsFile, RelativePathBuf, Arc<String>)>, | 371 | RemoveFile { root: VfsRoot, file: VfsFile, path: RelativePathBuf }, |
411 | }, | 372 | ChangeFile { file: VfsFile, text: Arc<String> }, |
412 | AddFile { | ||
413 | root: VfsRoot, | ||
414 | file: VfsFile, | ||
415 | path: RelativePathBuf, | ||
416 | text: Arc<String>, | ||
417 | }, | ||
418 | RemoveFile { | ||
419 | root: VfsRoot, | ||
420 | file: VfsFile, | ||
421 | path: RelativePathBuf, | ||
422 | }, | ||
423 | ChangeFile { | ||
424 | file: VfsFile, | ||
425 | text: Arc<String>, | ||
426 | }, | ||
427 | } | 373 | } |