aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_completion/src/completions/flyimport.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide_completion/src/completions/flyimport.rs')
-rw-r--r--crates/ide_completion/src/completions/flyimport.rs101
1 files changed, 54 insertions, 47 deletions
diff --git a/crates/ide_completion/src/completions/flyimport.rs b/crates/ide_completion/src/completions/flyimport.rs
index 1ef6f8afb..c1e3f091f 100644
--- a/crates/ide_completion/src/completions/flyimport.rs
+++ b/crates/ide_completion/src/completions/flyimport.rs
@@ -87,11 +87,12 @@
87//! Note that having this flag set to `true` does not guarantee that the feature is enabled: your client needs to have the corredponding 87//! Note that having this flag set to `true` does not guarantee that the feature is enabled: your client needs to have the corredponding
88//! capability enabled. 88//! capability enabled.
89 89
90use hir::{AsAssocItem, ModPath, ModuleDef, ScopeDef}; 90use hir::ModPath;
91use ide_db::helpers::{ 91use ide_db::helpers::{
92 import_assets::{ImportAssets, ImportCandidate}, 92 import_assets::{ImportAssets, ImportCandidate},
93 insert_use::ImportScope, 93 insert_use::ImportScope,
94}; 94};
95use itertools::Itertools;
95use syntax::{AstNode, SyntaxNode, T}; 96use syntax::{AstNode, SyntaxNode, T};
96 97
97use crate::{ 98use crate::{
@@ -130,27 +131,23 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
130 &ctx.sema, 131 &ctx.sema,
131 )?; 132 )?;
132 133
133 let mut all_imports = 134 acc.add_all(
134 import_assets.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind); 135 import_assets
135 all_imports.sort_by_cached_key(|import| { 136 .search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind)
136 compute_fuzzy_completion_order_key(import.display_path(), &user_input_lowercased) 137 .into_iter()
137 }); 138 .sorted_by_key(|located_import| {
138 139 compute_fuzzy_completion_order_key(
139 acc.add_all(all_imports.into_iter().filter_map(|import| { 140 &located_import.import_path,
140 let import_for_trait_assoc_item = import 141 &user_input_lowercased,
141 .item_to_display() 142 )
142 .as_module_def_id()
143 .and_then(|module_def_id| {
144 ModuleDef::from(module_def_id).as_assoc_item(ctx.db)?.containing_trait(ctx.db)
145 }) 143 })
146 .is_some(); 144 .filter_map(|import| {
147 let def_to_display = ScopeDef::from(import.item_to_display()); 145 render_resolution_with_import(
148 render_resolution_with_import( 146 RenderContext::new(ctx),
149 RenderContext::new(ctx), 147 ImportEdit { import, import_scope: import_scope.clone() },
150 ImportEdit { import, import_scope: import_scope.clone(), import_for_trait_assoc_item }, 148 )
151 &def_to_display, 149 }),
152 ) 150 );
153 }));
154 Some(()) 151 Some(())
155} 152}
156 153
@@ -190,6 +187,7 @@ fn import_assets<'a>(ctx: &'a CompletionContext, fuzzy_name: String) -> Option<I
190 ctx.scope.clone(), 187 ctx.scope.clone(),
191 )?; 188 )?;
192 189
190 // TODO kb bad: with the path prefix, the "min 3 symbols" limit applies. Fix in a separate PR on the symbol_index level
193 if matches!(assets_for_path.import_candidate(), ImportCandidate::Path(_)) 191 if matches!(assets_for_path.import_candidate(), ImportCandidate::Path(_))
194 && fuzzy_name_length < 2 192 && fuzzy_name_length < 2
195 { 193 {
@@ -796,9 +794,7 @@ fn main() {
796 794
797 #[test] 795 #[test]
798 fn unresolved_qualifier() { 796 fn unresolved_qualifier() {
799 check_edit( 797 let fixture = r#"
800 "Item",
801 r#"
802mod foo { 798mod foo {
803 pub mod bar { 799 pub mod bar {
804 pub mod baz { 800 pub mod baz {
@@ -809,31 +805,34 @@ mod foo {
809 805
810fn main() { 806fn main() {
811 bar::baz::Ite$0 807 bar::baz::Ite$0
812} 808}"#;
813"#, 809
810 check(fixture, expect![["st Item (foo::bar::baz::Item)"]]);
811
812 check_edit(
813 "Item",
814 fixture,
814 r#" 815 r#"
815use foo::bar; 816 use foo::bar;
816 817
817mod foo { 818 mod foo {
818 pub mod bar { 819 pub mod bar {
819 pub mod baz { 820 pub mod baz {
820 pub struct Item; 821 pub struct Item;
822 }
823 }
821 } 824 }
822 }
823}
824 825
825fn main() { 826 fn main() {
826 bar::baz::Item 827 bar::baz::Item
827} 828 }
828"#, 829 "#,
829 ); 830 );
830 } 831 }
831 832
832 #[test] 833 #[test]
833 fn unresolved_assoc_item_container() { 834 fn unresolved_assoc_item_container() {
834 check_edit( 835 let fixture = r#"
835 "TEST_ASSOC",
836 r#"
837mod foo { 836mod foo {
838 pub struct Item; 837 pub struct Item;
839 838
@@ -844,8 +843,13 @@ mod foo {
844 843
845fn main() { 844fn main() {
846 Item::TEST_A$0 845 Item::TEST_A$0
847} 846}"#;
848"#, 847
848 check(fixture, expect![["ct TEST_ASSOC (foo::bar::baz::Item)"]]);
849
850 check_edit(
851 "TEST_ASSOC",
852 fixture,
849 r#" 853 r#"
850use foo::Item; 854use foo::Item;
851 855
@@ -866,9 +870,7 @@ fn main() {
866 870
867 #[test] 871 #[test]
868 fn unresolved_assoc_item_container_with_path() { 872 fn unresolved_assoc_item_container_with_path() {
869 check_edit( 873 let fixture = r#"
870 "TEST_ASSOC",
871 r#"
872mod foo { 874mod foo {
873 pub mod bar { 875 pub mod bar {
874 pub struct Item; 876 pub struct Item;
@@ -881,8 +883,13 @@ mod foo {
881 883
882fn main() { 884fn main() {
883 bar::Item::TEST_A$0 885 bar::Item::TEST_A$0
884} 886}"#;
885"#, 887
888 check(fixture, expect![["ct TEST_ASSOC (foo::bar::baz::Item)"]]);
889
890 check_edit(
891 "TEST_ASSOC",
892 fixture,
886 r#" 893 r#"
887use foo::bar; 894use foo::bar;
888 895