1 //! This crate defines the core datastructure representing IDE state -- `RootDatabase`.
3 //! It is mainly a `HirDatabase` for semantic analysis, plus a `SymbolsDatabase`, for fuzzy search.
11 pub mod items_locator;
12 pub mod source_change;
21 use std::{fmt, sync::Arc};
24 salsa::{self, Durability},
25 AnchoredPath, CrateId, FileId, FileLoader, FileLoaderDelegate, SourceDatabase, Upcast,
27 use hir::db::{AstDatabase, DefDatabase, HirDatabase};
28 use rustc_hash::FxHashSet;
30 use crate::{line_index::LineIndex, symbol_index::SymbolsDatabase};
32 /// `base_db` is normally also needed in places where `ide_db` is used, so this re-export is for convenience.
36 base_db::SourceDatabaseStorage,
37 base_db::SourceDatabaseExtStorage,
38 LineIndexDatabaseStorage,
39 symbol_index::SymbolsDatabaseStorage,
40 hir::db::InternDatabaseStorage,
41 hir::db::AstDatabaseStorage,
42 hir::db::DefDatabaseStorage,
43 hir::db::HirDatabaseStorage
45 pub struct RootDatabase {
46 storage: salsa::Storage<RootDatabase>,
49 impl fmt::Debug for RootDatabase {
50 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51 f.debug_struct("RootDatabase").finish()
55 impl Upcast<dyn AstDatabase> for RootDatabase {
56 fn upcast(&self) -> &(dyn AstDatabase + 'static) {
61 impl Upcast<dyn DefDatabase> for RootDatabase {
62 fn upcast(&self) -> &(dyn DefDatabase + 'static) {
67 impl Upcast<dyn HirDatabase> for RootDatabase {
68 fn upcast(&self) -> &(dyn HirDatabase + 'static) {
73 impl FileLoader for RootDatabase {
74 fn file_text(&self, file_id: FileId) -> Arc<String> {
75 FileLoaderDelegate(self).file_text(file_id)
77 fn resolve_path(&self, path: AnchoredPath) -> Option<FileId> {
78 FileLoaderDelegate(self).resolve_path(path)
80 fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
81 FileLoaderDelegate(self).relevant_crates(file_id)
85 impl salsa::Database for RootDatabase {}
87 impl Default for RootDatabase {
88 fn default() -> RootDatabase {
89 RootDatabase::new(None)
94 pub fn new(lru_capacity: Option<usize>) -> RootDatabase {
95 let mut db = RootDatabase { storage: salsa::Storage::default() };
96 db.set_crate_graph_with_durability(Default::default(), Durability::HIGH);
97 db.set_local_roots_with_durability(Default::default(), Durability::HIGH);
98 db.set_library_roots_with_durability(Default::default(), Durability::HIGH);
99 db.set_enable_proc_attr_macros(Default::default());
100 db.update_lru_capacity(lru_capacity);
104 pub fn update_lru_capacity(&mut self, lru_capacity: Option<usize>) {
105 let lru_capacity = lru_capacity.unwrap_or(base_db::DEFAULT_LRU_CAP);
106 base_db::ParseQuery.in_db_mut(self).set_lru_capacity(lru_capacity);
107 hir::db::ParseMacroExpansionQuery.in_db_mut(self).set_lru_capacity(lru_capacity);
108 hir::db::MacroExpandQuery.in_db_mut(self).set_lru_capacity(lru_capacity);
112 impl salsa::ParallelDatabase for RootDatabase {
113 fn snapshot(&self) -> salsa::Snapshot<RootDatabase> {
114 salsa::Snapshot::new(RootDatabase { storage: self.storage.snapshot() })
118 #[salsa::query_group(LineIndexDatabaseStorage)]
119 pub trait LineIndexDatabase: base_db::SourceDatabase {
120 fn line_index(&self, file_id: FileId) -> Arc<LineIndex>;
123 fn line_index(db: &dyn LineIndexDatabase, file_id: FileId) -> Arc<LineIndex> {
124 let text = db.file_text(file_id);
125 Arc::new(LineIndex::new(&*text))
128 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
129 pub enum SymbolKind {