Skip to content

Commit 6463d3a

Browse files
author
Jonas Schievink
committed
symbol_index: allow querying a single crate
1 parent 4bcf8c8 commit 6463d3a

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

crates/ra_ide_db/src/symbol_index.rs

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ use std::{
2929
};
3030

3131
use fst::{self, Streamer};
32+
use hir::db::DefDatabase;
3233
use ra_db::{
3334
salsa::{self, ParallelDatabase},
34-
FileId, SourceDatabaseExt, SourceRootId,
35+
CrateId, FileId, SourceDatabaseExt, SourceRootId,
3536
};
3637
use ra_syntax::{
3738
ast::{self, NameOwner},
@@ -110,6 +111,14 @@ fn file_symbols(db: &impl SymbolsDatabase, file_id: FileId) -> Arc<SymbolIndex>
110111
Arc::new(SymbolIndex::new(symbols))
111112
}
112113

114+
/// Need to wrap Snapshot to provide `Clone` impl for `map_with`
115+
struct Snap(salsa::Snapshot<RootDatabase>);
116+
impl Clone for Snap {
117+
fn clone(&self) -> Snap {
118+
Snap(self.0.snapshot())
119+
}
120+
}
121+
113122
// Feature: Workspace Symbol
114123
//
115124
// Uses fuzzy-search to find types, modules and functions by name across your
@@ -134,14 +143,6 @@ fn file_symbols(db: &impl SymbolsDatabase, file_id: FileId) -> Arc<SymbolIndex>
134143
pub fn world_symbols(db: &RootDatabase, query: Query) -> Vec<FileSymbol> {
135144
let _p = ra_prof::profile("world_symbols").detail(|| query.query.clone());
136145

137-
/// Need to wrap Snapshot to provide `Clone` impl for `map_with`
138-
struct Snap(salsa::Snapshot<RootDatabase>);
139-
impl Clone for Snap {
140-
fn clone(&self) -> Snap {
141-
Snap(self.0.snapshot())
142-
}
143-
}
144-
145146
let buf: Vec<Arc<SymbolIndex>> = if query.libs {
146147
let snap = Snap(db.snapshot());
147148
#[cfg(not(feature = "wasm"))]
@@ -175,6 +176,30 @@ pub fn world_symbols(db: &RootDatabase, query: Query) -> Vec<FileSymbol> {
175176
query.search(&buf)
176177
}
177178

179+
pub fn crate_symbols(db: &RootDatabase, krate: CrateId, query: Query) -> Vec<FileSymbol> {
180+
let def_map = db.crate_def_map(krate);
181+
let mut files = Vec::new();
182+
let mut modules = vec![def_map.root];
183+
while let Some(module) = modules.pop() {
184+
let data = &def_map[module];
185+
files.extend(data.origin.file_id());
186+
modules.extend(data.children.values());
187+
}
188+
189+
let snap = Snap(db.snapshot());
190+
191+
#[cfg(not(feature = "wasm"))]
192+
let buf = files
193+
.par_iter()
194+
.map_with(snap, |db, &file_id| db.0.file_symbols(file_id))
195+
.collect::<Vec<_>>();
196+
197+
#[cfg(feature = "wasm")]
198+
let buf = files.iter().map(|&file_id| snap.0.file_symbols(file_id)).collect::<Vec<_>>();
199+
200+
query.search(&buf)
201+
}
202+
178203
pub fn index_resolve(db: &RootDatabase, name_ref: &ast::NameRef) -> Vec<FileSymbol> {
179204
let name = name_ref.text();
180205
let mut query = Query::new(name.to_string());

0 commit comments

Comments
 (0)