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