aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_db
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-11-17 17:50:08 +0000
committerGitHub <[email protected]>2020-11-17 17:50:08 +0000
commit156f7d6963fb6b47570bfa457fdf51733a182054 (patch)
tree1070543fd0be92d3ab720bc297962a5dad5e64e8 /crates/ide_db
parentf4b4f17662a7a1c80743b3108bb0da28b0fe47e5 (diff)
parent16f0b2fdde2fba8f45cc18a8de11b45751c0f923 (diff)
Merge #6553
6553: Auto imports in completion r=matklad a=SomeoneToIgnore ![completion](https://user-images.githubusercontent.com/2690773/99155339-ae4fb380-26bf-11eb-805a-655b1706ce70.gif) Closes https://github.com/rust-analyzer/rust-analyzer/issues/1062 but does not handle the completion order, since it's a separate task for https://github.com/rust-analyzer/rust-analyzer/issues/4922 , https://github.com/rust-analyzer/rust-analyzer/issues/4922 and maybe something else. 2 quirks in the current implementation: * traits are not auto imported during method completion If I understand the current situation right, we cannot search for traits by a **part** of a method name, we need a full name with correct case to get a trait for it. * VSCode (?) autocompletion is not as rigid as in Intellij Rust as you can notice on the animation. Intellij is able to refresh the completions on every new symbol added, yet VS Code does not query the completions on every symbol for me. With a few debug prints placed in RA, I've observed the following behaviour: after the first set of completion suggestions is received, next symbol input does not trigger a server request, if the completions contain this symbol. When more symbols added, the existing completion suggestions are filtered out until none are left and only then, on the next symbol it queries for completions. It seems like the only alternative to get an updated set of results is to manually retrigger it with Esc and Ctrl + Space. Despite the eerie latter bullet, the completion seems to work pretty fine and fast nontheless, but if you have any ideas on how to make it more smooth, I'll gladly try it out. Co-authored-by: Kirill Bulatov <[email protected]>
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>(