rust-analyzer/crates/ide-db/src/lib.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

292 lines
9 KiB
Rust
Raw Normal View History

2024-07-30 12:28:13 +00:00
//! This crate defines the core data structure representing IDE state -- `RootDatabase`.
2020-02-06 13:43:46 +00:00
//!
//! It is mainly a `HirDatabase` for semantic analysis, plus a `SymbolsDatabase`, for fuzzy search.
2020-02-06 11:07:06 +00:00
mod apply_change;
2021-10-12 06:59:39 +00:00
2022-03-06 18:01:30 +00:00
pub mod active_parameter;
pub mod assists;
2020-02-06 15:23:28 +00:00
pub mod defs;
2024-01-26 19:08:10 +00:00
pub mod documentation;
2022-03-06 18:01:30 +00:00
pub mod famous_defs;
pub mod helpers;
2021-03-02 23:26:53 +00:00
pub mod items_locator;
2022-03-06 18:01:30 +00:00
pub mod label;
pub mod path_transform;
pub mod prime_caches;
2022-03-06 18:01:30 +00:00
pub mod rename;
pub mod rust_doc;
pub mod search;
2020-05-06 09:31:26 +00:00
pub mod source_change;
2022-03-06 18:01:30 +00:00
pub mod symbol_index;
pub mod traits;
2022-03-06 18:01:30 +00:00
pub mod ty_filter;
2023-03-01 12:55:21 +00:00
pub mod use_trivial_constructor;
2020-02-06 11:17:40 +00:00
2022-03-06 18:01:30 +00:00
pub mod imports {
pub mod import_assets;
pub mod insert_use;
pub mod merge_imports;
}
pub mod generated {
pub mod lints;
}
pub mod syntax_helpers {
pub mod format_string;
2022-09-10 13:36:50 +00:00
pub mod format_string_exprs;
2024-01-26 19:08:10 +00:00
pub mod insert_whitespace_into_node;
pub mod node_ext;
pub use parser::LexedStr;
2022-03-06 18:01:30 +00:00
}
2024-03-04 10:10:06 +00:00
pub use hir::ChangeWithProcMacros;
2023-05-02 14:12:22 +00:00
use std::{fmt, mem::ManuallyDrop};
2020-02-06 11:08:08 +00:00
2020-08-13 14:25:38 +00:00
use base_db::{
salsa::{self, Durability},
AnchoredPath, CrateId, FileLoader, FileLoaderDelegate, SourceDatabase, Upcast,
2024-01-10 09:53:11 +00:00
DEFAULT_FILE_TEXT_LRU_CAP,
2020-02-06 11:08:08 +00:00
};
use hir::{
db::{DefDatabase, ExpandDatabase, HirDatabase},
FilePositionWrapper, FileRangeWrapper,
};
2023-05-02 14:12:22 +00:00
use triomphe::Arc;
2020-02-06 11:08:08 +00:00
2020-03-10 17:56:15 +00:00
use crate::{line_index::LineIndex, symbol_index::SymbolsDatabase};
pub use rustc_hash::{FxHashMap, FxHashSet, FxHasher};
2020-02-06 11:08:08 +00:00
2023-05-04 02:18:41 +00:00
pub use ::line_index;
2020-10-24 08:39:57 +00:00
/// `base_db` is normally also needed in places where `ide_db` is used, so this re-export is for convenience.
pub use base_db;
pub use span::{EditionedFileId, FileId};
2020-10-24 08:39:57 +00:00
pub type FxIndexSet<T> = indexmap::IndexSet<T, std::hash::BuildHasherDefault<rustc_hash::FxHasher>>;
pub type FxIndexMap<K, V> =
indexmap::IndexMap<K, V, std::hash::BuildHasherDefault<rustc_hash::FxHasher>>;
pub type FilePosition = FilePositionWrapper<FileId>;
pub type FileRange = FileRangeWrapper<FileId>;
2020-02-06 11:08:08 +00:00
#[salsa::database(
2024-08-03 16:00:36 +00:00
base_db::SourceRootDatabaseStorage,
2022-03-06 18:01:30 +00:00
base_db::SourceDatabaseStorage,
hir::db::ExpandDatabaseStorage,
2020-02-06 11:08:08 +00:00
hir::db::DefDatabaseStorage,
2022-03-06 18:01:30 +00:00
hir::db::HirDatabaseStorage,
hir::db::InternDatabaseStorage,
LineIndexDatabaseStorage,
symbol_index::SymbolsDatabaseStorage
2020-02-06 11:08:08 +00:00
)]
2020-02-06 11:43:56 +00:00
pub struct RootDatabase {
2021-08-28 21:05:40 +00:00
// We use `ManuallyDrop` here because every codegen unit that contains a
// `&RootDatabase -> &dyn OtherDatabase` cast will instantiate its drop glue in the vtable,
// which duplicates `Weak::drop` and `Arc::drop` tens of thousands of times, which makes
// compile times of all `ide_*` and downstream crates suffer greatly.
storage: ManuallyDrop<salsa::Storage<RootDatabase>>,
}
impl Drop for RootDatabase {
fn drop(&mut self) {
2022-03-06 18:01:30 +00:00
unsafe { ManuallyDrop::drop(&mut self.storage) };
2021-08-28 21:05:40 +00:00
}
2020-02-06 11:08:08 +00:00
}
impl fmt::Debug for RootDatabase {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("RootDatabase").finish()
}
}
impl Upcast<dyn ExpandDatabase> for RootDatabase {
#[inline]
fn upcast(&self) -> &(dyn ExpandDatabase + 'static) {
2024-01-18 12:59:49 +00:00
self
}
}
impl Upcast<dyn DefDatabase> for RootDatabase {
#[inline]
fn upcast(&self) -> &(dyn DefDatabase + 'static) {
2024-01-18 12:59:49 +00:00
self
}
}
impl Upcast<dyn HirDatabase> for RootDatabase {
#[inline]
fn upcast(&self) -> &(dyn HirDatabase + 'static) {
2024-01-18 12:59:49 +00:00
self
}
}
2020-02-06 11:08:08 +00:00
impl FileLoader for RootDatabase {
2022-07-20 13:02:08 +00:00
fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId> {
FileLoaderDelegate(self).resolve_path(path)
2020-02-06 11:08:08 +00:00
}
fn relevant_crates(&self, file_id: FileId) -> Arc<[CrateId]> {
2020-02-06 11:08:08 +00:00
FileLoaderDelegate(self).relevant_crates(file_id)
}
}
2021-05-17 17:07:10 +00:00
impl salsa::Database for RootDatabase {}
2020-02-06 11:08:08 +00:00
impl Default for RootDatabase {
fn default() -> RootDatabase {
2020-03-10 17:56:15 +00:00
RootDatabase::new(None)
2020-02-06 11:08:08 +00:00
}
}
impl RootDatabase {
pub fn new(lru_capacity: Option<u16>) -> RootDatabase {
2021-08-28 21:05:40 +00:00
let mut db = RootDatabase { storage: ManuallyDrop::new(salsa::Storage::default()) };
2020-02-06 11:08:08 +00:00
db.set_crate_graph_with_durability(Default::default(), Durability::HIGH);
db.set_proc_macros_with_durability(Default::default(), Durability::HIGH);
2020-02-06 11:08:08 +00:00
db.set_local_roots_with_durability(Default::default(), Durability::HIGH);
db.set_library_roots_with_durability(Default::default(), Durability::HIGH);
db.set_expand_proc_attr_macros_with_durability(false, Durability::HIGH);
2024-01-09 19:43:17 +00:00
db.update_base_query_lru_capacities(lru_capacity);
db.setup_syntax_context_root();
2020-02-06 11:08:08 +00:00
db
}
pub fn enable_proc_attr_macros(&mut self) {
self.set_expand_proc_attr_macros_with_durability(true, Durability::HIGH);
}
pub fn update_base_query_lru_capacities(&mut self, lru_capacity: Option<u16>) {
let lru_capacity = lru_capacity.unwrap_or(base_db::DEFAULT_PARSE_LRU_CAP);
2024-01-10 09:53:11 +00:00
base_db::FileTextQuery.in_db_mut(self).set_lru_capacity(DEFAULT_FILE_TEXT_LRU_CAP);
2020-08-13 14:25:38 +00:00
base_db::ParseQuery.in_db_mut(self).set_lru_capacity(lru_capacity);
// macro expansions are usually rather small, so we can afford to keep more of them alive
hir::db::ParseMacroExpansionQuery.in_db_mut(self).set_lru_capacity(4 * lru_capacity);
2024-01-09 19:43:17 +00:00
hir::db::BorrowckQuery.in_db_mut(self).set_lru_capacity(base_db::DEFAULT_BORROWCK_LRU_CAP);
2024-07-22 14:34:59 +00:00
hir::db::BodyWithSourceMapQuery.in_db_mut(self).set_lru_capacity(2048);
}
pub fn update_lru_capacities(&mut self, lru_capacities: &FxHashMap<Box<str>, u16>) {
use hir::db as hir_db;
2024-01-10 09:53:11 +00:00
base_db::FileTextQuery.in_db_mut(self).set_lru_capacity(DEFAULT_FILE_TEXT_LRU_CAP);
base_db::ParseQuery.in_db_mut(self).set_lru_capacity(
lru_capacities
.get(stringify!(ParseQuery))
.copied()
.unwrap_or(base_db::DEFAULT_PARSE_LRU_CAP),
);
hir_db::ParseMacroExpansionQuery.in_db_mut(self).set_lru_capacity(
lru_capacities
.get(stringify!(ParseMacroExpansionQuery))
.copied()
.unwrap_or(4 * base_db::DEFAULT_PARSE_LRU_CAP),
);
2024-01-09 19:43:17 +00:00
hir_db::BorrowckQuery.in_db_mut(self).set_lru_capacity(
lru_capacities
.get(stringify!(BorrowckQuery))
.copied()
.unwrap_or(base_db::DEFAULT_BORROWCK_LRU_CAP),
);
2024-07-22 14:34:59 +00:00
hir::db::BodyWithSourceMapQuery.in_db_mut(self).set_lru_capacity(2048);
}
2020-02-06 11:08:08 +00:00
}
impl salsa::ParallelDatabase for RootDatabase {
fn snapshot(&self) -> salsa::Snapshot<RootDatabase> {
2021-08-28 21:05:40 +00:00
salsa::Snapshot::new(RootDatabase { storage: ManuallyDrop::new(self.storage.snapshot()) })
2020-02-06 11:08:08 +00:00
}
}
#[salsa::query_group(LineIndexDatabaseStorage)]
2021-05-17 17:07:10 +00:00
pub trait LineIndexDatabase: base_db::SourceDatabase {
2020-02-06 11:08:08 +00:00
fn line_index(&self, file_id: FileId) -> Arc<LineIndex>;
}
fn line_index(db: &dyn LineIndexDatabase, file_id: FileId) -> Arc<LineIndex> {
2020-02-06 11:08:08 +00:00
let text = db.file_text(file_id);
Arc::new(LineIndex::new(&text))
2020-02-06 11:08:08 +00:00
}
2021-01-20 14:25:34 +00:00
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum SymbolKind {
2021-12-04 17:01:22 +00:00
Attribute,
BuiltinAttr,
Const,
2021-01-20 14:25:34 +00:00
ConstParam,
2021-12-04 17:18:09 +00:00
Derive,
DeriveHelper,
Enum,
Field,
Function,
Method,
Impl,
Label,
2021-01-20 14:25:34 +00:00
LifetimeParam,
Local,
Macro,
ProcMacro,
Module,
SelfParam,
2022-03-05 22:34:37 +00:00
SelfType,
2021-01-20 14:25:34 +00:00
Static,
Struct,
2021-12-03 16:15:19 +00:00
ToolModule,
2021-01-20 14:25:34 +00:00
Trait,
2023-03-03 15:24:07 +00:00
TraitAlias,
TypeAlias,
TypeParam,
Union,
ValueParam,
Variant,
2021-01-20 14:25:34 +00:00
}
2021-10-12 06:59:39 +00:00
2021-12-07 14:06:56 +00:00
impl From<hir::MacroKind> for SymbolKind {
fn from(it: hir::MacroKind) -> Self {
match it {
hir::MacroKind::Declarative | hir::MacroKind::BuiltIn => SymbolKind::Macro,
hir::MacroKind::ProcMacro => SymbolKind::ProcMacro,
2021-12-07 14:06:56 +00:00
hir::MacroKind::Derive => SymbolKind::Derive,
hir::MacroKind::Attr => SymbolKind::Attribute,
}
}
}
2023-05-02 09:56:48 +00:00
impl From<hir::ModuleDefId> for SymbolKind {
fn from(it: hir::ModuleDefId) -> Self {
2022-01-12 18:56:47 +00:00
match it {
2023-05-02 09:56:48 +00:00
hir::ModuleDefId::ConstId(..) => SymbolKind::Const,
hir::ModuleDefId::EnumVariantId(..) => SymbolKind::Variant,
hir::ModuleDefId::FunctionId(..) => SymbolKind::Function,
hir::ModuleDefId::MacroId(hir::MacroId::ProcMacroId(..)) => SymbolKind::ProcMacro,
2023-05-02 09:56:48 +00:00
hir::ModuleDefId::MacroId(..) => SymbolKind::Macro,
hir::ModuleDefId::ModuleId(..) => SymbolKind::Module,
hir::ModuleDefId::StaticId(..) => SymbolKind::Static,
hir::ModuleDefId::AdtId(hir::AdtId::StructId(..)) => SymbolKind::Struct,
hir::ModuleDefId::AdtId(hir::AdtId::EnumId(..)) => SymbolKind::Enum,
hir::ModuleDefId::AdtId(hir::AdtId::UnionId(..)) => SymbolKind::Union,
hir::ModuleDefId::TraitId(..) => SymbolKind::Trait,
hir::ModuleDefId::TraitAliasId(..) => SymbolKind::TraitAlias,
hir::ModuleDefId::TypeAliasId(..) => SymbolKind::TypeAlias,
hir::ModuleDefId::BuiltinType(..) => SymbolKind::TypeAlias,
2022-01-12 18:56:47 +00:00
}
}
}
2022-03-06 18:01:30 +00:00
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SnippetCap {
_private: (),
}
impl SnippetCap {
pub const fn new(allow_snippets: bool) -> Option<SnippetCap> {
if allow_snippets {
Some(SnippetCap { _private: () })
} else {
None
}
}
}