2021-09-08 10:56:06 -05:00
|
|
|
//! This module provides `StaticIndex` which is used for powering
|
|
|
|
//! read-only code browsers and emitting LSIF
|
|
|
|
|
2021-09-18 12:44:47 -05:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2021-11-22 06:19:46 -06:00
|
|
|
use hir::{db::HirDatabase, Crate, Module, Semantics};
|
|
|
|
use ide_db::{
|
|
|
|
base_db::{FileId, FileRange, SourceDatabaseExt},
|
2022-01-04 11:29:20 -06:00
|
|
|
defs::{Definition, IdentClass},
|
2022-04-25 11:51:59 -05:00
|
|
|
FxHashSet, RootDatabase,
|
2021-11-22 06:19:46 -06:00
|
|
|
};
|
|
|
|
use syntax::{AstNode, SyntaxKind::*, SyntaxToken, TextRange, T};
|
2021-09-08 06:35:28 -05:00
|
|
|
|
2021-10-01 08:07:11 -05:00
|
|
|
use crate::{
|
2022-05-12 06:39:32 -05:00
|
|
|
hover::hover_for_definition,
|
2022-03-19 13:01:19 -05:00
|
|
|
moniker::{crate_for_file, def_to_moniker, MonikerResult},
|
2022-05-12 06:39:32 -05:00
|
|
|
Analysis, Fold, HoverConfig, HoverDocFormat, HoverResult, InlayHint, InlayHintsConfig,
|
|
|
|
TryToNav,
|
2022-03-19 13:01:19 -05:00
|
|
|
};
|
2021-09-08 06:35:28 -05: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 08:07:11 -05:00
|
|
|
#[derive(Debug)]
|
2021-09-18 12:44:47 -05:00
|
|
|
pub struct StaticIndex<'a> {
|
2021-09-08 06:35:28 -05:00
|
|
|
pub files: Vec<StaticIndexedFile>,
|
2021-09-18 12:44:47 -05:00
|
|
|
pub tokens: TokenStore,
|
|
|
|
analysis: &'a Analysis,
|
|
|
|
db: &'a RootDatabase,
|
|
|
|
def_map: HashMap<Definition, TokenId>,
|
2021-09-08 06:35:28 -05:00
|
|
|
}
|
|
|
|
|
2021-10-01 08:07:11 -05:00
|
|
|
#[derive(Debug)]
|
2021-09-23 07:58:21 -05:00
|
|
|
pub struct ReferenceData {
|
|
|
|
pub range: FileRange,
|
|
|
|
pub is_definition: bool,
|
|
|
|
}
|
|
|
|
|
2021-10-01 08:07:11 -05:00
|
|
|
#[derive(Debug)]
|
2021-09-10 10:30:53 -05:00
|
|
|
pub struct TokenStaticData {
|
|
|
|
pub hover: Option<HoverResult>,
|
2021-09-23 07:58:21 -05:00
|
|
|
pub definition: Option<FileRange>,
|
|
|
|
pub references: Vec<ReferenceData>,
|
2021-11-22 11:44:46 -06:00
|
|
|
pub moniker: Option<MonikerResult>,
|
2021-09-10 10:30:53 -05:00
|
|
|
}
|
|
|
|
|
2021-10-01 08:07:11 -05:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
2021-09-18 12:44:47 -05:00
|
|
|
pub struct TokenId(usize);
|
|
|
|
|
2021-10-01 08:07:11 -05:00
|
|
|
impl TokenId {
|
|
|
|
pub fn raw(self) -> usize {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default, Debug)]
|
2021-09-18 12:44:47 -05: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
|
|
|
|
}
|
|
|
|
|
2021-09-23 07:58:21 -05:00
|
|
|
pub fn get_mut(&mut self, id: TokenId) -> Option<&mut TokenStaticData> {
|
|
|
|
self.0.get_mut(id.0)
|
|
|
|
}
|
|
|
|
|
2021-09-18 12:44:47 -05:00
|
|
|
pub fn get(&self, id: TokenId) -> Option<&TokenStaticData> {
|
|
|
|
self.0.get(id.0)
|
|
|
|
}
|
2021-09-23 07:58:21 -05:00
|
|
|
|
|
|
|
pub fn iter(self) -> impl Iterator<Item = (TokenId, TokenStaticData)> {
|
|
|
|
self.0.into_iter().enumerate().map(|(i, x)| (TokenId(i), x))
|
2021-09-18 12:44:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-01 08:07:11 -05:00
|
|
|
#[derive(Debug)]
|
2021-09-08 06:35:28 -05:00
|
|
|
pub struct StaticIndexedFile {
|
|
|
|
pub file_id: FileId,
|
|
|
|
pub folds: Vec<Fold>,
|
2021-10-01 08:07:11 -05:00
|
|
|
pub inlay_hints: Vec<InlayHint>,
|
2021-09-18 12:44:47 -05:00
|
|
|
pub tokens: Vec<(TextRange, TokenId)>,
|
2021-09-08 06:35:28 -05: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 12:44:47 -05:00
|
|
|
impl StaticIndex<'_> {
|
2021-09-29 07:41:58 -05:00
|
|
|
fn add_file(&mut self, file_id: FileId) {
|
2021-11-22 11:44:46 -06:00
|
|
|
let current_crate = crate_for_file(self.db, file_id);
|
2021-09-29 07:41:58 -05:00
|
|
|
let folds = self.analysis.folding_ranges(file_id).unwrap();
|
2021-10-01 08:07:11 -05:00
|
|
|
let inlay_hints = self
|
|
|
|
.analysis
|
|
|
|
.inlay_hints(
|
|
|
|
&InlayHintsConfig {
|
2022-03-11 14:06:26 -06:00
|
|
|
render_colons: true,
|
2021-10-01 08:07:11 -05:00
|
|
|
type_hints: true,
|
|
|
|
parameter_hints: true,
|
|
|
|
chaining_hints: true,
|
2022-05-28 07:13:25 -05:00
|
|
|
closure_return_type_hints: crate::ClosureReturnTypeHints::WithBlock,
|
2022-05-12 06:39:32 -05:00
|
|
|
lifetime_elision_hints: crate::LifetimeElisionHints::Never,
|
|
|
|
reborrow_hints: crate::ReborrowHints::Never,
|
2021-11-13 17:12:29 -06:00
|
|
|
hide_named_constructor_hints: false,
|
2022-05-15 06:17:52 -05:00
|
|
|
hide_closure_initialization_hints: false,
|
2022-03-19 12:11:56 -05:00
|
|
|
param_names_for_lifetime_elision_hints: false,
|
2022-05-14 07:26:08 -05:00
|
|
|
binding_mode_hints: false,
|
2021-10-01 08:07:11 -05:00
|
|
|
max_length: Some(25),
|
2022-05-13 12:42:59 -05:00
|
|
|
closing_brace_hints_min_lines: Some(25),
|
2021-10-01 08:07:11 -05:00
|
|
|
},
|
|
|
|
file_id,
|
2022-02-11 16:48:01 -06:00
|
|
|
None,
|
2021-10-01 08:07:11 -05:00
|
|
|
)
|
|
|
|
.unwrap();
|
2021-09-18 12:44:47 -05:00
|
|
|
// hovers
|
|
|
|
let sema = hir::Semantics::new(self.db);
|
|
|
|
let tokens_or_nodes = sema.parse(file_id).syntax().clone();
|
|
|
|
let tokens = tokens_or_nodes.descendants_with_tokens().filter_map(|x| match x {
|
|
|
|
syntax::NodeOrToken::Node(_) => None,
|
|
|
|
syntax::NodeOrToken::Token(x) => Some(x),
|
|
|
|
});
|
|
|
|
let hover_config =
|
|
|
|
HoverConfig { links_in_hover: true, documentation: Some(HoverDocFormat::Markdown) };
|
2021-10-16 06:32:55 -05:00
|
|
|
let tokens = tokens.filter(|token| {
|
|
|
|
matches!(
|
|
|
|
token.kind(),
|
2022-03-05 16:20:06 -06:00
|
|
|
IDENT | INT_NUMBER | LIFETIME_IDENT | T![self] | T![super] | T![crate] | T![Self]
|
2021-10-16 06:32:55 -05:00
|
|
|
)
|
2021-09-23 07:58:21 -05:00
|
|
|
});
|
2021-10-01 08:07:11 -05:00
|
|
|
let mut result = StaticIndexedFile { file_id, inlay_hints, folds, tokens: vec![] };
|
2021-09-18 12:44:47 -05:00
|
|
|
for token in tokens {
|
|
|
|
let range = token.text_range();
|
|
|
|
let node = token.parent().unwrap();
|
2021-10-03 07:53:01 -05:00
|
|
|
let def = match get_definition(&sema, token.clone()) {
|
|
|
|
Some(x) => x,
|
|
|
|
None => continue,
|
2021-09-18 12:44:47 -05:00
|
|
|
};
|
|
|
|
let id = if let Some(x) = self.def_map.get(&def) {
|
|
|
|
*x
|
|
|
|
} else {
|
|
|
|
let x = self.tokens.insert(TokenStaticData {
|
2021-09-23 07:58:21 -05:00
|
|
|
hover: hover_for_definition(&sema, file_id, def, &node, &hover_config),
|
|
|
|
definition: def
|
|
|
|
.try_to_nav(self.db)
|
|
|
|
.map(|x| FileRange { file_id: x.file_id, range: x.focus_or_full_range() }),
|
|
|
|
references: vec![],
|
2021-11-22 11:44:46 -06:00
|
|
|
moniker: current_crate.and_then(|cc| def_to_moniker(self.db, def, cc)),
|
2021-09-18 12:44:47 -05:00
|
|
|
});
|
|
|
|
self.def_map.insert(def, x);
|
|
|
|
x
|
|
|
|
};
|
2021-09-23 07:58:21 -05:00
|
|
|
let token = self.tokens.get_mut(id).unwrap();
|
|
|
|
token.references.push(ReferenceData {
|
|
|
|
range: FileRange { range, file_id },
|
2021-10-03 07:53:01 -05:00
|
|
|
is_definition: match def.try_to_nav(self.db) {
|
|
|
|
Some(x) => x.file_id == file_id && x.focus_or_full_range() == range,
|
|
|
|
None => false,
|
2021-09-23 07:58:21 -05:00
|
|
|
},
|
|
|
|
});
|
2021-09-18 12:44:47 -05:00
|
|
|
result.tokens.push((range, id));
|
|
|
|
}
|
|
|
|
self.files.push(result);
|
|
|
|
}
|
2021-09-23 07:58:21 -05:00
|
|
|
|
2021-10-16 06:32:55 -05:00
|
|
|
pub fn compute(analysis: &Analysis) -> StaticIndex {
|
2021-10-01 08:07:11 -05:00
|
|
|
let db = &*analysis.db;
|
2021-09-08 06:35:28 -05:00
|
|
|
let work = all_modules(db).into_iter().filter(|module| {
|
|
|
|
let file_id = module.definition_source(db).file_id.original_file(db);
|
|
|
|
let source_root = db.file_source_root(file_id);
|
|
|
|
let source_root = db.source_root(source_root);
|
|
|
|
!source_root.is_library
|
|
|
|
});
|
2021-09-18 12:44:47 -05:00
|
|
|
let mut this = StaticIndex {
|
|
|
|
files: vec![],
|
|
|
|
tokens: Default::default(),
|
2021-09-23 07:58:21 -05:00
|
|
|
analysis,
|
|
|
|
db,
|
2021-09-18 12:44:47 -05:00
|
|
|
def_map: Default::default(),
|
|
|
|
};
|
2021-09-08 06:35:28 -05:00
|
|
|
let mut visited_files = FxHashSet::default();
|
|
|
|
for module in work {
|
|
|
|
let file_id = module.definition_source(db).file_id.original_file(db);
|
2021-09-10 10:30:53 -05:00
|
|
|
if visited_files.contains(&file_id) {
|
|
|
|
continue;
|
2021-09-08 06:35:28 -05:00
|
|
|
}
|
2021-09-29 07:41:58 -05:00
|
|
|
this.add_file(file_id);
|
2021-09-10 10:30:53 -05:00
|
|
|
// mark the file
|
|
|
|
visited_files.insert(file_id);
|
2021-09-08 06:35:28 -05:00
|
|
|
}
|
2021-09-29 07:41:58 -05:00
|
|
|
this
|
2021-09-08 06:35:28 -05:00
|
|
|
}
|
|
|
|
}
|
2021-09-23 07:58:21 -05:00
|
|
|
|
|
|
|
fn get_definition(sema: &Semantics<RootDatabase>, token: SyntaxToken) -> Option<Definition> {
|
2021-10-12 10:52:31 -05:00
|
|
|
for token in sema.descend_into_macros(token) {
|
2022-01-04 11:29:20 -06:00
|
|
|
let def = IdentClass::classify_token(sema, &token).map(IdentClass::definitions);
|
|
|
|
if let Some(&[x]) = def.as_deref() {
|
|
|
|
return Some(x);
|
2021-09-23 07:58:21 -05:00
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
2021-09-26 04:17:57 -05:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::{fixture, StaticIndex};
|
|
|
|
use ide_db::base_db::FileRange;
|
|
|
|
use std::collections::HashSet;
|
2021-11-22 11:44:46 -06:00
|
|
|
use syntax::TextSize;
|
2021-09-26 04:17:57 -05:00
|
|
|
|
|
|
|
fn check_all_ranges(ra_fixture: &str) {
|
|
|
|
let (analysis, ranges) = fixture::annotations_without_marker(ra_fixture);
|
2021-10-01 08:07:11 -05:00
|
|
|
let s = StaticIndex::compute(&analysis);
|
2021-09-26 04:17:57 -05:00
|
|
|
let mut range_set: HashSet<_> = ranges.iter().map(|x| x.0).collect();
|
|
|
|
for f in s.files {
|
|
|
|
for (range, _) in f.tokens {
|
|
|
|
let x = FileRange { file_id: f.file_id, range };
|
|
|
|
if !range_set.contains(&x) {
|
|
|
|
panic!("additional range {:?}", x);
|
|
|
|
}
|
|
|
|
range_set.remove(&x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !range_set.is_empty() {
|
|
|
|
panic!("unfound ranges {:?}", range_set);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_definitions(ra_fixture: &str) {
|
|
|
|
let (analysis, ranges) = fixture::annotations_without_marker(ra_fixture);
|
2021-10-01 08:07:11 -05:00
|
|
|
let s = StaticIndex::compute(&analysis);
|
2021-09-26 04:17:57 -05:00
|
|
|
let mut range_set: HashSet<_> = ranges.iter().map(|x| x.0).collect();
|
|
|
|
for (_, t) in s.tokens.iter() {
|
|
|
|
if let Some(x) = t.definition {
|
2021-11-22 11:44:46 -06:00
|
|
|
if x.range.start() == TextSize::from(0) {
|
|
|
|
// ignore definitions that are whole of file
|
|
|
|
continue;
|
|
|
|
}
|
2021-09-26 04:17:57 -05:00
|
|
|
if !range_set.contains(&x) {
|
|
|
|
panic!("additional definition {:?}", x);
|
|
|
|
}
|
|
|
|
range_set.remove(&x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !range_set.is_empty() {
|
|
|
|
panic!("unfound definitions {:?}", range_set);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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 11:44:46 -06: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 04:17:57 -05:00
|
|
|
#[test]
|
|
|
|
fn derives() {
|
|
|
|
check_all_ranges(
|
|
|
|
r#"
|
2021-10-28 09:13:37 -05:00
|
|
|
//- minicore:derive
|
2021-09-26 04:17:57 -05:00
|
|
|
#[rustc_builtin_macro]
|
2021-12-03 09:32:14 -06:00
|
|
|
//^^^^^^^^^^^^^^^^^^^
|
2021-09-26 04:17:57 -05:00
|
|
|
pub macro Copy {}
|
|
|
|
//^^^^
|
|
|
|
#[derive(Copy)]
|
|
|
|
//^^^^^^ ^^^^
|
|
|
|
struct Hello(i32);
|
|
|
|
//^^^^^ ^^^
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|