diff options
author | Aleksey Kladov <[email protected]> | 2018-09-04 09:40:45 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-09-04 09:40:45 +0100 |
commit | 3a017aaa52fc41316b5dd2cd90b5171ca869697a (patch) | |
tree | 5017726e5ff94bfdb769bb31ff7ec5a7b8270f57 /crates/server/src/path_map.rs | |
parent | a668f703fa5361f59a170d40be667c7e59a4a3e5 (diff) |
dont change readonly files
Diffstat (limited to 'crates/server/src/path_map.rs')
-rw-r--r-- | crates/server/src/path_map.rs | 36 |
1 files changed, 21 insertions, 15 deletions
diff --git a/crates/server/src/path_map.rs b/crates/server/src/path_map.rs index f4ac47e70..de904e9db 100644 --- a/crates/server/src/path_map.rs +++ b/crates/server/src/path_map.rs | |||
@@ -3,41 +3,47 @@ use im; | |||
3 | use relative_path::RelativePath; | 3 | use relative_path::RelativePath; |
4 | use libanalysis::{FileId, FileResolver}; | 4 | use libanalysis::{FileId, FileResolver}; |
5 | 5 | ||
6 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
7 | pub enum Root { | ||
8 | Workspace, Lib | ||
9 | } | ||
10 | |||
6 | #[derive(Debug, Default, Clone)] | 11 | #[derive(Debug, Default, Clone)] |
7 | pub struct PathMap { | 12 | pub struct PathMap { |
8 | next_id: u32, | 13 | next_id: u32, |
9 | path2id: im::HashMap<PathBuf, FileId>, | 14 | path2id: im::HashMap<PathBuf, FileId>, |
10 | id2path: im::HashMap<FileId, PathBuf>, | 15 | id2path: im::HashMap<FileId, PathBuf>, |
16 | id2root: im::HashMap<FileId, Root>, | ||
11 | } | 17 | } |
12 | 18 | ||
13 | impl PathMap { | 19 | impl PathMap { |
14 | pub fn new() -> PathMap { | 20 | pub fn new() -> PathMap { |
15 | Default::default() | 21 | Default::default() |
16 | } | 22 | } |
17 | 23 | pub fn get_or_insert(&mut self, path: PathBuf, root: Root) -> FileId { | |
18 | pub fn get_or_insert(&mut self, path: PathBuf) -> FileId { | ||
19 | self.path2id.get(path.as_path()) | 24 | self.path2id.get(path.as_path()) |
20 | .map(|&id| id) | 25 | .map(|&id| id) |
21 | .unwrap_or_else(|| { | 26 | .unwrap_or_else(|| { |
22 | let id = self.new_file_id(); | 27 | let id = self.new_file_id(); |
23 | self.insert(path, id); | 28 | self.insert(path, id, root); |
24 | id | 29 | id |
25 | }) | 30 | }) |
26 | } | 31 | } |
27 | |||
28 | pub fn get_id(&self, path: &Path) -> Option<FileId> { | 32 | pub fn get_id(&self, path: &Path) -> Option<FileId> { |
29 | self.path2id.get(path).map(|&id| id) | 33 | self.path2id.get(path).map(|&id| id) |
30 | } | 34 | } |
31 | 35 | pub fn get_path(&self, file_id: FileId) -> &Path { | |
32 | pub fn get_path(&self, id: FileId) -> &Path { | 36 | self.id2path.get(&file_id) |
33 | self.id2path.get(&id) | ||
34 | .unwrap() | 37 | .unwrap() |
35 | .as_path() | 38 | .as_path() |
36 | } | 39 | } |
37 | 40 | pub fn get_root(&self, file_id: FileId) -> Root { | |
38 | fn insert(&mut self, path: PathBuf, id: FileId) { | 41 | self.id2root[&file_id] |
39 | self.path2id.insert(path.clone(), id); | 42 | } |
40 | self.id2path.insert(id, path.clone()); | 43 | fn insert(&mut self, path: PathBuf, file_id: FileId, root: Root) { |
44 | self.path2id.insert(path.clone(), file_id); | ||
45 | self.id2path.insert(file_id, path.clone()); | ||
46 | self.id2root.insert(file_id, root); | ||
41 | } | 47 | } |
42 | 48 | ||
43 | fn new_file_id(&mut self) -> FileId { | 49 | fn new_file_id(&mut self) -> FileId { |
@@ -48,12 +54,12 @@ impl PathMap { | |||
48 | } | 54 | } |
49 | 55 | ||
50 | impl FileResolver for PathMap { | 56 | impl FileResolver for PathMap { |
51 | fn file_stem(&self, id: FileId) -> String { | 57 | fn file_stem(&self, file_id: FileId) -> String { |
52 | self.get_path(id).file_stem().unwrap().to_str().unwrap().to_string() | 58 | self.get_path(file_id).file_stem().unwrap().to_str().unwrap().to_string() |
53 | } | 59 | } |
54 | 60 | ||
55 | fn resolve(&self, id: FileId, path: &RelativePath) -> Option<FileId> { | 61 | fn resolve(&self, file_id: FileId, path: &RelativePath) -> Option<FileId> { |
56 | let path = path.to_path(&self.get_path(id)); | 62 | let path = path.to_path(&self.get_path(file_id)); |
57 | let path = normalize(&path); | 63 | let path = normalize(&path); |
58 | self.get_id(&path) | 64 | self.get_id(&path) |
59 | } | 65 | } |