diff options
author | Jonas Schievink <[email protected]> | 2020-06-09 17:47:14 +0100 |
---|---|---|
committer | Jonas Schievink <[email protected]> | 2020-06-10 11:38:58 +0100 |
commit | 6463d3ac63a479e33d923593e720696b38a1a54c (patch) | |
tree | b7ba6f485573ce39ab7b5494732875240a703840 /crates/ra_ide_db | |
parent | 4bcf8c8c68bd791f295aa06ef7903c006be3f356 (diff) |
symbol_index: allow querying a single crate
Diffstat (limited to 'crates/ra_ide_db')
-rw-r--r-- | crates/ra_ide_db/src/symbol_index.rs | 43 |
1 files changed, 34 insertions, 9 deletions
diff --git a/crates/ra_ide_db/src/symbol_index.rs b/crates/ra_ide_db/src/symbol_index.rs index 78c714356..c974891ea 100644 --- a/crates/ra_ide_db/src/symbol_index.rs +++ b/crates/ra_ide_db/src/symbol_index.rs | |||
@@ -29,9 +29,10 @@ use std::{ | |||
29 | }; | 29 | }; |
30 | 30 | ||
31 | use fst::{self, Streamer}; | 31 | use fst::{self, Streamer}; |
32 | use hir::db::DefDatabase; | ||
32 | use ra_db::{ | 33 | use ra_db::{ |
33 | salsa::{self, ParallelDatabase}, | 34 | salsa::{self, ParallelDatabase}, |
34 | FileId, SourceDatabaseExt, SourceRootId, | 35 | CrateId, FileId, SourceDatabaseExt, SourceRootId, |
35 | }; | 36 | }; |
36 | use ra_syntax::{ | 37 | use ra_syntax::{ |
37 | ast::{self, NameOwner}, | 38 | ast::{self, NameOwner}, |
@@ -110,6 +111,14 @@ fn file_symbols(db: &impl SymbolsDatabase, file_id: FileId) -> Arc<SymbolIndex> | |||
110 | Arc::new(SymbolIndex::new(symbols)) | 111 | Arc::new(SymbolIndex::new(symbols)) |
111 | } | 112 | } |
112 | 113 | ||
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 | |||
113 | // Feature: Workspace Symbol | 122 | // Feature: Workspace Symbol |
114 | // | 123 | // |
115 | // Uses fuzzy-search to find types, modules and functions by name across your | 124 | // 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> | |||
134 | pub fn world_symbols(db: &RootDatabase, query: Query) -> Vec<FileSymbol> { | 143 | pub fn world_symbols(db: &RootDatabase, query: Query) -> Vec<FileSymbol> { |
135 | let _p = ra_prof::profile("world_symbols").detail(|| query.query.clone()); | 144 | let _p = ra_prof::profile("world_symbols").detail(|| query.query.clone()); |
136 | 145 | ||
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 | |||
145 | let buf: Vec<Arc<SymbolIndex>> = if query.libs { | 146 | let buf: Vec<Arc<SymbolIndex>> = if query.libs { |
146 | let snap = Snap(db.snapshot()); | 147 | let snap = Snap(db.snapshot()); |
147 | #[cfg(not(feature = "wasm"))] | 148 | #[cfg(not(feature = "wasm"))] |
@@ -175,6 +176,30 @@ pub fn world_symbols(db: &RootDatabase, query: Query) -> Vec<FileSymbol> { | |||
175 | query.search(&buf) | 176 | query.search(&buf) |
176 | } | 177 | } |
177 | 178 | ||
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 | |||
178 | pub fn index_resolve(db: &RootDatabase, name_ref: &ast::NameRef) -> Vec<FileSymbol> { | 203 | pub fn index_resolve(db: &RootDatabase, name_ref: &ast::NameRef) -> Vec<FileSymbol> { |
179 | let name = name_ref.text(); | 204 | let name = name_ref.text(); |
180 | let mut query = Query::new(name.to_string()); | 205 | let mut query = Query::new(name.to_string()); |