aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_db/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide_db/src')
-rw-r--r--crates/ra_ide_db/src/change.rs39
-rw-r--r--crates/ra_ide_db/src/defs.rs32
-rw-r--r--crates/ra_ide_db/src/lib.rs35
-rw-r--r--crates/ra_ide_db/src/source_change.rs2
-rw-r--r--crates/ra_ide_db/src/symbol_index.rs8
5 files changed, 73 insertions, 43 deletions
diff --git a/crates/ra_ide_db/src/change.rs b/crates/ra_ide_db/src/change.rs
index 2504d7a33..a1bb3043b 100644
--- a/crates/ra_ide_db/src/change.rs
+++ b/crates/ra_ide_db/src/change.rs
@@ -5,8 +5,7 @@ use std::{fmt, sync::Arc, time};
5 5
6use ra_db::{ 6use ra_db::{
7 salsa::{Database, Durability, SweepStrategy}, 7 salsa::{Database, Durability, SweepStrategy},
8 CrateGraph, FileId, RelativePathBuf, SourceDatabase, SourceDatabaseExt, SourceRoot, 8 CrateGraph, FileId, SourceDatabase, SourceDatabaseExt, SourceRoot, SourceRootId,
9 SourceRootId,
10}; 9};
11use ra_prof::{memory_usage, profile, Bytes}; 10use ra_prof::{memory_usage, profile, Bytes};
12use rustc_hash::FxHashSet; 11use rustc_hash::FxHashSet;
@@ -57,14 +56,14 @@ impl AnalysisChange {
57#[derive(Debug)] 56#[derive(Debug)]
58struct AddFile { 57struct AddFile {
59 file_id: FileId, 58 file_id: FileId,
60 path: RelativePathBuf, 59 path: String,
61 text: Arc<String>, 60 text: Arc<String>,
62} 61}
63 62
64#[derive(Debug)] 63#[derive(Debug)]
65struct RemoveFile { 64struct RemoveFile {
66 file_id: FileId, 65 file_id: FileId,
67 path: RelativePathBuf, 66 path: String,
68} 67}
69 68
70#[derive(Default)] 69#[derive(Default)]
@@ -147,37 +146,46 @@ impl RootDatabase {
147 146
148 let sweep = SweepStrategy::default().discard_values().sweep_all_revisions(); 147 let sweep = SweepStrategy::default().discard_values().sweep_all_revisions();
149 148
150 self.query(ra_db::ParseQuery).sweep(sweep); 149 ra_db::ParseQuery.in_db(self).sweep(sweep);
151 self.query(hir::db::ParseMacroQuery).sweep(sweep); 150 hir::db::ParseMacroQuery.in_db(self).sweep(sweep);
152 151
153 // Macros do take significant space, but less then the syntax trees 152 // Macros do take significant space, but less then the syntax trees
154 // self.query(hir::db::MacroDefQuery).sweep(sweep); 153 // self.query(hir::db::MacroDefQuery).sweep(sweep);
155 // self.query(hir::db::MacroArgQuery).sweep(sweep); 154 // self.query(hir::db::MacroArgQuery).sweep(sweep);
156 // self.query(hir::db::MacroExpandQuery).sweep(sweep); 155 // self.query(hir::db::MacroExpandQuery).sweep(sweep);
157 156
158 self.query(hir::db::AstIdMapQuery).sweep(sweep); 157 hir::db::AstIdMapQuery.in_db(self).sweep(sweep);
159 158
160 self.query(hir::db::BodyWithSourceMapQuery).sweep(sweep); 159 hir::db::BodyWithSourceMapQuery.in_db(self).sweep(sweep);
161 160
162 self.query(hir::db::ExprScopesQuery).sweep(sweep); 161 hir::db::ExprScopesQuery.in_db(self).sweep(sweep);
163 self.query(hir::db::InferQueryQuery).sweep(sweep); 162 hir::db::InferQueryQuery.in_db(self).sweep(sweep);
164 self.query(hir::db::BodyQuery).sweep(sweep); 163 hir::db::BodyQuery.in_db(self).sweep(sweep);
165 } 164 }
166 165
166 // Feature: Memory Usage
167 //
168 // Clears rust-analyzer's internal database and prints memory usage statistics.
169 //
170 // |===
171 // | Editor | Action Name
172 //
173 // | VS Code | **Rust Analyzer: Memory Usage (Clears Database)**
174 // |===
167 pub fn per_query_memory_usage(&mut self) -> Vec<(String, Bytes)> { 175 pub fn per_query_memory_usage(&mut self) -> Vec<(String, Bytes)> {
168 let mut acc: Vec<(String, Bytes)> = vec![]; 176 let mut acc: Vec<(String, Bytes)> = vec![];
169 let sweep = SweepStrategy::default().discard_values().sweep_all_revisions(); 177 let sweep = SweepStrategy::default().discard_values().sweep_all_revisions();
170 macro_rules! sweep_each_query { 178 macro_rules! sweep_each_query {
171 ($($q:path)*) => {$( 179 ($($q:path)*) => {$(
172 let before = memory_usage().allocated; 180 let before = memory_usage().allocated;
173 self.query($q).sweep(sweep); 181 $q.in_db(self).sweep(sweep);
174 let after = memory_usage().allocated; 182 let after = memory_usage().allocated;
175 let q: $q = Default::default(); 183 let q: $q = Default::default();
176 let name = format!("{:?}", q); 184 let name = format!("{:?}", q);
177 acc.push((name, before - after)); 185 acc.push((name, before - after));
178 186
179 let before = memory_usage().allocated; 187 let before = memory_usage().allocated;
180 self.query($q).sweep(sweep.discard_everything()); 188 $q.in_db(self).sweep(sweep.discard_everything());
181 let after = memory_usage().allocated; 189 let after = memory_usage().allocated;
182 let q: $q = Default::default(); 190 let q: $q = Default::default();
183 let name = format!("{:?} (deps)", q); 191 let name = format!("{:?} (deps)", q);
@@ -252,7 +260,7 @@ impl RootDatabase {
252 // write. 260 // write.
253 // We do this after collecting the non-interned queries to correctly attribute memory used 261 // We do this after collecting the non-interned queries to correctly attribute memory used
254 // by interned data. 262 // by interned data.
255 self.runtime.synthetic_write(Durability::HIGH); 263 self.salsa_runtime_mut().synthetic_write(Durability::HIGH);
256 264
257 sweep_each_query![ 265 sweep_each_query![
258 // AstDatabase 266 // AstDatabase
@@ -271,10 +279,7 @@ impl RootDatabase {
271 hir::db::InternImplQuery 279 hir::db::InternImplQuery
272 280
273 // HirDatabase 281 // HirDatabase
274 hir::db::InternTypeCtorQuery
275 hir::db::InternTypeParamIdQuery 282 hir::db::InternTypeParamIdQuery
276 hir::db::InternChalkImplQuery
277 hir::db::InternAssocTyValueQuery
278 ]; 283 ];
279 284
280 acc.sort_by_key(|it| std::cmp::Reverse(it.1)); 285 acc.sort_by_key(|it| std::cmp::Reverse(it.1));
diff --git a/crates/ra_ide_db/src/defs.rs b/crates/ra_ide_db/src/defs.rs
index 3ef5e74b6..e06b189a0 100644
--- a/crates/ra_ide_db/src/defs.rs
+++ b/crates/ra_ide_db/src/defs.rs
@@ -254,9 +254,37 @@ pub fn classify_name_ref(
254 } 254 }
255 } 255 }
256 256
257 if ast::AssocTypeArg::cast(parent.clone()).is_some() {
258 // `Trait<Assoc = Ty>`
259 // ^^^^^
260 let path = name_ref.syntax().ancestors().find_map(ast::Path::cast)?;
261 let resolved = sema.resolve_path(&path)?;
262 if let PathResolution::Def(ModuleDef::Trait(tr)) = resolved {
263 if let Some(ty) = tr
264 .items(sema.db)
265 .iter()
266 .filter_map(|assoc| match assoc {
267 hir::AssocItem::TypeAlias(it) => Some(*it),
268 _ => None,
269 })
270 .find(|alias| alias.name(sema.db).to_string() == **name_ref.text())
271 {
272 return Some(NameRefClass::Definition(Definition::ModuleDef(
273 ModuleDef::TypeAlias(ty),
274 )));
275 }
276 }
277 }
278
257 if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) { 279 if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) {
258 if let Some(macro_def) = sema.resolve_macro_call(&macro_call) { 280 if let Some(path) = macro_call.path() {
259 return Some(NameRefClass::Definition(Definition::Macro(macro_def))); 281 if path.qualifier().is_none() {
282 // Only use this to resolve single-segment macro calls like `foo!()`. Multi-segment
283 // paths are handled below (allowing `log<|>::info!` to resolve to the log crate).
284 if let Some(macro_def) = sema.resolve_macro_call(&macro_call) {
285 return Some(NameRefClass::Definition(Definition::Macro(macro_def)));
286 }
287 }
260 } 288 }
261 } 289 }
262 290
diff --git a/crates/ra_ide_db/src/lib.rs b/crates/ra_ide_db/src/lib.rs
index c78071ad6..6900cac73 100644
--- a/crates/ra_ide_db/src/lib.rs
+++ b/crates/ra_ide_db/src/lib.rs
@@ -11,11 +11,11 @@ pub mod imports_locator;
11pub mod source_change; 11pub mod source_change;
12mod wasm_shims; 12mod wasm_shims;
13 13
14use std::sync::Arc; 14use std::{fmt, sync::Arc};
15 15
16use hir::db::{AstDatabase, DefDatabase, HirDatabase}; 16use hir::db::{AstDatabase, DefDatabase, HirDatabase};
17use ra_db::{ 17use ra_db::{
18 salsa::{self, Database, Durability}, 18 salsa::{self, Durability},
19 Canceled, CheckCanceled, CrateId, FileId, FileLoader, FileLoaderDelegate, SourceDatabase, 19 Canceled, CheckCanceled, CrateId, FileId, FileLoader, FileLoaderDelegate, SourceDatabase,
20 Upcast, 20 Upcast,
21}; 21};
@@ -33,13 +33,18 @@ use crate::{line_index::LineIndex, symbol_index::SymbolsDatabase};
33 hir::db::DefDatabaseStorage, 33 hir::db::DefDatabaseStorage,
34 hir::db::HirDatabaseStorage 34 hir::db::HirDatabaseStorage
35)] 35)]
36#[derive(Debug)]
37pub struct RootDatabase { 36pub struct RootDatabase {
38 runtime: salsa::Runtime<RootDatabase>, 37 storage: salsa::Storage<RootDatabase>,
39 pub last_gc: crate::wasm_shims::Instant, 38 pub last_gc: crate::wasm_shims::Instant,
40 pub last_gc_check: crate::wasm_shims::Instant, 39 pub last_gc_check: crate::wasm_shims::Instant,
41} 40}
42 41
42impl fmt::Debug for RootDatabase {
43 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44 f.debug_struct("RootDatabase").finish()
45 }
46}
47
43impl Upcast<dyn AstDatabase> for RootDatabase { 48impl Upcast<dyn AstDatabase> for RootDatabase {
44 fn upcast(&self) -> &(dyn AstDatabase + 'static) { 49 fn upcast(&self) -> &(dyn AstDatabase + 'static) {
45 &*self 50 &*self
@@ -71,17 +76,11 @@ impl FileLoader for RootDatabase {
71} 76}
72 77
73impl salsa::Database for RootDatabase { 78impl salsa::Database for RootDatabase {
74 fn salsa_runtime(&self) -> &salsa::Runtime<RootDatabase> {
75 &self.runtime
76 }
77 fn salsa_runtime_mut(&mut self) -> &mut salsa::Runtime<Self> {
78 &mut self.runtime
79 }
80 fn on_propagated_panic(&self) -> ! { 79 fn on_propagated_panic(&self) -> ! {
81 Canceled::throw() 80 Canceled::throw()
82 } 81 }
83 fn salsa_event(&self, event: impl Fn() -> salsa::Event<RootDatabase>) { 82 fn salsa_event(&self, event: salsa::Event) {
84 match event().kind { 83 match event.kind {
85 salsa::EventKind::DidValidateMemoizedValue { .. } 84 salsa::EventKind::DidValidateMemoizedValue { .. }
86 | salsa::EventKind::WillExecute { .. } => { 85 | salsa::EventKind::WillExecute { .. } => {
87 self.check_canceled(); 86 self.check_canceled();
@@ -100,7 +99,7 @@ impl Default for RootDatabase {
100impl RootDatabase { 99impl RootDatabase {
101 pub fn new(lru_capacity: Option<usize>) -> RootDatabase { 100 pub fn new(lru_capacity: Option<usize>) -> RootDatabase {
102 let mut db = RootDatabase { 101 let mut db = RootDatabase {
103 runtime: salsa::Runtime::default(), 102 storage: salsa::Storage::default(),
104 last_gc: crate::wasm_shims::Instant::now(), 103 last_gc: crate::wasm_shims::Instant::now(),
105 last_gc_check: crate::wasm_shims::Instant::now(), 104 last_gc_check: crate::wasm_shims::Instant::now(),
106 }; 105 };
@@ -113,16 +112,16 @@ impl RootDatabase {
113 112
114 pub fn update_lru_capacity(&mut self, lru_capacity: Option<usize>) { 113 pub fn update_lru_capacity(&mut self, lru_capacity: Option<usize>) {
115 let lru_capacity = lru_capacity.unwrap_or(ra_db::DEFAULT_LRU_CAP); 114 let lru_capacity = lru_capacity.unwrap_or(ra_db::DEFAULT_LRU_CAP);
116 self.query_mut(ra_db::ParseQuery).set_lru_capacity(lru_capacity); 115 ra_db::ParseQuery.in_db_mut(self).set_lru_capacity(lru_capacity);
117 self.query_mut(hir::db::ParseMacroQuery).set_lru_capacity(lru_capacity); 116 hir::db::ParseMacroQuery.in_db_mut(self).set_lru_capacity(lru_capacity);
118 self.query_mut(hir::db::MacroExpandQuery).set_lru_capacity(lru_capacity); 117 hir::db::MacroExpandQuery.in_db_mut(self).set_lru_capacity(lru_capacity);
119 } 118 }
120} 119}
121 120
122impl salsa::ParallelDatabase for RootDatabase { 121impl salsa::ParallelDatabase for RootDatabase {
123 fn snapshot(&self) -> salsa::Snapshot<RootDatabase> { 122 fn snapshot(&self) -> salsa::Snapshot<RootDatabase> {
124 salsa::Snapshot::new(RootDatabase { 123 salsa::Snapshot::new(RootDatabase {
125 runtime: self.runtime.snapshot(self), 124 storage: self.storage.snapshot(),
126 last_gc: self.last_gc, 125 last_gc: self.last_gc,
127 last_gc_check: self.last_gc_check, 126 last_gc_check: self.last_gc_check,
128 }) 127 })
@@ -134,7 +133,7 @@ pub trait LineIndexDatabase: ra_db::SourceDatabase + CheckCanceled {
134 fn line_index(&self, file_id: FileId) -> Arc<LineIndex>; 133 fn line_index(&self, file_id: FileId) -> Arc<LineIndex>;
135} 134}
136 135
137fn line_index(db: &impl LineIndexDatabase, file_id: FileId) -> Arc<LineIndex> { 136fn line_index(db: &dyn LineIndexDatabase, file_id: FileId) -> Arc<LineIndex> {
138 let text = db.file_text(file_id); 137 let text = db.file_text(file_id);
139 Arc::new(LineIndex::new(&*text)) 138 Arc::new(LineIndex::new(&*text))
140} 139}
diff --git a/crates/ra_ide_db/src/source_change.rs b/crates/ra_ide_db/src/source_change.rs
index 0bbd3c3e5..abb83f421 100644
--- a/crates/ra_ide_db/src/source_change.rs
+++ b/crates/ra_ide_db/src/source_change.rs
@@ -6,7 +6,7 @@
6use ra_db::FileId; 6use ra_db::FileId;
7use ra_text_edit::TextEdit; 7use ra_text_edit::TextEdit;
8 8
9#[derive(Debug, Clone)] 9#[derive(Default, Debug, Clone)]
10pub struct SourceChange { 10pub struct SourceChange {
11 pub source_file_edits: Vec<SourceFileEdit>, 11 pub source_file_edits: Vec<SourceFileEdit>,
12 pub file_system_edits: Vec<FileSystemEdit>, 12 pub file_system_edits: Vec<FileSystemEdit>,
diff --git a/crates/ra_ide_db/src/symbol_index.rs b/crates/ra_ide_db/src/symbol_index.rs
index 5a09e7d1d..131e2a128 100644
--- a/crates/ra_ide_db/src/symbol_index.rs
+++ b/crates/ra_ide_db/src/symbol_index.rs
@@ -87,7 +87,7 @@ impl Query {
87} 87}
88 88
89#[salsa::query_group(SymbolsDatabaseStorage)] 89#[salsa::query_group(SymbolsDatabaseStorage)]
90pub trait SymbolsDatabase: hir::db::HirDatabase + SourceDatabaseExt + ParallelDatabase { 90pub trait SymbolsDatabase: hir::db::HirDatabase + SourceDatabaseExt {
91 fn file_symbols(&self, file_id: FileId) -> Arc<SymbolIndex>; 91 fn file_symbols(&self, file_id: FileId) -> Arc<SymbolIndex>;
92 fn library_symbols(&self) -> Arc<FxHashMap<SourceRootId, SymbolIndex>>; 92 fn library_symbols(&self) -> Arc<FxHashMap<SourceRootId, SymbolIndex>>;
93 /// The set of "local" (that is, from the current workspace) roots. 93 /// The set of "local" (that is, from the current workspace) roots.
@@ -100,9 +100,7 @@ pub trait SymbolsDatabase: hir::db::HirDatabase + SourceDatabaseExt + ParallelDa
100 fn library_roots(&self) -> Arc<FxHashSet<SourceRootId>>; 100 fn library_roots(&self) -> Arc<FxHashSet<SourceRootId>>;
101} 101}
102 102
103fn library_symbols( 103fn library_symbols(db: &dyn SymbolsDatabase) -> Arc<FxHashMap<SourceRootId, SymbolIndex>> {
104 db: &(impl SymbolsDatabase + ParallelDatabase),
105) -> Arc<FxHashMap<SourceRootId, SymbolIndex>> {
106 let _p = profile("library_symbols"); 104 let _p = profile("library_symbols");
107 105
108 let roots = db.library_roots(); 106 let roots = db.library_roots();
@@ -123,7 +121,7 @@ fn library_symbols(
123 Arc::new(res) 121 Arc::new(res)
124} 122}
125 123
126fn file_symbols(db: &impl SymbolsDatabase, file_id: FileId) -> Arc<SymbolIndex> { 124fn file_symbols(db: &dyn SymbolsDatabase, file_id: FileId) -> Arc<SymbolIndex> {
127 db.check_canceled(); 125 db.check_canceled();
128 let parse = db.parse(file_id); 126 let parse = db.parse(file_id);
129 127