1 //! This module provides `StaticIndex` which is used for powering
2 //! read-only code browsers and emitting LSIF
4 use std::collections::HashMap;
6 use hir::{db::HirDatabase, Crate, Module, Semantics};
8 base_db::{FileId, FileRange, SourceDatabaseExt},
9 defs::{Definition, IdentClass},
10 FxHashSet, RootDatabase,
12 use syntax::{AstNode, SyntaxKind::*, SyntaxToken, TextRange, T};
15 hover::hover_for_definition,
16 moniker::{crate_for_file, def_to_moniker, MonikerResult},
17 Analysis, Fold, HoverConfig, HoverDocFormat, HoverResult, InlayHint, InlayHintsConfig,
21 /// A static representation of fully analyzed source code.
23 /// The intended use-case is powering read-only code browsers and emitting LSIF
25 pub struct StaticIndex<'a> {
26 pub files: Vec<StaticIndexedFile>,
27 pub tokens: TokenStore,
28 analysis: &'a Analysis,
30 def_map: HashMap<Definition, TokenId>,
34 pub struct ReferenceData {
36 pub is_definition: bool,
40 pub struct TokenStaticData {
41 pub hover: Option<HoverResult>,
42 pub definition: Option<FileRange>,
43 pub references: Vec<ReferenceData>,
44 pub moniker: Option<MonikerResult>,
47 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
48 pub struct TokenId(usize);
51 pub fn raw(self) -> usize {
56 #[derive(Default, Debug)]
57 pub struct TokenStore(Vec<TokenStaticData>);
60 pub fn insert(&mut self, data: TokenStaticData) -> TokenId {
61 let id = TokenId(self.0.len());
66 pub fn get_mut(&mut self, id: TokenId) -> Option<&mut TokenStaticData> {
70 pub fn get(&self, id: TokenId) -> Option<&TokenStaticData> {
74 pub fn iter(self) -> impl Iterator<Item = (TokenId, TokenStaticData)> {
75 self.0.into_iter().enumerate().map(|(i, x)| (TokenId(i), x))
80 pub struct StaticIndexedFile {
83 pub inlay_hints: Vec<InlayHint>,
84 pub tokens: Vec<(TextRange, TokenId)>,
87 fn all_modules(db: &dyn HirDatabase) -> Vec<Module> {
88 let mut worklist: Vec<_> =
89 Crate::all(db).into_iter().map(|krate| krate.root_module(db)).collect();
90 let mut modules = Vec::new();
92 while let Some(module) = worklist.pop() {
94 worklist.extend(module.children(db));
100 impl StaticIndex<'_> {
101 fn add_file(&mut self, file_id: FileId) {
102 let current_crate = crate_for_file(self.db, file_id);
103 let folds = self.analysis.folding_ranges(file_id).unwrap();
104 let inlay_hints = self
110 parameter_hints: true,
111 chaining_hints: true,
112 closure_return_type_hints: crate::ClosureReturnTypeHints::WithBlock,
113 lifetime_elision_hints: crate::LifetimeElisionHints::Never,
114 reborrow_hints: crate::ReborrowHints::Never,
115 hide_named_constructor_hints: false,
116 hide_closure_initialization_hints: false,
117 param_names_for_lifetime_elision_hints: false,
118 binding_mode_hints: false,
119 max_length: Some(25),
120 closing_brace_hints_min_lines: Some(25),
127 let sema = hir::Semantics::new(self.db);
128 let tokens_or_nodes = sema.parse(file_id).syntax().clone();
129 let tokens = tokens_or_nodes.descendants_with_tokens().filter_map(|x| match x {
130 syntax::NodeOrToken::Node(_) => None,
131 syntax::NodeOrToken::Token(x) => Some(x),
134 HoverConfig { links_in_hover: true, documentation: Some(HoverDocFormat::Markdown) };
135 let tokens = tokens.filter(|token| {
138 IDENT | INT_NUMBER | LIFETIME_IDENT | T![self] | T![super] | T![crate] | T![Self]
141 let mut result = StaticIndexedFile { file_id, inlay_hints, folds, tokens: vec![] };
142 for token in tokens {
143 let range = token.text_range();
144 let node = token.parent().unwrap();
145 let def = match get_definition(&sema, token.clone()) {
149 let id = if let Some(x) = self.def_map.get(&def) {
152 let x = self.tokens.insert(TokenStaticData {
153 hover: hover_for_definition(&sema, file_id, def, &node, &hover_config),
156 .map(|x| FileRange { file_id: x.file_id, range: x.focus_or_full_range() }),
158 moniker: current_crate.and_then(|cc| def_to_moniker(self.db, def, cc)),
160 self.def_map.insert(def, x);
163 let token = self.tokens.get_mut(id).unwrap();
164 token.references.push(ReferenceData {
165 range: FileRange { range, file_id },
166 is_definition: match def.try_to_nav(self.db) {
167 Some(x) => x.file_id == file_id && x.focus_or_full_range() == range,
171 result.tokens.push((range, id));
173 self.files.push(result);
176 pub fn compute(analysis: &Analysis) -> StaticIndex<'_> {
177 let db = &*analysis.db;
178 let work = all_modules(db).into_iter().filter(|module| {
179 let file_id = module.definition_source(db).file_id.original_file(db);
180 let source_root = db.file_source_root(file_id);
181 let source_root = db.source_root(source_root);
182 !source_root.is_library
184 let mut this = StaticIndex {
186 tokens: Default::default(),
189 def_map: Default::default(),
191 let mut visited_files = FxHashSet::default();
193 let file_id = module.definition_source(db).file_id.original_file(db);
194 if visited_files.contains(&file_id) {
197 this.add_file(file_id);
199 visited_files.insert(file_id);
205 fn get_definition(sema: &Semantics<'_, RootDatabase>, token: SyntaxToken) -> Option<Definition> {
206 for token in sema.descend_into_macros(token) {
207 let def = IdentClass::classify_token(sema, &token).map(IdentClass::definitions_no_ops);
208 if let Some(&[x]) = def.as_deref() {
219 use crate::{fixture, StaticIndex};
220 use ide_db::base_db::FileRange;
221 use std::collections::HashSet;
222 use syntax::TextSize;
224 fn check_all_ranges(ra_fixture: &str) {
225 let (analysis, ranges) = fixture::annotations_without_marker(ra_fixture);
226 let s = StaticIndex::compute(&analysis);
227 let mut range_set: HashSet<_> = ranges.iter().map(|x| x.0).collect();
229 for (range, _) in f.tokens {
230 let x = FileRange { file_id: f.file_id, range };
231 if !range_set.contains(&x) {
232 panic!("additional range {:?}", x);
234 range_set.remove(&x);
237 if !range_set.is_empty() {
238 panic!("unfound ranges {:?}", range_set);
242 fn check_definitions(ra_fixture: &str) {
243 let (analysis, ranges) = fixture::annotations_without_marker(ra_fixture);
244 let s = StaticIndex::compute(&analysis);
245 let mut range_set: HashSet<_> = ranges.iter().map(|x| x.0).collect();
246 for (_, t) in s.tokens.iter() {
247 if let Some(x) = t.definition {
248 if x.range.start() == TextSize::from(0) {
249 // ignore definitions that are whole of file
252 if !range_set.contains(&x) {
253 panic!("additional definition {:?}", x);
255 range_set.remove(&x);
258 if !range_set.is_empty() {
259 panic!("unfound definitions {:?}", range_set);
264 fn struct_and_enum() {
287 //- /main.rs crate:main deps:foo
296 //- /foo/lib.rs crate:foo
310 #[rustc_builtin_macro]
311 //^^^^^^^^^^^^^^^^^^^