rust/crates/vfs/src/lib.rs

219 lines
7.5 KiB
Rust
Raw Normal View History

2020-06-15 06:29:07 -05:00
//! # Virtual File System
//!
//! VFS stores all files read by rust-analyzer. Reading file contents from VFS
//! always returns the same contents, unless VFS was explicitly modified with
//! [`set_file_contents`]. All changes to VFS are logged, and can be retrieved via
//! [`take_changes`] method. The pack of changes is then pushed to `salsa` and
2020-06-15 06:29:07 -05:00
//! triggers incremental recomputation.
//!
//! Files in VFS are identified with [`FileId`]s -- interned paths. The notion of
//! the path, [`VfsPath`] is somewhat abstract: at the moment, it is represented
//! as an [`std::path::PathBuf`] internally, but this is an implementation detail.
2020-06-15 06:29:07 -05:00
//!
//! VFS doesn't do IO or file watching itself. For that, see the [`loader`]
//! module. [`loader::Handle`] is an object-safe trait which abstracts both file
//! loading and file watching. [`Handle`] is dynamically configured with a set of
//! directory entries which should be scanned and watched. [`Handle`] then
2020-06-15 06:29:07 -05:00
//! asynchronously pushes file changes. Directory entries are configured in
//! free-form via list of globs, it's up to the [`Handle`] to interpret the globs
2020-06-15 06:29:07 -05:00
//! in any specific way.
//!
//! VFS stores a flat list of files. [`file_set::FileSet`] can partition this list
//! of files into disjoint sets of files. Traversal-like operations (including
//! getting the neighbor file by the relative path) are handled by the [`FileSet`].
//! [`FileSet`]s are also pushed to salsa and cause it to re-check `mod foo;`
2020-06-15 06:29:07 -05:00
//! declarations when files are created or deleted.
//!
//! [`FileSet`] and [`loader::Entry`] play similar, but different roles.
2020-06-15 06:29:07 -05:00
//! Both specify the "set of paths/files", one is geared towards file watching,
//! the other towards salsa changes. In particular, single [`FileSet`]
//! may correspond to several [`loader::Entry`]. For example, a crate from
//! crates.io which uses code generation would have two [`Entries`] -- for sources
2020-06-15 06:29:07 -05:00
//! in `~/.cargo`, and for generated code in `./target/debug/build`. It will
//! have a single [`FileSet`] which unions the two sources.
//!
//! [`set_file_contents`]: Vfs::set_file_contents
//! [`take_changes`]: Vfs::take_changes
//! [`FileSet`]: file_set::FileSet
//! [`Handle`]: loader::Handle
//! [`Entries`]: loader::Entry
2020-12-09 09:41:35 -06:00
mod anchored_path;
2020-06-15 06:29:07 -05:00
pub mod file_set;
pub mod loader;
mod path_interner;
mod vfs_path;
2020-06-15 06:29:07 -05:00
use std::{fmt, mem};
use crate::path_interner::PathInterner;
2020-12-09 09:41:35 -06:00
pub use crate::{
anchored_path::{AnchoredPath, AnchoredPathBuf},
vfs_path::VfsPath,
};
2020-06-15 06:29:07 -05:00
pub use paths::{AbsPath, AbsPathBuf};
2021-01-12 10:22:57 -06:00
/// Handle to a file in [`Vfs`]
///
/// Most functions in rust-analyzer use this when they need to refer to a file.
2020-06-15 06:29:07 -05:00
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct FileId(pub u32);
2021-01-12 10:22:57 -06:00
/// Storage for all files read by rust-analyzer.
///
/// For more informations see the [crate-level](crate) documentation.
2020-06-15 06:29:07 -05:00
#[derive(Default)]
pub struct Vfs {
interner: PathInterner,
data: Vec<Option<Vec<u8>>>,
changes: Vec<ChangedFile>,
}
2021-01-12 10:22:57 -06:00
/// Changed file in the [`Vfs`].
2020-06-15 06:29:07 -05:00
pub struct ChangedFile {
2021-01-12 10:22:57 -06:00
/// Id of the changed file
2020-06-15 06:29:07 -05:00
pub file_id: FileId,
2021-01-12 10:22:57 -06:00
/// Kind of change
2020-06-15 06:29:07 -05:00
pub change_kind: ChangeKind,
}
impl ChangedFile {
2021-01-12 10:22:57 -06:00
/// Returns `true` if the change is not [`Delete`](ChangeKind::Delete).
2020-06-15 06:29:07 -05:00
pub fn exists(&self) -> bool {
self.change_kind != ChangeKind::Delete
}
2021-01-12 10:22:57 -06:00
/// Returns `true` if the change is [`Create`](ChangeKind::Create) or
/// [`Delete`](ChangeKind::Delete).
2020-06-15 06:29:07 -05:00
pub fn is_created_or_deleted(&self) -> bool {
matches!(self.change_kind, ChangeKind::Create | ChangeKind::Delete)
}
}
2021-01-12 10:22:57 -06:00
/// Kind of [file change](ChangedFile).
2020-07-10 15:29:40 -05:00
#[derive(Eq, PartialEq, Copy, Clone, Debug)]
2020-06-15 06:29:07 -05:00
pub enum ChangeKind {
2021-01-12 10:22:57 -06:00
/// The file was (re-)created
2020-06-15 06:29:07 -05:00
Create,
2021-01-12 10:22:57 -06:00
/// The file was modified
2020-06-15 06:29:07 -05:00
Modify,
2021-01-12 10:22:57 -06:00
/// The file was deleted
2020-06-15 06:29:07 -05:00
Delete,
}
impl Vfs {
2021-01-12 10:22:57 -06:00
/// Amount of files currently stored.
///
/// Note that this includes deleted files.
pub fn len(&self) -> usize {
self.data.len()
}
2021-01-12 10:22:57 -06:00
/// Id of the given path if it exists in the `Vfs` and is not deleted.
2020-06-15 06:29:07 -05:00
pub fn file_id(&self, path: &VfsPath) -> Option<FileId> {
self.interner.get(path).filter(|&it| self.get(it).is_some())
}
2021-01-12 10:22:57 -06:00
/// File path corresponding to the given `file_id`.
///
/// # Panics
///
/// Panics if the id is not present in the `Vfs`.
2020-06-15 06:29:07 -05:00
pub fn file_path(&self, file_id: FileId) -> VfsPath {
self.interner.lookup(file_id).clone()
}
2021-01-12 10:22:57 -06:00
/// File content corresponding to the given `file_id`.
///
/// # Panics
///
/// Panics if the id is not present in the `Vfs`, or if the corresponding file is
/// deleted.
2020-06-15 06:29:07 -05:00
pub fn file_contents(&self, file_id: FileId) -> &[u8] {
self.get(file_id).as_deref().unwrap()
}
2021-01-12 10:22:57 -06:00
/// Returns an iterator over the stored ids and their corresponding paths.
///
/// This will skip deleted files.
2020-07-14 08:57:10 -05:00
pub fn iter(&self) -> impl Iterator<Item = (FileId, &VfsPath)> + '_ {
2020-06-15 06:29:07 -05:00
(0..self.data.len())
.map(|it| FileId(it as u32))
.filter(move |&file_id| self.get(file_id).is_some())
.map(move |file_id| {
2020-07-14 08:57:10 -05:00
let path = self.interner.lookup(file_id);
2020-06-15 06:29:07 -05:00
(file_id, path)
})
}
2021-01-12 10:22:57 -06:00
/// Update the `path` with the given `contents`. `None` means the file was deleted.
///
/// Returns `true` if the file was modified, and saves the [change](ChangedFile).
///
/// If the path does not currently exists in the `Vfs`, allocates a new
/// [`FileId`] for it.
pub fn set_file_contents(&mut self, path: VfsPath, contents: Option<Vec<u8>>) -> bool {
2020-06-15 06:29:07 -05:00
let file_id = self.alloc_file_id(path);
let change_kind = match (&self.get(file_id), &contents) {
(None, None) => return false,
2020-06-15 06:29:07 -05:00
(None, Some(_)) => ChangeKind::Create,
(Some(_), None) => ChangeKind::Delete,
(Some(old), Some(new)) if old == new => return false,
2020-06-15 06:29:07 -05:00
(Some(_), Some(_)) => ChangeKind::Modify,
};
*self.get_mut(file_id) = contents;
self.changes.push(ChangedFile { file_id, change_kind });
true
2020-06-15 06:29:07 -05:00
}
2021-01-12 10:22:57 -06:00
/// Returns `true` if the `Vfs` contains [changes](ChangedFile).
2020-06-15 06:29:07 -05:00
pub fn has_changes(&self) -> bool {
!self.changes.is_empty()
}
2021-01-12 10:22:57 -06:00
/// Drain and returns all the changes in the `Vfs`.
2020-06-15 06:29:07 -05:00
pub fn take_changes(&mut self) -> Vec<ChangedFile> {
mem::take(&mut self.changes)
}
2021-01-12 10:41:45 -06:00
/// Returns the id associated with `path`
///
/// - If `path` does not exists in the `Vfs`, allocate a new id for it, associated with a
/// deleted file;
/// - Else, returns `path`'s id.
///
/// Does not record a change.
2020-06-15 06:29:07 -05:00
fn alloc_file_id(&mut self, path: VfsPath) -> FileId {
let file_id = self.interner.intern(path);
let idx = file_id.0 as usize;
let len = self.data.len().max(idx + 1);
self.data.resize_with(len, || None);
file_id
}
2021-01-12 10:41:45 -06:00
/// Returns the content associated with the given `file_id`.
///
/// # Panics
///
/// Panics if no file is associated to that id.
2020-06-15 06:29:07 -05:00
fn get(&self, file_id: FileId) -> &Option<Vec<u8>> {
&self.data[file_id.0 as usize]
}
2021-01-12 10:41:45 -06:00
/// Mutably returns the content associated with the given `file_id`.
///
/// # Panics
///
/// Panics if no file is associated to that id.
2020-06-15 06:29:07 -05:00
fn get_mut(&mut self, file_id: FileId) -> &mut Option<Vec<u8>> {
&mut self.data[file_id.0 as usize]
}
}
impl fmt::Debug for Vfs {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Vfs").field("n_files", &self.data.len()).finish()
}
}