aboutsummaryrefslogtreecommitdiff
path: root/crates/completion/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/completion/src')
-rw-r--r--crates/completion/src/completions/flyimport.rs73
-rw-r--r--crates/completion/src/lib.rs8
2 files changed, 46 insertions, 35 deletions
diff --git a/crates/completion/src/completions/flyimport.rs b/crates/completion/src/completions/flyimport.rs
index a40843669..5c04d5c41 100644
--- a/crates/completion/src/completions/flyimport.rs
+++ b/crates/completion/src/completions/flyimport.rs
@@ -85,7 +85,7 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
85 let user_input_lowercased = potential_import_name.to_lowercase(); 85 let user_input_lowercased = potential_import_name.to_lowercase();
86 let import_assets = import_assets(ctx, potential_import_name)?; 86 let import_assets = import_assets(ctx, potential_import_name)?;
87 let import_scope = ImportScope::find_insert_use_container( 87 let import_scope = ImportScope::find_insert_use_container(
88 position_for_import(ctx, import_assets.import_candidate())?, 88 position_for_import(ctx, Some(import_assets.import_candidate()))?,
89 &ctx.sema, 89 &ctx.sema,
90 )?; 90 )?;
91 let mut all_mod_paths = import_assets 91 let mut all_mod_paths = import_assets
@@ -122,14 +122,20 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
122 Some(()) 122 Some(())
123} 123}
124 124
125fn position_for_import<'a>( 125pub(crate) fn position_for_import<'a>(
126 ctx: &'a CompletionContext, 126 ctx: &'a CompletionContext,
127 import_candidate: &ImportCandidate, 127 import_candidate: Option<&ImportCandidate>,
128) -> Option<&'a SyntaxNode> { 128) -> Option<&'a SyntaxNode> {
129 Some(match import_candidate { 129 Some(match import_candidate {
130 ImportCandidate::Path(_) => ctx.name_ref_syntax.as_ref()?.syntax(), 130 Some(ImportCandidate::Path(_)) => ctx.name_ref_syntax.as_ref()?.syntax(),
131 ImportCandidate::TraitAssocItem(_) => ctx.path_qual.as_ref()?.syntax(), 131 Some(ImportCandidate::TraitAssocItem(_)) => ctx.path_qual.as_ref()?.syntax(),
132 ImportCandidate::TraitMethod(_) => ctx.dot_receiver.as_ref()?.syntax(), 132 Some(ImportCandidate::TraitMethod(_)) => ctx.dot_receiver.as_ref()?.syntax(),
133 None => ctx
134 .name_ref_syntax
135 .as_ref()
136 .map(|name_ref| name_ref.syntax())
137 .or_else(|| ctx.path_qual.as_ref().map(|path| path.syntax()))
138 .or_else(|| ctx.dot_receiver.as_ref().map(|expr| expr.syntax()))?,
133 }) 139 })
134} 140}
135 141
@@ -565,7 +571,8 @@ fn main() {
565 571
566 #[test] 572 #[test]
567 fn blanket_trait_impl_import() { 573 fn blanket_trait_impl_import() {
568 check( 574 check_edit(
575 "another_function",
569 r#" 576 r#"
570//- /lib.rs crate:dep 577//- /lib.rs crate:dep
571pub mod test_mod { 578pub mod test_mod {
@@ -583,9 +590,13 @@ fn main() {
583 dep::test_mod::TestStruct::ano$0 590 dep::test_mod::TestStruct::ano$0
584} 591}
585"#, 592"#,
586 expect![[r#" 593 r#"
587 fn another_function() (dep::test_mod::TestTrait) fn another_function() 594use dep::test_mod::TestTrait;
588 "#]], 595
596fn main() {
597 dep::test_mod::TestStruct::another_function()$0
598}
599"#,
589 ); 600 );
590 } 601 }
591 602
@@ -593,28 +604,28 @@ fn main() {
593 fn zero_input_assoc_item_completion() { 604 fn zero_input_assoc_item_completion() {
594 check( 605 check(
595 r#" 606 r#"
596 //- /lib.rs crate:dep 607//- /lib.rs crate:dep
597 pub mod test_mod { 608pub mod test_mod {
598 pub trait TestTrait { 609 pub trait TestTrait {
599 const SPECIAL_CONST: u8; 610 const SPECIAL_CONST: u8;
600 type HumbleType; 611 type HumbleType;
601 fn weird_function(); 612 fn weird_function();
602 fn random_method(&self); 613 fn random_method(&self);
603 } 614 }
604 pub struct TestStruct {} 615 pub struct TestStruct {}
605 impl TestTrait for TestStruct { 616 impl TestTrait for TestStruct {
606 const SPECIAL_CONST: u8 = 42; 617 const SPECIAL_CONST: u8 = 42;
607 type HumbleType = (); 618 type HumbleType = ();
608 fn weird_function() {} 619 fn weird_function() {}
609 fn random_method(&self) {} 620 fn random_method(&self) {}
610 } 621 }
611 } 622}
612 623
613 //- /main.rs crate:main deps:dep 624//- /main.rs crate:main deps:dep
614 fn main() { 625fn main() {
615 let test_struct = dep::test_mod::TestStruct {}; 626 let test_struct = dep::test_mod::TestStruct {};
616 test_struct.$0 627 test_struct.$0
617 } 628}
618 "#, 629 "#,
619 expect![[r#" 630 expect![[r#"
620 me random_method() (dep::test_mod::TestTrait) fn random_method(&self) 631 me random_method() (dep::test_mod::TestTrait) fn random_method(&self)
diff --git a/crates/completion/src/lib.rs b/crates/completion/src/lib.rs
index 56ec13e8c..2c4e54524 100644
--- a/crates/completion/src/lib.rs
+++ b/crates/completion/src/lib.rs
@@ -11,10 +11,10 @@ mod render;
11 11
12mod completions; 12mod completions;
13 13
14use completions::flyimport::position_for_import;
14use ide_db::{ 15use ide_db::{
15 base_db::FilePosition, helpers::insert_use::ImportScope, imports_locator, RootDatabase, 16 base_db::FilePosition, helpers::insert_use::ImportScope, imports_locator, RootDatabase,
16}; 17};
17use syntax::AstNode;
18use text_edit::TextEdit; 18use text_edit::TextEdit;
19 19
20use crate::{completions::Completions, context::CompletionContext, item::CompletionKind}; 20use crate::{completions::Completions, context::CompletionContext, item::CompletionKind};
@@ -142,10 +142,10 @@ pub fn resolve_completion_edits(
142 import_for_trait_assoc_item: bool, 142 import_for_trait_assoc_item: bool,
143) -> Option<Vec<TextEdit>> { 143) -> Option<Vec<TextEdit>> {
144 let ctx = CompletionContext::new(db, position, config)?; 144 let ctx = CompletionContext::new(db, position, config)?;
145 let anchor = ctx.name_ref_syntax.as_ref()?; 145 let position_for_import = position_for_import(&ctx, None)?;
146 let import_scope = ImportScope::find_insert_use_container(anchor.syntax(), &ctx.sema)?; 146 let import_scope = ImportScope::find_insert_use_container(position_for_import, &ctx.sema)?;
147 147
148 let current_module = ctx.sema.scope(anchor.syntax()).module()?; 148 let current_module = ctx.sema.scope(position_for_import).module()?;
149 let current_crate = current_module.krate(); 149 let current_crate = current_module.krate();
150 150
151 let import_path = imports_locator::find_exact_imports(&ctx.sema, current_crate, imported_name) 151 let import_path = imports_locator::find_exact_imports(&ctx.sema, current_crate, imported_name)