aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide_api/src')
-rw-r--r--crates/ra_ide_api/src/change.rs255
-rw-r--r--crates/ra_ide_api/src/imp.rs101
-rw-r--r--crates/ra_ide_api/src/impls.rs2
-rw-r--r--crates/ra_ide_api/src/lib.rs159
4 files changed, 263 insertions, 254 deletions
diff --git a/crates/ra_ide_api/src/change.rs b/crates/ra_ide_api/src/change.rs
new file mode 100644
index 000000000..992955740
--- /dev/null
+++ b/crates/ra_ide_api/src/change.rs
@@ -0,0 +1,255 @@
1use std::{
2 fmt, time,
3 sync::Arc,
4};
5
6use rustc_hash::FxHashMap;
7use ra_db::{
8 SourceRootId, FileId, CrateGraph, SourceDatabase, SourceRoot,
9 salsa::{Database, SweepStrategy},
10};
11use ra_syntax::SourceFile;
12use relative_path::RelativePathBuf;
13use rayon::prelude::*;
14
15use crate::{
16 db::RootDatabase,
17 symbol_index::{SymbolIndex, SymbolsDatabase},
18 status::syntax_tree_stats,
19};
20
21#[derive(Default)]
22pub struct AnalysisChange {
23 new_roots: Vec<(SourceRootId, bool)>,
24 roots_changed: FxHashMap<SourceRootId, RootChange>,
25 files_changed: Vec<(FileId, Arc<String>)>,
26 libraries_added: Vec<LibraryData>,
27 crate_graph: Option<CrateGraph>,
28}
29
30impl fmt::Debug for AnalysisChange {
31 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
32 let mut d = fmt.debug_struct("AnalysisChange");
33 if !self.new_roots.is_empty() {
34 d.field("new_roots", &self.new_roots);
35 }
36 if !self.roots_changed.is_empty() {
37 d.field("roots_changed", &self.roots_changed);
38 }
39 if !self.files_changed.is_empty() {
40 d.field("files_changed", &self.files_changed.len());
41 }
42 if !self.libraries_added.is_empty() {
43 d.field("libraries_added", &self.libraries_added.len());
44 }
45 if !self.crate_graph.is_some() {
46 d.field("crate_graph", &self.crate_graph);
47 }
48 d.finish()
49 }
50}
51
52impl AnalysisChange {
53 pub fn new() -> AnalysisChange {
54 AnalysisChange::default()
55 }
56
57 pub fn add_root(&mut self, root_id: SourceRootId, is_local: bool) {
58 self.new_roots.push((root_id, is_local));
59 }
60
61 pub fn add_file(
62 &mut self,
63 root_id: SourceRootId,
64 file_id: FileId,
65 path: RelativePathBuf,
66 text: Arc<String>,
67 ) {
68 let file = AddFile {
69 file_id,
70 path,
71 text,
72 };
73 self.roots_changed
74 .entry(root_id)
75 .or_default()
76 .added
77 .push(file);
78 }
79
80 pub fn change_file(&mut self, file_id: FileId, new_text: Arc<String>) {
81 self.files_changed.push((file_id, new_text))
82 }
83
84 pub fn remove_file(&mut self, root_id: SourceRootId, file_id: FileId, path: RelativePathBuf) {
85 let file = RemoveFile { file_id, path };
86 self.roots_changed
87 .entry(root_id)
88 .or_default()
89 .removed
90 .push(file);
91 }
92
93 pub fn add_library(&mut self, data: LibraryData) {
94 self.libraries_added.push(data)
95 }
96
97 pub fn set_crate_graph(&mut self, graph: CrateGraph) {
98 self.crate_graph = Some(graph);
99 }
100}
101
102#[derive(Debug)]
103struct AddFile {
104 file_id: FileId,
105 path: RelativePathBuf,
106 text: Arc<String>,
107}
108
109#[derive(Debug)]
110struct RemoveFile {
111 file_id: FileId,
112 path: RelativePathBuf,
113}
114
115#[derive(Default)]
116struct RootChange {
117 added: Vec<AddFile>,
118 removed: Vec<RemoveFile>,
119}
120
121impl fmt::Debug for RootChange {
122 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
123 fmt.debug_struct("AnalysisChange")
124 .field("added", &self.added.len())
125 .field("removed", &self.removed.len())
126 .finish()
127 }
128}
129
130pub struct LibraryData {
131 root_id: SourceRootId,
132 root_change: RootChange,
133 symbol_index: SymbolIndex,
134}
135
136impl fmt::Debug for LibraryData {
137 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
138 f.debug_struct("LibraryData")
139 .field("root_id", &self.root_id)
140 .field("root_change", &self.root_change)
141 .field("n_symbols", &self.symbol_index.len())
142 .finish()
143 }
144}
145
146impl LibraryData {
147 pub fn prepare(
148 root_id: SourceRootId,
149 files: Vec<(FileId, RelativePathBuf, Arc<String>)>,
150 ) -> LibraryData {
151 let symbol_index = SymbolIndex::for_files(files.par_iter().map(|(file_id, _, text)| {
152 let file = SourceFile::parse(text);
153 (*file_id, file)
154 }));
155 let mut root_change = RootChange::default();
156 root_change.added = files
157 .into_iter()
158 .map(|(file_id, path, text)| AddFile {
159 file_id,
160 path,
161 text,
162 })
163 .collect();
164 LibraryData {
165 root_id,
166 root_change,
167 symbol_index,
168 }
169 }
170}
171
172const GC_COOLDOWN: time::Duration = time::Duration::from_millis(100);
173
174impl RootDatabase {
175 pub(crate) fn apply_change(&mut self, change: AnalysisChange) {
176 log::info!("apply_change {:?}", change);
177 if !change.new_roots.is_empty() {
178 let mut local_roots = Vec::clone(&self.local_roots());
179 for (root_id, is_local) in change.new_roots {
180 self.set_source_root(root_id, Default::default());
181 if is_local {
182 local_roots.push(root_id);
183 }
184 }
185 self.set_local_roots(Arc::new(local_roots));
186 }
187
188 for (root_id, root_change) in change.roots_changed {
189 self.apply_root_change(root_id, root_change);
190 }
191 for (file_id, text) in change.files_changed {
192 self.set_file_text(file_id, text)
193 }
194 if !change.libraries_added.is_empty() {
195 let mut libraries = Vec::clone(&self.library_roots());
196 for library in change.libraries_added {
197 libraries.push(library.root_id);
198 self.set_source_root(library.root_id, Default::default());
199 self.set_constant_library_symbols(library.root_id, Arc::new(library.symbol_index));
200 self.apply_root_change(library.root_id, library.root_change);
201 }
202 self.set_library_roots(Arc::new(libraries));
203 }
204 if let Some(crate_graph) = change.crate_graph {
205 self.set_crate_graph(Arc::new(crate_graph))
206 }
207 }
208
209 fn apply_root_change(&mut self, root_id: SourceRootId, root_change: RootChange) {
210 let mut source_root = SourceRoot::clone(&self.source_root(root_id));
211 for add_file in root_change.added {
212 self.set_file_text(add_file.file_id, add_file.text);
213 self.set_file_relative_path(add_file.file_id, add_file.path.clone());
214 self.set_file_source_root(add_file.file_id, root_id);
215 source_root.files.insert(add_file.path, add_file.file_id);
216 }
217 for remove_file in root_change.removed {
218 self.set_file_text(remove_file.file_id, Default::default());
219 source_root.files.remove(&remove_file.path);
220 }
221 self.set_source_root(root_id, Arc::new(source_root));
222 }
223
224 pub(crate) fn maybe_collect_garbage(&mut self) {
225 if self.last_gc_check.elapsed() > GC_COOLDOWN {
226 self.last_gc_check = time::Instant::now();
227 let retained_trees = syntax_tree_stats(self).retained;
228 if retained_trees > 100 {
229 log::info!(
230 "automatic garbadge collection, {} retained trees",
231 retained_trees
232 );
233 self.collect_garbage();
234 }
235 }
236 }
237
238 pub(crate) fn collect_garbage(&mut self) {
239 self.last_gc = time::Instant::now();
240
241 let sweep = SweepStrategy::default()
242 .discard_values()
243 .sweep_all_revisions();
244
245 self.query(ra_db::ParseQuery).sweep(sweep);
246
247 self.query(hir::db::HirParseQuery).sweep(sweep);
248 self.query(hir::db::FileItemsQuery).sweep(sweep);
249 self.query(hir::db::FileItemQuery).sweep(sweep);
250
251 self.query(hir::db::LowerModuleQuery).sweep(sweep);
252 self.query(hir::db::LowerModuleSourceMapQuery).sweep(sweep);
253 self.query(hir::db::BodySyntaxMappingQuery).sweep(sweep);
254 }
255}
diff --git a/crates/ra_ide_api/src/imp.rs b/crates/ra_ide_api/src/imp.rs
index b139efabf..7d672656f 100644
--- a/crates/ra_ide_api/src/imp.rs
+++ b/crates/ra_ide_api/src/imp.rs
@@ -1,115 +1,20 @@
1use std::{
2 sync::Arc,
3 time,
4};
5
6use hir::{ 1use hir::{
7 self, Problem, source_binder 2 self, Problem, source_binder
8}; 3};
9use ra_db::{
10 SourceDatabase, SourceRoot, SourceRootId,
11 salsa::{Database, SweepStrategy},
12};
13use ra_ide_api_light::{self, LocalEdit, Severity}; 4use ra_ide_api_light::{self, LocalEdit, Severity};
14use ra_syntax::{ 5use ra_syntax::{
15 algo::find_node_at_offset, ast::{self, NameOwner}, AstNode, 6 algo::find_node_at_offset, ast::{self, NameOwner}, AstNode,
16 SourceFile, 7 SourceFile,
17 TextRange, 8 TextRange,
18}; 9};
10use ra_db::SourceDatabase;
19 11
20use crate::{ 12use crate::{
21 AnalysisChange,
22 CrateId, db, Diagnostic, FileId, FilePosition, FileSystemEdit, 13 CrateId, db, Diagnostic, FileId, FilePosition, FileSystemEdit,
23 Query, RootChange, SourceChange, SourceFileEdit, 14 Query, SourceChange, SourceFileEdit,
24 symbol_index::{FileSymbol, SymbolsDatabase}, 15 symbol_index::FileSymbol,
25 status::syntax_tree_stats
26}; 16};
27 17
28const GC_COOLDOWN: time::Duration = time::Duration::from_millis(100);
29
30impl db::RootDatabase {
31 pub(crate) fn apply_change(&mut self, change: AnalysisChange) {
32 log::info!("apply_change {:?}", change);
33 if !change.new_roots.is_empty() {
34 let mut local_roots = Vec::clone(&self.local_roots());
35 for (root_id, is_local) in change.new_roots {
36 self.set_source_root(root_id, Default::default());
37 if is_local {
38 local_roots.push(root_id);
39 }
40 }
41 self.set_local_roots(Arc::new(local_roots));
42 }
43
44 for (root_id, root_change) in change.roots_changed {
45 self.apply_root_change(root_id, root_change);
46 }
47 for (file_id, text) in change.files_changed {
48 self.set_file_text(file_id, text)
49 }
50 if !change.libraries_added.is_empty() {
51 let mut libraries = Vec::clone(&self.library_roots());
52 for library in change.libraries_added {
53 libraries.push(library.root_id);
54 self.set_source_root(library.root_id, Default::default());
55 self.set_constant_library_symbols(library.root_id, Arc::new(library.symbol_index));
56 self.apply_root_change(library.root_id, library.root_change);
57 }
58 self.set_library_roots(Arc::new(libraries));
59 }
60 if let Some(crate_graph) = change.crate_graph {
61 self.set_crate_graph(Arc::new(crate_graph))
62 }
63 }
64
65 fn apply_root_change(&mut self, root_id: SourceRootId, root_change: RootChange) {
66 let mut source_root = SourceRoot::clone(&self.source_root(root_id));
67 for add_file in root_change.added {
68 self.set_file_text(add_file.file_id, add_file.text);
69 self.set_file_relative_path(add_file.file_id, add_file.path.clone());
70 self.set_file_source_root(add_file.file_id, root_id);
71 source_root.files.insert(add_file.path, add_file.file_id);
72 }
73 for remove_file in root_change.removed {
74 self.set_file_text(remove_file.file_id, Default::default());
75 source_root.files.remove(&remove_file.path);
76 }
77 self.set_source_root(root_id, Arc::new(source_root));
78 }
79
80 pub(crate) fn maybe_collect_garbage(&mut self) {
81 if self.last_gc_check.elapsed() > GC_COOLDOWN {
82 self.last_gc_check = time::Instant::now();
83 let retained_trees = syntax_tree_stats(self).retained;
84 if retained_trees > 100 {
85 log::info!(
86 "automatic garbadge collection, {} retained trees",
87 retained_trees
88 );
89 self.collect_garbage();
90 }
91 }
92 }
93
94 pub(crate) fn collect_garbage(&mut self) {
95 self.last_gc = time::Instant::now();
96
97 let sweep = SweepStrategy::default()
98 .discard_values()
99 .sweep_all_revisions();
100
101 self.query(ra_db::ParseQuery).sweep(sweep);
102
103 self.query(hir::db::HirParseQuery).sweep(sweep);
104 self.query(hir::db::FileItemsQuery).sweep(sweep);
105 self.query(hir::db::FileItemQuery).sweep(sweep);
106
107 self.query(hir::db::LowerModuleQuery).sweep(sweep);
108 self.query(hir::db::LowerModuleSourceMapQuery).sweep(sweep);
109 self.query(hir::db::BodySyntaxMappingQuery).sweep(sweep);
110 }
111}
112
113impl db::RootDatabase { 18impl db::RootDatabase {
114 /// Returns `Vec` for the same reason as `parent_module` 19 /// Returns `Vec` for the same reason as `parent_module`
115 pub(crate) fn crate_for(&self, file_id: FileId) -> Vec<CrateId> { 20 pub(crate) fn crate_for(&self, file_id: FileId) -> Vec<CrateId> {
diff --git a/crates/ra_ide_api/src/impls.rs b/crates/ra_ide_api/src/impls.rs
index 91fa41f1f..4fb054139 100644
--- a/crates/ra_ide_api/src/impls.rs
+++ b/crates/ra_ide_api/src/impls.rs
@@ -1,4 +1,4 @@
1use ra_db::{SourceDatabase}; 1use ra_db::SourceDatabase;
2use ra_syntax::{ 2use ra_syntax::{
3 AstNode, ast, 3 AstNode, ast,
4 algo::find_node_at_offset, 4 algo::find_node_at_offset,
diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs
index 68d59aae1..22a601ec8 100644
--- a/crates/ra_ide_api/src/lib.rs
+++ b/crates/ra_ide_api/src/lib.rs
@@ -18,6 +18,7 @@ mod imp;
18pub mod mock_analysis; 18pub mod mock_analysis;
19mod symbol_index; 19mod symbol_index;
20mod navigation_target; 20mod navigation_target;
21mod change;
21 22
22mod status; 23mod status;
23mod completion; 24mod completion;
@@ -35,7 +36,7 @@ mod assists;
35#[cfg(test)] 36#[cfg(test)]
36mod marks; 37mod marks;
37 38
38use std::{fmt, sync::Arc}; 39use std::sync::Arc;
39 40
40use ra_syntax::{SourceFile, TreeArc, TextRange, TextUnit}; 41use ra_syntax::{SourceFile, TreeArc, TextRange, TextUnit};
41use ra_text_edit::TextEdit; 42use ra_text_edit::TextEdit;
@@ -43,12 +44,10 @@ use ra_db::{
43 SourceDatabase, CheckCanceled, 44 SourceDatabase, CheckCanceled,
44 salsa::{self, ParallelDatabase}, 45 salsa::{self, ParallelDatabase},
45}; 46};
46use rayon::prelude::*;
47use relative_path::RelativePathBuf; 47use relative_path::RelativePathBuf;
48use rustc_hash::FxHashMap;
49 48
50use crate::{ 49use crate::{
51 symbol_index::{FileSymbol, SymbolIndex}, 50 symbol_index::FileSymbol,
52 db::LineIndexDatabase, 51 db::LineIndexDatabase,
53}; 52};
54 53
@@ -56,6 +55,7 @@ pub use crate::{
56 completion::{CompletionItem, CompletionItemKind, InsertTextFormat}, 55 completion::{CompletionItem, CompletionItemKind, InsertTextFormat},
57 runnables::{Runnable, RunnableKind}, 56 runnables::{Runnable, RunnableKind},
58 navigation_target::NavigationTarget, 57 navigation_target::NavigationTarget,
58 change::{AnalysisChange, LibraryData},
59}; 59};
60pub use ra_ide_api_light::{ 60pub use ra_ide_api_light::{
61 Fold, FoldKind, HighlightedRange, Severity, StructureNode, 61 Fold, FoldKind, HighlightedRange, Severity, StructureNode,
@@ -74,115 +74,6 @@ static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
74 74
75pub type Cancelable<T> = Result<T, Canceled>; 75pub type Cancelable<T> = Result<T, Canceled>;
76 76
77#[derive(Default)]
78pub struct AnalysisChange {
79 new_roots: Vec<(SourceRootId, bool)>,
80 roots_changed: FxHashMap<SourceRootId, RootChange>,
81 files_changed: Vec<(FileId, Arc<String>)>,
82 libraries_added: Vec<LibraryData>,
83 crate_graph: Option<CrateGraph>,
84}
85
86#[derive(Default)]
87struct RootChange {
88 added: Vec<AddFile>,
89 removed: Vec<RemoveFile>,
90}
91
92#[derive(Debug)]
93struct AddFile {
94 file_id: FileId,
95 path: RelativePathBuf,
96 text: Arc<String>,
97}
98
99#[derive(Debug)]
100struct RemoveFile {
101 file_id: FileId,
102 path: RelativePathBuf,
103}
104
105impl fmt::Debug for AnalysisChange {
106 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
107 let mut d = fmt.debug_struct("AnalysisChange");
108 if !self.new_roots.is_empty() {
109 d.field("new_roots", &self.new_roots);
110 }
111 if !self.roots_changed.is_empty() {
112 d.field("roots_changed", &self.roots_changed);
113 }
114 if !self.files_changed.is_empty() {
115 d.field("files_changed", &self.files_changed.len());
116 }
117 if !self.libraries_added.is_empty() {
118 d.field("libraries_added", &self.libraries_added.len());
119 }
120 if self.crate_graph.is_none() {
121 d.field("crate_graph", &self.crate_graph);
122 }
123 d.finish()
124 }
125}
126
127impl fmt::Debug for RootChange {
128 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
129 fmt.debug_struct("AnalysisChange")
130 .field("added", &self.added.len())
131 .field("removed", &self.removed.len())
132 .finish()
133 }
134}
135
136impl AnalysisChange {
137 pub fn new() -> AnalysisChange {
138 AnalysisChange::default()
139 }
140
141 pub fn add_root(&mut self, root_id: SourceRootId, is_local: bool) {
142 self.new_roots.push((root_id, is_local));
143 }
144
145 pub fn add_file(
146 &mut self,
147 root_id: SourceRootId,
148 file_id: FileId,
149 path: RelativePathBuf,
150 text: Arc<String>,
151 ) {
152 let file = AddFile {
153 file_id,
154 path,
155 text,
156 };
157 self.roots_changed
158 .entry(root_id)
159 .or_default()
160 .added
161 .push(file);
162 }
163
164 pub fn change_file(&mut self, file_id: FileId, new_text: Arc<String>) {
165 self.files_changed.push((file_id, new_text))
166 }
167
168 pub fn remove_file(&mut self, root_id: SourceRootId, file_id: FileId, path: RelativePathBuf) {
169 let file = RemoveFile { file_id, path };
170 self.roots_changed
171 .entry(root_id)
172 .or_default()
173 .removed
174 .push(file);
175 }
176
177 pub fn add_library(&mut self, data: LibraryData) {
178 self.libraries_added.push(data)
179 }
180
181 pub fn set_crate_graph(&mut self, graph: CrateGraph) {
182 self.crate_graph = Some(graph);
183 }
184}
185
186#[derive(Debug)] 77#[derive(Debug)]
187pub struct SourceChange { 78pub struct SourceChange {
188 pub label: String, 79 pub label: String,
@@ -508,48 +399,6 @@ impl Analysis {
508 } 399 }
509} 400}
510 401
511pub struct LibraryData {
512 root_id: SourceRootId,
513 root_change: RootChange,
514 symbol_index: SymbolIndex,
515}
516
517impl fmt::Debug for LibraryData {
518 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
519 f.debug_struct("LibraryData")
520 .field("root_id", &self.root_id)
521 .field("root_change", &self.root_change)
522 .field("n_symbols", &self.symbol_index.len())
523 .finish()
524 }
525}
526
527impl LibraryData {
528 pub fn prepare(
529 root_id: SourceRootId,
530 files: Vec<(FileId, RelativePathBuf, Arc<String>)>,
531 ) -> LibraryData {
532 let symbol_index = SymbolIndex::for_files(files.par_iter().map(|(file_id, _, text)| {
533 let file = SourceFile::parse(text);
534 (*file_id, file)
535 }));
536 let mut root_change = RootChange::default();
537 root_change.added = files
538 .into_iter()
539 .map(|(file_id, path, text)| AddFile {
540 file_id,
541 path,
542 text,
543 })
544 .collect();
545 LibraryData {
546 root_id,
547 root_change,
548 symbol_index,
549 }
550 }
551}
552
553#[test] 402#[test]
554fn analysis_is_send() { 403fn analysis_is_send() {
555 fn is_send<T: Send>() {} 404 fn is_send<T: Send>() {}