vfs/lib.rs
1//! # Virtual File System
2//!
3//! VFS records all file changes pushed to it via [`set_file_contents`].
4//! As such it only ever stores changes, not the actual content of a file at any given moment.
5//! All file changes are logged, and can be retrieved via
6//! [`take_changes`] method. The pack of changes is then pushed to `salsa` and
7//! triggers incremental recomputation.
8//!
9//! Files in VFS are identified with [`FileId`]s -- interned paths. The notion of
10//! the path, [`VfsPath`] is somewhat abstract: at the moment, it is represented
11//! as an [`std::path::PathBuf`] internally, but this is an implementation detail.
12//!
13//! VFS doesn't do IO or file watching itself. For that, see the [`loader`]
14//! module. [`loader::Handle`] is an object-safe trait which abstracts both file
15//! loading and file watching. [`Handle`] is dynamically configured with a set of
16//! directory entries which should be scanned and watched. [`Handle`] then
17//! asynchronously pushes file changes. Directory entries are configured in
18//! free-form via list of globs, it's up to the [`Handle`] to interpret the globs
19//! in any specific way.
20//!
21//! VFS stores a flat list of files. [`file_set::FileSet`] can partition this list
22//! of files into disjoint sets of files. Traversal-like operations (including
23//! getting the neighbor file by the relative path) are handled by the [`FileSet`].
24//! [`FileSet`]s are also pushed to salsa and cause it to re-check `mod foo;`
25//! declarations when files are created or deleted.
26//!
27//! [`FileSet`] and [`loader::Entry`] play similar, but different roles.
28//! Both specify the "set of paths/files", one is geared towards file watching,
29//! the other towards salsa changes. In particular, single [`FileSet`]
30//! may correspond to several [`loader::Entry`]. For example, a crate from
31//! crates.io which uses code generation would have two [`Entries`] -- for sources
32//! in `~/.cargo`, and for generated code in `./target/debug/build`. It will
33//! have a single [`FileSet`] which unions the two sources.
34//!
35//! [`set_file_contents`]: Vfs::set_file_contents
36//! [`take_changes`]: Vfs::take_changes
37//! [`FileSet`]: file_set::FileSet
38//! [`Handle`]: loader::Handle
39//! [`Entries`]: loader::Entry
40
41mod anchored_path;
42pub mod file_set;
43pub mod loader;
44mod path_interner;
45mod vfs_path;
46
47use std::{fmt, hash::BuildHasherDefault, mem};
48
49use crate::path_interner::PathInterner;
50
51pub use crate::{
52 anchored_path::{AnchoredPath, AnchoredPathBuf},
53 vfs_path::VfsPath,
54};
55use indexmap::{IndexMap, map::Entry};
56pub use paths::{AbsPath, AbsPathBuf};
57
58use rustc_hash::FxHasher;
59use stdx::hash_once;
60use tracing::{Level, span};
61
62/// Handle to a file in [`Vfs`]
63///
64/// Most functions in rust-analyzer use this when they need to refer to a file.
65#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
66pub struct FileId(u32);
67// pub struct FileId(NonMaxU32);
68
69impl FileId {
70 const MAX: u32 = 0x7fff_ffff;
71
72 #[inline]
73 pub const fn from_raw(raw: u32) -> FileId {
74 assert!(raw <= Self::MAX);
75 FileId(raw)
76 }
77
78 #[inline]
79 pub const fn index(self) -> u32 {
80 self.0
81 }
82}
83
84/// safe because `FileId` is a newtype of `u32`
85impl nohash_hasher::IsEnabled for FileId {}
86
87/// Storage for all file changes and the file id to path mapping.
88///
89/// For more information see the [crate-level](crate) documentation.
90#[derive(Default)]
91pub struct Vfs {
92 interner: PathInterner,
93 data: Vec<FileState>,
94 changes: IndexMap<FileId, ChangedFile, BuildHasherDefault<FxHasher>>,
95}
96
97#[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
98pub enum FileState {
99 /// The file exists with the given content hash.
100 Exists(u64),
101 /// The file is deleted.
102 Deleted,
103 /// The file was specifically excluded by the user. We still include excluded files
104 /// when they're opened (without their contents).
105 Excluded,
106}
107
108/// Changed file in the [`Vfs`].
109#[derive(Debug)]
110pub struct ChangedFile {
111 /// Id of the changed file
112 pub file_id: FileId,
113 /// Kind of change
114 pub change: Change,
115}
116
117impl ChangedFile {
118 /// Returns `true` if the change is not [`Delete`](ChangeKind::Delete).
119 pub fn exists(&self) -> bool {
120 !matches!(self.change, Change::Delete)
121 }
122
123 /// Returns `true` if the change is [`Create`](ChangeKind::Create) or
124 /// [`Delete`](Change::Delete).
125 pub fn is_created_or_deleted(&self) -> bool {
126 matches!(self.change, Change::Create(_, _) | Change::Delete)
127 }
128
129 /// Returns `true` if the change is [`Create`](ChangeKind::Create).
130 pub fn is_created(&self) -> bool {
131 matches!(self.change, Change::Create(_, _))
132 }
133
134 /// Returns `true` if the change is [`Modify`](ChangeKind::Modify).
135 pub fn is_modified(&self) -> bool {
136 matches!(self.change, Change::Modify(_, _))
137 }
138
139 pub fn kind(&self) -> ChangeKind {
140 match self.change {
141 Change::Create(_, _) => ChangeKind::Create,
142 Change::Modify(_, _) => ChangeKind::Modify,
143 Change::Delete => ChangeKind::Delete,
144 }
145 }
146}
147
148/// Kind of [file change](ChangedFile).
149#[derive(Eq, PartialEq, Debug)]
150pub enum Change {
151 /// The file was (re-)created
152 Create(Vec<u8>, u64),
153 /// The file was modified
154 Modify(Vec<u8>, u64),
155 /// The file was deleted
156 Delete,
157}
158
159/// Kind of [file change](ChangedFile).
160#[derive(Eq, PartialEq, Debug)]
161pub enum ChangeKind {
162 /// The file was (re-)created
163 Create,
164 /// The file was modified
165 Modify,
166 /// The file was deleted
167 Delete,
168}
169
170#[derive(Debug, Clone, Copy, PartialEq, Eq)]
171pub enum FileExcluded {
172 Yes,
173 No,
174}
175
176impl Vfs {
177 /// Id of the given path if it exists in the `Vfs` and is not deleted.
178 pub fn file_id(&self, path: &VfsPath) -> Option<(FileId, FileExcluded)> {
179 let file_id = self.interner.get(path)?;
180 let file_state = self.get(file_id);
181 match file_state {
182 FileState::Exists(_) => Some((file_id, FileExcluded::No)),
183 FileState::Deleted => None,
184 FileState::Excluded => Some((file_id, FileExcluded::Yes)),
185 }
186 }
187
188 /// File path corresponding to the given `file_id`.
189 ///
190 /// # Panics
191 ///
192 /// Panics if the id is not present in the `Vfs`.
193 pub fn file_path(&self, file_id: FileId) -> &VfsPath {
194 self.interner.lookup(file_id)
195 }
196
197 /// Returns an iterator over the stored ids and their corresponding paths.
198 ///
199 /// This will skip deleted files.
200 pub fn iter(&self) -> impl Iterator<Item = (FileId, &VfsPath)> + '_ {
201 (0..self.data.len())
202 .map(|it| FileId(it as u32))
203 .filter(move |&file_id| matches!(self.get(file_id), FileState::Exists(_)))
204 .map(move |file_id| {
205 let path = self.interner.lookup(file_id);
206 (file_id, path)
207 })
208 }
209
210 /// Update the `path` with the given `contents`. `None` means the file was deleted.
211 ///
212 /// Returns `true` if the file was modified, and saves the [change](ChangedFile).
213 ///
214 /// If the path does not currently exists in the `Vfs`, allocates a new
215 /// [`FileId`] for it.
216 pub fn set_file_contents(&mut self, path: VfsPath, contents: Option<Vec<u8>>) -> bool {
217 let _p = span!(Level::INFO, "Vfs::set_file_contents").entered();
218 let file_id = self.alloc_file_id(path);
219 let state: FileState = self.get(file_id);
220 let change = match (state, contents) {
221 (FileState::Deleted, None) => return false,
222 (FileState::Deleted, Some(v)) => {
223 let hash = hash_once::<FxHasher>(&*v);
224 Change::Create(v, hash)
225 }
226 (FileState::Exists(_), None) => Change::Delete,
227 (FileState::Exists(hash), Some(v)) => {
228 let new_hash = hash_once::<FxHasher>(&*v);
229 if new_hash == hash {
230 return false;
231 }
232 Change::Modify(v, new_hash)
233 }
234 (FileState::Excluded, _) => return false,
235 };
236
237 let mut set_data = |change_kind| {
238 self.data[file_id.0 as usize] = match change_kind {
239 &Change::Create(_, hash) | &Change::Modify(_, hash) => FileState::Exists(hash),
240 Change::Delete => FileState::Deleted,
241 };
242 };
243
244 let changed_file = ChangedFile { file_id, change };
245 match self.changes.entry(file_id) {
246 // two changes to the same file in one cycle, merge them appropriately
247 Entry::Occupied(mut o) => {
248 use Change::*;
249
250 match (&mut o.get_mut().change, changed_file.change) {
251 // newer `Delete` wins
252 (change, Delete) => *change = Delete,
253 // merge `Create` with `Create` or `Modify`
254 (Create(prev, old_hash), Create(new, new_hash) | Modify(new, new_hash)) => {
255 *prev = new;
256 *old_hash = new_hash;
257 }
258 // collapse identical `Modify`es
259 (Modify(prev, old_hash), Modify(new, new_hash)) => {
260 *prev = new;
261 *old_hash = new_hash;
262 }
263 // equivalent to `Modify`
264 (change @ Delete, Create(new, new_hash)) => {
265 *change = Modify(new, new_hash);
266 }
267 // shouldn't occur, but collapse into `Create`
268 (change @ Delete, Modify(new, new_hash)) => {
269 stdx::never!();
270 *change = Create(new, new_hash);
271 }
272 // shouldn't occur, but keep the Create
273 (prev @ Modify(_, _), new @ Create(_, _)) => *prev = new,
274 }
275 set_data(&o.get().change);
276 }
277 Entry::Vacant(v) => set_data(&v.insert(changed_file).change),
278 };
279
280 true
281 }
282
283 /// Drain and returns all the changes in the `Vfs`.
284 pub fn take_changes(&mut self) -> IndexMap<FileId, ChangedFile, BuildHasherDefault<FxHasher>> {
285 mem::take(&mut self.changes)
286 }
287
288 /// Provides a panic-less way to verify file_id validity.
289 pub fn exists(&self, file_id: FileId) -> bool {
290 matches!(self.get(file_id), FileState::Exists(_))
291 }
292
293 /// Returns the id associated with `path`
294 ///
295 /// - If `path` does not exists in the `Vfs`, allocate a new id for it, associated with a
296 /// deleted file;
297 /// - Else, returns `path`'s id.
298 ///
299 /// Does not record a change.
300 fn alloc_file_id(&mut self, path: VfsPath) -> FileId {
301 let file_id = self.interner.intern(path);
302 let idx = file_id.0 as usize;
303 let len = self.data.len().max(idx + 1);
304 self.data.resize(len, FileState::Deleted);
305 file_id
306 }
307
308 /// Returns the status of the file associated with the given `file_id`.
309 ///
310 /// # Panics
311 ///
312 /// Panics if no file is associated to that id.
313 fn get(&self, file_id: FileId) -> FileState {
314 self.data[file_id.0 as usize]
315 }
316
317 /// We cannot ignore excluded files, because this will lead to errors when the client
318 /// requests semantic information for them, so we instead mark them specially.
319 pub fn insert_excluded_file(&mut self, path: VfsPath) {
320 let file_id = self.alloc_file_id(path);
321 self.data[file_id.0 as usize] = FileState::Excluded;
322 }
323}
324
325impl fmt::Debug for Vfs {
326 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
327 f.debug_struct("Vfs").field("n_files", &self.data.len()).finish()
328 }
329}