aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/hir/src/code_model.rs2
-rw-r--r--crates/ide_completion/src/completions/flyimport.rs94
-rw-r--r--crates/ide_completion/src/lib.rs2
3 files changed, 94 insertions, 4 deletions
diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs
index 7d43d4097..021e4ad31 100644
--- a/crates/hir/src/code_model.rs
+++ b/crates/hir/src/code_model.rs
@@ -2076,7 +2076,7 @@ impl Callable {
2076} 2076}
2077 2077
2078/// For IDE only 2078/// For IDE only
2079#[derive(Debug)] 2079#[derive(Debug, PartialEq, Eq, Hash)]
2080pub enum ScopeDef { 2080pub enum ScopeDef {
2081 ModuleDef(ModuleDef), 2081 ModuleDef(ModuleDef),
2082 MacroDef(MacroDef), 2082 MacroDef(MacroDef),
diff --git a/crates/ide_completion/src/completions/flyimport.rs b/crates/ide_completion/src/completions/flyimport.rs
index c9f928483..da8375af9 100644
--- a/crates/ide_completion/src/completions/flyimport.rs
+++ b/crates/ide_completion/src/completions/flyimport.rs
@@ -53,6 +53,7 @@ use ide_db::helpers::{
53 import_assets::{ImportAssets, ImportCandidate}, 53 import_assets::{ImportAssets, ImportCandidate},
54 insert_use::ImportScope, 54 insert_use::ImportScope,
55}; 55};
56use rustc_hash::FxHashSet;
56use syntax::{AstNode, SyntaxNode, T}; 57use syntax::{AstNode, SyntaxNode, T};
57use test_utils::mark; 58use test_utils::mark;
58 59
@@ -91,8 +92,10 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
91 position_for_import(ctx, Some(import_assets.import_candidate()))?, 92 position_for_import(ctx, Some(import_assets.import_candidate()))?,
92 &ctx.sema, 93 &ctx.sema,
93 )?; 94 )?;
95
96 let scope_definitions = scope_definitions(ctx);
94 let mut all_mod_paths = import_assets 97 let mut all_mod_paths = import_assets
95 .search_for_relative_paths(&ctx.sema) 98 .search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind)
96 .into_iter() 99 .into_iter()
97 .map(|(mod_path, item_in_ns)| { 100 .map(|(mod_path, item_in_ns)| {
98 let scope_item = match item_in_ns { 101 let scope_item = match item_in_ns {
@@ -102,6 +105,7 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
102 }; 105 };
103 (mod_path, scope_item) 106 (mod_path, scope_item)
104 }) 107 })
108 .filter(|(_, proposed_def)| !scope_definitions.contains(proposed_def))
105 .collect::<Vec<_>>(); 109 .collect::<Vec<_>>();
106 all_mod_paths.sort_by_cached_key(|(mod_path, _)| { 110 all_mod_paths.sort_by_cached_key(|(mod_path, _)| {
107 compute_fuzzy_completion_order_key(mod_path, &user_input_lowercased) 111 compute_fuzzy_completion_order_key(mod_path, &user_input_lowercased)
@@ -125,6 +129,14 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
125 Some(()) 129 Some(())
126} 130}
127 131
132fn scope_definitions(ctx: &CompletionContext) -> FxHashSet<ScopeDef> {
133 let mut scope_definitions = FxHashSet::default();
134 ctx.scope.process_all_names(&mut |_, scope_def| {
135 scope_definitions.insert(scope_def);
136 });
137 scope_definitions
138}
139
128pub(crate) fn position_for_import<'a>( 140pub(crate) fn position_for_import<'a>(
129 ctx: &'a CompletionContext, 141 ctx: &'a CompletionContext,
130 import_candidate: Option<&ImportCandidate>, 142 import_candidate: Option<&ImportCandidate>,
@@ -192,7 +204,7 @@ mod tests {
192 204
193 use crate::{ 205 use crate::{
194 item::CompletionKind, 206 item::CompletionKind,
195 test_utils::{check_edit, completion_list}, 207 test_utils::{check_edit, check_edit_with_config, completion_list, TEST_CONFIG},
196 }; 208 };
197 209
198 fn check(ra_fixture: &str, expect: Expect) { 210 fn check(ra_fixture: &str, expect: Expect) {
@@ -685,4 +697,82 @@ fn main() {}
685 expect![[]], 697 expect![[]],
686 ); 698 );
687 } 699 }
700
701 #[test]
702 fn prefix_config_usage() {
703 let fixture = r#"
704mod foo {
705 pub mod bar {
706 pub struct Item;
707 }
708}
709
710use crate::foo::bar;
711
712fn main() {
713 Ite$0
714}"#;
715 let mut config = TEST_CONFIG;
716
717 config.insert_use.prefix_kind = hir::PrefixKind::ByCrate;
718 check_edit_with_config(
719 config.clone(),
720 "Item",
721 fixture,
722 r#"
723mod foo {
724 pub mod bar {
725 pub struct Item;
726 }
727}
728
729use crate::foo::bar::{self, Item};
730
731fn main() {
732 Item
733}"#,
734 );
735
736 config.insert_use.prefix_kind = hir::PrefixKind::BySelf;
737 check_edit_with_config(
738 config.clone(),
739 "Item",
740 fixture,
741 r#"
742mod foo {
743 pub mod bar {
744 pub struct Item;
745 }
746}
747
748use crate::foo::bar;
749
750use self::foo::bar::Item;
751
752fn main() {
753 Item
754}"#,
755 );
756
757 config.insert_use.prefix_kind = hir::PrefixKind::Plain;
758 check_edit_with_config(
759 config,
760 "Item",
761 fixture,
762 r#"
763mod foo {
764 pub mod bar {
765 pub struct Item;
766 }
767}
768
769use foo::bar::Item;
770
771use crate::foo::bar;
772
773fn main() {
774 Item
775}"#,
776 );
777 }
688} 778}
diff --git a/crates/ide_completion/src/lib.rs b/crates/ide_completion/src/lib.rs
index db8bfbbc3..76f31de9e 100644
--- a/crates/ide_completion/src/lib.rs
+++ b/crates/ide_completion/src/lib.rs
@@ -151,7 +151,7 @@ pub fn resolve_completion_edits(
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)
152 .filter_map(|candidate| { 152 .filter_map(|candidate| {
153 let item: hir::ItemInNs = candidate.either(Into::into, Into::into); 153 let item: hir::ItemInNs = candidate.either(Into::into, Into::into);
154 current_module.find_use_path(db, item) 154 current_module.find_use_path_prefixed(db, item, config.insert_use.prefix_kind)
155 }) 155 })
156 .find(|mod_path| mod_path.to_string() == full_import_path)?; 156 .find(|mod_path| mod_path.to_string() == full_import_path)?;
157 157