aboutsummaryrefslogtreecommitdiff
path: root/crates/server
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-09-04 09:40:45 +0100
committerAleksey Kladov <[email protected]>2018-09-04 09:40:45 +0100
commit3a017aaa52fc41316b5dd2cd90b5171ca869697a (patch)
tree5017726e5ff94bfdb769bb31ff7ec5a7b8270f57 /crates/server
parenta668f703fa5361f59a170d40be667c7e59a4a3e5 (diff)
dont change readonly files
Diffstat (limited to 'crates/server')
-rw-r--r--crates/server/src/path_map.rs36
-rw-r--r--crates/server/src/server_world.rs20
2 files changed, 34 insertions, 22 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;
3use relative_path::RelativePath; 3use relative_path::RelativePath;
4use libanalysis::{FileId, FileResolver}; 4use libanalysis::{FileId, FileResolver};
5 5
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum Root {
8 Workspace, Lib
9}
10
6#[derive(Debug, Default, Clone)] 11#[derive(Debug, Default, Clone)]
7pub struct PathMap { 12pub 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
13impl PathMap { 19impl 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
50impl FileResolver for PathMap { 56impl 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 }
diff --git a/crates/server/src/server_world.rs b/crates/server/src/server_world.rs
index 729418eaa..8ceec594f 100644
--- a/crates/server/src/server_world.rs
+++ b/crates/server/src/server_world.rs
@@ -10,7 +10,7 @@ use libanalysis::{FileId, AnalysisHost, Analysis, CrateGraph, CrateId, LibraryDa
10 10
11use { 11use {
12 Result, 12 Result,
13 path_map::PathMap, 13 path_map::{PathMap, Root},
14 vfs::{FileEvent, FileEventKind}, 14 vfs::{FileEvent, FileEventKind},
15 project_model::CargoWorkspace, 15 project_model::CargoWorkspace,
16}; 16};
@@ -51,7 +51,7 @@ impl ServerWorldState {
51 (event.path, text) 51 (event.path, text)
52 }) 52 })
53 .map(|(path, text)| { 53 .map(|(path, text)| {
54 (pm.get_or_insert(path), text) 54 (pm.get_or_insert(path, Root::Workspace), text)
55 }) 55 })
56 .filter_map(|(id, text)| { 56 .filter_map(|(id, text)| {
57 if mm.contains_key(&id) { 57 if mm.contains_key(&id) {
@@ -73,7 +73,7 @@ impl ServerWorldState {
73 }; 73 };
74 (event.path, text) 74 (event.path, text)
75 }) 75 })
76 .map(|(path, text)| (pm.get_or_insert(path), text)) 76 .map(|(path, text)| (pm.get_or_insert(path, Root::Lib), text))
77 .collect() 77 .collect()
78 } 78 }
79 pub fn add_lib(&mut self, data: LibraryData) { 79 pub fn add_lib(&mut self, data: LibraryData) {
@@ -81,9 +81,11 @@ impl ServerWorldState {
81 } 81 }
82 82
83 pub fn add_mem_file(&mut self, path: PathBuf, text: String) -> FileId { 83 pub fn add_mem_file(&mut self, path: PathBuf, text: String) -> FileId {
84 let file_id = self.path_map.get_or_insert(path); 84 let file_id = self.path_map.get_or_insert(path, Root::Workspace);
85 self.mem_map.insert(file_id, None); 85 self.mem_map.insert(file_id, None);
86 self.analysis_host.change_file(file_id, Some(text)); 86 if self.path_map.get_root(file_id) != Root::Lib {
87 self.analysis_host.change_file(file_id, Some(text));
88 }
87 file_id 89 file_id
88 } 90 }
89 91
@@ -91,7 +93,9 @@ impl ServerWorldState {
91 let file_id = self.path_map.get_id(path).ok_or_else(|| { 93 let file_id = self.path_map.get_id(path).ok_or_else(|| {
92 format_err!("change to unknown file: {}", path.display()) 94 format_err!("change to unknown file: {}", path.display())
93 })?; 95 })?;
94 self.analysis_host.change_file(file_id, Some(text)); 96 if self.path_map.get_root(file_id) != Root::Lib {
97 self.analysis_host.change_file(file_id, Some(text));
98 }
95 Ok(()) 99 Ok(())
96 } 100 }
97 101
@@ -105,7 +109,9 @@ impl ServerWorldState {
105 }; 109 };
106 // Do this via file watcher ideally. 110 // Do this via file watcher ideally.
107 let text = fs::read_to_string(path).ok(); 111 let text = fs::read_to_string(path).ok();
108 self.analysis_host.change_file(file_id, text); 112 if self.path_map.get_root(file_id) != Root::Lib {
113 self.analysis_host.change_file(file_id, text);
114 }
109 Ok(file_id) 115 Ok(file_id)
110 } 116 }
111 pub fn set_workspaces(&mut self, ws: Vec<CargoWorkspace>) { 117 pub fn set_workspaces(&mut self, ws: Vec<CargoWorkspace>) {