aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_lsp_server/src/path_map.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_lsp_server/src/path_map.rs')
-rw-r--r--crates/ra_lsp_server/src/path_map.rs126
1 files changed, 0 insertions, 126 deletions
diff --git a/crates/ra_lsp_server/src/path_map.rs b/crates/ra_lsp_server/src/path_map.rs
deleted file mode 100644
index 02e54629c..000000000
--- a/crates/ra_lsp_server/src/path_map.rs
+++ /dev/null
@@ -1,126 +0,0 @@
1use std::{
2 fmt,
3 path::{Component, Path, PathBuf},
4};
5
6use im;
7use ra_analysis::{FileId, FileResolver};
8use relative_path::RelativePath;
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub enum Root {
12 Workspace,
13 Lib,
14}
15
16#[derive(Default, Clone)]
17pub struct PathMap {
18 next_id: u32,
19 path2id: im::HashMap<PathBuf, FileId>,
20 id2path: im::HashMap<FileId, PathBuf>,
21 id2root: im::HashMap<FileId, Root>,
22}
23
24impl fmt::Debug for PathMap {
25 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
26 f.write_str("PathMap { ... }")
27 }
28}
29
30impl PathMap {
31 pub fn get_or_insert(&mut self, path: PathBuf, root: Root) -> (bool, FileId) {
32 let mut inserted = false;
33 let file_id = self
34 .path2id
35 .get(path.as_path())
36 .map(|&id| id)
37 .unwrap_or_else(|| {
38 inserted = true;
39 let id = self.new_file_id();
40 self.insert(path, id, root);
41 id
42 });
43 (inserted, file_id)
44 }
45 pub fn get_id(&self, path: &Path) -> Option<FileId> {
46 self.path2id.get(path).cloned()
47 }
48 pub fn get_path(&self, file_id: FileId) -> &Path {
49 self.id2path.get(&file_id).unwrap().as_path()
50 }
51 pub fn get_root(&self, file_id: FileId) -> Root {
52 self.id2root[&file_id]
53 }
54 fn insert(&mut self, path: PathBuf, file_id: FileId, root: Root) {
55 self.path2id.insert(path.clone(), file_id);
56 self.id2path.insert(file_id, path.clone());
57 self.id2root.insert(file_id, root);
58 }
59
60 fn new_file_id(&mut self) -> FileId {
61 let id = FileId(self.next_id);
62 self.next_id += 1;
63 id
64 }
65}
66
67impl FileResolver for PathMap {
68 fn file_stem(&self, file_id: FileId) -> String {
69 self.get_path(file_id)
70 .file_stem()
71 .unwrap()
72 .to_str()
73 .unwrap()
74 .to_string()
75 }
76
77 fn resolve(&self, file_id: FileId, path: &RelativePath) -> Option<FileId> {
78 let path = path.to_path(&self.get_path(file_id));
79 let path = normalize(&path);
80 self.get_id(&path)
81 }
82
83 fn debug_path(&self, file_id: FileId) -> Option<PathBuf> {
84 Some(self.get_path(file_id).to_owned())
85 }
86}
87
88fn normalize(path: &Path) -> PathBuf {
89 let mut components = path.components().peekable();
90 let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
91 components.next();
92 PathBuf::from(c.as_os_str())
93 } else {
94 PathBuf::new()
95 };
96
97 for component in components {
98 match component {
99 Component::Prefix(..) => unreachable!(),
100 Component::RootDir => {
101 ret.push(component.as_os_str());
102 }
103 Component::CurDir => {}
104 Component::ParentDir => {
105 ret.pop();
106 }
107 Component::Normal(c) => {
108 ret.push(c);
109 }
110 }
111 }
112 ret
113}
114
115#[cfg(test)]
116mod test {
117 use super::*;
118
119 #[test]
120 fn test_resolve() {
121 let mut m = PathMap::default();
122 let (_, id1) = m.get_or_insert(PathBuf::from("/foo"), Root::Workspace);
123 let (_, id2) = m.get_or_insert(PathBuf::from("/foo/bar.rs"), Root::Workspace);
124 assert_eq!(m.resolve(id1, &RelativePath::new("bar.rs")), Some(id2),)
125 }
126}