mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 20:42:04 +00:00
New VFS API
This commit is contained in:
parent
db6100dbaa
commit
c002322bde
8 changed files with 520 additions and 0 deletions
49
crates/vfs/src/vfs_path.rs
Normal file
49
crates/vfs/src/vfs_path.rs
Normal file
|
@ -0,0 +1,49 @@
|
|||
//! Abstract-ish representation of paths for VFS.
|
||||
use std::fmt;
|
||||
|
||||
use paths::{AbsPath, AbsPathBuf};
|
||||
|
||||
/// Long-term, we want to support files which do not reside in the file-system,
|
||||
/// so we treat VfsPaths as opaque identifiers.
|
||||
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
|
||||
pub struct VfsPath(VfsPathRepr);
|
||||
|
||||
impl VfsPath {
|
||||
pub fn as_path(&self) -> Option<&AbsPath> {
|
||||
match &self.0 {
|
||||
VfsPathRepr::PathBuf(it) => Some(it.as_path()),
|
||||
}
|
||||
}
|
||||
pub fn join(&self, path: &str) -> VfsPath {
|
||||
match &self.0 {
|
||||
VfsPathRepr::PathBuf(it) => {
|
||||
let res = it.join(path).normalize();
|
||||
VfsPath(VfsPathRepr::PathBuf(res))
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn pop(&mut self) -> bool {
|
||||
match &mut self.0 {
|
||||
VfsPathRepr::PathBuf(it) => it.pop(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
|
||||
enum VfsPathRepr {
|
||||
PathBuf(AbsPathBuf),
|
||||
}
|
||||
|
||||
impl From<AbsPathBuf> for VfsPath {
|
||||
fn from(v: AbsPathBuf) -> Self {
|
||||
VfsPath(VfsPathRepr::PathBuf(v))
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for VfsPath {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match &self.0 {
|
||||
VfsPathRepr::PathBuf(it) => fmt::Display::fmt(&it.display(), f),
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue