aboutsummaryrefslogtreecommitdiff
path: root/crates/server/src/path_map.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/server/src/path_map.rs')
-rw-r--r--crates/server/src/path_map.rs23
1 files changed, 15 insertions, 8 deletions
diff --git a/crates/server/src/path_map.rs b/crates/server/src/path_map.rs
index d2b811a3b..f4ac47e70 100644
--- a/crates/server/src/path_map.rs
+++ b/crates/server/src/path_map.rs
@@ -1,6 +1,7 @@
1use std::path::{PathBuf, Path, Component}; 1use std::path::{PathBuf, Path, Component};
2use im; 2use im;
3use libanalysis::{FileId}; 3use relative_path::RelativePath;
4use libanalysis::{FileId, FileResolver};
4 5
5#[derive(Debug, Default, Clone)] 6#[derive(Debug, Default, Clone)]
6pub struct PathMap { 7pub struct PathMap {
@@ -34,12 +35,6 @@ impl PathMap {
34 .as_path() 35 .as_path()
35 } 36 }
36 37
37 pub fn resolve(&self, id: FileId, relpath: &Path) -> Option<FileId> {
38 let path = self.get_path(id).join(relpath);
39 let path = normalize(&path);
40 self.get_id(&path)
41 }
42
43 fn insert(&mut self, path: PathBuf, id: FileId) { 38 fn insert(&mut self, path: PathBuf, id: FileId) {
44 self.path2id.insert(path.clone(), id); 39 self.path2id.insert(path.clone(), id);
45 self.id2path.insert(id, path.clone()); 40 self.id2path.insert(id, path.clone());
@@ -52,6 +47,18 @@ impl PathMap {
52 } 47 }
53} 48}
54 49
50impl FileResolver for PathMap {
51 fn file_stem(&self, id: FileId) -> String {
52 self.get_path(id).file_stem().unwrap().to_str().unwrap().to_string()
53 }
54
55 fn resolve(&self, id: FileId, path: &RelativePath) -> Option<FileId> {
56 let path = path.to_path(&self.get_path(id));
57 let path = normalize(&path);
58 self.get_id(&path)
59 }
60}
61
55fn normalize(path: &Path) -> PathBuf { 62fn normalize(path: &Path) -> PathBuf {
56 let mut components = path.components().peekable(); 63 let mut components = path.components().peekable();
57 let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() { 64 let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
@@ -89,7 +96,7 @@ mod test {
89 let id1 = m.get_or_insert(PathBuf::from("/foo")); 96 let id1 = m.get_or_insert(PathBuf::from("/foo"));
90 let id2 = m.get_or_insert(PathBuf::from("/foo/bar.rs")); 97 let id2 = m.get_or_insert(PathBuf::from("/foo/bar.rs"));
91 assert_eq!( 98 assert_eq!(
92 m.resolve(id1, &PathBuf::from("bar.rs")), 99 m.resolve(id1, &RelativePath::new("bar.rs")),
93 Some(id2), 100 Some(id2),
94 ) 101 )
95 } 102 }