diff options
Diffstat (limited to 'crates/ra_ide_api/src/imp.rs')
-rw-r--r-- | crates/ra_ide_api/src/imp.rs | 101 |
1 files changed, 3 insertions, 98 deletions
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 @@ | |||
1 | use std::{ | ||
2 | sync::Arc, | ||
3 | time, | ||
4 | }; | ||
5 | |||
6 | use hir::{ | 1 | use hir::{ |
7 | self, Problem, source_binder | 2 | self, Problem, source_binder |
8 | }; | 3 | }; |
9 | use ra_db::{ | ||
10 | SourceDatabase, SourceRoot, SourceRootId, | ||
11 | salsa::{Database, SweepStrategy}, | ||
12 | }; | ||
13 | use ra_ide_api_light::{self, LocalEdit, Severity}; | 4 | use ra_ide_api_light::{self, LocalEdit, Severity}; |
14 | use ra_syntax::{ | 5 | use 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 | }; |
10 | use ra_db::SourceDatabase; | ||
19 | 11 | ||
20 | use crate::{ | 12 | use 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 | ||
28 | const GC_COOLDOWN: time::Duration = time::Duration::from_millis(100); | ||
29 | |||
30 | impl 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 | |||
113 | impl db::RootDatabase { | 18 | impl 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> { |