]> git.lizzy.rs Git - rust.git/blob - crates/ide_db/src/lib.rs
Merge #9267 #9279
[rust.git] / crates / ide_db / src / lib.rs
1 //! This crate defines the core datastructure representing IDE state -- `RootDatabase`.
2 //!
3 //! It is mainly a `HirDatabase` for semantic analysis, plus a `SymbolsDatabase`, for fuzzy search.
4
5 mod apply_change;
6 pub mod assists;
7 pub mod label;
8 pub mod line_index;
9 pub mod symbol_index;
10 pub mod defs;
11 pub mod items_locator;
12 pub mod source_change;
13 pub mod ty_filter;
14 pub mod traits;
15 pub mod call_info;
16 pub mod helpers;
17
18 pub mod search;
19 pub mod rename;
20
21 use std::{fmt, sync::Arc};
22
23 use base_db::{
24     salsa::{self, Durability},
25     AnchoredPath, CrateId, FileId, FileLoader, FileLoaderDelegate, SourceDatabase, Upcast,
26 };
27 use hir::db::{AstDatabase, DefDatabase, HirDatabase};
28 use rustc_hash::FxHashSet;
29
30 use crate::{line_index::LineIndex, symbol_index::SymbolsDatabase};
31
32 /// `base_db` is normally also needed in places where `ide_db` is used, so this re-export is for convenience.
33 pub use base_db;
34
35 #[salsa::database(
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
44 )]
45 pub struct RootDatabase {
46     storage: salsa::Storage<RootDatabase>,
47 }
48
49 impl fmt::Debug for RootDatabase {
50     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51         f.debug_struct("RootDatabase").finish()
52     }
53 }
54
55 impl Upcast<dyn AstDatabase> for RootDatabase {
56     fn upcast(&self) -> &(dyn AstDatabase + 'static) {
57         &*self
58     }
59 }
60
61 impl Upcast<dyn DefDatabase> for RootDatabase {
62     fn upcast(&self) -> &(dyn DefDatabase + 'static) {
63         &*self
64     }
65 }
66
67 impl Upcast<dyn HirDatabase> for RootDatabase {
68     fn upcast(&self) -> &(dyn HirDatabase + 'static) {
69         &*self
70     }
71 }
72
73 impl FileLoader for RootDatabase {
74     fn file_text(&self, file_id: FileId) -> Arc<String> {
75         FileLoaderDelegate(self).file_text(file_id)
76     }
77     fn resolve_path(&self, path: AnchoredPath) -> Option<FileId> {
78         FileLoaderDelegate(self).resolve_path(path)
79     }
80     fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
81         FileLoaderDelegate(self).relevant_crates(file_id)
82     }
83 }
84
85 impl salsa::Database for RootDatabase {}
86
87 impl Default for RootDatabase {
88     fn default() -> RootDatabase {
89         RootDatabase::new(None)
90     }
91 }
92
93 impl RootDatabase {
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);
101         db
102     }
103
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);
109     }
110 }
111
112 impl salsa::ParallelDatabase for RootDatabase {
113     fn snapshot(&self) -> salsa::Snapshot<RootDatabase> {
114         salsa::Snapshot::new(RootDatabase { storage: self.storage.snapshot() })
115     }
116 }
117
118 #[salsa::query_group(LineIndexDatabaseStorage)]
119 pub trait LineIndexDatabase: base_db::SourceDatabase {
120     fn line_index(&self, file_id: FileId) -> Arc<LineIndex>;
121 }
122
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))
126 }
127
128 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
129 pub enum SymbolKind {
130     Const,
131     ConstParam,
132     Enum,
133     Field,
134     Function,
135     Impl,
136     Label,
137     LifetimeParam,
138     Local,
139     Macro,
140     Module,
141     SelfParam,
142     Static,
143     Struct,
144     Trait,
145     TypeAlias,
146     TypeParam,
147     Union,
148     ValueParam,
149     Variant,
150 }