From d50a1a0fe9055bf03b9746cb341b251af8e7b326 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Mon, 8 Jun 2020 13:00:31 +0200 Subject: Profile `world_symbols` --- crates/ra_ide_db/src/symbol_index.rs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'crates/ra_ide_db') diff --git a/crates/ra_ide_db/src/symbol_index.rs b/crates/ra_ide_db/src/symbol_index.rs index acc31fe3b..299b47c6a 100644 --- a/crates/ra_ide_db/src/symbol_index.rs +++ b/crates/ra_ide_db/src/symbol_index.rs @@ -132,6 +132,8 @@ fn file_symbols(db: &impl SymbolsDatabase, file_id: FileId) -> Arc // | VS Code | kbd:[Ctrl+T] // |=== pub fn world_symbols(db: &RootDatabase, query: Query) -> Vec { + let _p = ra_prof::profile("world_symbols").detail(|| query.query.clone()); + /// Need to wrap Snapshot to provide `Clone` impl for `map_with` struct Snap(salsa::Snapshot); impl Clone for Snap { -- cgit v1.2.3 From 54936e8aa212ea5fdf737d8e1b0a02c231ed89eb Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Mon, 8 Jun 2020 13:00:53 +0200 Subject: Fix the symbol query limit --- crates/ra_ide_db/src/symbol_index.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'crates/ra_ide_db') diff --git a/crates/ra_ide_db/src/symbol_index.rs b/crates/ra_ide_db/src/symbol_index.rs index 299b47c6a..78c714356 100644 --- a/crates/ra_ide_db/src/symbol_index.rs +++ b/crates/ra_ide_db/src/symbol_index.rs @@ -300,9 +300,6 @@ impl Query { let mut stream = op.union(); let mut res = Vec::new(); while let Some((_, indexed_values)) = stream.next() { - if res.len() >= self.limit { - break; - } for indexed_value in indexed_values { let symbol_index = &indices[indexed_value.index]; let (start, end) = SymbolIndex::map_value_to_range(indexed_value.value); @@ -314,6 +311,10 @@ impl Query { if self.exact && symbol.name != self.query { continue; } + + if res.len() >= self.limit { + return res; + } res.push(symbol.clone()); } } -- cgit v1.2.3 From 6463d3ac63a479e33d923593e720696b38a1a54c Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 9 Jun 2020 18:47:14 +0200 Subject: symbol_index: allow querying a single crate --- crates/ra_ide_db/src/symbol_index.rs | 43 ++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 9 deletions(-) (limited to 'crates/ra_ide_db') 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::{ }; use fst::{self, Streamer}; +use hir::db::DefDatabase; use ra_db::{ salsa::{self, ParallelDatabase}, - FileId, SourceDatabaseExt, SourceRootId, + CrateId, FileId, SourceDatabaseExt, SourceRootId, }; use ra_syntax::{ ast::{self, NameOwner}, @@ -110,6 +111,14 @@ fn file_symbols(db: &impl SymbolsDatabase, file_id: FileId) -> Arc Arc::new(SymbolIndex::new(symbols)) } +/// Need to wrap Snapshot to provide `Clone` impl for `map_with` +struct Snap(salsa::Snapshot); +impl Clone for Snap { + fn clone(&self) -> Snap { + Snap(self.0.snapshot()) + } +} + // Feature: Workspace Symbol // // 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 pub fn world_symbols(db: &RootDatabase, query: Query) -> Vec { let _p = ra_prof::profile("world_symbols").detail(|| query.query.clone()); - /// Need to wrap Snapshot to provide `Clone` impl for `map_with` - struct Snap(salsa::Snapshot); - impl Clone for Snap { - fn clone(&self) -> Snap { - Snap(self.0.snapshot()) - } - } - let buf: Vec> = if query.libs { let snap = Snap(db.snapshot()); #[cfg(not(feature = "wasm"))] @@ -175,6 +176,30 @@ pub fn world_symbols(db: &RootDatabase, query: Query) -> Vec { query.search(&buf) } +pub fn crate_symbols(db: &RootDatabase, krate: CrateId, query: Query) -> Vec { + let def_map = db.crate_def_map(krate); + let mut files = Vec::new(); + let mut modules = vec![def_map.root]; + while let Some(module) = modules.pop() { + let data = &def_map[module]; + files.extend(data.origin.file_id()); + modules.extend(data.children.values()); + } + + let snap = Snap(db.snapshot()); + + #[cfg(not(feature = "wasm"))] + let buf = files + .par_iter() + .map_with(snap, |db, &file_id| db.0.file_symbols(file_id)) + .collect::>(); + + #[cfg(feature = "wasm")] + let buf = files.iter().map(|&file_id| snap.0.file_symbols(file_id)).collect::>(); + + query.search(&buf) +} + pub fn index_resolve(db: &RootDatabase, name_ref: &ast::NameRef) -> Vec { let name = name_ref.text(); let mut query = Query::new(name.to_string()); -- cgit v1.2.3 From a70a0ca73ceac339de4e1df6d561894e485ba5ee Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 9 Jun 2020 18:48:44 +0200 Subject: ImportsLocator: use ImportMap for non-local crates --- crates/ra_ide_db/src/imports_locator.rs | 46 +++++++++++++++++---------------- 1 file changed, 24 insertions(+), 22 deletions(-) (limited to 'crates/ra_ide_db') diff --git a/crates/ra_ide_db/src/imports_locator.rs b/crates/ra_ide_db/src/imports_locator.rs index bf0d8db60..fff112e66 100644 --- a/crates/ra_ide_db/src/imports_locator.rs +++ b/crates/ra_ide_db/src/imports_locator.rs @@ -1,7 +1,7 @@ //! This module contains an import search funcionality that is provided to the ra_assists module. //! Later, this should be moved away to a separate crate that is accessible from the ra_assists module. -use hir::{MacroDef, ModuleDef, Semantics}; +use hir::{Crate, MacroDef, ModuleDef, Semantics}; use ra_prof::profile; use ra_syntax::{ast, AstNode, SyntaxKind::NAME}; @@ -11,44 +11,46 @@ use crate::{ RootDatabase, }; use either::Either; +use rustc_hash::FxHashSet; pub struct ImportsLocator<'a> { sema: Semantics<'a, RootDatabase>, + krate: Crate, } impl<'a> ImportsLocator<'a> { - pub fn new(db: &'a RootDatabase) -> Self { - Self { sema: Semantics::new(db) } + pub fn new(db: &'a RootDatabase, krate: Crate) -> Self { + Self { sema: Semantics::new(db), krate } } pub fn find_imports(&mut self, name_to_import: &str) -> Vec> { let _p = profile("search_for_imports"); let db = self.sema.db; - let project_results = { - let mut query = Query::new(name_to_import.to_string()); - query.exact(); - query.limit(40); - symbol_index::world_symbols(db, query) - }; - let lib_results = { + // Query dependencies first. + let mut candidates: FxHashSet<_> = + self.krate.query_external_importables(db, name_to_import).collect(); + + // Query the local crate using the symbol index. + let local_results = { let mut query = Query::new(name_to_import.to_string()); - query.libs(); query.exact(); query.limit(40); - symbol_index::world_symbols(db, query) + symbol_index::crate_symbols(db, self.krate.into(), query) }; - project_results - .into_iter() - .chain(lib_results.into_iter()) - .filter_map(|import_candidate| self.get_name_definition(&import_candidate)) - .filter_map(|name_definition_to_import| match name_definition_to_import { - Definition::ModuleDef(module_def) => Some(Either::Left(module_def)), - Definition::Macro(macro_def) => Some(Either::Right(macro_def)), - _ => None, - }) - .collect() + candidates.extend( + local_results + .into_iter() + .filter_map(|import_candidate| self.get_name_definition(&import_candidate)) + .filter_map(|name_definition_to_import| match name_definition_to_import { + Definition::ModuleDef(module_def) => Some(Either::Left(module_def)), + Definition::Macro(macro_def) => Some(Either::Right(macro_def)), + _ => None, + }), + ); + + candidates.into_iter().collect() } fn get_name_definition(&mut self, import_candidate: &FileSymbol) -> Option { -- cgit v1.2.3 From ed2817e599a9c0e812af26587badad6da7a4d949 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Wed, 10 Jun 2020 12:37:00 +0200 Subject: Move limit check down --- crates/ra_ide_db/src/symbol_index.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_ide_db') diff --git a/crates/ra_ide_db/src/symbol_index.rs b/crates/ra_ide_db/src/symbol_index.rs index c974891ea..ac0a201df 100644 --- a/crates/ra_ide_db/src/symbol_index.rs +++ b/crates/ra_ide_db/src/symbol_index.rs @@ -337,10 +337,10 @@ impl Query { continue; } + res.push(symbol.clone()); if res.len() >= self.limit { return res; } - res.push(symbol.clone()); } } } -- cgit v1.2.3 From 6766a6b0e189f47d7a405c872598bca9a2395360 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Thu, 11 Jun 2020 12:03:08 +0200 Subject: Add symbol index FIXME --- crates/ra_ide_db/src/symbol_index.rs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'crates/ra_ide_db') diff --git a/crates/ra_ide_db/src/symbol_index.rs b/crates/ra_ide_db/src/symbol_index.rs index ac0a201df..aab918973 100644 --- a/crates/ra_ide_db/src/symbol_index.rs +++ b/crates/ra_ide_db/src/symbol_index.rs @@ -177,6 +177,9 @@ pub fn world_symbols(db: &RootDatabase, query: Query) -> Vec { } pub fn crate_symbols(db: &RootDatabase, krate: CrateId, query: Query) -> Vec { + // FIXME(#4842): This now depends on CrateDefMap, why not build the entire symbol index from + // that instead? + let def_map = db.crate_def_map(krate); let mut files = Vec::new(); let mut modules = vec![def_map.root]; -- cgit v1.2.3