1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
use std::path::{PathBuf, Path, Component};
use im;
use relative_path::RelativePath;
use libanalysis::{FileId, FileResolver};
#[derive(Debug, Default, Clone)]
pub struct PathMap {
next_id: u32,
path2id: im::HashMap<PathBuf, FileId>,
id2path: im::HashMap<FileId, PathBuf>,
}
impl PathMap {
pub fn new() -> PathMap {
Default::default()
}
pub fn get_or_insert(&mut self, path: PathBuf) -> FileId {
self.path2id.get(path.as_path())
.map(|&id| id)
.unwrap_or_else(|| {
let id = self.new_file_id();
self.insert(path, id);
id
})
}
pub fn get_id(&self, path: &Path) -> Option<FileId> {
self.path2id.get(path).map(|&id| id)
}
pub fn get_path(&self, id: FileId) -> &Path {
self.id2path.get(&id)
.unwrap()
.as_path()
}
fn insert(&mut self, path: PathBuf, id: FileId) {
self.path2id.insert(path.clone(), id);
self.id2path.insert(id, path.clone());
}
fn new_file_id(&mut self) -> FileId {
let id = FileId(self.next_id);
self.next_id += 1;
id
}
}
impl FileResolver for PathMap {
fn file_stem(&self, id: FileId) -> String {
self.get_path(id).file_stem().unwrap().to_str().unwrap().to_string()
}
fn resolve(&self, id: FileId, path: &RelativePath) -> Option<FileId> {
let path = path.to_path(&self.get_path(id));
let path = normalize(&path);
self.get_id(&path)
}
}
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"));
let id2 = m.get_or_insert(PathBuf::from("/foo/bar.rs"));
assert_eq!(
m.resolve(id1, &RelativePath::new("bar.rs")),
Some(id2),
)
}
}
|