]> git.lizzy.rs Git - rust.git/blob - crates/ide_db/src/lib.rs
Merge #8866
[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 label;
7 pub mod line_index;
8 pub mod symbol_index;
9 pub mod defs;
10 pub mod search;
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 use std::{fmt, sync::Arc};
19
20 use base_db::{
21     salsa::{self, Durability},
22     AnchoredPath, CrateId, FileId, FileLoader, FileLoaderDelegate, SourceDatabase, Upcast,
23 };
24 use hir::db::{AstDatabase, DefDatabase, HirDatabase};
25 use rustc_hash::FxHashSet;
26
27 use crate::{line_index::LineIndex, symbol_index::SymbolsDatabase};
28
29 /// `base_db` is normally also needed in places where `ide_db` is used, so this re-export is for convenience.
30 pub use base_db;
31
32 #[salsa::database(
33     base_db::SourceDatabaseStorage,
34     base_db::SourceDatabaseExtStorage,
35     LineIndexDatabaseStorage,
36     symbol_index::SymbolsDatabaseStorage,
37     hir::db::InternDatabaseStorage,
38     hir::db::AstDatabaseStorage,
39     hir::db::DefDatabaseStorage,
40     hir::db::HirDatabaseStorage
41 )]
42 pub struct RootDatabase {
43     storage: salsa::Storage<RootDatabase>,
44 }
45
46 impl fmt::Debug for RootDatabase {
47     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48         f.debug_struct("RootDatabase").finish()
49     }
50 }
51
52 impl Upcast<dyn AstDatabase> for RootDatabase {
53     fn upcast(&self) -> &(dyn AstDatabase + 'static) {
54         &*self
55     }
56 }
57
58 impl Upcast<dyn DefDatabase> for RootDatabase {
59     fn upcast(&self) -> &(dyn DefDatabase + 'static) {
60         &*self
61     }
62 }
63
64 impl Upcast<dyn HirDatabase> for RootDatabase {
65     fn upcast(&self) -> &(dyn HirDatabase + 'static) {
66         &*self
67     }
68 }
69
70 impl FileLoader for RootDatabase {
71     fn file_text(&self, file_id: FileId) -> Arc<String> {
72         FileLoaderDelegate(self).file_text(file_id)
73     }
74     fn resolve_path(&self, path: AnchoredPath) -> Option<FileId> {
75         FileLoaderDelegate(self).resolve_path(path)
76     }
77     fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
78         FileLoaderDelegate(self).relevant_crates(file_id)
79     }
80 }
81
82 impl salsa::Database for RootDatabase {}
83
84 impl Default for RootDatabase {
85     fn default() -> RootDatabase {
86         RootDatabase::new(None)
87     }
88 }
89
90 impl RootDatabase {
91     pub fn new(lru_capacity: Option<usize>) -> RootDatabase {
92         let mut db = RootDatabase { storage: salsa::Storage::default() };
93         db.set_crate_graph_with_durability(Default::default(), Durability::HIGH);
94         db.set_local_roots_with_durability(Default::default(), Durability::HIGH);
95         db.set_library_roots_with_durability(Default::default(), Durability::HIGH);
96         db.update_lru_capacity(lru_capacity);
97         db
98     }
99
100     pub fn update_lru_capacity(&mut self, lru_capacity: Option<usize>) {
101         let lru_capacity = lru_capacity.unwrap_or(base_db::DEFAULT_LRU_CAP);
102         base_db::ParseQuery.in_db_mut(self).set_lru_capacity(lru_capacity);
103         hir::db::ParseMacroExpansionQuery.in_db_mut(self).set_lru_capacity(lru_capacity);
104         hir::db::MacroExpandQuery.in_db_mut(self).set_lru_capacity(lru_capacity);
105     }
106 }
107
108 impl salsa::ParallelDatabase for RootDatabase {
109     fn snapshot(&self) -> salsa::Snapshot<RootDatabase> {
110         salsa::Snapshot::new(RootDatabase { storage: self.storage.snapshot() })
111     }
112 }
113
114 #[salsa::query_group(LineIndexDatabaseStorage)]
115 pub trait LineIndexDatabase: base_db::SourceDatabase {
116     fn line_index(&self, file_id: FileId) -> Arc<LineIndex>;
117 }
118
119 fn line_index(db: &dyn LineIndexDatabase, file_id: FileId) -> Arc<LineIndex> {
120     let text = db.file_text(file_id);
121     Arc::new(LineIndex::new(&*text))
122 }
123
124 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
125 pub enum SymbolKind {
126     Const,
127     ConstParam,
128     Enum,
129     Field,
130     Function,
131     Impl,
132     Label,
133     LifetimeParam,
134     Local,
135     Macro,
136     Module,
137     SelfParam,
138     Static,
139     Struct,
140     Trait,
141     TypeAlias,
142     TypeParam,
143     Union,
144     ValueParam,
145     Variant,
146 }