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

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

246 lines
6.8 KiB
Rust
Raw Normal View History

2020-02-06 07:43:46 -06:00
//! This crate defines the core datastructure representing IDE state -- `RootDatabase`.
//!
//! It is mainly a `HirDatabase` for semantic analysis, plus a `SymbolsDatabase`, for fuzzy search.
2020-02-06 05:07:06 -06:00
#![warn(rust_2018_idioms, unused_lifetimes, semicolon_in_expressions_from_macros)]
mod apply_change;
2021-10-12 01:59:39 -05:00
2022-03-06 12:01:30 -06:00
pub mod active_parameter;
pub mod assists;
2020-02-06 09:23:28 -06:00
pub mod defs;
2022-03-06 12:01:30 -06:00
pub mod famous_defs;
pub mod helpers;
2021-03-02 17:26:53 -06:00
pub mod items_locator;
2022-03-06 12:01:30 -06:00
pub mod label;
pub mod line_index;
pub mod path_transform;
pub mod rename;
pub mod rust_doc;
pub mod search;
2020-05-06 04:31:26 -05:00
pub mod source_change;
2022-03-06 12:01:30 -06:00
pub mod symbol_index;
pub mod traits;
2022-03-06 12:01:30 -06:00
pub mod ty_filter;
2022-06-22 09:29:59 -05:00
pub mod use_trivial_contructor;
2020-02-06 05:17:40 -06:00
2022-03-06 12:01:30 -06: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 node_ext;
pub mod insert_whitespace_into_node;
pub mod format_string;
pub use parser::LexedStr;
2022-03-06 12:01:30 -06:00
}
2021-08-28 16:05:40 -05:00
use std::{fmt, mem::ManuallyDrop, sync::Arc};
2020-02-06 05:08:08 -06:00
2020-08-13 09:25:38 -05:00
use base_db::{
salsa::{self, Durability},
2021-05-17 12:07:10 -05:00
AnchoredPath, CrateId, FileId, FileLoader, FileLoaderDelegate, SourceDatabase, Upcast,
2020-02-06 05:08:08 -06:00
};
2022-01-12 12:56:47 -06:00
use hir::{
db::{AstDatabase, DefDatabase, HirDatabase},
symbols::FileSymbolKind,
};
2020-02-06 05:08:08 -06:00
2020-03-10 12:56:15 -05:00
use crate::{line_index::LineIndex, symbol_index::SymbolsDatabase};
pub use rustc_hash::{FxHashMap, FxHashSet, FxHasher};
2020-02-06 05:08:08 -06:00
2020-10-24 03:39:57 -05: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 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>>;
2020-02-06 05:08:08 -06:00
#[salsa::database(
2020-08-13 09:25:38 -05:00
base_db::SourceDatabaseExtStorage,
2022-03-06 12:01:30 -06:00
base_db::SourceDatabaseStorage,
2020-02-06 05:08:08 -06:00
hir::db::AstDatabaseStorage,
hir::db::DefDatabaseStorage,
2022-03-06 12:01:30 -06:00
hir::db::HirDatabaseStorage,
hir::db::InternDatabaseStorage,
LineIndexDatabaseStorage,
symbol_index::SymbolsDatabaseStorage
2020-02-06 05:08:08 -06:00
)]
2020-02-06 05:43:56 -06:00
pub struct RootDatabase {
2021-08-28 16:05:40 -05: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 12:01:30 -06:00
unsafe { ManuallyDrop::drop(&mut self.storage) };
2021-08-28 16:05:40 -05:00
}
2020-02-06 05:08:08 -06:00
}
impl fmt::Debug for RootDatabase {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("RootDatabase").finish()
}
}
impl Upcast<dyn AstDatabase> for RootDatabase {
fn upcast(&self) -> &(dyn AstDatabase + 'static) {
&*self
}
}
impl Upcast<dyn DefDatabase> for RootDatabase {
fn upcast(&self) -> &(dyn DefDatabase + 'static) {
&*self
}
}
impl Upcast<dyn HirDatabase> for RootDatabase {
fn upcast(&self) -> &(dyn HirDatabase + 'static) {
&*self
}
}
2020-02-06 05:08:08 -06:00
impl FileLoader for RootDatabase {
fn file_text(&self, file_id: FileId) -> Arc<String> {
FileLoaderDelegate(self).file_text(file_id)
}
2022-07-20 08:02:08 -05:00
fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId> {
FileLoaderDelegate(self).resolve_path(path)
2020-02-06 05:08:08 -06:00
}
2020-06-11 04:30:06 -05:00
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
2020-02-06 05:08:08 -06:00
FileLoaderDelegate(self).relevant_crates(file_id)
}
}
2021-05-17 12:07:10 -05:00
impl salsa::Database for RootDatabase {}
2020-02-06 05:08:08 -06:00
impl Default for RootDatabase {
fn default() -> RootDatabase {
2020-03-10 12:56:15 -05:00
RootDatabase::new(None)
2020-02-06 05:08:08 -06:00
}
}
impl RootDatabase {
2020-03-10 12:56:15 -05:00
pub fn new(lru_capacity: Option<usize>) -> RootDatabase {
2021-08-28 16:05:40 -05:00
let mut db = RootDatabase { storage: ManuallyDrop::new(salsa::Storage::default()) };
2020-02-06 05:08:08 -06:00
db.set_crate_graph_with_durability(Default::default(), Durability::HIGH);
db.set_local_roots_with_durability(Default::default(), Durability::HIGH);
db.set_library_roots_with_durability(Default::default(), Durability::HIGH);
2022-03-06 12:01:30 -06:00
db.set_enable_proc_attr_macros(false);
db.update_lru_capacity(lru_capacity);
2020-02-06 05:08:08 -06:00
db
}
pub fn update_lru_capacity(&mut self, lru_capacity: Option<usize>) {
2020-08-13 09:25:38 -05:00
let lru_capacity = lru_capacity.unwrap_or(base_db::DEFAULT_LRU_CAP);
base_db::ParseQuery.in_db_mut(self).set_lru_capacity(lru_capacity);
hir::db::ParseMacroExpansionQuery.in_db_mut(self).set_lru_capacity(lru_capacity);
hir::db::MacroExpandQuery.in_db_mut(self).set_lru_capacity(lru_capacity);
}
2020-02-06 05:08:08 -06:00
}
impl salsa::ParallelDatabase for RootDatabase {
fn snapshot(&self) -> salsa::Snapshot<RootDatabase> {
2021-08-28 16:05:40 -05:00
salsa::Snapshot::new(RootDatabase { storage: ManuallyDrop::new(self.storage.snapshot()) })
2020-02-06 05:08:08 -06:00
}
}
#[salsa::query_group(LineIndexDatabaseStorage)]
2021-05-17 12:07:10 -05:00
pub trait LineIndexDatabase: base_db::SourceDatabase {
2020-02-06 05:08:08 -06:00
fn line_index(&self, file_id: FileId) -> Arc<LineIndex>;
}
fn line_index(db: &dyn LineIndexDatabase, file_id: FileId) -> Arc<LineIndex> {
2020-02-06 05:08:08 -06:00
let text = db.file_text(file_id);
Arc::new(LineIndex::new(&*text))
}
2021-01-20 08:25:34 -06:00
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum SymbolKind {
2021-12-04 11:01:22 -06:00
Attribute,
BuiltinAttr,
Const,
2021-01-20 08:25:34 -06:00
ConstParam,
2021-12-04 11:18:09 -06:00
Derive,
Enum,
Field,
Function,
Impl,
Label,
2021-01-20 08:25:34 -06:00
LifetimeParam,
Local,
Macro,
Module,
SelfParam,
2022-03-05 16:34:37 -06:00
SelfType,
2021-01-20 08:25:34 -06:00
Static,
Struct,
2021-12-03 10:15:19 -06:00
ToolModule,
2021-01-20 08:25:34 -06:00
Trait,
TypeAlias,
TypeParam,
Union,
ValueParam,
Variant,
2021-01-20 08:25:34 -06:00
}
2021-10-12 01:59:39 -05:00
2021-12-07 08:06:56 -06:00
impl From<hir::MacroKind> for SymbolKind {
fn from(it: hir::MacroKind) -> Self {
match it {
hir::MacroKind::Declarative | hir::MacroKind::BuiltIn | hir::MacroKind::ProcMacro => {
SymbolKind::Macro
}
hir::MacroKind::Derive => SymbolKind::Derive,
hir::MacroKind::Attr => SymbolKind::Attribute,
}
}
}
2022-01-12 12:56:47 -06:00
impl From<FileSymbolKind> for SymbolKind {
fn from(it: FileSymbolKind) -> Self {
match it {
FileSymbolKind::Const => SymbolKind::Const,
FileSymbolKind::Enum => SymbolKind::Enum,
FileSymbolKind::Function => SymbolKind::Function,
FileSymbolKind::Macro => SymbolKind::Macro,
FileSymbolKind::Module => SymbolKind::Module,
FileSymbolKind::Static => SymbolKind::Static,
FileSymbolKind::Struct => SymbolKind::Struct,
FileSymbolKind::Trait => SymbolKind::Trait,
FileSymbolKind::TypeAlias => SymbolKind::TypeAlias,
FileSymbolKind::Union => SymbolKind::Union,
}
}
}
2022-03-06 12:01:30 -06: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
}
}
}
2021-10-12 01:59:39 -05:00
#[cfg(test)]
mod tests {
2021-10-12 02:27:17 -05:00
mod sourcegen_lints;
2021-10-12 01:59:39 -05:00
}