rust-analyzer/crates/ide/src/static_index.rs

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

332 lines
9.9 KiB
Rust
Raw Normal View History

2021-09-08 15:56:06 +00:00
//! This module provides `StaticIndex` which is used for powering
//! read-only code browsers and emitting LSIF
2021-09-18 17:44:47 +00:00
use std::collections::HashMap;
use hir::{db::HirDatabase, Crate, Module, Semantics};
use ide_db::{
base_db::{FileId, FileRange, SourceDatabaseExt},
defs::{Definition, IdentClass},
FxHashSet, RootDatabase,
};
use syntax::{AstNode, SyntaxKind::*, SyntaxToken, TextRange, T};
2021-09-08 11:35:28 +00:00
2021-10-01 13:07:11 +00:00
use crate::{
hover::hover_for_definition,
inlay_hints::AdjustmentHintsMode,
2022-11-07 14:49:26 +00:00
moniker::{def_to_moniker, MonikerResult},
parent_module::crates_for,
Analysis, Fold, HoverConfig, HoverResult, InlayHint, InlayHintsConfig, TryToNav,
2022-03-19 18:01:19 +00:00
};
2021-09-08 11:35:28 +00:00
/// A static representation of fully analyzed source code.
///
/// The intended use-case is powering read-only code browsers and emitting LSIF
2021-10-01 13:07:11 +00:00
#[derive(Debug)]
2021-09-18 17:44:47 +00:00
pub struct StaticIndex<'a> {
2021-09-08 11:35:28 +00:00
pub files: Vec<StaticIndexedFile>,
2021-09-18 17:44:47 +00:00
pub tokens: TokenStore,
analysis: &'a Analysis,
db: &'a RootDatabase,
def_map: HashMap<Definition, TokenId>,
2021-09-08 11:35:28 +00:00
}
2021-10-01 13:07:11 +00:00
#[derive(Debug)]
pub struct ReferenceData {
pub range: FileRange,
pub is_definition: bool,
}
2021-10-01 13:07:11 +00:00
#[derive(Debug)]
2021-09-10 15:30:53 +00:00
pub struct TokenStaticData {
pub hover: Option<HoverResult>,
pub definition: Option<FileRange>,
pub references: Vec<ReferenceData>,
2021-11-22 17:44:46 +00:00
pub moniker: Option<MonikerResult>,
2021-09-10 15:30:53 +00:00
}
2021-10-01 13:07:11 +00:00
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2021-09-18 17:44:47 +00:00
pub struct TokenId(usize);
2021-10-01 13:07:11 +00:00
impl TokenId {
pub fn raw(self) -> usize {
self.0
}
}
#[derive(Default, Debug)]
2021-09-18 17:44:47 +00:00
pub struct TokenStore(Vec<TokenStaticData>);
impl TokenStore {
pub fn insert(&mut self, data: TokenStaticData) -> TokenId {
let id = TokenId(self.0.len());
self.0.push(data);
id
}
pub fn get_mut(&mut self, id: TokenId) -> Option<&mut TokenStaticData> {
self.0.get_mut(id.0)
}
2021-09-18 17:44:47 +00:00
pub fn get(&self, id: TokenId) -> Option<&TokenStaticData> {
self.0.get(id.0)
}
pub fn iter(self) -> impl Iterator<Item = (TokenId, TokenStaticData)> {
2023-07-06 14:03:17 +00:00
self.0.into_iter().enumerate().map(|(id, data)| (TokenId(id), data))
2021-09-18 17:44:47 +00:00
}
}
2021-10-01 13:07:11 +00:00
#[derive(Debug)]
2021-09-08 11:35:28 +00:00
pub struct StaticIndexedFile {
pub file_id: FileId,
pub folds: Vec<Fold>,
2021-10-01 13:07:11 +00:00
pub inlay_hints: Vec<InlayHint>,
2021-09-18 17:44:47 +00:00
pub tokens: Vec<(TextRange, TokenId)>,
2021-09-08 11:35:28 +00:00
}
fn all_modules(db: &dyn HirDatabase) -> Vec<Module> {
let mut worklist: Vec<_> =
Crate::all(db).into_iter().map(|krate| krate.root_module(db)).collect();
let mut modules = Vec::new();
while let Some(module) = worklist.pop() {
modules.push(module);
worklist.extend(module.children(db));
}
modules
}
2021-09-18 17:44:47 +00:00
impl StaticIndex<'_> {
2021-09-29 12:41:58 +00:00
fn add_file(&mut self, file_id: FileId) {
2022-11-07 14:49:26 +00:00
let current_crate = crates_for(self.db, file_id).pop().map(Into::into);
2021-09-29 12:41:58 +00:00
let folds = self.analysis.folding_ranges(file_id).unwrap();
2021-10-01 13:07:11 +00:00
let inlay_hints = self
.analysis
.inlay_hints(
&InlayHintsConfig {
render_colons: true,
2022-12-23 10:28:46 +00:00
discriminant_hints: crate::DiscriminantHints::Fieldless,
2021-10-01 13:07:11 +00:00
type_hints: true,
parameter_hints: true,
chaining_hints: true,
closure_return_type_hints: crate::ClosureReturnTypeHints::WithBlock,
lifetime_elision_hints: crate::LifetimeElisionHints::Never,
adjustment_hints: crate::AdjustmentHints::Never,
adjustment_hints_mode: AdjustmentHintsMode::Prefix,
adjustment_hints_hide_outside_unsafe: false,
hide_named_constructor_hints: false,
hide_closure_initialization_hints: false,
2023-04-06 12:44:38 +00:00
closure_style: hir::ClosureStyle::ImplFn,
param_names_for_lifetime_elision_hints: false,
2022-05-14 12:26:08 +00:00
binding_mode_hints: false,
2021-10-01 13:07:11 +00:00
max_length: Some(25),
2023-05-05 11:34:55 +00:00
closure_capture_hints: false,
closing_brace_hints_min_lines: Some(25),
2021-10-01 13:07:11 +00:00
},
file_id,
2022-02-11 22:48:01 +00:00
None,
2021-10-01 13:07:11 +00:00
)
.unwrap();
2021-09-18 17:44:47 +00:00
// hovers
let sema = hir::Semantics::new(self.db);
let tokens_or_nodes = sema.parse(file_id).syntax().clone();
2023-07-06 14:03:17 +00:00
let tokens = tokens_or_nodes.descendants_with_tokens().filter_map(|it| match it {
2021-09-18 17:44:47 +00:00
syntax::NodeOrToken::Node(_) => None,
2023-07-06 14:03:17 +00:00
syntax::NodeOrToken::Token(it) => Some(it),
2021-09-18 17:44:47 +00:00
});
2022-08-16 14:51:40 +00:00
let hover_config = HoverConfig {
links_in_hover: true,
memory_layout: None,
documentation: true,
2022-08-16 14:51:40 +00:00
keywords: true,
format: crate::HoverDocFormat::Markdown,
2022-08-16 14:51:40 +00:00
};
2021-10-16 11:32:55 +00:00
let tokens = tokens.filter(|token| {
matches!(
token.kind(),
IDENT | INT_NUMBER | LIFETIME_IDENT | T![self] | T![super] | T![crate] | T![Self]
2021-10-16 11:32:55 +00:00
)
});
2021-10-01 13:07:11 +00:00
let mut result = StaticIndexedFile { file_id, inlay_hints, folds, tokens: vec![] };
2021-09-18 17:44:47 +00:00
for token in tokens {
let range = token.text_range();
let node = token.parent().unwrap();
let def = match get_definition(&sema, token.clone()) {
2023-07-06 14:03:17 +00:00
Some(it) => it,
None => continue,
2021-09-18 17:44:47 +00:00
};
2023-07-06 14:03:17 +00:00
let id = if let Some(it) = self.def_map.get(&def) {
*it
2021-09-18 17:44:47 +00:00
} else {
2023-07-06 14:03:17 +00:00
let it = self.tokens.insert(TokenStaticData {
hover: hover_for_definition(&sema, file_id, def, &node, &hover_config),
2023-07-06 14:03:17 +00:00
definition: def.try_to_nav(self.db).map(|it| FileRange {
file_id: it.file_id,
range: it.focus_or_full_range(),
}),
references: vec![],
2021-11-22 17:44:46 +00:00
moniker: current_crate.and_then(|cc| def_to_moniker(self.db, def, cc)),
2021-09-18 17:44:47 +00:00
});
2023-07-06 14:03:17 +00:00
self.def_map.insert(def, it);
it
2021-09-18 17:44:47 +00:00
};
let token = self.tokens.get_mut(id).unwrap();
token.references.push(ReferenceData {
range: FileRange { range, file_id },
is_definition: match def.try_to_nav(self.db) {
2023-07-06 14:03:17 +00:00
Some(it) => it.file_id == file_id && it.focus_or_full_range() == range,
None => false,
},
});
2021-09-18 17:44:47 +00:00
result.tokens.push((range, id));
}
self.files.push(result);
}
2022-07-20 13:02:08 +00:00
pub fn compute(analysis: &Analysis) -> StaticIndex<'_> {
2021-10-01 13:07:11 +00:00
let db = &*analysis.db;
2021-09-08 11:35:28 +00:00
let work = all_modules(db).into_iter().filter(|module| {
2023-06-17 08:58:52 +00:00
let file_id = module.definition_source_file_id(db).original_file(db);
2021-09-08 11:35:28 +00:00
let source_root = db.file_source_root(file_id);
let source_root = db.source_root(source_root);
!source_root.is_library
});
2021-09-18 17:44:47 +00:00
let mut this = StaticIndex {
files: vec![],
tokens: Default::default(),
analysis,
db,
2021-09-18 17:44:47 +00:00
def_map: Default::default(),
};
2021-09-08 11:35:28 +00:00
let mut visited_files = FxHashSet::default();
for module in work {
2023-06-17 08:58:52 +00:00
let file_id = module.definition_source_file_id(db).original_file(db);
2021-09-10 15:30:53 +00:00
if visited_files.contains(&file_id) {
continue;
2021-09-08 11:35:28 +00:00
}
2021-09-29 12:41:58 +00:00
this.add_file(file_id);
2021-09-10 15:30:53 +00:00
// mark the file
visited_files.insert(file_id);
2021-09-08 11:35:28 +00:00
}
2021-09-29 12:41:58 +00:00
this
2021-09-08 11:35:28 +00:00
}
}
2022-07-20 13:02:08 +00:00
fn get_definition(sema: &Semantics<'_, RootDatabase>, token: SyntaxToken) -> Option<Definition> {
for token in sema.descend_into_macros(token) {
let def = IdentClass::classify_token(sema, &token).map(IdentClass::definitions_no_ops);
2023-07-06 14:03:17 +00:00
if let Some(&[it]) = def.as_deref() {
return Some(it);
2022-10-22 12:04:47 +00:00
}
}
None
}
2021-09-26 09:17:57 +00:00
#[cfg(test)]
mod tests {
use crate::{fixture, StaticIndex};
use ide_db::base_db::FileRange;
use std::collections::HashSet;
2021-11-22 17:44:46 +00:00
use syntax::TextSize;
2021-09-26 09:17:57 +00:00
fn check_all_ranges(ra_fixture: &str) {
let (analysis, ranges) = fixture::annotations_without_marker(ra_fixture);
2021-10-01 13:07:11 +00:00
let s = StaticIndex::compute(&analysis);
2023-07-06 14:03:17 +00:00
let mut range_set: HashSet<_> = ranges.iter().map(|it| it.0).collect();
2021-09-26 09:17:57 +00:00
for f in s.files {
for (range, _) in f.tokens {
2023-07-06 14:03:17 +00:00
let it = FileRange { file_id: f.file_id, range };
if !range_set.contains(&it) {
panic!("additional range {it:?}");
2021-09-26 09:17:57 +00:00
}
2023-07-06 14:03:17 +00:00
range_set.remove(&it);
2021-09-26 09:17:57 +00:00
}
}
if !range_set.is_empty() {
panic!("unfound ranges {range_set:?}");
2021-09-26 09:17:57 +00:00
}
}
fn check_definitions(ra_fixture: &str) {
let (analysis, ranges) = fixture::annotations_without_marker(ra_fixture);
2021-10-01 13:07:11 +00:00
let s = StaticIndex::compute(&analysis);
2023-07-06 14:03:17 +00:00
let mut range_set: HashSet<_> = ranges.iter().map(|it| it.0).collect();
2021-09-26 09:17:57 +00:00
for (_, t) in s.tokens.iter() {
2023-07-06 14:03:17 +00:00
if let Some(t) = t.definition {
if t.range.start() == TextSize::from(0) {
2021-11-22 17:44:46 +00:00
// ignore definitions that are whole of file
continue;
}
2023-07-06 14:03:17 +00:00
if !range_set.contains(&t) {
panic!("additional definition {t:?}");
2021-09-26 09:17:57 +00:00
}
2023-07-06 14:03:17 +00:00
range_set.remove(&t);
2021-09-26 09:17:57 +00:00
}
}
if !range_set.is_empty() {
panic!("unfound definitions {range_set:?}");
2021-09-26 09:17:57 +00:00
}
}
#[test]
fn struct_and_enum() {
check_all_ranges(
r#"
struct Foo;
//^^^
enum E { X(Foo) }
//^ ^ ^^^
"#,
);
check_definitions(
r#"
struct Foo;
//^^^
enum E { X(Foo) }
//^ ^
"#,
);
}
2021-11-22 17:44:46 +00:00
#[test]
fn multi_crate() {
check_definitions(
r#"
//- /main.rs crate:main deps:foo
use foo::func;
fn main() {
//^^^^
func();
}
//- /foo/lib.rs crate:foo
pub func() {
}
"#,
);
}
2021-09-26 09:17:57 +00:00
#[test]
fn derives() {
check_all_ranges(
r#"
//- minicore:derive
2021-09-26 09:17:57 +00:00
#[rustc_builtin_macro]
//^^^^^^^^^^^^^^^^^^^
2021-09-26 09:17:57 +00:00
pub macro Copy {}
//^^^^
#[derive(Copy)]
//^^^^^^ ^^^^
struct Hello(i32);
//^^^^^ ^^^
"#,
);
}
}