diff options
author | Aleksey Kladov <[email protected]> | 2018-08-17 13:37:17 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-08-17 13:37:17 +0100 |
commit | 081c16c77642a5c86ed72c5fbd11deccc2edd5d5 (patch) | |
tree | 90ddf4f92954fcb00f4c20a46968932c5f0bbdea /crates/server/src | |
parent | 55e87e0b742b46d40b1a5ef1598804e48e45f0e0 (diff) |
initial mod resolve
Diffstat (limited to 'crates/server/src')
-rw-r--r-- | crates/server/src/path_map.rs | 47 |
1 files changed, 46 insertions, 1 deletions
diff --git a/crates/server/src/path_map.rs b/crates/server/src/path_map.rs index e198e165d..d2b811a3b 100644 --- a/crates/server/src/path_map.rs +++ b/crates/server/src/path_map.rs | |||
@@ -1,4 +1,4 @@ | |||
1 | use std::path::{PathBuf, Path}; | 1 | use std::path::{PathBuf, Path, Component}; |
2 | use im; | 2 | use im; |
3 | use libanalysis::{FileId}; | 3 | use libanalysis::{FileId}; |
4 | 4 | ||
@@ -36,6 +36,7 @@ impl PathMap { | |||
36 | 36 | ||
37 | pub fn resolve(&self, id: FileId, relpath: &Path) -> Option<FileId> { | 37 | pub fn resolve(&self, id: FileId, relpath: &Path) -> Option<FileId> { |
38 | let path = self.get_path(id).join(relpath); | 38 | let path = self.get_path(id).join(relpath); |
39 | let path = normalize(&path); | ||
39 | self.get_id(&path) | 40 | self.get_id(&path) |
40 | } | 41 | } |
41 | 42 | ||
@@ -50,3 +51,47 @@ impl PathMap { | |||
50 | id | 51 | id |
51 | } | 52 | } |
52 | } | 53 | } |
54 | |||
55 | fn normalize(path: &Path) -> PathBuf { | ||
56 | let mut components = path.components().peekable(); | ||
57 | let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() { | ||
58 | components.next(); | ||
59 | PathBuf::from(c.as_os_str()) | ||
60 | } else { | ||
61 | PathBuf::new() | ||
62 | }; | ||
63 | |||
64 | for component in components { | ||
65 | match component { | ||
66 | Component::Prefix(..) => unreachable!(), | ||
67 | Component::RootDir => { | ||
68 | ret.push(component.as_os_str()); | ||
69 | } | ||
70 | Component::CurDir => {} | ||
71 | Component::ParentDir => { | ||
72 | ret.pop(); | ||
73 | } | ||
74 | Component::Normal(c) => { | ||
75 | ret.push(c); | ||
76 | } | ||
77 | } | ||
78 | } | ||
79 | ret | ||
80 | } | ||
81 | |||
82 | #[cfg(test)] | ||
83 | mod test { | ||
84 | use super::*; | ||
85 | |||
86 | #[test] | ||
87 | fn test_resolve() { | ||
88 | let mut m = PathMap::new(); | ||
89 | let id1 = m.get_or_insert(PathBuf::from("/foo")); | ||
90 | let id2 = m.get_or_insert(PathBuf::from("/foo/bar.rs")); | ||
91 | assert_eq!( | ||
92 | m.resolve(id1, &PathBuf::from("bar.rs")), | ||
93 | Some(id2), | ||
94 | ) | ||
95 | } | ||
96 | } | ||
97 | |||