5 import_assets::{ImportCandidate, LocatedImport},
8 use ide_db::RootDatabase;
11 ast::{make, HasArgList},
16 assist_context::{AssistContext, Assists},
17 handlers::auto_import::find_importable_node,
18 AssistId, AssistKind, GroupLabel,
21 // Assist: qualify_path
23 // If the name is unresolved, provides all possible qualified paths for it.
27 // let map = HashMap$0::new();
29 // # pub mod std { pub mod collections { pub struct HashMap { } } }
34 // let map = std::collections::HashMap::new();
36 // # pub mod std { pub mod collections { pub struct HashMap { } } }
38 pub(crate) fn qualify_path(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
39 let (import_assets, syntax_under_caret) = find_importable_node(ctx)?;
40 let mut proposed_imports = import_assets.search_for_relative_paths(&ctx.sema);
41 if proposed_imports.is_empty() {
45 let range = ctx.sema.original_range(&syntax_under_caret).range;
46 let candidate = import_assets.import_candidate();
47 let qualify_candidate = match candidate {
48 ImportCandidate::Path(candidate) if candidate.qualifier.is_some() => {
49 cov_mark::hit!(qualify_path_qualifier_start);
50 let path = ast::Path::cast(syntax_under_caret)?;
51 let (prev_segment, segment) = (path.qualifier()?.segment()?, path.segment()?);
52 QualifyCandidate::QualifierStart(segment, prev_segment.generic_arg_list())
54 ImportCandidate::Path(_) => {
55 cov_mark::hit!(qualify_path_unqualified_name);
56 let path = ast::Path::cast(syntax_under_caret)?;
57 let generics = path.segment()?.generic_arg_list();
58 QualifyCandidate::UnqualifiedName(generics)
60 ImportCandidate::TraitAssocItem(_) => {
61 cov_mark::hit!(qualify_path_trait_assoc_item);
62 let path = ast::Path::cast(syntax_under_caret)?;
63 let (qualifier, segment) = (path.qualifier()?, path.segment()?);
64 QualifyCandidate::TraitAssocItem(qualifier, segment)
66 ImportCandidate::TraitMethod(_) => {
67 cov_mark::hit!(qualify_path_trait_method);
68 let mcall_expr = ast::MethodCallExpr::cast(syntax_under_caret)?;
69 QualifyCandidate::TraitMethod(ctx.sema.db, mcall_expr)
73 // we aren't interested in different namespaces
74 proposed_imports.dedup_by(|a, b| a.import_path == b.import_path);
76 let group_label = group_label(candidate);
77 for import in proposed_imports {
80 AssistId("qualify_path", AssistKind::QuickFix),
81 label(candidate, &import),
84 qualify_candidate.qualify(
85 |replace_with: String| builder.replace(range, replace_with),
87 import.item_to_import,
95 enum QualifyCandidate<'db> {
96 QualifierStart(ast::PathSegment, Option<ast::GenericArgList>),
97 UnqualifiedName(Option<ast::GenericArgList>),
98 TraitAssocItem(ast::Path, ast::PathSegment),
99 TraitMethod(&'db RootDatabase, ast::MethodCallExpr),
102 impl QualifyCandidate<'_> {
105 mut replacer: impl FnMut(String),
106 import: &hir::ModPath,
109 let import = mod_path_to_ast(import);
111 QualifyCandidate::QualifierStart(segment, generics) => {
112 let generics = generics.as_ref().map_or_else(String::new, ToString::to_string);
113 replacer(format!("{}{}::{}", import, generics, segment));
115 QualifyCandidate::UnqualifiedName(generics) => {
116 let generics = generics.as_ref().map_or_else(String::new, ToString::to_string);
117 replacer(format!("{}{}", import.to_string(), generics));
119 QualifyCandidate::TraitAssocItem(qualifier, segment) => {
120 replacer(format!("<{} as {}>::{}", qualifier, import, segment));
122 QualifyCandidate::TraitMethod(db, mcall_expr) => {
123 Self::qualify_trait_method(db, mcall_expr, replacer, import, item);
128 fn qualify_trait_method(
130 mcall_expr: &ast::MethodCallExpr,
131 mut replacer: impl FnMut(String),
135 let receiver = mcall_expr.receiver()?;
136 let trait_method_name = mcall_expr.name_ref()?;
138 mcall_expr.generic_arg_list().as_ref().map_or_else(String::new, ToString::to_string);
139 let arg_list = mcall_expr.arg_list().map(|arg_list| arg_list.args());
140 let trait_ = item_as_trait(db, item)?;
141 let method = find_trait_method(db, trait_, &trait_method_name)?;
142 if let Some(self_access) = method.self_param(db).map(|sp| sp.access(db)) {
143 let receiver = match self_access {
144 hir::Access::Shared => make::expr_ref(receiver, false),
145 hir::Access::Exclusive => make::expr_ref(receiver, true),
146 hir::Access::Owned => receiver,
154 Some(args) => make::arg_list(iter::once(receiver).chain(args)),
155 None => make::arg_list(iter::once(receiver)),
163 fn find_trait_method(
166 trait_method_name: &ast::NameRef,
167 ) -> Option<hir::Function> {
168 if let Some(hir::AssocItem::Function(method)) =
169 trait_.items(db).into_iter().find(|item: &hir::AssocItem| {
171 .map(|name| name.to_string() == trait_method_name.to_string())
181 fn item_as_trait(db: &RootDatabase, item: hir::ItemInNs) -> Option<hir::Trait> {
182 let item_module_def = item.as_module_def()?;
184 match item_module_def {
185 hir::ModuleDef::Trait(trait_) => Some(trait_),
186 _ => item_module_def.as_assoc_item(db)?.containing_trait(db),
190 fn group_label(candidate: &ImportCandidate) -> GroupLabel {
191 let name = match candidate {
192 ImportCandidate::Path(it) => &it.name,
193 ImportCandidate::TraitAssocItem(it) | ImportCandidate::TraitMethod(it) => {
198 GroupLabel(format!("Qualify {}", name))
201 fn label(candidate: &ImportCandidate, import: &LocatedImport) -> String {
203 ImportCandidate::Path(candidate) if candidate.qualifier.is_none() => {
204 format!("Qualify as `{}`", import.import_path)
206 _ => format!("Qualify with `{}`", import.import_path),
212 use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target};
217 fn applicable_when_found_an_import_partial() {
218 cov_mark::check!(qualify_path_unqualified_name);
224 pub struct Formatter;
235 pub struct Formatter;
247 fn applicable_when_found_an_import() {
254 pub struct PubStruct;
261 pub struct PubStruct;
268 fn applicable_in_macros() {
273 ($i:ident) => { fn foo(a: $i) {} }
278 pub struct PubStruct;
283 ($i:ident) => { fn foo(a: $i) {} }
285 foo!(PubMod::PubStruct);
288 pub struct PubStruct;
295 fn applicable_when_found_multiple_imports() {
302 pub struct PubStruct;
305 pub struct PubStruct;
308 pub struct PubStruct;
315 pub struct PubStruct;
318 pub struct PubStruct;
321 pub struct PubStruct;
328 fn not_applicable_for_already_imported_types() {
329 check_assist_not_applicable(
332 use PubMod::PubStruct;
337 pub struct PubStruct;
344 fn not_applicable_for_types_with_private_paths() {
345 check_assist_not_applicable(
351 struct PrivateStruct;
358 fn not_applicable_when_no_imports_found() {
359 check_assist_not_applicable(qualify_path, r#"PubStruct$0"#);
363 fn not_applicable_in_import_statements() {
364 check_assist_not_applicable(
370 pub struct PubStruct;
377 fn qualify_function() {
384 pub fn test_function() {};
388 PubMod::test_function
391 pub fn test_function() {};
402 //- /lib.rs crate:crate_with_macro
408 //- /main.rs crate:main deps:crate_with_macro
415 crate_with_macro::foo
422 fn qualify_path_target() {
427 group_label: Option<$0GroupLabel>,
430 mod m { pub struct GroupLabel; }
437 fn not_applicable_when_path_start_is_imported() {
438 check_assist_not_applicable(
444 pub struct TestStruct;
451 mod2::mod3::TestStruct$0
458 fn not_applicable_for_imported_function() {
459 check_assist_not_applicable(
463 pub fn test_function() {}
466 use test_mod::test_function;
475 fn associated_struct_function() {
480 pub struct TestStruct {}
482 pub fn test_function() {}
487 TestStruct::test_function$0
492 pub struct TestStruct {}
494 pub fn test_function() {}
499 test_mod::TestStruct::test_function
506 fn associated_struct_const() {
507 cov_mark::check!(qualify_path_qualifier_start);
512 pub struct TestStruct {}
514 const TEST_CONST: u8 = 42;
519 TestStruct::TEST_CONST$0
524 pub struct TestStruct {}
526 const TEST_CONST: u8 = 42;
531 test_mod::TestStruct::TEST_CONST
538 fn associated_struct_const_unqualified() {
539 // FIXME: non-trait assoc items completion is unsupported yet, see FIXME in the import_assets.rs for more details
540 check_assist_not_applicable(
544 pub struct TestStruct {}
546 const TEST_CONST: u8 = 42;
558 fn associated_trait_function() {
563 pub trait TestTrait {
566 pub struct TestStruct {}
567 impl TestTrait for TestStruct {
568 fn test_function() {}
573 test_mod::TestStruct::test_function$0
578 pub trait TestTrait {
581 pub struct TestStruct {}
582 impl TestTrait for TestStruct {
583 fn test_function() {}
588 <test_mod::TestStruct as test_mod::TestTrait>::test_function
595 fn not_applicable_for_imported_trait_for_function() {
596 check_assist_not_applicable(
600 pub trait TestTrait {
603 pub trait TestTrait2 {
610 impl TestTrait2 for TestEnum {
611 fn test_function() {}
613 impl TestTrait for TestEnum {
614 fn test_function() {}
618 use test_mod::TestTrait2;
620 test_mod::TestEnum::test_function$0;
627 fn associated_trait_const() {
628 cov_mark::check!(qualify_path_trait_assoc_item);
633 pub trait TestTrait {
634 const TEST_CONST: u8;
636 pub struct TestStruct {}
637 impl TestTrait for TestStruct {
638 const TEST_CONST: u8 = 42;
643 test_mod::TestStruct::TEST_CONST$0
648 pub trait TestTrait {
649 const TEST_CONST: u8;
651 pub struct TestStruct {}
652 impl TestTrait for TestStruct {
653 const TEST_CONST: u8 = 42;
658 <test_mod::TestStruct as test_mod::TestTrait>::TEST_CONST
665 fn not_applicable_for_imported_trait_for_const() {
666 check_assist_not_applicable(
670 pub trait TestTrait {
671 const TEST_CONST: u8;
673 pub trait TestTrait2 {
674 const TEST_CONST: f64;
680 impl TestTrait2 for TestEnum {
681 const TEST_CONST: f64 = 42.0;
683 impl TestTrait for TestEnum {
684 const TEST_CONST: u8 = 42;
688 use test_mod::TestTrait2;
690 test_mod::TestEnum::TEST_CONST$0;
698 cov_mark::check!(qualify_path_trait_method);
703 pub trait TestTrait {
704 fn test_method(&self);
706 pub struct TestStruct {}
707 impl TestTrait for TestStruct {
708 fn test_method(&self) {}
713 let test_struct = test_mod::TestStruct {};
714 test_struct.test_meth$0od()
719 pub trait TestTrait {
720 fn test_method(&self);
722 pub struct TestStruct {}
723 impl TestTrait for TestStruct {
724 fn test_method(&self) {}
729 let test_struct = test_mod::TestStruct {};
730 test_mod::TestTrait::test_method(&test_struct)
737 fn trait_method_multi_params() {
742 pub trait TestTrait {
743 fn test_method(&self, test: i32);
745 pub struct TestStruct {}
746 impl TestTrait for TestStruct {
747 fn test_method(&self, test: i32) {}
752 let test_struct = test_mod::TestStruct {};
753 test_struct.test_meth$0od(42)
758 pub trait TestTrait {
759 fn test_method(&self, test: i32);
761 pub struct TestStruct {}
762 impl TestTrait for TestStruct {
763 fn test_method(&self, test: i32) {}
768 let test_struct = test_mod::TestStruct {};
769 test_mod::TestTrait::test_method(&test_struct, 42)
776 fn trait_method_consume() {
781 pub trait TestTrait {
782 fn test_method(self);
784 pub struct TestStruct {}
785 impl TestTrait for TestStruct {
786 fn test_method(self) {}
791 let test_struct = test_mod::TestStruct {};
792 test_struct.test_meth$0od()
797 pub trait TestTrait {
798 fn test_method(self);
800 pub struct TestStruct {}
801 impl TestTrait for TestStruct {
802 fn test_method(self) {}
807 let test_struct = test_mod::TestStruct {};
808 test_mod::TestTrait::test_method(test_struct)
815 fn trait_method_cross_crate() {
819 //- /main.rs crate:main deps:dep
821 let test_struct = dep::test_mod::TestStruct {};
822 test_struct.test_meth$0od()
824 //- /dep.rs crate:dep
826 pub trait TestTrait {
827 fn test_method(&self);
829 pub struct TestStruct {}
830 impl TestTrait for TestStruct {
831 fn test_method(&self) {}
837 let test_struct = dep::test_mod::TestStruct {};
838 dep::test_mod::TestTrait::test_method(&test_struct)
845 fn assoc_fn_cross_crate() {
849 //- /main.rs crate:main deps:dep
851 dep::test_mod::TestStruct::test_func$0tion
853 //- /dep.rs crate:dep
855 pub trait TestTrait {
858 pub struct TestStruct {}
859 impl TestTrait for TestStruct {
860 fn test_function() {}
866 <dep::test_mod::TestStruct as dep::test_mod::TestTrait>::test_function
873 fn assoc_const_cross_crate() {
877 //- /main.rs crate:main deps:dep
879 dep::test_mod::TestStruct::CONST$0
881 //- /dep.rs crate:dep
883 pub trait TestTrait {
886 pub struct TestStruct {}
887 impl TestTrait for TestStruct {
888 const CONST: bool = true;
894 <dep::test_mod::TestStruct as dep::test_mod::TestTrait>::CONST
901 fn assoc_fn_as_method_cross_crate() {
902 check_assist_not_applicable(
905 //- /main.rs crate:main deps:dep
907 let test_struct = dep::test_mod::TestStruct {};
908 test_struct.test_func$0tion()
910 //- /dep.rs crate:dep
912 pub trait TestTrait {
915 pub struct TestStruct {}
916 impl TestTrait for TestStruct {
917 fn test_function() {}
925 fn private_trait_cross_crate() {
926 check_assist_not_applicable(
929 //- /main.rs crate:main deps:dep
931 let test_struct = dep::test_mod::TestStruct {};
932 test_struct.test_meth$0od()
934 //- /dep.rs crate:dep
937 fn test_method(&self);
939 pub struct TestStruct {}
940 impl TestTrait for TestStruct {
941 fn test_method(&self) {}
949 fn not_applicable_for_imported_trait_for_method() {
950 check_assist_not_applicable(
954 pub trait TestTrait {
955 fn test_method(&self);
957 pub trait TestTrait2 {
958 fn test_method(&self);
964 impl TestTrait2 for TestEnum {
965 fn test_method(&self) {}
967 impl TestTrait for TestEnum {
968 fn test_method(&self) {}
972 use test_mod::TestTrait2;
974 let one = test_mod::TestEnum::One;
986 //- /lib.rs crate:dep
989 //- /main.rs crate:main deps:dep
1003 fn whole_segment() {
1004 // Tests that only imports whose last segment matches the identifier get suggested.
1008 //- /lib.rs crate:dep
1010 pub trait Display {}
1013 pub fn panic_fmt() {}
1015 //- /main.rs crate:main deps:dep
1018 impl f$0mt::Display for S {}
1023 impl dep::fmt::Display for S {}
1029 fn macro_generated() {
1030 // Tests that macro-generated items are suggested from external crates.
1034 //- /lib.rs crate:dep
1043 //- /main.rs crate:main deps:dep
1058 // Tests that differently cased names don't interfere and we only suggest the matching one.
1062 //- /lib.rs crate:dep
1066 //- /main.rs crate:main deps:dep
1080 fn keep_generic_annotations() {
1084 //- /lib.rs crate:dep
1085 pub mod generic { pub struct Thing<'a, T>(&'a T); }
1087 //- /main.rs crate:main deps:dep
1088 fn foo() -> Thin$0g<'static, ()> {}
1093 fn foo() -> dep::generic::Thing<'static, ()> {}
1101 fn keep_generic_annotations_leading_colon() {
1105 //- /lib.rs crate:dep
1106 pub mod generic { pub struct Thing<'a, T>(&'a T); }
1108 //- /main.rs crate:main deps:dep
1109 fn foo() -> Thin$0g::<'static, ()> {}
1114 fn foo() -> dep::generic::Thing::<'static, ()> {}
1122 fn associated_struct_const_generic() {
1127 pub struct TestStruct<T> {}
1128 impl<T> TestStruct<T> {
1129 const TEST_CONST: u8 = 42;
1134 TestStruct::<()>::TEST_CONST$0
1139 pub struct TestStruct<T> {}
1140 impl<T> TestStruct<T> {
1141 const TEST_CONST: u8 = 42;
1146 test_mod::TestStruct::<()>::TEST_CONST
1153 fn associated_trait_const_generic() {
1158 pub trait TestTrait {
1159 const TEST_CONST: u8;
1161 pub struct TestStruct<T> {}
1162 impl<T> TestTrait for TestStruct<T> {
1163 const TEST_CONST: u8 = 42;
1168 test_mod::TestStruct::<()>::TEST_CONST$0
1173 pub trait TestTrait {
1174 const TEST_CONST: u8;
1176 pub struct TestStruct<T> {}
1177 impl<T> TestTrait for TestStruct<T> {
1178 const TEST_CONST: u8 = 42;
1183 <test_mod::TestStruct::<()> as test_mod::TestTrait>::TEST_CONST
1190 fn trait_method_generic() {
1195 pub trait TestTrait {
1196 fn test_method<T>(&self);
1198 pub struct TestStruct {}
1199 impl TestTrait for TestStruct {
1200 fn test_method<T>(&self) {}
1205 let test_struct = test_mod::TestStruct {};
1206 test_struct.test_meth$0od::<()>()
1211 pub trait TestTrait {
1212 fn test_method<T>(&self);
1214 pub struct TestStruct {}
1215 impl TestTrait for TestStruct {
1216 fn test_method<T>(&self) {}
1221 let test_struct = test_mod::TestStruct {};
1222 test_mod::TestTrait::test_method::<()>(&test_struct)