Files
rust/crates/ra_lsp_server/src/path_map.rs

126 lines
3.2 KiB
Rust
Raw Normal View History

use std::{
fmt,
path::{Component, Path, PathBuf},
};
2018-08-15 17:24:20 +03:00
use im;
2018-09-16 12:54:24 +03:00
use ra_analysis::{FileId, FileResolver};
use relative_path::RelativePath;
2018-09-04 11:40:45 +03:00
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Root {
Workspace,
Lib,
2018-09-04 11:40:45 +03:00
}
#[derive(Default, Clone)]
2018-08-15 17:24:20 +03:00
pub struct PathMap {
next_id: u32,
path2id: im::HashMap<PathBuf, FileId>,
id2path: im::HashMap<FileId, PathBuf>,
2018-09-04 11:40:45 +03:00
id2root: im::HashMap<FileId, Root>,
2018-08-15 17:24:20 +03:00
}
impl fmt::Debug for PathMap {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("PathMap { ... }")
}
}
2018-08-15 17:24:20 +03:00
impl PathMap {
pub fn new() -> PathMap {
Default::default()
}
pub fn get_or_insert(&mut self, path: PathBuf, root: Root) -> (bool, FileId) {
let mut inserted = false;
2018-10-31 23:41:43 +03:00
let file_id = self
.path2id
.get(path.as_path())
2018-08-15 17:24:20 +03:00
.map(|&id| id)
.unwrap_or_else(|| {
inserted = true;
2018-08-15 17:24:20 +03:00
let id = self.new_file_id();
2018-09-04 11:40:45 +03:00
self.insert(path, id, root);
2018-08-15 17:24:20 +03:00
id
});
(inserted, file_id)
2018-08-15 17:24:20 +03:00
}
pub fn get_id(&self, path: &Path) -> Option<FileId> {
self.path2id.get(path).map(|&id| id)
}
2018-09-04 11:40:45 +03:00
pub fn get_path(&self, file_id: FileId) -> &Path {
self.id2path.get(&file_id).unwrap().as_path()
2018-08-15 17:24:20 +03:00
}
2018-09-04 11:40:45 +03:00
pub fn get_root(&self, file_id: FileId) -> Root {
self.id2root[&file_id]
}
fn insert(&mut self, path: PathBuf, file_id: FileId, root: Root) {
self.path2id.insert(path.clone(), file_id);
self.id2path.insert(file_id, path.clone());
self.id2root.insert(file_id, root);
2018-08-15 17:24:20 +03:00
}
fn new_file_id(&mut self) -> FileId {
let id = FileId(self.next_id);
self.next_id += 1;
id
}
}
2018-08-17 15:37:17 +03:00
2018-08-28 18:22:52 +03:00
impl FileResolver for PathMap {
2018-09-04 11:40:45 +03:00
fn file_stem(&self, file_id: FileId) -> String {
self.get_path(file_id)
.file_stem()
.unwrap()
.to_str()
.unwrap()
.to_string()
2018-08-28 18:22:52 +03:00
}
2018-09-04 11:40:45 +03:00
fn resolve(&self, file_id: FileId, path: &RelativePath) -> Option<FileId> {
let path = path.to_path(&self.get_path(file_id));
2018-08-28 18:22:52 +03:00
let path = normalize(&path);
self.get_id(&path)
}
}
2018-08-17 15:37:17 +03:00
fn normalize(path: &Path) -> PathBuf {
let mut components = path.components().peekable();
let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
components.next();
PathBuf::from(c.as_os_str())
} else {
PathBuf::new()
};
for component in components {
match component {
Component::Prefix(..) => unreachable!(),
Component::RootDir => {
ret.push(component.as_os_str());
}
Component::CurDir => {}
Component::ParentDir => {
ret.pop();
}
Component::Normal(c) => {
ret.push(c);
}
}
}
ret
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_resolve() {
let mut m = PathMap::new();
let (_, id1) = m.get_or_insert(PathBuf::from("/foo"), Root::Workspace);
let (_, id2) = m.get_or_insert(PathBuf::from("/foo/bar.rs"), Root::Workspace);
assert_eq!(m.resolve(id1, &RelativePath::new("bar.rs")), Some(id2),)
2018-08-17 15:37:17 +03:00
}
}