1 //! This module handles fuzzy-searching of functions, structs and other symbols
2 //! by name across the whole workspace and dependencies.
4 //! It works by building an incrementally-updated text-search index of all
5 //! symbols. The backbone of the index is the **awesome** `fst` crate by
8 //! In a nutshell, you give a set of strings to `fst`, and it builds a
9 //! finite state machine describing this set of strings. The strings which
10 //! could fuzzy-match a pattern can also be described by a finite state machine.
11 //! What is freaking cool is that you can now traverse both state machines in
12 //! lock-step to enumerate the strings which are both in the input set and
13 //! fuzz-match the query. Or, more formally, given two languages described by
14 //! FSTs, one can build a product FST which describes the intersection of the
17 //! `fst` does not support cheap updating of the index, but it supports unioning
18 //! of state machines. So, to account for changing source code, we build an FST
19 //! for each library (which is assumed to never change) and an FST for each Rust
20 //! file in the current workspace, and run a query against the union of all
32 salsa::{self, ParallelDatabase},
33 CrateId, FileRange, SourceDatabaseExt, SourceRootId, Upcast,
36 use fst::{self, Streamer};
38 db::{DefDatabase, HirDatabase},
39 AdtId, AssocItemId, AssocItemLoc, DefHasSource, DefWithBodyId, HasSource, HirFileId, ImplId,
40 InFile, ItemContainerId, ItemLoc, ItemTreeNode, Lookup, MacroDef, Module, ModuleDefId,
41 ModuleId, Semantics, TraitId,
43 use rayon::prelude::*;
44 use rustc_hash::FxHashSet;
45 use syntax::{ast::HasName, AstNode, SmolStr, SyntaxNode, SyntaxNodePtr};
47 use crate::{RootDatabase, SymbolKind};
61 pub fn new(query: String) -> Query {
62 let lowercased = query.to_lowercase();
69 case_sensitive: false,
70 limit: usize::max_value(),
74 pub fn only_types(&mut self) {
75 self.only_types = true;
78 pub fn libs(&mut self) {
82 pub fn exact(&mut self) {
86 pub fn case_sensitive(&mut self) {
87 self.case_sensitive = true;
90 pub fn limit(&mut self, limit: usize) {
95 #[salsa::query_group(SymbolsDatabaseStorage)]
96 pub trait SymbolsDatabase: HirDatabase + SourceDatabaseExt + Upcast<dyn HirDatabase> {
97 /// The symbol index for a given module. These modules should only be in source roots that
98 /// are inside local_roots.
99 fn module_symbols(&self, module_id: ModuleId) -> Arc<SymbolIndex>;
101 /// The symbol index for a given source root within library_roots.
102 fn library_symbols(&self, source_root_id: SourceRootId) -> Arc<SymbolIndex>;
104 /// The set of "local" (that is, from the current workspace) roots.
105 /// Files in local roots are assumed to change frequently.
107 fn local_roots(&self) -> Arc<FxHashSet<SourceRootId>>;
109 /// The set of roots for crates.io libraries.
110 /// Files in libraries are assumed to never change.
112 fn library_roots(&self) -> Arc<FxHashSet<SourceRootId>>;
115 fn library_symbols(db: &dyn SymbolsDatabase, source_root_id: SourceRootId) -> Arc<SymbolIndex> {
116 let _p = profile::span("library_symbols");
118 // todo: this could be parallelized, once I figure out how to do that...
120 .source_root_crates(source_root_id)
122 .flat_map(|&krate| module_ids_for_crate(db.upcast(), krate))
123 // we specifically avoid calling SymbolsDatabase::module_symbols here, even they do the same thing,
124 // as the index for a library is not going to really ever change, and we do not want to store each
125 // module's index in salsa.
126 .map(|module_id| SymbolCollector::collect(db, module_id))
130 Arc::new(SymbolIndex::new(symbols))
133 fn module_symbols(db: &dyn SymbolsDatabase, module_id: ModuleId) -> Arc<SymbolIndex> {
134 let _p = profile::span("module_symbols");
135 let symbols = SymbolCollector::collect(db, module_id);
136 Arc::new(SymbolIndex::new(symbols))
139 /// Need to wrap Snapshot to provide `Clone` impl for `map_with`
141 impl<DB: ParallelDatabase> Snap<salsa::Snapshot<DB>> {
142 fn new(db: &DB) -> Self {
146 impl<DB: ParallelDatabase> Clone for Snap<salsa::Snapshot<DB>> {
147 fn clone(&self) -> Snap<salsa::Snapshot<DB>> {
148 Snap(self.0.snapshot())
151 impl<DB> std::ops::Deref for Snap<DB> {
154 fn deref(&self) -> &Self::Target {
159 // Feature: Workspace Symbol
161 // Uses fuzzy-search to find types, modules and functions by name across your
162 // project and dependencies. This is **the** most useful feature, which improves code
163 // navigation tremendously. It mostly works on top of the built-in LSP
164 // functionality, however `#` and `*` symbols can be used to narrow down the
165 // search. Specifically,
167 // - `Foo` searches for `Foo` type in the current workspace
168 // - `foo#` searches for `foo` function in the current workspace
169 // - `Foo*` searches for `Foo` type among dependencies, including `stdlib`
170 // - `foo#*` searches for `foo` function among dependencies
172 // That is, `#` switches from "types" to all symbols, `*` switches from the current
173 // workspace to dependencies.
175 // Note that filtering does not currently work in VSCode due to the editor never
176 // sending the special symbols to the language server. Instead, you can configure
177 // the filtering via the `rust-analyzer.workspace.symbol.search.scope` and
178 // `rust-analyzer.workspace.symbol.search.kind` settings.
181 // | Editor | Shortcut
183 // | VS Code | kbd:[Ctrl+T]
185 pub fn world_symbols(db: &RootDatabase, query: Query) -> Vec<FileSymbol> {
186 let _p = profile::span("world_symbols").detail(|| query.query.clone());
188 let indices: Vec<_> = if query.libs {
191 .map_with(Snap::new(db), |snap, &root| snap.library_symbols(root))
194 let mut module_ids = Vec::new();
196 for &root in db.local_roots().iter() {
197 let crates = db.source_root_crates(root);
198 for &krate in crates.iter() {
199 module_ids.extend(module_ids_for_crate(db, krate));
205 .map_with(Snap::new(db), |snap, &module_id| snap.module_symbols(module_id))
209 query.search(&indices)
212 pub fn crate_symbols(db: &RootDatabase, krate: CrateId, query: Query) -> Vec<FileSymbol> {
213 let _p = profile::span("crate_symbols").detail(|| format!("{:?}", query));
215 let module_ids = module_ids_for_crate(db, krate);
216 let indices: Vec<_> = module_ids
218 .map_with(Snap::new(db), |snap, &module_id| snap.module_symbols(module_id))
221 query.search(&indices)
224 fn module_ids_for_crate(db: &dyn DefDatabase, krate: CrateId) -> Vec<ModuleId> {
225 let def_map = db.crate_def_map(krate);
226 def_map.modules().map(|(id, _)| def_map.module_id(id)).collect()
229 pub fn index_resolve(db: &RootDatabase, name: &str) -> Vec<FileSymbol> {
230 let mut query = Query::new(name.to_string());
233 world_symbols(db, query)
237 pub struct SymbolIndex {
238 symbols: Vec<FileSymbol>,
239 map: fst::Map<Vec<u8>>,
242 impl fmt::Debug for SymbolIndex {
243 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
244 f.debug_struct("SymbolIndex").field("n_symbols", &self.symbols.len()).finish()
248 impl PartialEq for SymbolIndex {
249 fn eq(&self, other: &SymbolIndex) -> bool {
250 self.symbols == other.symbols
254 impl Eq for SymbolIndex {}
256 impl Hash for SymbolIndex {
257 fn hash<H: Hasher>(&self, hasher: &mut H) {
258 self.symbols.hash(hasher)
263 fn new(mut symbols: Vec<FileSymbol>) -> SymbolIndex {
264 fn cmp(lhs: &FileSymbol, rhs: &FileSymbol) -> Ordering {
265 let lhs_chars = lhs.name.chars().map(|c| c.to_ascii_lowercase());
266 let rhs_chars = rhs.name.chars().map(|c| c.to_ascii_lowercase());
267 lhs_chars.cmp(rhs_chars)
270 symbols.par_sort_by(cmp);
272 let mut builder = fst::MapBuilder::memory();
274 let mut last_batch_start = 0;
276 for idx in 0..symbols.len() {
277 if let Some(next_symbol) = symbols.get(idx + 1) {
278 if cmp(&symbols[last_batch_start], next_symbol) == Ordering::Equal {
283 let start = last_batch_start;
285 last_batch_start = end;
287 let key = symbols[start].name.as_str().to_ascii_lowercase();
288 let value = SymbolIndex::range_to_map_value(start, end);
290 builder.insert(key, value).unwrap();
293 let map = fst::Map::new(builder.into_inner().unwrap()).unwrap();
294 SymbolIndex { symbols, map }
297 pub fn len(&self) -> usize {
301 pub fn memory_size(&self) -> usize {
302 self.map.as_fst().size() + self.symbols.len() * mem::size_of::<FileSymbol>()
305 fn range_to_map_value(start: usize, end: usize) -> u64 {
306 debug_assert![start <= (std::u32::MAX as usize)];
307 debug_assert![end <= (std::u32::MAX as usize)];
309 ((start as u64) << 32) | end as u64
312 fn map_value_to_range(value: u64) -> (usize, usize) {
313 let end = value as u32 as usize;
314 let start = (value >> 32) as usize;
320 pub(crate) fn search(self, indices: &[Arc<SymbolIndex>]) -> Vec<FileSymbol> {
321 let _p = profile::span("symbol_index::Query::search");
322 let mut op = fst::map::OpBuilder::new();
323 for file_symbols in indices.iter() {
324 let automaton = fst::automaton::Subsequence::new(&self.lowercased);
325 op = op.add(file_symbols.map.search(automaton))
327 let mut stream = op.union();
328 let mut res = Vec::new();
329 while let Some((_, indexed_values)) = stream.next() {
330 for indexed_value in indexed_values {
331 let symbol_index = &indices[indexed_value.index];
332 let (start, end) = SymbolIndex::map_value_to_range(indexed_value.value);
334 for symbol in &symbol_index.symbols[start..end] {
335 if self.only_types && !symbol.kind.is_type() {
339 if symbol.name != self.query {
342 } else if self.case_sensitive {
343 if self.query.chars().any(|c| !symbol.name.contains(c)) {
348 res.push(symbol.clone());
349 if res.len() >= self.limit {
359 /// The actual data that is stored in the index. It should be as compact as
361 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
362 pub struct FileSymbol {
364 pub loc: DeclarationLocation,
365 pub kind: FileSymbolKind,
366 pub container_name: Option<SmolStr>,
369 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
370 pub struct DeclarationLocation {
371 /// The file id for both the `ptr` and `name_ptr`.
372 pub hir_file_id: HirFileId,
373 /// This points to the whole syntax node of the declaration.
374 pub ptr: SyntaxNodePtr,
375 /// This points to the [`syntax::ast::Name`] identifier of the declaration.
376 pub name_ptr: SyntaxNodePtr,
379 impl DeclarationLocation {
380 pub fn syntax(&self, semantics: &Semantics<'_, RootDatabase>) -> Option<SyntaxNode> {
381 let root = semantics.parse_or_expand(self.hir_file_id)?;
382 Some(self.ptr.to_node(&root))
385 pub fn original_range(&self, db: &dyn HirDatabase) -> Option<FileRange> {
386 find_original_file_range(db, self.hir_file_id, &self.ptr)
389 pub fn original_name_range(&self, db: &dyn HirDatabase) -> Option<FileRange> {
390 find_original_file_range(db, self.hir_file_id, &self.name_ptr)
394 fn find_original_file_range(
395 db: &dyn HirDatabase,
398 ) -> Option<FileRange> {
399 let root = db.parse_or_expand(file_id)?;
400 let node = ptr.to_node(&root);
401 let node = InFile::new(file_id, &node);
403 Some(node.original_file_range(db.upcast()))
406 #[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
407 pub enum FileSymbolKind {
420 impl FileSymbolKind {
421 fn is_type(self: FileSymbolKind) -> bool {
424 FileSymbolKind::Struct
425 | FileSymbolKind::Enum
426 | FileSymbolKind::Trait
427 | FileSymbolKind::TypeAlias
428 | FileSymbolKind::Union
433 impl From<FileSymbolKind> for SymbolKind {
434 fn from(it: FileSymbolKind) -> Self {
436 FileSymbolKind::Const => SymbolKind::Const,
437 FileSymbolKind::Enum => SymbolKind::Enum,
438 FileSymbolKind::Function => SymbolKind::Function,
439 FileSymbolKind::Macro => SymbolKind::Macro,
440 FileSymbolKind::Module => SymbolKind::Module,
441 FileSymbolKind::Static => SymbolKind::Static,
442 FileSymbolKind::Struct => SymbolKind::Struct,
443 FileSymbolKind::Trait => SymbolKind::Trait,
444 FileSymbolKind::TypeAlias => SymbolKind::TypeAlias,
445 FileSymbolKind::Union => SymbolKind::Union,
450 /// Represents an outstanding module that the symbol collector must collect symbols from.
451 struct SymbolCollectorWork {
453 parent: Option<DefWithBodyId>,
456 struct SymbolCollector<'a> {
457 db: &'a dyn SymbolsDatabase,
458 symbols: Vec<FileSymbol>,
459 work: Vec<SymbolCollectorWork>,
460 current_container_name: Option<SmolStr>,
463 /// Given a [`ModuleId`] and a [`SymbolsDatabase`], use the DefMap for the module's crate to collect all symbols that should be
464 /// indexed for the given module.
465 impl<'a> SymbolCollector<'a> {
466 fn collect(db: &dyn SymbolsDatabase, module_id: ModuleId) -> Vec<FileSymbol> {
467 let mut symbol_collector = SymbolCollector {
469 symbols: Default::default(),
470 current_container_name: None,
471 // The initial work is the root module we're collecting, additional work will
472 // be populated as we traverse the module's definitions.
473 work: vec![SymbolCollectorWork { module_id, parent: None }],
476 while let Some(work) = symbol_collector.work.pop() {
477 symbol_collector.do_work(work);
480 symbol_collector.symbols
483 fn do_work(&mut self, work: SymbolCollectorWork) {
484 self.db.unwind_if_cancelled();
486 let parent_name = work.parent.and_then(|id| self.def_with_body_id_name(id));
487 self.with_container_name(parent_name, |s| s.collect_from_module(work.module_id));
490 fn collect_from_module(&mut self, module_id: ModuleId) {
491 let def_map = module_id.def_map(self.db.upcast());
492 let scope = &def_map[module_id.local_id].scope;
494 for module_def_id in scope.declarations() {
495 match module_def_id {
496 ModuleDefId::ModuleId(id) => self.push_module(id),
497 ModuleDefId::FunctionId(id) => {
498 self.push_decl_assoc(id, FileSymbolKind::Function);
499 self.collect_from_body(id);
501 ModuleDefId::AdtId(AdtId::StructId(id)) => {
502 self.push_decl(id, FileSymbolKind::Struct)
504 ModuleDefId::AdtId(AdtId::EnumId(id)) => self.push_decl(id, FileSymbolKind::Enum),
505 ModuleDefId::AdtId(AdtId::UnionId(id)) => self.push_decl(id, FileSymbolKind::Union),
506 ModuleDefId::ConstId(id) => {
507 self.push_decl_assoc(id, FileSymbolKind::Const);
508 self.collect_from_body(id);
510 ModuleDefId::StaticId(id) => {
511 self.push_decl_assoc(id, FileSymbolKind::Static);
512 self.collect_from_body(id);
514 ModuleDefId::TraitId(id) => {
515 self.push_decl(id, FileSymbolKind::Trait);
516 self.collect_from_trait(id);
518 ModuleDefId::TypeAliasId(id) => {
519 self.push_decl_assoc(id, FileSymbolKind::TypeAlias);
521 // Don't index these.
522 ModuleDefId::BuiltinType(_) => {}
523 ModuleDefId::EnumVariantId(_) => {}
527 for impl_id in scope.impls() {
528 self.collect_from_impl(impl_id);
531 for const_id in scope.unnamed_consts() {
532 self.collect_from_body(const_id);
535 for macro_def_id in scope.macro_declarations() {
536 self.push_decl_macro(macro_def_id.into());
540 fn collect_from_body(&mut self, body_id: impl Into<DefWithBodyId>) {
541 let body_id = body_id.into();
542 let body = self.db.body(body_id);
544 // Descend into the blocks and enqueue collection of all modules within.
545 for (_, def_map) in body.blocks(self.db.upcast()) {
546 for (id, _) in def_map.modules() {
547 self.work.push(SymbolCollectorWork {
548 module_id: def_map.module_id(id),
549 parent: Some(body_id),
555 fn collect_from_impl(&mut self, impl_id: ImplId) {
556 let impl_data = self.db.impl_data(impl_id);
557 for &assoc_item_id in &impl_data.items {
558 self.push_assoc_item(assoc_item_id)
562 fn collect_from_trait(&mut self, trait_id: TraitId) {
563 let trait_data = self.db.trait_data(trait_id);
564 self.with_container_name(trait_data.name.as_text(), |s| {
565 for &(_, assoc_item_id) in &trait_data.items {
566 s.push_assoc_item(assoc_item_id);
571 fn with_container_name(&mut self, container_name: Option<SmolStr>, f: impl FnOnce(&mut Self)) {
572 if let Some(container_name) = container_name {
573 let prev = self.current_container_name.replace(container_name);
575 self.current_container_name = prev;
581 fn current_container_name(&self) -> Option<SmolStr> {
582 self.current_container_name.clone()
585 fn def_with_body_id_name(&self, body_id: DefWithBodyId) -> Option<SmolStr> {
587 DefWithBodyId::FunctionId(id) => Some(
588 id.lookup(self.db.upcast()).source(self.db.upcast()).value.name()?.text().into(),
590 DefWithBodyId::StaticId(id) => Some(
591 id.lookup(self.db.upcast()).source(self.db.upcast()).value.name()?.text().into(),
593 DefWithBodyId::ConstId(id) => Some(
594 id.lookup(self.db.upcast()).source(self.db.upcast()).value.name()?.text().into(),
599 fn push_assoc_item(&mut self, assoc_item_id: AssocItemId) {
600 match assoc_item_id {
601 AssocItemId::FunctionId(id) => self.push_decl_assoc(id, FileSymbolKind::Function),
602 AssocItemId::ConstId(id) => self.push_decl_assoc(id, FileSymbolKind::Const),
603 AssocItemId::TypeAliasId(id) => self.push_decl_assoc(id, FileSymbolKind::TypeAlias),
607 fn push_decl_assoc<L, T>(&mut self, id: L, kind: FileSymbolKind)
609 L: Lookup<Data = AssocItemLoc<T>>,
611 <T as ItemTreeNode>::Source: HasName,
613 fn container_name(db: &dyn HirDatabase, container: ItemContainerId) -> Option<SmolStr> {
615 ItemContainerId::ModuleId(module_id) => {
616 let module = Module::from(module_id);
617 module.name(db).and_then(|name| name.as_text())
619 ItemContainerId::TraitId(trait_id) => {
620 let trait_data = db.trait_data(trait_id);
621 trait_data.name.as_text()
623 ItemContainerId::ImplId(_) | ItemContainerId::ExternBlockId(_) => None,
627 self.push_file_symbol(|s| {
628 let loc = id.lookup(s.db.upcast());
629 let source = loc.source(s.db.upcast());
630 let name_node = source.value.name()?;
632 container_name(s.db.upcast(), loc.container).or_else(|| s.current_container_name());
635 name: name_node.text().into(),
638 loc: DeclarationLocation {
639 hir_file_id: source.file_id,
640 ptr: SyntaxNodePtr::new(source.value.syntax()),
641 name_ptr: SyntaxNodePtr::new(name_node.syntax()),
647 fn push_decl<L, T>(&mut self, id: L, kind: FileSymbolKind)
649 L: Lookup<Data = ItemLoc<T>>,
651 <T as ItemTreeNode>::Source: HasName,
653 self.push_file_symbol(|s| {
654 let loc = id.lookup(s.db.upcast());
655 let source = loc.source(s.db.upcast());
656 let name_node = source.value.name()?;
659 name: name_node.text().into(),
661 container_name: s.current_container_name(),
662 loc: DeclarationLocation {
663 hir_file_id: source.file_id,
664 ptr: SyntaxNodePtr::new(source.value.syntax()),
665 name_ptr: SyntaxNodePtr::new(name_node.syntax()),
671 fn push_module(&mut self, module_id: ModuleId) {
672 self.push_file_symbol(|s| {
673 let def_map = module_id.def_map(s.db.upcast());
674 let module_data = &def_map[module_id.local_id];
675 let declaration = module_data.origin.declaration()?;
676 let module = declaration.to_node(s.db.upcast());
677 let name_node = module.name()?;
680 name: name_node.text().into(),
681 kind: FileSymbolKind::Module,
682 container_name: s.current_container_name(),
683 loc: DeclarationLocation {
684 hir_file_id: declaration.file_id,
685 ptr: SyntaxNodePtr::new(module.syntax()),
686 name_ptr: SyntaxNodePtr::new(name_node.syntax()),
692 fn push_decl_macro(&mut self, macro_def: MacroDef) {
693 self.push_file_symbol(|s| {
694 let name = macro_def.name(s.db.upcast())?.as_text()?;
695 let source = macro_def.source(s.db.upcast())?;
697 let (ptr, name_ptr) = match source.value {
699 (SyntaxNodePtr::new(m.syntax()), SyntaxNodePtr::new(m.name()?.syntax()))
701 Either::Right(f) => {
702 (SyntaxNodePtr::new(f.syntax()), SyntaxNodePtr::new(f.name()?.syntax()))
708 kind: FileSymbolKind::Macro,
709 container_name: s.current_container_name(),
710 loc: DeclarationLocation { hir_file_id: source.file_id, name_ptr, ptr },
715 fn push_file_symbol(&mut self, f: impl FnOnce(&Self) -> Option<FileSymbol>) {
716 if let Some(file_symbol) = f(self) {
717 self.symbols.push(file_symbol);
725 use base_db::fixture::WithFixture;
726 use expect_test::expect_file;
731 fn test_symbol_index_collection() {
732 let (db, _) = RootDatabase::with_many_files(
736 macro_rules! macro_rules_macro {
740 macro_rules! define_struct {
742 struct StructFromMacro;
768 const CONST: u32 = 1;
769 static STATIC: &'static str = "2";
777 struct StructInUnnamedConst;
782 const CONST_WITH_INNER: () = {
783 struct StructInNamedConst;
795 let symbols: Vec<_> = module_ids_for_crate(db.upcast(), db.test_crate())
798 (module_id, SymbolCollector::collect(&db as &dyn SymbolsDatabase, module_id))
802 expect_file!["./test_data/test_symbol_index_collection.txt"].assert_debug_eq(&symbols);