aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_db/src/change.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide_db/src/change.rs')
-rw-r--r--crates/ra_ide_db/src/change.rs85
1 files changed, 23 insertions, 62 deletions
diff --git a/crates/ra_ide_db/src/change.rs b/crates/ra_ide_db/src/change.rs
index 98993d571..a95f6c13c 100644
--- a/crates/ra_ide_db/src/change.rs
+++ b/crates/ra_ide_db/src/change.rs
@@ -9,26 +9,22 @@ use ra_db::{
9 SourceRootId, 9 SourceRootId,
10}; 10};
11use ra_prof::{memory_usage, profile, Bytes}; 11use ra_prof::{memory_usage, profile, Bytes};
12use rustc_hash::FxHashMap; 12use rustc_hash::FxHashSet;
13 13
14use crate::{symbol_index::SymbolsDatabase, RootDatabase}; 14use crate::{symbol_index::SymbolsDatabase, RootDatabase};
15 15
16#[derive(Default)] 16#[derive(Default)]
17pub struct AnalysisChange { 17pub struct AnalysisChange {
18 new_roots: Vec<(SourceRootId, bool)>, 18 roots: Option<Vec<SourceRoot>>,
19 roots_changed: FxHashMap<SourceRootId, RootChange>, 19 files_changed: Vec<(FileId, Option<Arc<String>>)>,
20 files_changed: Vec<(FileId, Arc<String>)>,
21 crate_graph: Option<CrateGraph>, 20 crate_graph: Option<CrateGraph>,
22} 21}
23 22
24impl fmt::Debug for AnalysisChange { 23impl fmt::Debug for AnalysisChange {
25 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { 24 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
26 let mut d = fmt.debug_struct("AnalysisChange"); 25 let mut d = fmt.debug_struct("AnalysisChange");
27 if !self.new_roots.is_empty() { 26 if let Some(roots) = &self.roots {
28 d.field("new_roots", &self.new_roots); 27 d.field("roots", roots);
29 }
30 if !self.roots_changed.is_empty() {
31 d.field("roots_changed", &self.roots_changed);
32 } 28 }
33 if !self.files_changed.is_empty() { 29 if !self.files_changed.is_empty() {
34 d.field("files_changed", &self.files_changed.len()); 30 d.field("files_changed", &self.files_changed.len());
@@ -45,30 +41,14 @@ impl AnalysisChange {
45 AnalysisChange::default() 41 AnalysisChange::default()
46 } 42 }
47 43
48 pub fn add_root(&mut self, root_id: SourceRootId, is_local: bool) { 44 pub fn set_roots(&mut self, roots: Vec<SourceRoot>) {
49 self.new_roots.push((root_id, is_local)); 45 self.roots = Some(roots);
50 }
51
52 pub fn add_file(
53 &mut self,
54 root_id: SourceRootId,
55 file_id: FileId,
56 path: RelativePathBuf,
57 text: Arc<String>,
58 ) {
59 let file = AddFile { file_id, path, text };
60 self.roots_changed.entry(root_id).or_default().added.push(file);
61 } 46 }
62 47
63 pub fn change_file(&mut self, file_id: FileId, new_text: Arc<String>) { 48 pub fn change_file(&mut self, file_id: FileId, new_text: Option<Arc<String>>) {
64 self.files_changed.push((file_id, new_text)) 49 self.files_changed.push((file_id, new_text))
65 } 50 }
66 51
67 pub fn remove_file(&mut self, root_id: SourceRootId, file_id: FileId, path: RelativePathBuf) {
68 let file = RemoveFile { file_id, path };
69 self.roots_changed.entry(root_id).or_default().removed.push(file);
70 }
71
72 pub fn set_crate_graph(&mut self, graph: CrateGraph) { 52 pub fn set_crate_graph(&mut self, graph: CrateGraph) {
73 self.crate_graph = Some(graph); 53 self.crate_graph = Some(graph);
74 } 54 }
@@ -114,31 +94,32 @@ impl RootDatabase {
114 let _p = profile("RootDatabase::apply_change"); 94 let _p = profile("RootDatabase::apply_change");
115 self.request_cancellation(); 95 self.request_cancellation();
116 log::info!("apply_change {:?}", change); 96 log::info!("apply_change {:?}", change);
117 if !change.new_roots.is_empty() { 97 if let Some(roots) = change.roots {
118 let mut local_roots = Vec::clone(&self.local_roots()); 98 let mut local_roots = FxHashSet::default();
119 let mut libraries = Vec::clone(&self.library_roots()); 99 let mut library_roots = FxHashSet::default();
120 for (root_id, is_local) in change.new_roots { 100 for (idx, root) in roots.into_iter().enumerate() {
121 let root = 101 let root_id = SourceRootId(idx as u32);
122 if is_local { SourceRoot::new_local() } else { SourceRoot::new_library() };
123 let durability = durability(&root); 102 let durability = durability(&root);
124 self.set_source_root_with_durability(root_id, Arc::new(root), durability); 103 if root.is_library {
125 if is_local { 104 library_roots.insert(root_id);
126 local_roots.push(root_id);
127 } else { 105 } else {
128 libraries.push(root_id) 106 local_roots.insert(root_id);
107 }
108 for file_id in root.iter() {
109 self.set_file_source_root_with_durability(file_id, root_id, durability);
129 } 110 }
111 self.set_source_root_with_durability(root_id, Arc::new(root), durability);
130 } 112 }
131 self.set_local_roots_with_durability(Arc::new(local_roots), Durability::HIGH); 113 self.set_local_roots_with_durability(Arc::new(local_roots), Durability::HIGH);
132 self.set_library_roots_with_durability(Arc::new(libraries), Durability::HIGH); 114 self.set_library_roots_with_durability(Arc::new(library_roots), Durability::HIGH);
133 } 115 }
134 116
135 for (root_id, root_change) in change.roots_changed {
136 self.apply_root_change(root_id, root_change);
137 }
138 for (file_id, text) in change.files_changed { 117 for (file_id, text) in change.files_changed {
139 let source_root_id = self.file_source_root(file_id); 118 let source_root_id = self.file_source_root(file_id);
140 let source_root = self.source_root(source_root_id); 119 let source_root = self.source_root(source_root_id);
141 let durability = durability(&source_root); 120 let durability = durability(&source_root);
121 // XXX: can't actually remove the file, just reset the text
122 let text = text.unwrap_or_default();
142 self.set_file_text_with_durability(file_id, text, durability) 123 self.set_file_text_with_durability(file_id, text, durability)
143 } 124 }
144 if let Some(crate_graph) = change.crate_graph { 125 if let Some(crate_graph) = change.crate_graph {
@@ -146,26 +127,6 @@ impl RootDatabase {
146 } 127 }
147 } 128 }
148 129
149 fn apply_root_change(&mut self, root_id: SourceRootId, root_change: RootChange) {
150 let mut source_root = SourceRoot::clone(&self.source_root(root_id));
151 let durability = durability(&source_root);
152 for add_file in root_change.added {
153 self.set_file_text_with_durability(add_file.file_id, add_file.text, durability);
154 self.set_file_relative_path_with_durability(
155 add_file.file_id,
156 add_file.path.clone(),
157 durability,
158 );
159 self.set_file_source_root_with_durability(add_file.file_id, root_id, durability);
160 source_root.insert_file(add_file.path, add_file.file_id);
161 }
162 for remove_file in root_change.removed {
163 self.set_file_text_with_durability(remove_file.file_id, Default::default(), durability);
164 source_root.remove_file(&remove_file.path);
165 }
166 self.set_source_root_with_durability(root_id, Arc::new(source_root), durability);
167 }
168
169 pub fn maybe_collect_garbage(&mut self) { 130 pub fn maybe_collect_garbage(&mut self) {
170 if cfg!(feature = "wasm") { 131 if cfg!(feature = "wasm") {
171 return; 132 return;