aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/db.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-09-08 07:48:45 +0100
committerAleksey Kladov <[email protected]>2019-09-09 10:32:16 +0100
commitef2b84ddf119c950272c5f1eb321f3f9e90bedd4 (patch)
treed746c95cef14b27f67f1e5fd32d289e6d20b4d57 /crates/ra_ide_api/src/db.rs
parent734a43e95afc97773c234956a95b78caed88f2a3 (diff)
introduce hir debugging infra
This is to make debugging rust-analyzer easier. The idea is that `dbg!(krate.debug(db))` will print the actual, fuzzy crate name, instead of precise ID. Debug printing infra is a separate thing, to make sure that the actual hir doesn't have access to global information. Do not use `.debug` for `log::` logging: debugging executes queries, and might introduce unneded dependencies to the crate graph
Diffstat (limited to 'crates/ra_ide_api/src/db.rs')
-rw-r--r--crates/ra_ide_api/src/db.rs31
1 files changed, 30 insertions, 1 deletions
diff --git a/crates/ra_ide_api/src/db.rs b/crates/ra_ide_api/src/db.rs
index f2e6b8f12..4c5159d61 100644
--- a/crates/ra_ide_api/src/db.rs
+++ b/crates/ra_ide_api/src/db.rs
@@ -2,8 +2,9 @@ use std::{sync::Arc, time};
2 2
3use ra_db::{ 3use ra_db::{
4 salsa::{self, Database, Durability}, 4 salsa::{self, Database, Durability},
5 Canceled, CheckCanceled, FileId, SourceDatabase, 5 Canceled, CheckCanceled, CrateId, FileId, SourceDatabase, SourceRootId,
6}; 6};
7use rustc_hash::FxHashMap;
7 8
8use crate::{ 9use crate::{
9 symbol_index::{self, SymbolsDatabase}, 10 symbol_index::{self, SymbolsDatabase},
@@ -23,10 +24,23 @@ use crate::{
23pub(crate) struct RootDatabase { 24pub(crate) struct RootDatabase {
24 runtime: salsa::Runtime<RootDatabase>, 25 runtime: salsa::Runtime<RootDatabase>,
25 pub(crate) feature_flags: Arc<FeatureFlags>, 26 pub(crate) feature_flags: Arc<FeatureFlags>,
27 pub(crate) debug_data: Arc<DebugData>,
26 pub(crate) last_gc: time::Instant, 28 pub(crate) last_gc: time::Instant,
27 pub(crate) last_gc_check: time::Instant, 29 pub(crate) last_gc_check: time::Instant,
28} 30}
29 31
32impl hir::debug::HirDebugHelper for RootDatabase {
33 fn crate_name(&self, krate: CrateId) -> Option<String> {
34 self.debug_data.crate_names.get(&krate).cloned()
35 }
36 fn file_path(&self, file_id: FileId) -> Option<String> {
37 let source_root_id = self.file_source_root(file_id);
38 let source_root_path = self.debug_data.root_paths.get(&source_root_id)?;
39 let file_path = self.file_relative_path(file_id);
40 Some(format!("{}/{}", source_root_path, file_path.display()))
41 }
42}
43
30impl salsa::Database for RootDatabase { 44impl salsa::Database for RootDatabase {
31 fn salsa_runtime(&self) -> &salsa::Runtime<RootDatabase> { 45 fn salsa_runtime(&self) -> &salsa::Runtime<RootDatabase> {
32 &self.runtime 46 &self.runtime
@@ -58,6 +72,7 @@ impl RootDatabase {
58 last_gc: time::Instant::now(), 72 last_gc: time::Instant::now(),
59 last_gc_check: time::Instant::now(), 73 last_gc_check: time::Instant::now(),
60 feature_flags: Arc::new(feature_flags), 74 feature_flags: Arc::new(feature_flags),
75 debug_data: Default::default(),
61 }; 76 };
62 db.set_crate_graph_with_durability(Default::default(), Durability::HIGH); 77 db.set_crate_graph_with_durability(Default::default(), Durability::HIGH);
63 db.set_local_roots_with_durability(Default::default(), Durability::HIGH); 78 db.set_local_roots_with_durability(Default::default(), Durability::HIGH);
@@ -77,6 +92,7 @@ impl salsa::ParallelDatabase for RootDatabase {
77 last_gc: self.last_gc, 92 last_gc: self.last_gc,
78 last_gc_check: self.last_gc_check, 93 last_gc_check: self.last_gc_check,
79 feature_flags: Arc::clone(&self.feature_flags), 94 feature_flags: Arc::clone(&self.feature_flags),
95 debug_data: Arc::clone(&self.debug_data),
80 }) 96 })
81 } 97 }
82} 98}
@@ -90,3 +106,16 @@ fn line_index(db: &impl ra_db::SourceDatabase, file_id: FileId) -> Arc<LineIndex
90 let text = db.file_text(file_id); 106 let text = db.file_text(file_id);
91 Arc::new(LineIndex::new(&*text)) 107 Arc::new(LineIndex::new(&*text))
92} 108}
109
110#[derive(Debug, Default, Clone)]
111pub(crate) struct DebugData {
112 pub(crate) root_paths: FxHashMap<SourceRootId, String>,
113 pub(crate) crate_names: FxHashMap<CrateId, String>,
114}
115
116impl DebugData {
117 pub(crate) fn merge(&mut self, other: DebugData) {
118 self.root_paths.extend(other.root_paths.into_iter());
119 self.crate_names.extend(other.crate_names.into_iter());
120 }
121}