1 //! VFS stands for Virtual File System.
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.
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)
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
21 path::{Path, PathBuf},
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};
31 pub use crate::io::TaskResult as VfsTask;
32 use io::{Task, TaskResult, WatcherChange, WatcherChangeData, Worker};
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 {
38 filter: fn(&Path, &RelativePath) -> bool,
42 fn new(root: PathBuf) -> RootFilter {
45 filter: default_filter,
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()) {
60 pub(crate) fn default_filter(path: &Path, rel_path: &RelativePath) -> bool {
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" {
72 rel_path.extension() == Some("rs")
76 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
77 pub struct VfsRoot(pub RawId);
78 impl_arena_id!(VfsRoot);
80 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
81 pub struct VfsFile(pub RawId);
82 impl_arena_id!(VfsFile);
86 path: RelativePathBuf,
92 roots: Arena<VfsRoot, Arc<RootFilter>>,
93 files: Arena<VfsFile, VfsFileData>,
94 root2files: FxHashMap<VfsRoot, FxHashSet<VfsFile>>,
95 pending_changes: Vec<VfsChange>,
99 impl fmt::Debug for Vfs {
100 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
101 f.write_str("Vfs { ... }")
106 pub fn new(mut roots: Vec<PathBuf>) -> (Vfs, Vec<VfsRoot>) {
107 let worker = io::Worker::start();
110 roots: Arena::default(),
111 files: Arena::default(),
112 root2files: FxHashMap::default(),
114 pending_changes: Vec::new(),
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()));
122 let root = res.roots.alloc(root_filter.clone());
123 res.root2files.insert(root, Default::default());
125 let nested_roots = roots[..i]
127 .filter(|it| it.starts_with(path))
128 .map(|it| it.clone())
129 .collect::<Vec<_>>();
131 let task = io::Task::AddRoot {
137 res.worker.sender().send(task).unwrap();
139 let roots = res.roots.iter().map(|(id, _)| id).collect();
143 pub fn root2path(&self, root: VfsRoot) -> PathBuf {
144 self.roots[root].root.clone()
147 pub fn path2file(&self, path: &Path) -> Option<VfsFile> {
148 if let Some((_root, _path, Some(file))) = self.find_root(path) {
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)
160 pub fn file_for_path(&self, path: &Path) -> Option<VfsFile> {
161 if let Some((_root, _path, Some(file))) = self.find_root(path) {
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 {
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 {
181 self.pending_changes.push(change);
188 pub fn task_receiver(&self) -> &Receiver<io::TaskResult> {
189 self.worker.receiver()
192 pub fn handle_task(&mut self, task: io::TaskResult) {
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]
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));
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));
213 let change = VfsChange::AddRoot {
217 self.pending_changes.push(change);
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();
226 dir: path.to_path_buf(),
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()
239 WatcherChange::Rescan => {
240 // TODO we should reload all files
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);
250 self.do_add_file(root, path, text, false);
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);
262 TaskResult::NoOp => {}
266 fn should_handle_change(&self, path: &Path) -> bool {
267 if let Some((_root, _rel_path, file)) = self.find_root(&path) {
268 if let Some(file) = file {
269 if self.files[file].is_overlayed {
271 log::debug!("skipping overlayed \"{}\"", path.display());
277 // file doesn't belong to any root
285 path: RelativePathBuf,
288 ) -> Option<VfsFile> {
289 let text = Arc::new(text);
290 let file = self.add_file(root, path.clone(), text.clone(), is_overlay);
291 self.pending_changes.push(VfsChange::AddFile {
300 fn do_change_file(&mut self, file: VfsFile, text: String, is_overlay: bool) {
301 if !is_overlay && self.files[file].is_overlayed {
304 let text = Arc::new(text);
305 self.change_file(file, text.clone(), is_overlay);
307 .push(VfsChange::ChangeFile { file, text });
313 path: RelativePathBuf,
317 if !is_overlay && self.files[file].is_overlayed {
320 self.remove_file(file);
322 .push(VfsChange::RemoveFile { root, path, file });
325 pub fn add_file_overlay(&mut self, path: &Path, text: String) -> Option<VfsFile> {
326 if let Some((root, rel_path, file)) = self.find_root(path) {
327 if let Some(file) = file {
328 self.do_change_file(file, text, true);
331 self.do_add_file(root, rel_path, text, true)
338 pub fn change_file_overlay(&mut self, path: &Path, new_text: String) {
339 if let Some((_root, _path, file)) = self.find_root(path) {
340 let file = file.expect("can't change a file which wasn't added");
341 self.do_change_file(file, new_text, true);
345 pub fn remove_file_overlay(&mut self, path: &Path) -> Option<VfsFile> {
346 if let Some((root, path, file)) = self.find_root(path) {
347 let file = file.expect("can't remove a file which wasn't added");
348 let full_path = path.to_path(&self.roots[root].root);
349 if let Ok(text) = fs::read_to_string(&full_path) {
350 self.do_change_file(file, text, true);
352 self.do_remove_file(root, path, file, true);
360 pub fn commit_changes(&mut self) -> Vec<VfsChange> {
361 mem::replace(&mut self.pending_changes, Vec::new())
364 /// Sutdown the VFS and terminate the background watching thread.
365 pub fn shutdown(self) -> thread::Result<()> {
366 self.worker.shutdown()
372 path: RelativePathBuf,
376 let data = VfsFileData {
382 let file = self.files.alloc(data);
383 self.root2files.get_mut(&root).unwrap().insert(file);
387 fn change_file(&mut self, file: VfsFile, new_text: Arc<String>, is_overlayed: bool) {
388 let mut file_data = &mut self.files[file];
389 file_data.text = new_text;
390 file_data.is_overlayed = is_overlayed;
393 fn remove_file(&mut self, file: VfsFile) {
394 //FIXME: use arena with removal
395 self.files[file].text = Default::default();
396 self.files[file].path = Default::default();
397 let root = self.files[file].root;
398 let removed = self.root2files.get_mut(&root).unwrap().remove(&file);
402 fn find_root(&self, path: &Path) -> Option<(VfsRoot, RelativePathBuf, Option<VfsFile>)> {
403 let (root, path) = self
406 .find_map(|(root, data)| data.can_contain(path).map(|it| (root, it)))?;
407 let file = self.root2files[&root]
410 .find(|&file| self.files[file].path == path);
411 Some((root, path, file))
415 #[derive(Debug, Clone)]
419 files: Vec<(VfsFile, RelativePathBuf, Arc<String>)>,
424 path: RelativePathBuf,
430 path: RelativePathBuf,