ide/
static_index.rs

1//! This module provides `StaticIndex` which is used for powering
2//! read-only code browsers and emitting LSIF
3
4use arrayvec::ArrayVec;
5use hir::{Crate, Module, Semantics, db::HirDatabase};
6use ide_db::{
7    FileId, FileRange, FxHashMap, FxHashSet, MiniCore, RootDatabase,
8    base_db::{RootQueryDb, SourceDatabase, VfsPath},
9    defs::{Definition, IdentClass},
10    documentation::Documentation,
11    famous_defs::FamousDefs,
12};
13use span::Edition;
14use syntax::{AstNode, SyntaxKind::*, SyntaxNode, SyntaxToken, T, TextRange};
15
16use crate::navigation_target::UpmappingResult;
17use crate::{
18    Analysis, Fold, HoverConfig, HoverResult, InlayHint, InlayHintsConfig, TryToNav,
19    hover::{SubstTyLen, hover_for_definition},
20    inlay_hints::{AdjustmentHintsMode, InlayFieldsToResolve},
21    moniker::{MonikerResult, SymbolInformationKind, def_to_kind, def_to_moniker},
22    parent_module::crates_for,
23};
24
25/// A static representation of fully analyzed source code.
26///
27/// The intended use-case is powering read-only code browsers and emitting LSIF/SCIP.
28#[derive(Debug)]
29pub struct StaticIndex<'a> {
30    pub files: Vec<StaticIndexedFile>,
31    pub tokens: TokenStore,
32    analysis: &'a Analysis,
33    db: &'a RootDatabase,
34    def_map: FxHashMap<Definition, TokenId>,
35}
36
37#[derive(Debug)]
38pub struct ReferenceData {
39    pub range: FileRange,
40    pub is_definition: bool,
41}
42
43#[derive(Debug)]
44pub struct TokenStaticData {
45    pub documentation: Option<Documentation>,
46    pub hover: Option<HoverResult>,
47    pub definition: Option<FileRange>,
48    pub references: Vec<ReferenceData>,
49    pub moniker: Option<MonikerResult>,
50    pub display_name: Option<String>,
51    pub signature: Option<String>,
52    pub kind: SymbolInformationKind,
53}
54
55#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
56pub struct TokenId(usize);
57
58impl TokenId {
59    pub fn raw(self) -> usize {
60        self.0
61    }
62}
63
64#[derive(Default, Debug)]
65pub struct TokenStore(Vec<TokenStaticData>);
66
67impl TokenStore {
68    pub fn insert(&mut self, data: TokenStaticData) -> TokenId {
69        let id = TokenId(self.0.len());
70        self.0.push(data);
71        id
72    }
73
74    pub fn get_mut(&mut self, id: TokenId) -> Option<&mut TokenStaticData> {
75        self.0.get_mut(id.0)
76    }
77
78    pub fn get(&self, id: TokenId) -> Option<&TokenStaticData> {
79        self.0.get(id.0)
80    }
81
82    pub fn iter(self) -> impl Iterator<Item = (TokenId, TokenStaticData)> {
83        self.0.into_iter().enumerate().map(|(id, data)| (TokenId(id), data))
84    }
85}
86
87#[derive(Debug)]
88pub struct StaticIndexedFile {
89    pub file_id: FileId,
90    pub folds: Vec<Fold>,
91    pub inlay_hints: Vec<InlayHint>,
92    pub tokens: Vec<(TextRange, TokenId)>,
93}
94
95fn all_modules(db: &dyn HirDatabase) -> Vec<Module> {
96    let mut worklist: Vec<_> =
97        Crate::all(db).into_iter().map(|krate| krate.root_module()).collect();
98    let mut modules = Vec::new();
99
100    while let Some(module) = worklist.pop() {
101        modules.push(module);
102        worklist.extend(module.children(db));
103    }
104
105    modules
106}
107
108fn documentation_for_definition(
109    sema: &Semantics<'_, RootDatabase>,
110    def: Definition,
111    scope_node: &SyntaxNode,
112) -> Option<Documentation> {
113    let famous_defs = match &def {
114        Definition::BuiltinType(_) => Some(FamousDefs(sema, sema.scope(scope_node)?.krate())),
115        _ => None,
116    };
117
118    def.docs(
119        sema.db,
120        famous_defs.as_ref(),
121        def.krate(sema.db)
122            .unwrap_or_else(|| {
123                (*sema.db.all_crates().last().expect("no crate graph present")).into()
124            })
125            .to_display_target(sema.db),
126    )
127}
128
129// FIXME: This is a weird function
130fn get_definitions(
131    sema: &Semantics<'_, RootDatabase>,
132    token: SyntaxToken,
133) -> Option<ArrayVec<Definition, 2>> {
134    for token in sema.descend_into_macros_exact(token) {
135        let def = IdentClass::classify_token(sema, &token).map(IdentClass::definitions_no_ops);
136        if let Some(defs) = def
137            && !defs.is_empty()
138        {
139            return Some(defs);
140        }
141    }
142    None
143}
144
145pub enum VendoredLibrariesConfig<'a> {
146    Included { workspace_root: &'a VfsPath },
147    Excluded,
148}
149
150impl StaticIndex<'_> {
151    fn add_file(&mut self, file_id: FileId) {
152        let current_crate = crates_for(self.db, file_id).pop().map(Into::into);
153        let folds = self.analysis.folding_ranges(file_id).unwrap();
154        let inlay_hints = self
155            .analysis
156            .inlay_hints(
157                &InlayHintsConfig {
158                    render_colons: true,
159                    discriminant_hints: crate::DiscriminantHints::Fieldless,
160                    type_hints: true,
161                    sized_bound: false,
162                    parameter_hints: true,
163                    generic_parameter_hints: crate::GenericParameterHints {
164                        type_hints: false,
165                        lifetime_hints: false,
166                        const_hints: true,
167                    },
168                    chaining_hints: true,
169                    closure_return_type_hints: crate::ClosureReturnTypeHints::WithBlock,
170                    lifetime_elision_hints: crate::LifetimeElisionHints::Never,
171                    adjustment_hints: crate::AdjustmentHints::Never,
172                    adjustment_hints_disable_reborrows: true,
173                    adjustment_hints_mode: AdjustmentHintsMode::Prefix,
174                    adjustment_hints_hide_outside_unsafe: false,
175                    implicit_drop_hints: false,
176                    hide_named_constructor_hints: false,
177                    hide_closure_initialization_hints: false,
178                    hide_closure_parameter_hints: false,
179                    closure_style: hir::ClosureStyle::ImplFn,
180                    param_names_for_lifetime_elision_hints: false,
181                    binding_mode_hints: false,
182                    max_length: Some(25),
183                    closure_capture_hints: false,
184                    closing_brace_hints_min_lines: Some(25),
185                    fields_to_resolve: InlayFieldsToResolve::empty(),
186                    range_exclusive_hints: false,
187                    minicore: MiniCore::default(),
188                },
189                file_id,
190                None,
191            )
192            .unwrap();
193        // hovers
194        let sema = hir::Semantics::new(self.db);
195        let root = sema.parse_guess_edition(file_id).syntax().clone();
196        let edition = sema
197            .attach_first_edition(file_id)
198            .map(|it| it.edition(self.db))
199            .unwrap_or(Edition::CURRENT);
200        let display_target = match sema.first_crate(file_id) {
201            Some(krate) => krate.to_display_target(sema.db),
202            None => return,
203        };
204        let tokens = root.descendants_with_tokens().filter_map(|it| match it {
205            syntax::NodeOrToken::Node(_) => None,
206            syntax::NodeOrToken::Token(it) => Some(it),
207        });
208        let hover_config = HoverConfig {
209            links_in_hover: true,
210            memory_layout: None,
211            documentation: true,
212            keywords: true,
213            format: crate::HoverDocFormat::Markdown,
214            max_trait_assoc_items_count: None,
215            max_fields_count: Some(5),
216            max_enum_variants_count: Some(5),
217            max_subst_ty_len: SubstTyLen::Unlimited,
218            show_drop_glue: true,
219            minicore: MiniCore::default(),
220        };
221        let tokens = tokens.filter(|token| {
222            matches!(
223                token.kind(),
224                IDENT | INT_NUMBER | LIFETIME_IDENT | T![self] | T![super] | T![crate] | T![Self]
225            )
226        });
227        let mut result = StaticIndexedFile { file_id, inlay_hints, folds, tokens: vec![] };
228
229        let mut add_token = |def: Definition, range: TextRange, scope_node: &SyntaxNode| {
230            let id = if let Some(it) = self.def_map.get(&def) {
231                *it
232            } else {
233                let it = self.tokens.insert(TokenStaticData {
234                    documentation: documentation_for_definition(&sema, def, scope_node),
235                    hover: Some(hover_for_definition(
236                        &sema,
237                        file_id,
238                        def,
239                        None,
240                        scope_node,
241                        None,
242                        false,
243                        &hover_config,
244                        edition,
245                        display_target,
246                    )),
247                    definition: def.try_to_nav(&sema).map(UpmappingResult::call_site).map(|it| {
248                        FileRange { file_id: it.file_id, range: it.focus_or_full_range() }
249                    }),
250                    references: vec![],
251                    moniker: current_crate.and_then(|cc| def_to_moniker(self.db, def, cc)),
252                    display_name: def
253                        .name(self.db)
254                        .map(|name| name.display(self.db, edition).to_string()),
255                    signature: Some(def.label(self.db, display_target)),
256                    kind: def_to_kind(self.db, def),
257                });
258                self.def_map.insert(def, it);
259                it
260            };
261            let token = self.tokens.get_mut(id).unwrap();
262            token.references.push(ReferenceData {
263                range: FileRange { range, file_id },
264                is_definition: match def.try_to_nav(&sema).map(UpmappingResult::call_site) {
265                    Some(it) => it.file_id == file_id && it.focus_or_full_range() == range,
266                    None => false,
267                },
268            });
269            result.tokens.push((range, id));
270        };
271
272        if let Some(module) = sema.file_to_module_def(file_id) {
273            let def = Definition::Module(module);
274            let range = root.text_range();
275            add_token(def, range, &root);
276        }
277
278        for token in tokens {
279            let range = token.text_range();
280            let node = token.parent().unwrap();
281            match hir::attach_db(self.db, || get_definitions(&sema, token.clone())) {
282                Some(it) => {
283                    for i in it {
284                        add_token(i, range, &node);
285                    }
286                }
287                None => continue,
288            };
289        }
290        self.files.push(result);
291    }
292
293    pub fn compute<'a>(
294        analysis: &'a Analysis,
295        vendored_libs_config: VendoredLibrariesConfig<'_>,
296    ) -> StaticIndex<'a> {
297        let db = &analysis.db;
298        hir::attach_db(db, || {
299            let work = all_modules(db).into_iter().filter(|module| {
300                let file_id = module.definition_source_file_id(db).original_file(db);
301                let source_root =
302                    db.file_source_root(file_id.file_id(&analysis.db)).source_root_id(db);
303                let source_root = db.source_root(source_root).source_root(db);
304                let is_vendored = match vendored_libs_config {
305                    VendoredLibrariesConfig::Included { workspace_root } => source_root
306                        .path_for_file(&file_id.file_id(&analysis.db))
307                        .is_some_and(|module_path| module_path.starts_with(workspace_root)),
308                    VendoredLibrariesConfig::Excluded => false,
309                };
310
311                !source_root.is_library || is_vendored
312            });
313            let mut this = StaticIndex {
314                files: vec![],
315                tokens: Default::default(),
316                analysis,
317                db,
318                def_map: Default::default(),
319            };
320            let mut visited_files = FxHashSet::default();
321            for module in work {
322                let file_id = module.definition_source_file_id(db).original_file(db);
323                if visited_files.contains(&file_id) {
324                    continue;
325                }
326                this.add_file(file_id.file_id(&analysis.db));
327                // mark the file
328                visited_files.insert(file_id);
329            }
330            this
331        })
332    }
333}
334
335#[cfg(test)]
336mod tests {
337    use crate::{StaticIndex, fixture};
338    use ide_db::{FileRange, FxHashMap, FxHashSet, base_db::VfsPath};
339    use syntax::TextSize;
340
341    use super::VendoredLibrariesConfig;
342
343    fn check_all_ranges(
344        #[rust_analyzer::rust_fixture] ra_fixture: &str,
345        vendored_libs_config: VendoredLibrariesConfig<'_>,
346    ) {
347        let (analysis, ranges) = fixture::annotations_without_marker(ra_fixture);
348        let s = StaticIndex::compute(&analysis, vendored_libs_config);
349        let mut range_set: FxHashSet<_> = ranges.iter().map(|it| it.0).collect();
350        for f in s.files {
351            for (range, _) in f.tokens {
352                if range.start() == TextSize::from(0) {
353                    // ignore whole file range corresponding to module definition
354                    continue;
355                }
356                let it = FileRange { file_id: f.file_id, range };
357                if !range_set.contains(&it) {
358                    panic!("additional range {it:?}");
359                }
360                range_set.remove(&it);
361            }
362        }
363        if !range_set.is_empty() {
364            panic!("unfound ranges {range_set:?}");
365        }
366    }
367
368    #[track_caller]
369    fn check_definitions(
370        #[rust_analyzer::rust_fixture] ra_fixture: &str,
371        vendored_libs_config: VendoredLibrariesConfig<'_>,
372    ) {
373        let (analysis, ranges) = fixture::annotations_without_marker(ra_fixture);
374        let s = StaticIndex::compute(&analysis, vendored_libs_config);
375        let mut range_set: FxHashSet<_> = ranges.iter().map(|it| it.0).collect();
376        for (_, t) in s.tokens.iter() {
377            if let Some(t) = t.definition {
378                if t.range.start() == TextSize::from(0) {
379                    // ignore definitions that are whole of file
380                    continue;
381                }
382                if !range_set.contains(&t) {
383                    panic!("additional definition {t:?}");
384                }
385                range_set.remove(&t);
386            }
387        }
388        if !range_set.is_empty() {
389            panic!("unfound definitions {range_set:?}");
390        }
391    }
392
393    #[track_caller]
394    fn check_references(
395        #[rust_analyzer::rust_fixture] ra_fixture: &str,
396        vendored_libs_config: VendoredLibrariesConfig<'_>,
397    ) {
398        let (analysis, ranges) = fixture::annotations_without_marker(ra_fixture);
399        let s = StaticIndex::compute(&analysis, vendored_libs_config);
400        let mut range_set: FxHashMap<_, i32> = ranges.iter().map(|it| (it.0, 0)).collect();
401
402        // Make sure that all references have at least one range. We use a HashMap instead of a
403        // a HashSet so that we can have more than one reference at the same range.
404        for (_, t) in s.tokens.iter() {
405            for r in &t.references {
406                if r.is_definition {
407                    continue;
408                }
409                if r.range.range.start() == TextSize::from(0) {
410                    // ignore whole file range corresponding to module definition
411                    continue;
412                }
413                match range_set.entry(r.range) {
414                    std::collections::hash_map::Entry::Occupied(mut entry) => {
415                        let count = entry.get_mut();
416                        *count += 1;
417                    }
418                    std::collections::hash_map::Entry::Vacant(_) => {
419                        panic!("additional reference {r:?}");
420                    }
421                }
422            }
423        }
424        for (range, count) in range_set.iter() {
425            if *count == 0 {
426                panic!("unfound reference {range:?}");
427            }
428        }
429    }
430
431    #[test]
432    fn field_initialization() {
433        check_references(
434            r#"
435struct Point {
436    x: f64,
437     //^^^
438    y: f64,
439     //^^^
440}
441    fn foo() {
442        let x = 5.;
443        let y = 10.;
444        let mut p = Point { x, y };
445                  //^^^^^   ^  ^
446        p.x = 9.;
447      //^ ^
448        p.y = 10.;
449      //^ ^
450    }
451"#,
452            VendoredLibrariesConfig::Included {
453                workspace_root: &VfsPath::new_virtual_path("/workspace".to_owned()),
454            },
455        );
456    }
457
458    #[test]
459    fn struct_and_enum() {
460        check_all_ranges(
461            r#"
462struct Foo;
463     //^^^
464enum E { X(Foo) }
465   //^   ^ ^^^
466"#,
467            VendoredLibrariesConfig::Included {
468                workspace_root: &VfsPath::new_virtual_path("/workspace".to_owned()),
469            },
470        );
471        check_definitions(
472            r#"
473struct Foo;
474     //^^^
475enum E { X(Foo) }
476   //^   ^
477"#,
478            VendoredLibrariesConfig::Included {
479                workspace_root: &VfsPath::new_virtual_path("/workspace".to_owned()),
480            },
481        );
482
483        check_references(
484            r#"
485struct Foo;
486enum E { X(Foo) }
487   //      ^^^
488"#,
489            VendoredLibrariesConfig::Included {
490                workspace_root: &VfsPath::new_virtual_path("/workspace".to_owned()),
491            },
492        );
493    }
494
495    #[test]
496    fn multi_crate() {
497        check_definitions(
498            r#"
499//- /workspace/main.rs crate:main deps:foo
500
501
502use foo::func;
503
504fn main() {
505 //^^^^
506    func();
507}
508//- /workspace/foo/lib.rs crate:foo
509
510pub func() {
511
512}
513"#,
514            VendoredLibrariesConfig::Included {
515                workspace_root: &VfsPath::new_virtual_path("/workspace".to_owned()),
516            },
517        );
518    }
519
520    #[test]
521    fn vendored_crate() {
522        check_all_ranges(
523            r#"
524//- /workspace/main.rs crate:main deps:external,vendored
525struct Main(i32);
526     //^^^^ ^^^
527
528//- /external/lib.rs new_source_root:library crate:external@0.1.0,https://a.b/foo.git library
529struct ExternalLibrary(i32);
530
531//- /workspace/vendored/lib.rs new_source_root:library crate:vendored@0.1.0,https://a.b/bar.git library
532struct VendoredLibrary(i32);
533     //^^^^^^^^^^^^^^^ ^^^
534"#,
535            VendoredLibrariesConfig::Included {
536                workspace_root: &VfsPath::new_virtual_path("/workspace".to_owned()),
537            },
538        );
539    }
540
541    #[test]
542    fn vendored_crate_excluded() {
543        check_all_ranges(
544            r#"
545//- /workspace/main.rs crate:main deps:external,vendored
546struct Main(i32);
547     //^^^^ ^^^
548
549//- /external/lib.rs new_source_root:library crate:external@0.1.0,https://a.b/foo.git library
550struct ExternalLibrary(i32);
551
552//- /workspace/vendored/lib.rs new_source_root:library crate:vendored@0.1.0,https://a.b/bar.git library
553struct VendoredLibrary(i32);
554"#,
555            VendoredLibrariesConfig::Excluded,
556        )
557    }
558
559    #[test]
560    fn derives() {
561        check_all_ranges(
562            r#"
563//- minicore:derive
564#[rustc_builtin_macro]
565//^^^^^^^^^^^^^^^^^^^
566pub macro Copy {}
567        //^^^^
568#[derive(Copy)]
569//^^^^^^ ^^^^
570struct Hello(i32);
571     //^^^^^ ^^^
572"#,
573            VendoredLibrariesConfig::Included {
574                workspace_root: &VfsPath::new_virtual_path("/workspace".to_owned()),
575            },
576        );
577    }
578}