From 3a017aaa52fc41316b5dd2cd90b5171ca869697a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 4 Sep 2018 11:40:45 +0300 Subject: dont change readonly files --- crates/server/src/path_map.rs | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) (limited to 'crates/server/src/path_map.rs') 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; use relative_path::RelativePath; use libanalysis::{FileId, FileResolver}; +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Root { + Workspace, Lib +} + #[derive(Debug, Default, Clone)] pub struct PathMap { next_id: u32, path2id: im::HashMap, id2path: im::HashMap, + id2root: im::HashMap, } impl PathMap { pub fn new() -> PathMap { Default::default() } - - pub fn get_or_insert(&mut self, path: PathBuf) -> FileId { + pub fn get_or_insert(&mut self, path: PathBuf, root: Root) -> FileId { self.path2id.get(path.as_path()) .map(|&id| id) .unwrap_or_else(|| { let id = self.new_file_id(); - self.insert(path, id); + self.insert(path, id, root); id }) } - pub fn get_id(&self, path: &Path) -> Option { self.path2id.get(path).map(|&id| id) } - - pub fn get_path(&self, id: FileId) -> &Path { - self.id2path.get(&id) + pub fn get_path(&self, file_id: FileId) -> &Path { + self.id2path.get(&file_id) .unwrap() .as_path() } - - fn insert(&mut self, path: PathBuf, id: FileId) { - self.path2id.insert(path.clone(), id); - self.id2path.insert(id, path.clone()); + pub fn get_root(&self, file_id: FileId) -> Root { + self.id2root[&file_id] + } + fn insert(&mut self, path: PathBuf, file_id: FileId, root: Root) { + self.path2id.insert(path.clone(), file_id); + self.id2path.insert(file_id, path.clone()); + self.id2root.insert(file_id, root); } fn new_file_id(&mut self) -> FileId { @@ -48,12 +54,12 @@ impl PathMap { } impl FileResolver for PathMap { - fn file_stem(&self, id: FileId) -> String { - self.get_path(id).file_stem().unwrap().to_str().unwrap().to_string() + fn file_stem(&self, file_id: FileId) -> String { + self.get_path(file_id).file_stem().unwrap().to_str().unwrap().to_string() } - fn resolve(&self, id: FileId, path: &RelativePath) -> Option { - let path = path.to_path(&self.get_path(id)); + fn resolve(&self, file_id: FileId, path: &RelativePath) -> Option { + let path = path.to_path(&self.get_path(file_id)); let path = normalize(&path); self.get_id(&path) } -- cgit v1.2.3