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.rs26
1 files changed, 19 insertions, 7 deletions
diff --git a/crates/ra_lsp_server/src/path_map.rs b/crates/ra_lsp_server/src/path_map.rs
index d32829382..d5957d673 100644
--- a/crates/ra_lsp_server/src/path_map.rs
+++ b/crates/ra_lsp_server/src/path_map.rs
@@ -1,4 +1,7 @@
1use std::path::{Component, Path, PathBuf}; 1use std::{
2 fmt,
3 path::{Component, Path, PathBuf},
4};
2 5
3use im; 6use im;
4use ra_analysis::{FileId, FileResolver}; 7use ra_analysis::{FileId, FileResolver};
@@ -10,7 +13,7 @@ pub enum Root {
10 Lib, 13 Lib,
11} 14}
12 15
13#[derive(Debug, Default, Clone)] 16#[derive(Default, Clone)]
14pub struct PathMap { 17pub struct PathMap {
15 next_id: u32, 18 next_id: u32,
16 path2id: im::HashMap<PathBuf, FileId>, 19 path2id: im::HashMap<PathBuf, FileId>,
@@ -18,19 +21,28 @@ pub struct PathMap {
18 id2root: im::HashMap<FileId, Root>, 21 id2root: im::HashMap<FileId, Root>,
19} 22}
20 23
24impl fmt::Debug for PathMap {
25 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
26 f.write_str("PathMap { ... }")
27 }
28}
29
21impl PathMap { 30impl PathMap {
22 pub fn new() -> PathMap { 31 pub fn new() -> PathMap {
23 Default::default() 32 Default::default()
24 } 33 }
25 pub fn get_or_insert(&mut self, path: PathBuf, root: Root) -> FileId { 34 pub fn get_or_insert(&mut self, path: PathBuf, root: Root) -> (bool, FileId) {
26 self.path2id 35 let mut inserted = false;
36 let file_id = self.path2id
27 .get(path.as_path()) 37 .get(path.as_path())
28 .map(|&id| id) 38 .map(|&id| id)
29 .unwrap_or_else(|| { 39 .unwrap_or_else(|| {
40 inserted = true;
30 let id = self.new_file_id(); 41 let id = self.new_file_id();
31 self.insert(path, id, root); 42 self.insert(path, id, root);
32 id 43 id
33 }) 44 });
45 (inserted, file_id)
34 } 46 }
35 pub fn get_id(&self, path: &Path) -> Option<FileId> { 47 pub fn get_id(&self, path: &Path) -> Option<FileId> {
36 self.path2id.get(path).map(|&id| id) 48 self.path2id.get(path).map(|&id| id)
@@ -105,8 +117,8 @@ mod test {
105 #[test] 117 #[test]
106 fn test_resolve() { 118 fn test_resolve() {
107 let mut m = PathMap::new(); 119 let mut m = PathMap::new();
108 let id1 = m.get_or_insert(PathBuf::from("/foo"), Root::Workspace); 120 let (_, id1) = m.get_or_insert(PathBuf::from("/foo"), Root::Workspace);
109 let id2 = m.get_or_insert(PathBuf::from("/foo/bar.rs"), Root::Workspace); 121 let (_, id2) = m.get_or_insert(PathBuf::from("/foo/bar.rs"), Root::Workspace);
110 assert_eq!(m.resolve(id1, &RelativePath::new("bar.rs")), Some(id2),) 122 assert_eq!(m.resolve(id1, &RelativePath::new("bar.rs")), Some(id2),)
111 } 123 }
112} 124}