@@ -53,6 +53,7 @@ use ide_db::helpers::{
53
53
import_assets:: { ImportAssets , ImportCandidate } ,
54
54
insert_use:: ImportScope ,
55
55
} ;
56
+ use rustc_hash:: FxHashSet ;
56
57
use syntax:: { AstNode , SyntaxNode , T } ;
57
58
use test_utils:: mark;
58
59
@@ -91,8 +92,10 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
91
92
position_for_import ( ctx, Some ( import_assets. import_candidate ( ) ) ) ?,
92
93
& ctx. sema ,
93
94
) ?;
95
+
96
+ let scope_definitions = scope_definitions ( ctx) ;
94
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
99
. into_iter ( )
97
100
. map ( |( mod_path, item_in_ns) | {
98
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
106
( mod_path, scope_item)
104
107
} )
108
+ . filter ( |( _, proposed_def) | !scope_definitions. contains ( proposed_def) )
105
109
. collect :: < Vec < _ > > ( ) ;
106
110
all_mod_paths. sort_by_cached_key ( |( mod_path, _) | {
107
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
129
Some ( ( ) )
126
130
}
127
131
132
+ fn 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
+
128
140
pub ( crate ) fn position_for_import < ' a > (
129
141
ctx : & ' a CompletionContext ,
130
142
import_candidate : Option < & ImportCandidate > ,
@@ -192,7 +204,7 @@ mod tests {
192
204
193
205
use crate :: {
194
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
210
fn check ( ra_fixture : & str , expect : Expect ) {
@@ -685,4 +697,82 @@ fn main() {}
685
697
expect ! [ [ ] ] ,
686
698
) ;
687
699
}
700
+
701
+ #[ test]
702
+ fn prefix_config_usage ( ) {
703
+ let fixture = r#"
704
+ mod foo {
705
+ pub mod bar {
706
+ pub struct Item;
707
+ }
708
+ }
709
+
710
+ use crate::foo::bar;
711
+
712
+ fn 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#"
723
+ mod foo {
724
+ pub mod bar {
725
+ pub struct Item;
726
+ }
727
+ }
728
+
729
+ use crate::foo::bar::{self, Item};
730
+
731
+ fn 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#"
742
+ mod foo {
743
+ pub mod bar {
744
+ pub struct Item;
745
+ }
746
+ }
747
+
748
+ use crate::foo::bar;
749
+
750
+ use self::foo::bar::Item;
751
+
752
+ fn 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#"
763
+ mod foo {
764
+ pub mod bar {
765
+ pub struct Item;
766
+ }
767
+ }
768
+
769
+ use foo::bar::Item;
770
+
771
+ use crate::foo::bar;
772
+
773
+ fn main() {
774
+ Item
775
+ }"# ,
776
+ ) ;
777
+ }
688
778
}
0 commit comments