]> git.lizzy.rs Git - rust.git/blob - crates/ra_vfs/src/lib.rs
refator to move all io to io module
[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 mod watcher;
18
19 use std::{
20     cmp::Reverse,
21     ffi::OsStr,
22     fmt, fs, mem,
23     path::{Path, PathBuf},
24     sync::Arc,
25     thread,
26 };
27
28 use crossbeam_channel::Receiver;
29 use ra_arena::{impl_arena_id, Arena, RawId};
30 use relative_path::RelativePathBuf;
31 use rustc_hash::{FxHashMap, FxHashSet};
32 use thread_worker::WorkerHandle;
33 use walkdir::DirEntry;
34
35 pub use crate::io::TaskResult as VfsTask;
36 pub use crate::watcher::{Watcher, WatcherChange};
37
38 /// `RootFilter` is a predicate that checks if a file can belong to a root. If
39 /// several filters match a file (nested dirs), the most nested one wins.
40 struct RootFilter {
41     root: PathBuf,
42     file_filter: fn(&Path) -> bool,
43 }
44
45 impl RootFilter {
46     fn new(root: PathBuf) -> RootFilter {
47         RootFilter {
48             root,
49             file_filter: has_rs_extension,
50         }
51     }
52     /// Check if this root can contain `path`. NB: even if this returns
53     /// true, the `path` might actually be conained in some nested root.
54     fn can_contain(&self, path: &Path) -> Option<RelativePathBuf> {
55         if !(self.file_filter)(path) {
56             return None;
57         }
58         let path = path.strip_prefix(&self.root).ok()?;
59         RelativePathBuf::from_path(path).ok()
60     }
61 }
62
63 pub(crate) fn has_rs_extension(p: &Path) -> bool {
64     p.extension() == Some(OsStr::new("rs"))
65 }
66
67 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
68 pub struct VfsRoot(pub RawId);
69 impl_arena_id!(VfsRoot);
70
71 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
72 pub struct VfsFile(pub RawId);
73 impl_arena_id!(VfsFile);
74
75 struct VfsFileData {
76     root: VfsRoot,
77     path: RelativePathBuf,
78     text: Arc<String>,
79 }
80
81 pub struct Vfs {
82     roots: Arena<VfsRoot, RootFilter>,
83     files: Arena<VfsFile, VfsFileData>,
84     root2files: FxHashMap<VfsRoot, FxHashSet<VfsFile>>,
85     pending_changes: Vec<VfsChange>,
86     worker: io::Worker,
87     worker_handle: WorkerHandle,
88     watcher: Watcher,
89 }
90
91 impl fmt::Debug for Vfs {
92     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
93         f.write_str("Vfs { ... }")
94     }
95 }
96
97 impl Vfs {
98     pub fn new(mut roots: Vec<PathBuf>) -> (Vfs, Vec<VfsRoot>) {
99         let (worker, worker_handle) = io::start();
100
101         let watcher = Watcher::start(worker.inp.clone()).unwrap(); // TODO return Result?
102
103         let mut res = Vfs {
104             roots: Arena::default(),
105             files: Arena::default(),
106             root2files: FxHashMap::default(),
107             worker,
108             worker_handle,
109             watcher,
110             pending_changes: Vec::new(),
111         };
112
113         // A hack to make nesting work.
114         roots.sort_by_key(|it| Reverse(it.as_os_str().len()));
115         for (i, path) in roots.iter().enumerate() {
116             let root = res.roots.alloc(RootFilter::new(path.clone()));
117             res.root2files.insert(root, Default::default());
118             let nested = roots[..i]
119                 .iter()
120                 .filter(|it| it.starts_with(path))
121                 .map(|it| it.clone())
122                 .collect::<Vec<_>>();
123             let filter = move |entry: &DirEntry| {
124                 if entry.file_type().is_file() {
125                     has_rs_extension(entry.path())
126                 } else {
127                     nested.iter().all(|it| it != entry.path())
128                 }
129             };
130             let task = io::Task::AddRoot {
131                 root,
132                 path: path.clone(),
133                 filter: Box::new(filter),
134             };
135             res.worker.inp.send(task).unwrap();
136             res.watcher.watch(path).unwrap();
137         }
138         let roots = res.roots.iter().map(|(id, _)| id).collect();
139         (res, roots)
140     }
141
142     pub fn root2path(&self, root: VfsRoot) -> PathBuf {
143         self.roots[root].root.clone()
144     }
145
146     pub fn path2file(&self, path: &Path) -> Option<VfsFile> {
147         if let Some((_root, _path, Some(file))) = self.find_root(path) {
148             return Some(file);
149         }
150         None
151     }
152
153     pub fn file2path(&self, file: VfsFile) -> PathBuf {
154         let rel_path = &self.files[file].path;
155         let root_path = &self.roots[self.files[file].root].root;
156         rel_path.to_path(root_path)
157     }
158
159     pub fn file_for_path(&self, path: &Path) -> Option<VfsFile> {
160         if let Some((_root, _path, Some(file))) = self.find_root(path) {
161             return Some(file);
162         }
163         None
164     }
165
166     pub fn load(&mut self, path: &Path) -> Option<VfsFile> {
167         if let Some((root, rel_path, file)) = self.find_root(path) {
168             return if let Some(file) = file {
169                 Some(file)
170             } else {
171                 let text = fs::read_to_string(path).unwrap_or_default();
172                 let text = Arc::new(text);
173                 let file = self.add_file(root, rel_path.clone(), Arc::clone(&text));
174                 let change = VfsChange::AddFile {
175                     file,
176                     text,
177                     root,
178                     path: rel_path,
179                 };
180                 self.pending_changes.push(change);
181                 Some(file)
182             };
183         }
184         None
185     }
186
187     pub fn task_receiver(&self) -> &Receiver<io::TaskResult> {
188         &self.worker.out
189     }
190
191     pub fn handle_task(&mut self, task: io::TaskResult) {
192         match task {
193             io::TaskResult::AddRoot(task) => {
194                 let mut files = Vec::new();
195                 // While we were scanning the root in the backgound, a file might have
196                 // been open in the editor, so we need to account for that.
197                 let exising = self.root2files[&task.root]
198                     .iter()
199                     .map(|&file| (self.files[file].path.clone(), file))
200                     .collect::<FxHashMap<_, _>>();
201                 for (path, text) in task.files {
202                     if let Some(&file) = exising.get(&path) {
203                         let text = Arc::clone(&self.files[file].text);
204                         files.push((file, path, text));
205                         continue;
206                     }
207                     let text = Arc::new(text);
208                     let file = self.add_file(task.root, path.clone(), Arc::clone(&text));
209                     files.push((file, path, text));
210                 }
211
212                 let change = VfsChange::AddRoot {
213                     root: task.root,
214                     files,
215                 };
216                 self.pending_changes.push(change);
217             }
218             io::TaskResult::WatcherChange(change) => {
219                 // TODO
220                 unimplemented!()
221             }
222         }
223     }
224
225     pub fn add_file_overlay(&mut self, path: &Path, text: String) -> Option<VfsFile> {
226         let mut res = None;
227         if let Some((root, rel_path, file)) = self.find_root(path) {
228             let text = Arc::new(text);
229             let change = if let Some(file) = file {
230                 res = Some(file);
231                 self.change_file(file, Arc::clone(&text));
232                 VfsChange::ChangeFile { file, text }
233             } else {
234                 let file = self.add_file(root, rel_path.clone(), Arc::clone(&text));
235                 res = Some(file);
236                 VfsChange::AddFile {
237                     file,
238                     text,
239                     root,
240                     path: rel_path,
241                 }
242             };
243             self.pending_changes.push(change);
244         }
245         res
246     }
247
248     pub fn change_file_overlay(&mut self, path: &Path, new_text: String) {
249         if let Some((_root, _path, file)) = self.find_root(path) {
250             let file = file.expect("can't change a file which wasn't added");
251             let text = Arc::new(new_text);
252             self.change_file(file, Arc::clone(&text));
253             let change = VfsChange::ChangeFile { file, text };
254             self.pending_changes.push(change);
255         }
256     }
257
258     pub fn remove_file_overlay(&mut self, path: &Path) -> Option<VfsFile> {
259         let mut res = None;
260         if let Some((root, path, file)) = self.find_root(path) {
261             let file = file.expect("can't remove a file which wasn't added");
262             res = Some(file);
263             let full_path = path.to_path(&self.roots[root].root);
264             let change = if let Ok(text) = fs::read_to_string(&full_path) {
265                 let text = Arc::new(text);
266                 self.change_file(file, Arc::clone(&text));
267                 VfsChange::ChangeFile { file, text }
268             } else {
269                 self.remove_file(file);
270                 VfsChange::RemoveFile { root, file, path }
271             };
272             self.pending_changes.push(change);
273         }
274         res
275     }
276
277     pub fn commit_changes(&mut self) -> Vec<VfsChange> {
278         mem::replace(&mut self.pending_changes, Vec::new())
279     }
280
281     /// Sutdown the VFS and terminate the background watching thread.
282     pub fn shutdown(self) -> thread::Result<()> {
283         let _ = self.watcher.shutdown();
284         let _ = self.worker.shutdown();
285         self.worker_handle.shutdown()
286     }
287
288     fn add_file(&mut self, root: VfsRoot, path: RelativePathBuf, text: Arc<String>) -> VfsFile {
289         let data = VfsFileData { root, path, text };
290         let file = self.files.alloc(data);
291         self.root2files.get_mut(&root).unwrap().insert(file);
292         file
293     }
294
295     fn change_file(&mut self, file: VfsFile, new_text: Arc<String>) {
296         self.files[file].text = new_text;
297     }
298
299     fn remove_file(&mut self, file: VfsFile) {
300         //FIXME: use arena with removal
301         self.files[file].text = Default::default();
302         self.files[file].path = Default::default();
303         let root = self.files[file].root;
304         let removed = self.root2files.get_mut(&root).unwrap().remove(&file);
305         assert!(removed);
306     }
307
308     fn find_root(&self, path: &Path) -> Option<(VfsRoot, RelativePathBuf, Option<VfsFile>)> {
309         let (root, path) = self
310             .roots
311             .iter()
312             .find_map(|(root, data)| data.can_contain(path).map(|it| (root, it)))?;
313         let file = self.root2files[&root]
314             .iter()
315             .map(|&it| it)
316             .find(|&file| self.files[file].path == path);
317         Some((root, path, file))
318     }
319 }
320
321 #[derive(Debug, Clone)]
322 pub enum VfsChange {
323     AddRoot {
324         root: VfsRoot,
325         files: Vec<(VfsFile, RelativePathBuf, Arc<String>)>,
326     },
327     AddFile {
328         root: VfsRoot,
329         file: VfsFile,
330         path: RelativePathBuf,
331         text: Arc<String>,
332     },
333     RemoveFile {
334         root: VfsRoot,
335         file: VfsFile,
336         path: RelativePathBuf,
337     },
338     ChangeFile {
339         file: VfsFile,
340         text: Arc<String>,
341     },
342 }