aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_db
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide_db')
-rw-r--r--crates/ide_db/src/imports_locator.rs60
1 files changed, 47 insertions, 13 deletions
diff --git a/crates/ide_db/src/imports_locator.rs b/crates/ide_db/src/imports_locator.rs
index df74be00b..9d8ea7368 100644
--- a/crates/ide_db/src/imports_locator.rs
+++ b/crates/ide_db/src/imports_locator.rs
@@ -1,36 +1,70 @@
1//! This module contains an import search funcionality that is provided to the assists module. 1//! This module contains an import search funcionality that is provided to the assists module.
2//! Later, this should be moved away to a separate crate that is accessible from the assists module. 2//! Later, this should be moved away to a separate crate that is accessible from the assists module.
3 3
4use hir::{Crate, MacroDef, ModuleDef, Semantics}; 4use hir::{import_map, Crate, MacroDef, ModuleDef, Semantics};
5use syntax::{ast, AstNode, SyntaxKind::NAME}; 5use syntax::{ast, AstNode, SyntaxKind::NAME};
6 6
7use crate::{ 7use crate::{
8 defs::{Definition, NameClass}, 8 defs::{Definition, NameClass},
9 symbol_index::{self, FileSymbol, Query}, 9 symbol_index::{self, FileSymbol},
10 RootDatabase, 10 RootDatabase,
11}; 11};
12use either::Either; 12use either::Either;
13use rustc_hash::FxHashSet; 13use rustc_hash::FxHashSet;
14 14
15pub fn find_imports<'a>( 15pub fn find_exact_imports<'a>(
16 sema: &Semantics<'a, RootDatabase>, 16 sema: &Semantics<'a, RootDatabase>,
17 krate: Crate, 17 krate: Crate,
18 name_to_import: &str, 18 name_to_import: &str,
19) -> Vec<Either<ModuleDef, MacroDef>> { 19) -> impl Iterator<Item = Either<ModuleDef, MacroDef>> {
20 let _p = profile::span("search_for_imports"); 20 let _p = profile::span("find_exact_imports");
21 find_imports(
22 sema,
23 krate,
24 {
25 let mut local_query = symbol_index::Query::new(name_to_import.to_string());
26 local_query.exact();
27 local_query.limit(40);
28 local_query
29 },
30 import_map::Query::new(name_to_import).anchor_end().case_sensitive().limit(40),
31 )
32}
33
34pub fn find_similar_imports<'a>(
35 sema: &Semantics<'a, RootDatabase>,
36 krate: Crate,
37 name_to_import: &str,
38 limit: usize,
39) -> impl Iterator<Item = Either<ModuleDef, MacroDef>> {
40 let _p = profile::span("find_similar_imports");
41 find_imports(
42 sema,
43 krate,
44 {
45 let mut local_query = symbol_index::Query::new(name_to_import.to_string());
46 local_query.limit(limit);
47 local_query
48 },
49 import_map::Query::new(name_to_import).limit(limit),
50 )
51}
52
53fn find_imports<'a>(
54 sema: &Semantics<'a, RootDatabase>,
55 krate: Crate,
56 local_query: symbol_index::Query,
57 external_query: import_map::Query,
58) -> impl Iterator<Item = Either<ModuleDef, MacroDef>> {
59 let _p = profile::span("find_similar_imports");
21 let db = sema.db; 60 let db = sema.db;
22 61
23 // Query dependencies first. 62 // Query dependencies first.
24 let mut candidates: FxHashSet<_> = 63 let mut candidates: FxHashSet<_> =
25 krate.query_external_importables(db, name_to_import).collect(); 64 krate.query_external_importables(db, external_query).collect();
26 65
27 // Query the local crate using the symbol index. 66 // Query the local crate using the symbol index.
28 let local_results = { 67 let local_results = symbol_index::crate_symbols(db, krate.into(), local_query);
29 let mut query = Query::new(name_to_import.to_string());
30 query.exact();
31 query.limit(40);
32 symbol_index::crate_symbols(db, krate.into(), query)
33 };
34 68
35 candidates.extend( 69 candidates.extend(
36 local_results 70 local_results
@@ -43,7 +77,7 @@ pub fn find_imports<'a>(
43 }), 77 }),
44 ); 78 );
45 79
46 candidates.into_iter().collect() 80 candidates.into_iter()
47} 81}
48 82
49fn get_name_definition<'a>( 83fn get_name_definition<'a>(