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.
11 //! TODO: Some LSP clients support watching the disk, so this crate should to
12 //! support custom watcher events (related to
13 //! <https://github.com/rust-analyzer/rust-analyzer/issues/131>)
15 //! VFS is based on a concept of roots: a set of directories on the file system
16 //! which are watched for changes. Typically, there will be a root for each
23 path::{Path, PathBuf},
27 use crossbeam_channel::Receiver;
28 use ra_arena::{impl_arena_id, Arena, RawId, map::ArenaMap};
29 use relative_path::{Component, RelativePath, RelativePathBuf};
30 use rustc_hash::{FxHashMap, FxHashSet};
32 pub use crate::io::TaskResult as VfsTask;
33 use io::{TaskResult, Worker};
35 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
36 pub struct VfsRoot(pub RawId);
37 impl_arena_id!(VfsRoot);
39 /// Describes the contents of a single source root.
41 /// `RootConfig` can be thought of as a glob pattern like `src/**.rs` which
42 /// specifies the source root or as a function which takes a `PathBuf` and
43 /// returns `true` iff path belongs to the source root
44 pub(crate) struct RootConfig {
46 // result of `root.canonicalize()` if that differs from `root`; `None` otherwise.
47 canonical_root: Option<PathBuf>,
48 excluded_dirs: Vec<PathBuf>,
51 pub(crate) struct Roots {
52 roots: Arena<VfsRoot, Arc<RootConfig>>,
55 impl std::ops::Deref for Roots {
56 type Target = Arena<VfsRoot, Arc<RootConfig>>;
57 fn deref(&self) -> &Self::Target {
63 fn new(root: PathBuf, excluded_dirs: Vec<PathBuf>) -> RootConfig {
64 let mut canonical_root = root.canonicalize().ok();
65 if Some(&root) == canonical_root.as_ref() {
66 canonical_root = None;
68 RootConfig { root, canonical_root, excluded_dirs }
70 /// Checks if root contains a path and returns a root-relative path.
71 pub(crate) fn contains(&self, path: &Path) -> Option<RelativePathBuf> {
72 // First, check excluded dirs
73 if self.excluded_dirs.iter().any(|it| path.starts_with(it)) {
77 .strip_prefix(&self.root)
78 .or_else(|err_payload| {
81 .map_or(Err(err_payload), |canonical_root| path.strip_prefix(canonical_root))
84 let rel_path = RelativePathBuf::from_path(rel_path).ok()?;
86 // Ignore some common directories.
88 // FIXME: don't hard-code, specify at source-root creation time using
90 for (i, c) in rel_path.components().enumerate() {
91 if let Component::Normal(c) = c {
92 if (i == 0 && c == "target") || c == ".git" || c == "node_modules" {
98 if path.is_file() && rel_path.extension() != Some("rs") {
107 pub(crate) fn new(mut paths: Vec<PathBuf>) -> Roots {
108 let mut roots = Arena::default();
109 // A hack to make nesting work.
110 paths.sort_by_key(|it| Reverse(it.as_os_str().len()));
112 for (i, path) in paths.iter().enumerate() {
113 let nested_roots = paths[..i]
115 .filter(|it| it.starts_with(path))
116 .map(|it| it.clone())
117 .collect::<Vec<_>>();
119 let config = Arc::new(RootConfig::new(path.clone(), nested_roots));
121 roots.alloc(config.clone());
125 pub(crate) fn find(&self, path: &Path) -> Option<(VfsRoot, RelativePathBuf)> {
126 self.roots.iter().find_map(|(root, data)| data.contains(path).map(|it| (root, it)))
130 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
131 pub struct VfsFile(pub RawId);
132 impl_arena_id!(VfsFile);
136 path: RelativePathBuf,
143 files: Arena<VfsFile, VfsFileData>,
144 root2files: ArenaMap<VfsRoot, FxHashSet<VfsFile>>,
145 pending_changes: Vec<VfsChange>,
149 impl fmt::Debug for Vfs {
150 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
151 f.debug_struct("Vfs")
152 .field("n_roots", &self.roots.len())
153 .field("n_files", &self.files.len())
154 .field("n_pending_changes", &self.pending_changes.len())
160 pub fn new(roots: Vec<PathBuf>) -> (Vfs, Vec<VfsRoot>) {
161 let roots = Arc::new(Roots::new(roots));
162 let worker = io::start(Arc::clone(&roots));
163 let mut root2files = ArenaMap::default();
165 for (root, config) in roots.iter() {
166 root2files.insert(root, Default::default());
167 worker.sender().send(io::Task::AddRoot { root, config: Arc::clone(config) }).unwrap();
170 Vfs { roots, files: Arena::default(), root2files, worker, pending_changes: Vec::new() };
171 let vfs_roots = res.roots.iter().map(|(id, _)| id).collect();
175 pub fn root2path(&self, root: VfsRoot) -> PathBuf {
176 self.roots[root].root.clone()
179 pub fn path2file(&self, path: &Path) -> Option<VfsFile> {
180 if let Some((_root, _path, Some(file))) = self.find_root(path) {
186 pub fn file2path(&self, file: VfsFile) -> PathBuf {
187 let rel_path = &self.files[file].path;
188 let root_path = &self.roots[self.files[file].root].root;
189 rel_path.to_path(root_path)
192 pub fn file_for_path(&self, path: &Path) -> Option<VfsFile> {
193 if let Some((_root, _path, Some(file))) = self.find_root(path) {
199 pub fn num_roots(&self) -> usize {
203 pub fn load(&mut self, path: &Path) -> Option<VfsFile> {
204 if let Some((root, rel_path, file)) = self.find_root(path) {
205 return if let Some(file) = file {
208 let text = fs::read_to_string(path).unwrap_or_default();
209 let text = Arc::new(text);
210 let file = self.add_file(root, rel_path.clone(), Arc::clone(&text), false);
211 let change = VfsChange::AddFile { file, text, root, path: rel_path };
212 self.pending_changes.push(change);
219 pub fn task_receiver(&self) -> &Receiver<io::TaskResult> {
220 self.worker.receiver()
223 pub fn handle_task(&mut self, task: io::TaskResult) {
225 TaskResult::BulkLoadRoot { root, files } => {
226 let mut cur_files = Vec::new();
227 // While we were scanning the root in the background, a file might have
228 // been open in the editor, so we need to account for that.
229 let existing = self.root2files[root]
231 .map(|&file| (self.files[file].path.clone(), file))
232 .collect::<FxHashMap<_, _>>();
233 for (path, text) in files {
234 if let Some(&file) = existing.get(&path) {
235 let text = Arc::clone(&self.files[file].text);
236 cur_files.push((file, path, text));
239 let text = Arc::new(text);
240 let file = self.add_file(root, path.clone(), Arc::clone(&text), false);
241 cur_files.push((file, path, text));
244 let change = VfsChange::AddRoot { root, files: cur_files };
245 self.pending_changes.push(change);
247 TaskResult::SingleFile { root, path, text } => {
248 match (self.find_file(root, &path), text) {
249 (Some(file), None) => {
250 self.do_remove_file(root, path, file, false);
252 (None, Some(text)) => {
253 self.do_add_file(root, path, text, false);
255 (Some(file), Some(text)) => {
256 self.do_change_file(file, text, false);
267 path: RelativePathBuf,
270 ) -> Option<VfsFile> {
271 let text = Arc::new(text);
272 let file = self.add_file(root, path.clone(), text.clone(), is_overlay);
273 self.pending_changes.push(VfsChange::AddFile { file, root, path, text });
277 fn do_change_file(&mut self, file: VfsFile, text: String, is_overlay: bool) {
278 if !is_overlay && self.files[file].is_overlayed {
281 let text = Arc::new(text);
282 self.change_file(file, text.clone(), is_overlay);
283 self.pending_changes.push(VfsChange::ChangeFile { file, text });
289 path: RelativePathBuf,
293 if !is_overlay && self.files[file].is_overlayed {
296 self.remove_file(file);
297 self.pending_changes.push(VfsChange::RemoveFile { root, path, file });
300 pub fn add_file_overlay(&mut self, path: &Path, text: String) -> Option<VfsFile> {
301 if let Some((root, rel_path, file)) = self.find_root(path) {
302 if let Some(file) = file {
303 self.do_change_file(file, text, true);
306 self.do_add_file(root, rel_path, text, true)
313 pub fn change_file_overlay(&mut self, path: &Path, new_text: String) {
314 if let Some((_root, _path, file)) = self.find_root(path) {
315 let file = file.expect("can't change a file which wasn't added");
316 self.do_change_file(file, new_text, true);
320 pub fn remove_file_overlay(&mut self, path: &Path) -> Option<VfsFile> {
321 if let Some((root, path, file)) = self.find_root(path) {
322 let file = file.expect("can't remove a file which wasn't added");
323 let full_path = path.to_path(&self.roots[root].root);
324 if let Ok(text) = fs::read_to_string(&full_path) {
325 self.do_change_file(file, text, true);
327 self.do_remove_file(root, path, file, true);
335 pub fn commit_changes(&mut self) -> Vec<VfsChange> {
336 mem::replace(&mut self.pending_changes, Vec::new())
342 path: RelativePathBuf,
346 let data = VfsFileData { root, path, text, is_overlayed };
347 let file = self.files.alloc(data);
348 self.root2files.get_mut(root).unwrap().insert(file);
352 fn change_file(&mut self, file: VfsFile, new_text: Arc<String>, is_overlayed: bool) {
353 let mut file_data = &mut self.files[file];
354 file_data.text = new_text;
355 file_data.is_overlayed = is_overlayed;
358 fn remove_file(&mut self, file: VfsFile) {
359 // FIXME: use arena with removal
360 self.files[file].text = Default::default();
361 self.files[file].path = Default::default();
362 let root = self.files[file].root;
363 let removed = self.root2files.get_mut(root).unwrap().remove(&file);
367 fn find_root(&self, path: &Path) -> Option<(VfsRoot, RelativePathBuf, Option<VfsFile>)> {
368 let (root, path) = self.roots.find(&path)?;
369 let file = self.find_file(root, &path);
370 Some((root, path, file))
373 fn find_file(&self, root: VfsRoot, path: &RelativePath) -> Option<VfsFile> {
374 self.root2files[root].iter().map(|&it| it).find(|&file| self.files[file].path == path)
378 #[derive(Debug, Clone)]
380 AddRoot { root: VfsRoot, files: Vec<(VfsFile, RelativePathBuf, Arc<String>)> },
381 AddFile { root: VfsRoot, file: VfsFile, path: RelativePathBuf, text: Arc<String> },
382 RemoveFile { root: VfsRoot, file: VfsFile, path: RelativePathBuf },
383 ChangeFile { file: VfsFile, text: Arc<String> },