aboutsummaryrefslogtreecommitdiff
path: root/crates/completion/src/completions
diff options
context:
space:
mode:
authorKirill Bulatov <[email protected]>2020-11-13 17:16:56 +0000
committerKirill Bulatov <[email protected]>2020-11-16 19:19:06 +0000
commit4c8edd003aa447bd2da10fd81b24f582deacdc11 (patch)
tree6b7b97ba769e7b1c7de4feebfdcd06173bf52853 /crates/completion/src/completions
parentd1556550f83b7b8e9dd42c80ab6e08a632dfd256 (diff)
Use imports_locator
Diffstat (limited to 'crates/completion/src/completions')
-rw-r--r--crates/completion/src/completions/magic.rs74
1 files changed, 36 insertions, 38 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
3use assists::utils::{insert_use, mod_path_to_ast, ImportScope}; 3use assists::utils::{insert_use, mod_path_to_ast, ImportScope};
4use either::Either; 4use either::Either;
5use hir::{db::HirDatabase, MacroDef, ModuleDef, Query}; 5use hir::{db::HirDatabase, MacroDef, ModuleDef};
6use itertools::Itertools; 6use ide_db::imports_locator;
7use syntax::{algo, AstNode}; 7use syntax::{algo, AstNode};
8use text_edit::TextEdit; 8use 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(())