diff options
author | Kirill Bulatov <[email protected]> | 2020-11-13 17:16:56 +0000 |
---|---|---|
committer | Kirill Bulatov <[email protected]> | 2020-11-16 19:19:06 +0000 |
commit | 4c8edd003aa447bd2da10fd81b24f582deacdc11 (patch) | |
tree | 6b7b97ba769e7b1c7de4feebfdcd06173bf52853 /crates/completion | |
parent | d1556550f83b7b8e9dd42c80ab6e08a632dfd256 (diff) |
Use imports_locator
Diffstat (limited to 'crates/completion')
-rw-r--r-- | crates/completion/src/completions/magic.rs | 74 | ||||
-rw-r--r-- | crates/completion/src/lib.rs | 2 |
2 files changed, 37 insertions, 39 deletions
diff --git a/crates/completion/src/completions/magic.rs b/crates/completion/src/completions/magic.rs index 34fc35847..272c9a354 100644 --- a/crates/completion/src/completions/magic.rs +++ b/crates/completion/src/completions/magic.rs | |||
@@ -2,8 +2,8 @@ | |||
2 | 2 | ||
3 | use assists::utils::{insert_use, mod_path_to_ast, ImportScope}; | 3 | use assists::utils::{insert_use, mod_path_to_ast, ImportScope}; |
4 | use either::Either; | 4 | use either::Either; |
5 | use hir::{db::HirDatabase, MacroDef, ModuleDef, Query}; | 5 | use hir::{db::HirDatabase, MacroDef, ModuleDef}; |
6 | use itertools::Itertools; | 6 | use ide_db::imports_locator; |
7 | use syntax::{algo, AstNode}; | 7 | use syntax::{algo, AstNode}; |
8 | use text_edit::TextEdit; | 8 | use text_edit::TextEdit; |
9 | 9 | ||
@@ -22,42 +22,40 @@ pub(crate) fn complete_magic(acc: &mut Completions, ctx: &CompletionContext) -> | |||
22 | 22 | ||
23 | let potential_import_name = ctx.token.to_string(); | 23 | let potential_import_name = ctx.token.to_string(); |
24 | 24 | ||
25 | let possible_imports = ctx | 25 | let possible_imports = |
26 | .krate? | 26 | imports_locator::find_similar_imports(&ctx.sema, ctx.krate?, &potential_import_name) |
27 | // TODO kb use imports_locator instead? | 27 | .filter_map(|import_candidate| { |
28 | .query_external_importables(ctx.db, Query::new(&potential_import_name).limit(40)) | 28 | let use_path = match import_candidate { |
29 | .unique() | 29 | Either::Left(module_def) => current_module.find_use_path(ctx.db, module_def), |
30 | .filter_map(|import_candidate| { | 30 | Either::Right(macro_def) => current_module.find_use_path(ctx.db, macro_def), |
31 | let use_path = match import_candidate { | 31 | }?; |
32 | Either::Left(module_def) => current_module.find_use_path(ctx.db, module_def), | 32 | Some((use_path, additional_completion(ctx.db, import_candidate))) |
33 | Either::Right(macro_def) => current_module.find_use_path(ctx.db, macro_def), | 33 | }) |
34 | }?; | 34 | .filter_map(|(mod_path, additional_completion)| { |
35 | Some((use_path, additional_completion(ctx.db, import_candidate))) | 35 | let mut builder = TextEdit::builder(); |
36 | }) | 36 | |
37 | .filter_map(|(mod_path, additional_completion)| { | 37 | let correct_qualifier = format!( |
38 | let mut builder = TextEdit::builder(); | 38 | "{}{}", |
39 | 39 | mod_path.segments.last()?, | |
40 | let correct_qualifier = format!( | 40 | additional_completion.unwrap_or_default() |
41 | "{}{}", | 41 | ); |
42 | mod_path.segments.last()?, | 42 | builder.replace(anchor.syntax().text_range(), correct_qualifier); |
43 | additional_completion.unwrap_or_default() | 43 | |
44 | ); | 44 | let rewriter = |
45 | builder.replace(anchor.syntax().text_range(), correct_qualifier); | 45 | insert_use(&import_scope, mod_path_to_ast(&mod_path), ctx.config.merge); |
46 | 46 | let old_ast = rewriter.rewrite_root()?; | |
47 | let rewriter = insert_use(&import_scope, mod_path_to_ast(&mod_path), ctx.config.merge); | 47 | algo::diff(&old_ast, &rewriter.rewrite(&old_ast)).into_text_edit(&mut builder); |
48 | let old_ast = rewriter.rewrite_root()?; | 48 | |
49 | algo::diff(&old_ast, &rewriter.rewrite(&old_ast)).into_text_edit(&mut builder); | 49 | let completion_item: CompletionItem = CompletionItem::new( |
50 | 50 | CompletionKind::Magic, | |
51 | let completion_item: CompletionItem = CompletionItem::new( | 51 | ctx.source_range(), |
52 | CompletionKind::Magic, | 52 | mod_path.to_string(), |
53 | ctx.source_range(), | 53 | ) |
54 | mod_path.to_string(), | 54 | .kind(CompletionItemKind::Struct) |
55 | ) | 55 | .text_edit(builder.finish()) |
56 | .kind(CompletionItemKind::Struct) | 56 | .into(); |
57 | .text_edit(builder.finish()) | 57 | Some(completion_item) |
58 | .into(); | 58 | }); |
59 | Some(completion_item) | ||
60 | }); | ||
61 | acc.add_all(possible_imports); | 59 | acc.add_all(possible_imports); |
62 | 60 | ||
63 | Some(()) | 61 | Some(()) |
diff --git a/crates/completion/src/lib.rs b/crates/completion/src/lib.rs index e920fa6b5..8323af8b2 100644 --- a/crates/completion/src/lib.rs +++ b/crates/completion/src/lib.rs | |||
@@ -118,7 +118,7 @@ pub fn completions( | |||
118 | completions::macro_in_item_position::complete_macro_in_item_position(&mut acc, &ctx); | 118 | completions::macro_in_item_position::complete_macro_in_item_position(&mut acc, &ctx); |
119 | completions::trait_impl::complete_trait_impl(&mut acc, &ctx); | 119 | completions::trait_impl::complete_trait_impl(&mut acc, &ctx); |
120 | completions::mod_::complete_mod(&mut acc, &ctx); | 120 | completions::mod_::complete_mod(&mut acc, &ctx); |
121 | completions::complete_magic::complete_magic(&mut acc, &ctx); | 121 | completions::magic::complete_magic(&mut acc, &ctx); |
122 | 122 | ||
123 | Some(acc) | 123 | Some(acc) |
124 | } | 124 | } |