diff options
author | Aleksey Kladov <[email protected]> | 2018-12-19 12:04:15 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-12-20 09:15:38 +0000 |
commit | a5ef8ad05b7c1f7148c59814b55d641fd75aff75 (patch) | |
tree | df6c46378d81dc7cb3242ff7f57d836353bde5ed /crates/ra_lsp_server/src/path_map.rs | |
parent | 6a755ed83a583d1f70a5fbcff2d4933b52628cfe (diff) |
swtich lsp server to vfs
Diffstat (limited to 'crates/ra_lsp_server/src/path_map.rs')
-rw-r--r-- | crates/ra_lsp_server/src/path_map.rs | 105 |
1 files changed, 0 insertions, 105 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 86cf29540..000000000 --- a/crates/ra_lsp_server/src/path_map.rs +++ /dev/null | |||
@@ -1,105 +0,0 @@ | |||
1 | use std::{ | ||
2 | fmt, | ||
3 | path::{Component, Path, PathBuf}, | ||
4 | }; | ||
5 | |||
6 | use im; | ||
7 | use ra_analysis::{FileId}; | ||
8 | use relative_path::RelativePath; | ||
9 | |||
10 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
11 | pub enum Root { | ||
12 | Workspace, | ||
13 | Lib, | ||
14 | } | ||
15 | |||
16 | #[derive(Default, Clone)] | ||
17 | pub 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 | |||
24 | impl fmt::Debug for PathMap { | ||
25 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
26 | f.write_str("PathMap { ... }") | ||
27 | } | ||
28 | } | ||
29 | |||
30 | impl 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 | |||
67 | fn normalize(path: &Path) -> PathBuf { | ||
68 | let mut components = path.components().peekable(); | ||
69 | let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() { | ||
70 | components.next(); | ||
71 | PathBuf::from(c.as_os_str()) | ||
72 | } else { | ||
73 | PathBuf::new() | ||
74 | }; | ||
75 | |||
76 | for component in components { | ||
77 | match component { | ||
78 | Component::Prefix(..) => unreachable!(), | ||
79 | Component::RootDir => { | ||
80 | ret.push(component.as_os_str()); | ||
81 | } | ||
82 | Component::CurDir => {} | ||
83 | Component::ParentDir => { | ||
84 | ret.pop(); | ||
85 | } | ||
86 | Component::Normal(c) => { | ||
87 | ret.push(c); | ||
88 | } | ||
89 | } | ||
90 | } | ||
91 | ret | ||
92 | } | ||
93 | |||
94 | #[cfg(test)] | ||
95 | mod test { | ||
96 | use super::*; | ||
97 | |||
98 | #[test] | ||
99 | fn test_resolve() { | ||
100 | let mut m = PathMap::default(); | ||
101 | let (_, id1) = m.get_or_insert(PathBuf::from("/foo"), Root::Workspace); | ||
102 | let (_, id2) = m.get_or_insert(PathBuf::from("/foo/bar.rs"), Root::Workspace); | ||
103 | assert_eq!(m.resolve(id1, &RelativePath::new("bar.rs")), Some(id2),) | ||
104 | } | ||
105 | } | ||