aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_db/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide_db/src')
-rw-r--r--crates/ide_db/src/apply_change.rs (renamed from crates/ide_db/src/change.rs)93
-rw-r--r--crates/ide_db/src/lib.rs17
-rw-r--r--crates/ide_db/src/wasm_shims.rs19
3 files changed, 10 insertions, 119 deletions
diff --git a/crates/ide_db/src/change.rs b/crates/ide_db/src/apply_change.rs
index 8b4fd7ab8..da16fa21d 100644
--- a/crates/ide_db/src/change.rs
+++ b/crates/ide_db/src/apply_change.rs
@@ -1,58 +1,16 @@
1//! Defines a unit of change that can applied to a state of IDE to get the next 1//! Applies changes to the IDE state transactionally.
2//! state. Changes are transactional.
3 2
4use std::{fmt, sync::Arc, time}; 3use std::{fmt, sync::Arc};
5 4
6use base_db::{ 5use base_db::{
7 salsa::{Database, Durability, SweepStrategy}, 6 salsa::{Database, Durability, SweepStrategy},
8 CrateGraph, FileId, SourceDatabase, SourceDatabaseExt, SourceRoot, SourceRootId, 7 Change, FileId, SourceRootId,
9}; 8};
10use profile::{memory_usage, Bytes}; 9use profile::{memory_usage, Bytes};
11use rustc_hash::FxHashSet; 10use rustc_hash::FxHashSet;
12 11
13use crate::{symbol_index::SymbolsDatabase, RootDatabase}; 12use crate::{symbol_index::SymbolsDatabase, RootDatabase};
14 13
15#[derive(Default)]
16pub struct AnalysisChange {
17 roots: Option<Vec<SourceRoot>>,
18 files_changed: Vec<(FileId, Option<Arc<String>>)>,
19 crate_graph: Option<CrateGraph>,
20}
21
22impl fmt::Debug for AnalysisChange {
23 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
24 let mut d = fmt.debug_struct("AnalysisChange");
25 if let Some(roots) = &self.roots {
26 d.field("roots", roots);
27 }
28 if !self.files_changed.is_empty() {
29 d.field("files_changed", &self.files_changed.len());
30 }
31 if self.crate_graph.is_some() {
32 d.field("crate_graph", &self.crate_graph);
33 }
34 d.finish()
35 }
36}
37
38impl AnalysisChange {
39 pub fn new() -> AnalysisChange {
40 AnalysisChange::default()
41 }
42
43 pub fn set_roots(&mut self, roots: Vec<SourceRoot>) {
44 self.roots = Some(roots);
45 }
46
47 pub fn change_file(&mut self, file_id: FileId, new_text: Option<Arc<String>>) {
48 self.files_changed.push((file_id, new_text))
49 }
50
51 pub fn set_crate_graph(&mut self, graph: CrateGraph) {
52 self.crate_graph = Some(graph);
53 }
54}
55
56#[derive(Debug)] 14#[derive(Debug)]
57struct AddFile { 15struct AddFile {
58 file_id: FileId, 16 file_id: FileId,
@@ -81,59 +39,31 @@ impl fmt::Debug for RootChange {
81 } 39 }
82} 40}
83 41
84const GC_COOLDOWN: time::Duration = time::Duration::from_millis(100);
85
86impl RootDatabase { 42impl RootDatabase {
87 pub fn request_cancellation(&mut self) { 43 pub fn request_cancellation(&mut self) {
88 let _p = profile::span("RootDatabase::request_cancellation"); 44 let _p = profile::span("RootDatabase::request_cancellation");
89 self.salsa_runtime_mut().synthetic_write(Durability::LOW); 45 self.salsa_runtime_mut().synthetic_write(Durability::LOW);
90 } 46 }
91 47
92 pub fn apply_change(&mut self, change: AnalysisChange) { 48 pub fn apply_change(&mut self, change: Change) {
93 let _p = profile::span("RootDatabase::apply_change"); 49 let _p = profile::span("RootDatabase::apply_change");
94 self.request_cancellation(); 50 self.request_cancellation();
95 log::info!("apply_change {:?}", change); 51 log::info!("apply_change {:?}", change);
96 if let Some(roots) = change.roots { 52 if let Some(roots) = &change.roots {
97 let mut local_roots = FxHashSet::default(); 53 let mut local_roots = FxHashSet::default();
98 let mut library_roots = FxHashSet::default(); 54 let mut library_roots = FxHashSet::default();
99 for (idx, root) in roots.into_iter().enumerate() { 55 for (idx, root) in roots.iter().enumerate() {
100 let root_id = SourceRootId(idx as u32); 56 let root_id = SourceRootId(idx as u32);
101 let durability = durability(&root);
102 if root.is_library { 57 if root.is_library {
103 library_roots.insert(root_id); 58 library_roots.insert(root_id);
104 } else { 59 } else {
105 local_roots.insert(root_id); 60 local_roots.insert(root_id);
106 } 61 }
107 for file_id in root.iter() {
108 self.set_file_source_root_with_durability(file_id, root_id, durability);
109 }
110 self.set_source_root_with_durability(root_id, Arc::new(root), durability);
111 } 62 }
112 self.set_local_roots_with_durability(Arc::new(local_roots), Durability::HIGH); 63 self.set_local_roots_with_durability(Arc::new(local_roots), Durability::HIGH);
113 self.set_library_roots_with_durability(Arc::new(library_roots), Durability::HIGH); 64 self.set_library_roots_with_durability(Arc::new(library_roots), Durability::HIGH);
114 } 65 }
115 66 change.apply(self);
116 for (file_id, text) in change.files_changed {
117 let source_root_id = self.file_source_root(file_id);
118 let source_root = self.source_root(source_root_id);
119 let durability = durability(&source_root);
120 // XXX: can't actually remove the file, just reset the text
121 let text = text.unwrap_or_default();
122 self.set_file_text_with_durability(file_id, text, durability)
123 }
124 if let Some(crate_graph) = change.crate_graph {
125 self.set_crate_graph_with_durability(Arc::new(crate_graph), Durability::HIGH)
126 }
127 }
128
129 pub fn maybe_collect_garbage(&mut self) {
130 if cfg!(feature = "wasm") {
131 return;
132 }
133
134 if self.last_gc_check.elapsed() > GC_COOLDOWN {
135 self.last_gc_check = crate::wasm_shims::Instant::now();
136 }
137 } 67 }
138 68
139 pub fn collect_garbage(&mut self) { 69 pub fn collect_garbage(&mut self) {
@@ -142,7 +72,6 @@ impl RootDatabase {
142 } 72 }
143 73
144 let _p = profile::span("RootDatabase::collect_garbage"); 74 let _p = profile::span("RootDatabase::collect_garbage");
145 self.last_gc = crate::wasm_shims::Instant::now();
146 75
147 let sweep = SweepStrategy::default().discard_values().sweep_all_revisions(); 76 let sweep = SweepStrategy::default().discard_values().sweep_all_revisions();
148 77
@@ -308,11 +237,3 @@ impl RootDatabase {
308 acc 237 acc
309 } 238 }
310} 239}
311
312fn durability(source_root: &SourceRoot) -> Durability {
313 if source_root.is_library {
314 Durability::HIGH
315 } else {
316 Durability::LOW
317 }
318}
diff --git a/crates/ide_db/src/lib.rs b/crates/ide_db/src/lib.rs
index 70ada02f3..7eff247c7 100644
--- a/crates/ide_db/src/lib.rs
+++ b/crates/ide_db/src/lib.rs
@@ -2,15 +2,14 @@
2//! 2//!
3//! It is mainly a `HirDatabase` for semantic analysis, plus a `SymbolsDatabase`, for fuzzy search. 3//! It is mainly a `HirDatabase` for semantic analysis, plus a `SymbolsDatabase`, for fuzzy search.
4 4
5mod apply_change;
5pub mod label; 6pub mod label;
6pub mod line_index; 7pub mod line_index;
7pub mod symbol_index; 8pub mod symbol_index;
8pub mod change;
9pub mod defs; 9pub mod defs;
10pub mod search; 10pub mod search;
11pub mod imports_locator; 11pub mod imports_locator;
12pub mod source_change; 12pub mod source_change;
13mod wasm_shims;
14 13
15use std::{fmt, sync::Arc}; 14use std::{fmt, sync::Arc};
16 15
@@ -36,8 +35,6 @@ use crate::{line_index::LineIndex, symbol_index::SymbolsDatabase};
36)] 35)]
37pub struct RootDatabase { 36pub struct RootDatabase {
38 storage: salsa::Storage<RootDatabase>, 37 storage: salsa::Storage<RootDatabase>,
39 pub last_gc: crate::wasm_shims::Instant,
40 pub last_gc_check: crate::wasm_shims::Instant,
41} 38}
42 39
43impl fmt::Debug for RootDatabase { 40impl fmt::Debug for RootDatabase {
@@ -99,11 +96,7 @@ impl Default for RootDatabase {
99 96
100impl RootDatabase { 97impl RootDatabase {
101 pub fn new(lru_capacity: Option<usize>) -> RootDatabase { 98 pub fn new(lru_capacity: Option<usize>) -> RootDatabase {
102 let mut db = RootDatabase { 99 let mut db = RootDatabase { storage: salsa::Storage::default() };
103 storage: salsa::Storage::default(),
104 last_gc: crate::wasm_shims::Instant::now(),
105 last_gc_check: crate::wasm_shims::Instant::now(),
106 };
107 db.set_crate_graph_with_durability(Default::default(), Durability::HIGH); 100 db.set_crate_graph_with_durability(Default::default(), Durability::HIGH);
108 db.set_local_roots_with_durability(Default::default(), Durability::HIGH); 101 db.set_local_roots_with_durability(Default::default(), Durability::HIGH);
109 db.set_library_roots_with_durability(Default::default(), Durability::HIGH); 102 db.set_library_roots_with_durability(Default::default(), Durability::HIGH);
@@ -121,11 +114,7 @@ impl RootDatabase {
121 114
122impl salsa::ParallelDatabase for RootDatabase { 115impl salsa::ParallelDatabase for RootDatabase {
123 fn snapshot(&self) -> salsa::Snapshot<RootDatabase> { 116 fn snapshot(&self) -> salsa::Snapshot<RootDatabase> {
124 salsa::Snapshot::new(RootDatabase { 117 salsa::Snapshot::new(RootDatabase { storage: self.storage.snapshot() })
125 storage: self.storage.snapshot(),
126 last_gc: self.last_gc,
127 last_gc_check: self.last_gc_check,
128 })
129 } 118 }
130} 119}
131 120
diff --git a/crates/ide_db/src/wasm_shims.rs b/crates/ide_db/src/wasm_shims.rs
deleted file mode 100644
index 7af9f9d9b..000000000
--- a/crates/ide_db/src/wasm_shims.rs
+++ /dev/null
@@ -1,19 +0,0 @@
1//! A version of `std::time::Instant` that doesn't panic in WASM.
2
3#[cfg(not(feature = "wasm"))]
4pub use std::time::Instant;
5
6#[cfg(feature = "wasm")]
7#[derive(Clone, Copy, Debug)]
8pub struct Instant;
9
10#[cfg(feature = "wasm")]
11impl Instant {
12 pub fn now() -> Self {
13 Self
14 }
15
16 pub fn elapsed(&self) -> std::time::Duration {
17 std::time::Duration::new(0, 0)
18 }
19}