aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api
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
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')
-rw-r--r--crates/ra_ide_api/src/change.rs15
-rw-r--r--crates/ra_ide_api/src/db.rs31
2 files changed, 43 insertions, 3 deletions
diff --git a/crates/ra_ide_api/src/change.rs b/crates/ra_ide_api/src/change.rs
index 89631935a..0d52f5ffb 100644
--- a/crates/ra_ide_api/src/change.rs
+++ b/crates/ra_ide_api/src/change.rs
@@ -2,7 +2,7 @@ use std::{fmt, sync::Arc, time};
2 2
3use ra_db::{ 3use ra_db::{
4 salsa::{Database, Durability, SweepStrategy}, 4 salsa::{Database, Durability, SweepStrategy},
5 CrateGraph, FileId, SourceDatabase, SourceRoot, SourceRootId, 5 CrateGraph, CrateId, FileId, SourceDatabase, SourceRoot, SourceRootId,
6}; 6};
7use ra_prof::{memory_usage, profile, Bytes}; 7use ra_prof::{memory_usage, profile, Bytes};
8use ra_syntax::SourceFile; 8use ra_syntax::SourceFile;
@@ -11,7 +11,7 @@ use relative_path::RelativePathBuf;
11use rustc_hash::FxHashMap; 11use rustc_hash::FxHashMap;
12 12
13use crate::{ 13use crate::{
14 db::RootDatabase, 14 db::{DebugData, RootDatabase},
15 status::syntax_tree_stats, 15 status::syntax_tree_stats,
16 symbol_index::{SymbolIndex, SymbolsDatabase}, 16 symbol_index::{SymbolIndex, SymbolsDatabase},
17}; 17};
@@ -23,6 +23,7 @@ pub struct AnalysisChange {
23 files_changed: Vec<(FileId, Arc<String>)>, 23 files_changed: Vec<(FileId, Arc<String>)>,
24 libraries_added: Vec<LibraryData>, 24 libraries_added: Vec<LibraryData>,
25 crate_graph: Option<CrateGraph>, 25 crate_graph: Option<CrateGraph>,
26 debug_data: DebugData,
26} 27}
27 28
28impl fmt::Debug for AnalysisChange { 29impl fmt::Debug for AnalysisChange {
@@ -83,6 +84,14 @@ impl AnalysisChange {
83 pub fn set_crate_graph(&mut self, graph: CrateGraph) { 84 pub fn set_crate_graph(&mut self, graph: CrateGraph) {
84 self.crate_graph = Some(graph); 85 self.crate_graph = Some(graph);
85 } 86 }
87
88 pub fn set_debug_crate_name(&mut self, crate_id: CrateId, name: String) {
89 self.debug_data.crate_names.insert(crate_id, name);
90 }
91
92 pub fn set_debug_root_path(&mut self, source_root_id: SourceRootId, path: String) {
93 self.debug_data.root_paths.insert(source_root_id, path);
94 }
86} 95}
87 96
88#[derive(Debug)] 97#[derive(Debug)]
@@ -200,6 +209,8 @@ impl RootDatabase {
200 if let Some(crate_graph) = change.crate_graph { 209 if let Some(crate_graph) = change.crate_graph {
201 self.set_crate_graph_with_durability(Arc::new(crate_graph), Durability::HIGH) 210 self.set_crate_graph_with_durability(Arc::new(crate_graph), Durability::HIGH)
202 } 211 }
212
213 Arc::make_mut(&mut self.debug_data).merge(change.debug_data)
203 } 214 }
204 215
205 fn apply_root_change(&mut self, root_id: SourceRootId, root_change: RootChange) { 216 fn apply_root_change(&mut self, root_id: SourceRootId, root_change: RootChange) {
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}