]> git.lizzy.rs Git - rust.git/blob - crates/ra_vfs/src/lib.rs
review fixes
[rust.git] / crates / ra_vfs / src / lib.rs
1 //! VFS stands for Virtual File System.
2 //!
3 //! When doing analysis, we don't want to do any IO, we want to keep all source
4 //! code in memory. However, the actual source code is stored on disk, so you
5 //! need to get it into the memory in the first place somehow. VFS is the
6 //! component which does this.
7 //!
8 //! It is also responsible for watching the disk for changes, and for merging
9 //! editor state (modified, unsaved files) with disk state.
10 //! TODO: Some LSP clients support watching the disk, so this crate should
11 //! to support custom watcher events (related to https://github.com/rust-analyzer/rust-analyzer/issues/131)
12 //!
13 //! VFS is based on a concept of roots: a set of directories on the file system
14 //! which are watched for changes. Typically, there will be a root for each
15 //! Cargo package.
16 mod io;
17
18 use std::{
19     cmp::Reverse,
20     fmt, fs, mem,
21     path::{Path, PathBuf},
22     sync::Arc,
23     thread,
24 };
25
26 use crossbeam_channel::Receiver;
27 use ra_arena::{impl_arena_id, Arena, RawId};
28 use relative_path::{Component, RelativePath, RelativePathBuf};
29 use rustc_hash::{FxHashMap, FxHashSet};
30
31 pub use crate::io::TaskResult as VfsTask;
32 use io::{Task, TaskResult, WatcherChange, WatcherChangeData, Worker};
33
34 /// `RootFilter` is a predicate that checks if a file can belong to a root. If
35 /// several filters match a file (nested dirs), the most nested one wins.
36 pub(crate) struct RootFilter {
37     root: PathBuf,
38     filter: fn(&Path, &RelativePath) -> bool,
39 }
40
41 impl RootFilter {
42     fn new(root: PathBuf) -> RootFilter {
43         RootFilter {
44             root,
45             filter: default_filter,
46         }
47     }
48     /// Check if this root can contain `path`. NB: even if this returns
49     /// true, the `path` might actually be conained in some nested root.
50     pub(crate) fn can_contain(&self, path: &Path) -> Option<RelativePathBuf> {
51         let rel_path = path.strip_prefix(&self.root).ok()?;
52         let rel_path = RelativePathBuf::from_path(rel_path).ok()?;
53         if !(self.filter)(path, rel_path.as_relative_path()) {
54             return None;
55         }
56         Some(rel_path)
57     }
58 }
59
60 pub(crate) fn default_filter(path: &Path, rel_path: &RelativePath) -> bool {
61     if path.is_dir() {
62         for (i, c) in rel_path.components().enumerate() {
63             if let Component::Normal(c) = c {
64                 // TODO hardcoded for now
65                 if (i == 0 && c == "target") || c == ".git" || c == "node_modules" {
66                     return false;
67                 }
68             }
69         }
70         true
71     } else {
72         rel_path.extension() == Some("rs")
73     }
74 }
75
76 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
77 pub struct VfsRoot(pub RawId);
78 impl_arena_id!(VfsRoot);
79
80 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
81 pub struct VfsFile(pub RawId);
82 impl_arena_id!(VfsFile);
83
84 struct VfsFileData {
85     root: VfsRoot,
86     path: RelativePathBuf,
87     is_overlayed: bool,
88     text: Arc<String>,
89 }
90
91 pub struct Vfs {
92     roots: Arena<VfsRoot, Arc<RootFilter>>,
93     files: Arena<VfsFile, VfsFileData>,
94     root2files: FxHashMap<VfsRoot, FxHashSet<VfsFile>>,
95     pending_changes: Vec<VfsChange>,
96     worker: Worker,
97 }
98
99 impl fmt::Debug for Vfs {
100     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
101         f.write_str("Vfs { ... }")
102     }
103 }
104
105 impl Vfs {
106     pub fn new(mut roots: Vec<PathBuf>) -> (Vfs, Vec<VfsRoot>) {
107         let worker = io::Worker::start();
108
109         let mut res = Vfs {
110             roots: Arena::default(),
111             files: Arena::default(),
112             root2files: FxHashMap::default(),
113             worker,
114             pending_changes: Vec::new(),
115         };
116
117         // A hack to make nesting work.
118         roots.sort_by_key(|it| Reverse(it.as_os_str().len()));
119         for (i, path) in roots.iter().enumerate() {
120             let root_filter = Arc::new(RootFilter::new(path.clone()));
121
122             let root = res.roots.alloc(root_filter.clone());
123             res.root2files.insert(root, Default::default());
124
125             let nested_roots = roots[..i]
126                 .iter()
127                 .filter(|it| it.starts_with(path))
128                 .map(|it| it.clone())
129                 .collect::<Vec<_>>();
130
131             let task = io::Task::AddRoot {
132                 root,
133                 path: path.clone(),
134                 root_filter,
135                 nested_roots,
136             };
137             res.worker.sender().send(task).unwrap();
138         }
139         let roots = res.roots.iter().map(|(id, _)| id).collect();
140         (res, roots)
141     }
142
143     pub fn root2path(&self, root: VfsRoot) -> PathBuf {
144         self.roots[root].root.clone()
145     }
146
147     pub fn path2file(&self, path: &Path) -> Option<VfsFile> {
148         if let Some((_root, _path, Some(file))) = self.find_root(path) {
149             return Some(file);
150         }
151         None
152     }
153
154     pub fn file2path(&self, file: VfsFile) -> PathBuf {
155         let rel_path = &self.files[file].path;
156         let root_path = &self.roots[self.files[file].root].root;
157         rel_path.to_path(root_path)
158     }
159
160     pub fn file_for_path(&self, path: &Path) -> Option<VfsFile> {
161         if let Some((_root, _path, Some(file))) = self.find_root(path) {
162             return Some(file);
163         }
164         None
165     }
166
167     pub fn load(&mut self, path: &Path) -> Option<VfsFile> {
168         if let Some((root, rel_path, file)) = self.find_root(path) {
169             return if let Some(file) = file {
170                 Some(file)
171             } else {
172                 let text = fs::read_to_string(path).unwrap_or_default();
173                 let text = Arc::new(text);
174                 let file = self.add_file(root, rel_path.clone(), Arc::clone(&text), false);
175                 let change = VfsChange::AddFile {
176                     file,
177                     text,
178                     root,
179                     path: rel_path,
180                 };
181                 self.pending_changes.push(change);
182                 Some(file)
183             };
184         }
185         None
186     }
187
188     pub fn task_receiver(&self) -> &Receiver<io::TaskResult> {
189         self.worker.receiver()
190     }
191
192     pub fn handle_task(&mut self, task: io::TaskResult) {
193         match task {
194             TaskResult::AddRoot(task) => {
195                 let mut files = Vec::new();
196                 // While we were scanning the root in the backgound, a file might have
197                 // been open in the editor, so we need to account for that.
198                 let exising = self.root2files[&task.root]
199                     .iter()
200                     .map(|&file| (self.files[file].path.clone(), file))
201                     .collect::<FxHashMap<_, _>>();
202                 for (path, text) in task.files {
203                     if let Some(&file) = exising.get(&path) {
204                         let text = Arc::clone(&self.files[file].text);
205                         files.push((file, path, text));
206                         continue;
207                     }
208                     let text = Arc::new(text);
209                     let file = self.add_file(task.root, path.clone(), Arc::clone(&text), false);
210                     files.push((file, path, text));
211                 }
212
213                 let change = VfsChange::AddRoot {
214                     root: task.root,
215                     files,
216                 };
217                 self.pending_changes.push(change);
218             }
219             TaskResult::HandleChange(change) => match &change {
220                 WatcherChange::Create(path) if path.is_dir() => {
221                     if let Some((root, _path, _file)) = self.find_root(&path) {
222                         let root_filter = self.roots[root].clone();
223                         self.worker
224                             .sender()
225                             .send(Task::Watch {
226                                 dir: path.to_path_buf(),
227                                 root_filter,
228                             })
229                             .unwrap()
230                     }
231                 }
232                 WatcherChange::Create(path)
233                 | WatcherChange::Remove(path)
234                 | WatcherChange::Write(path) => {
235                     if self.should_handle_change(&path) {
236                         self.worker.sender().send(Task::LoadChange(change)).unwrap()
237                     }
238                 }
239                 WatcherChange::Rescan => {
240                     // TODO we should reload all files
241                 }
242             },
243             TaskResult::LoadChange(change) => match change {
244                 WatcherChangeData::Create { path, text }
245                 | WatcherChangeData::Write { path, text } => {
246                     if let Some((root, path, file)) = self.find_root(&path) {
247                         if let Some(file) = file {
248                             self.do_change_file(file, text, false);
249                         } else {
250                             self.do_add_file(root, path, text, false);
251                         }
252                     }
253                 }
254                 WatcherChangeData::Remove { path } => {
255                     if let Some((root, path, file)) = self.find_root(&path) {
256                         if let Some(file) = file {
257                             self.do_remove_file(root, path, file, false);
258                         }
259                     }
260                 }
261             },
262         }
263     }
264
265     fn should_handle_change(&self, path: &Path) -> bool {
266         if let Some((_root, _rel_path, file)) = self.find_root(&path) {
267             if let Some(file) = file {
268                 if self.files[file].is_overlayed {
269                     // file is overlayed
270                     log::debug!("skipping overlayed \"{}\"", path.display());
271                     return false;
272                 }
273             }
274             true
275         } else {
276             // file doesn't belong to any root
277             false
278         }
279     }
280
281     fn do_add_file(
282         &mut self,
283         root: VfsRoot,
284         path: RelativePathBuf,
285         text: String,
286         is_overlay: bool,
287     ) -> Option<VfsFile> {
288         let text = Arc::new(text);
289         let file = self.add_file(root, path.clone(), text.clone(), is_overlay);
290         self.pending_changes.push(VfsChange::AddFile {
291             file,
292             root,
293             path,
294             text,
295         });
296         Some(file)
297     }
298
299     fn do_change_file(&mut self, file: VfsFile, text: String, is_overlay: bool) {
300         if !is_overlay && self.files[file].is_overlayed {
301             return;
302         }
303         let text = Arc::new(text);
304         self.change_file(file, text.clone(), is_overlay);
305         self.pending_changes
306             .push(VfsChange::ChangeFile { file, text });
307     }
308
309     fn do_remove_file(
310         &mut self,
311         root: VfsRoot,
312         path: RelativePathBuf,
313         file: VfsFile,
314         is_overlay: bool,
315     ) {
316         if !is_overlay && self.files[file].is_overlayed {
317             return;
318         }
319         self.remove_file(file);
320         self.pending_changes
321             .push(VfsChange::RemoveFile { root, path, file });
322     }
323
324     pub fn add_file_overlay(&mut self, path: &Path, text: String) -> Option<VfsFile> {
325         if let Some((root, rel_path, file)) = self.find_root(path) {
326             if let Some(file) = file {
327                 self.do_change_file(file, text, true);
328                 Some(file)
329             } else {
330                 self.do_add_file(root, rel_path, text, true)
331             }
332         } else {
333             None
334         }
335     }
336
337     pub fn change_file_overlay(&mut self, path: &Path, new_text: String) {
338         if let Some((_root, _path, file)) = self.find_root(path) {
339             let file = file.expect("can't change a file which wasn't added");
340             self.do_change_file(file, new_text, true);
341         }
342     }
343
344     pub fn remove_file_overlay(&mut self, path: &Path) -> Option<VfsFile> {
345         if let Some((root, path, file)) = self.find_root(path) {
346             let file = file.expect("can't remove a file which wasn't added");
347             let full_path = path.to_path(&self.roots[root].root);
348             if let Ok(text) = fs::read_to_string(&full_path) {
349                 self.do_change_file(file, text, true);
350             } else {
351                 self.do_remove_file(root, path, file, true);
352             }
353             Some(file)
354         } else {
355             None
356         }
357     }
358
359     pub fn commit_changes(&mut self) -> Vec<VfsChange> {
360         mem::replace(&mut self.pending_changes, Vec::new())
361     }
362
363     /// Sutdown the VFS and terminate the background watching thread.
364     pub fn shutdown(self) -> thread::Result<()> {
365         self.worker.shutdown()
366     }
367
368     fn add_file(
369         &mut self,
370         root: VfsRoot,
371         path: RelativePathBuf,
372         text: Arc<String>,
373         is_overlayed: bool,
374     ) -> VfsFile {
375         let data = VfsFileData {
376             root,
377             path,
378             text,
379             is_overlayed,
380         };
381         let file = self.files.alloc(data);
382         self.root2files.get_mut(&root).unwrap().insert(file);
383         file
384     }
385
386     fn change_file(&mut self, file: VfsFile, new_text: Arc<String>, is_overlayed: bool) {
387         let mut file_data = &mut self.files[file];
388         file_data.text = new_text;
389         file_data.is_overlayed = is_overlayed;
390     }
391
392     fn remove_file(&mut self, file: VfsFile) {
393         //FIXME: use arena with removal
394         self.files[file].text = Default::default();
395         self.files[file].path = Default::default();
396         let root = self.files[file].root;
397         let removed = self.root2files.get_mut(&root).unwrap().remove(&file);
398         assert!(removed);
399     }
400
401     fn find_root(&self, path: &Path) -> Option<(VfsRoot, RelativePathBuf, Option<VfsFile>)> {
402         let (root, path) = self
403             .roots
404             .iter()
405             .find_map(|(root, data)| data.can_contain(path).map(|it| (root, it)))?;
406         let file = self.root2files[&root]
407             .iter()
408             .map(|&it| it)
409             .find(|&file| self.files[file].path == path);
410         Some((root, path, file))
411     }
412 }
413
414 #[derive(Debug, Clone)]
415 pub enum VfsChange {
416     AddRoot {
417         root: VfsRoot,
418         files: Vec<(VfsFile, RelativePathBuf, Arc<String>)>,
419     },
420     AddFile {
421         root: VfsRoot,
422         file: VfsFile,
423         path: RelativePathBuf,
424         text: Arc<String>,
425     },
426     RemoveFile {
427         root: VfsRoot,
428         file: VfsFile,
429         path: RelativePathBuf,
430     },
431     ChangeFile {
432         file: VfsFile,
433         text: Arc<String>,
434     },
435 }