4 use ide_db::RootDatabase;
6 helpers::mod_path_to_ast,
7 imports::import_assets::{ImportCandidate, LocatedImport},
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 =
41 import_assets.search_for_relative_paths(&ctx.sema, ctx.config.prefer_no_std);
42 if proposed_imports.is_empty() {
46 let range = match &syntax_under_caret {
47 NodeOrToken::Node(node) => ctx.sema.original_range(node).range,
48 NodeOrToken::Token(token) => token.text_range(),
50 let candidate = import_assets.import_candidate();
51 let qualify_candidate = match syntax_under_caret {
52 NodeOrToken::Node(syntax_under_caret) => match candidate {
53 ImportCandidate::Path(candidate) if candidate.qualifier.is_some() => {
54 cov_mark::hit!(qualify_path_qualifier_start);
55 let path = ast::Path::cast(syntax_under_caret)?;
56 let (prev_segment, segment) = (path.qualifier()?.segment()?, path.segment()?);
57 QualifyCandidate::QualifierStart(segment, prev_segment.generic_arg_list())
59 ImportCandidate::Path(_) => {
60 cov_mark::hit!(qualify_path_unqualified_name);
61 let path = ast::Path::cast(syntax_under_caret)?;
62 let generics = path.segment()?.generic_arg_list();
63 QualifyCandidate::UnqualifiedName(generics)
65 ImportCandidate::TraitAssocItem(_) => {
66 cov_mark::hit!(qualify_path_trait_assoc_item);
67 let path = ast::Path::cast(syntax_under_caret)?;
68 let (qualifier, segment) = (path.qualifier()?, path.segment()?);
69 QualifyCandidate::TraitAssocItem(qualifier, segment)
71 ImportCandidate::TraitMethod(_) => {
72 cov_mark::hit!(qualify_path_trait_method);
73 let mcall_expr = ast::MethodCallExpr::cast(syntax_under_caret)?;
74 QualifyCandidate::TraitMethod(ctx.sema.db, mcall_expr)
77 // derive attribute path
78 NodeOrToken::Token(_) => QualifyCandidate::UnqualifiedName(None),
81 // we aren't interested in different namespaces
82 proposed_imports.dedup_by(|a, b| a.import_path == b.import_path);
84 let group_label = group_label(candidate);
85 for import in proposed_imports {
88 AssistId("qualify_path", AssistKind::QuickFix),
89 label(candidate, &import),
92 qualify_candidate.qualify(
93 |replace_with: String| builder.replace(range, replace_with),
95 import.item_to_import,
102 pub(crate) enum QualifyCandidate<'db> {
103 QualifierStart(ast::PathSegment, Option<ast::GenericArgList>),
104 UnqualifiedName(Option<ast::GenericArgList>),
105 TraitAssocItem(ast::Path, ast::PathSegment),
106 TraitMethod(&'db RootDatabase, ast::MethodCallExpr),
107 ImplMethod(&'db RootDatabase, ast::MethodCallExpr, hir::Function),
110 impl QualifyCandidate<'_> {
111 pub(crate) fn qualify(
113 mut replacer: impl FnMut(String),
114 import: &hir::ModPath,
117 let import = mod_path_to_ast(import);
119 QualifyCandidate::QualifierStart(segment, generics) => {
120 let generics = generics.as_ref().map_or_else(String::new, ToString::to_string);
121 replacer(format!("{import}{generics}::{segment}"));
123 QualifyCandidate::UnqualifiedName(generics) => {
124 let generics = generics.as_ref().map_or_else(String::new, ToString::to_string);
125 replacer(format!("{import}{generics}"));
127 QualifyCandidate::TraitAssocItem(qualifier, segment) => {
128 replacer(format!("<{qualifier} as {import}>::{segment}"));
130 QualifyCandidate::TraitMethod(db, mcall_expr) => {
131 Self::qualify_trait_method(db, mcall_expr, replacer, import, item);
133 QualifyCandidate::ImplMethod(db, mcall_expr, hir_fn) => {
134 Self::qualify_fn_call(db, mcall_expr, replacer, import, hir_fn);
141 mcall_expr: &ast::MethodCallExpr,
142 mut replacer: impl FnMut(String),
144 hir_fn: &hir::Function,
146 let receiver = mcall_expr.receiver()?;
147 let method_name = mcall_expr.name_ref()?;
149 mcall_expr.generic_arg_list().as_ref().map_or_else(String::new, ToString::to_string);
150 let arg_list = mcall_expr.arg_list().map(|arg_list| arg_list.args());
152 if let Some(self_access) = hir_fn.self_param(db).map(|sp| sp.access(db)) {
153 let receiver = match self_access {
154 hir::Access::Shared => make::expr_ref(receiver, false),
155 hir::Access::Exclusive => make::expr_ref(receiver, true),
156 hir::Access::Owned => receiver,
158 let arg_list = match arg_list {
159 Some(args) => make::arg_list(iter::once(receiver).chain(args)),
160 None => make::arg_list(iter::once(receiver)),
162 replacer(format!("{import}::{method_name}{generics}{arg_list}"));
167 fn qualify_trait_method(
169 mcall_expr: &ast::MethodCallExpr,
170 replacer: impl FnMut(String),
174 let trait_method_name = mcall_expr.name_ref()?;
175 let trait_ = item_as_trait(db, item)?;
176 let method = find_trait_method(db, trait_, &trait_method_name)?;
177 Self::qualify_fn_call(db, mcall_expr, replacer, import, &method)
181 fn find_trait_method(
184 trait_method_name: &ast::NameRef,
185 ) -> Option<hir::Function> {
186 if let Some(hir::AssocItem::Function(method)) =
187 trait_.items(db).into_iter().find(|item: &hir::AssocItem| {
189 .map(|name| name.to_string() == trait_method_name.to_string())
199 fn item_as_trait(db: &RootDatabase, item: hir::ItemInNs) -> Option<hir::Trait> {
200 let item_module_def = item.as_module_def()?;
202 match item_module_def {
203 hir::ModuleDef::Trait(trait_) => Some(trait_),
204 _ => item_module_def.as_assoc_item(db)?.containing_trait(db),
208 fn group_label(candidate: &ImportCandidate) -> GroupLabel {
209 let name = match candidate {
210 ImportCandidate::Path(it) => &it.name,
211 ImportCandidate::TraitAssocItem(it) | ImportCandidate::TraitMethod(it) => {
216 GroupLabel(format!("Qualify {name}"))
219 fn label(candidate: &ImportCandidate, import: &LocatedImport) -> String {
220 let import_path = &import.import_path;
223 ImportCandidate::Path(candidate) if candidate.qualifier.is_none() => {
224 format!("Qualify as `{import_path}`")
226 _ => format!("Qualify with `{import_path}`"),
232 use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target};
237 fn applicable_when_found_an_import_partial() {
238 cov_mark::check!(qualify_path_unqualified_name);
244 pub struct Formatter;
255 pub struct Formatter;
267 fn applicable_when_found_an_import() {
274 pub struct PubStruct;
281 pub struct PubStruct;
288 fn applicable_in_macros() {
293 ($i:ident) => { fn foo(a: $i) {} }
298 pub struct PubStruct;
303 ($i:ident) => { fn foo(a: $i) {} }
305 foo!(PubMod::PubStruct);
308 pub struct PubStruct;
315 fn applicable_when_found_multiple_imports() {
322 pub struct PubStruct;
325 pub struct PubStruct;
328 pub struct PubStruct;
335 pub struct PubStruct;
338 pub struct PubStruct;
341 pub struct PubStruct;
348 fn not_applicable_for_already_imported_types() {
349 check_assist_not_applicable(
352 use PubMod::PubStruct;
357 pub struct PubStruct;
364 fn not_applicable_for_types_with_private_paths() {
365 check_assist_not_applicable(
371 struct PrivateStruct;
378 fn not_applicable_when_no_imports_found() {
379 check_assist_not_applicable(qualify_path, r#"PubStruct$0"#);
383 fn qualify_function() {
390 pub fn test_function() {};
394 PubMod::test_function
397 pub fn test_function() {};
408 //- /lib.rs crate:crate_with_macro
414 //- /main.rs crate:main deps:crate_with_macro
421 crate_with_macro::foo
428 fn qualify_path_target() {
433 group_label: Option<$0GroupLabel>,
436 mod m { pub struct GroupLabel; }
443 fn not_applicable_when_path_start_is_imported() {
444 check_assist_not_applicable(
450 pub struct TestStruct;
457 mod2::mod3::TestStruct$0
464 fn not_applicable_for_imported_function() {
465 check_assist_not_applicable(
469 pub fn test_function() {}
472 use test_mod::test_function;
481 fn associated_struct_function() {
486 pub struct TestStruct {}
488 pub fn test_function() {}
493 TestStruct::test_function$0
498 pub struct TestStruct {}
500 pub fn test_function() {}
505 test_mod::TestStruct::test_function
512 fn associated_struct_const() {
513 cov_mark::check!(qualify_path_qualifier_start);
518 pub struct TestStruct {}
520 const TEST_CONST: u8 = 42;
525 TestStruct::TEST_CONST$0
530 pub struct TestStruct {}
532 const TEST_CONST: u8 = 42;
537 test_mod::TestStruct::TEST_CONST
544 fn associated_struct_const_unqualified() {
545 // FIXME: non-trait assoc items completion is unsupported yet, see FIXME in the import_assets.rs for more details
546 check_assist_not_applicable(
550 pub struct TestStruct {}
552 const TEST_CONST: u8 = 42;
564 fn associated_trait_function() {
569 pub trait TestTrait {
572 pub struct TestStruct {}
573 impl TestTrait for TestStruct {
574 fn test_function() {}
579 test_mod::TestStruct::test_function$0
584 pub trait TestTrait {
587 pub struct TestStruct {}
588 impl TestTrait for TestStruct {
589 fn test_function() {}
594 <test_mod::TestStruct as test_mod::TestTrait>::test_function
601 fn not_applicable_for_imported_trait_for_function() {
602 check_assist_not_applicable(
606 pub trait TestTrait {
609 pub trait TestTrait2 {
616 impl TestTrait2 for TestEnum {
617 fn test_function() {}
619 impl TestTrait for TestEnum {
620 fn test_function() {}
624 use test_mod::TestTrait2;
626 test_mod::TestEnum::test_function$0;
633 fn associated_trait_const() {
634 cov_mark::check!(qualify_path_trait_assoc_item);
639 pub trait TestTrait {
640 const TEST_CONST: u8;
642 pub struct TestStruct {}
643 impl TestTrait for TestStruct {
644 const TEST_CONST: u8 = 42;
649 test_mod::TestStruct::TEST_CONST$0
654 pub trait TestTrait {
655 const TEST_CONST: u8;
657 pub struct TestStruct {}
658 impl TestTrait for TestStruct {
659 const TEST_CONST: u8 = 42;
664 <test_mod::TestStruct as test_mod::TestTrait>::TEST_CONST
671 fn not_applicable_for_imported_trait_for_const() {
672 check_assist_not_applicable(
676 pub trait TestTrait {
677 const TEST_CONST: u8;
679 pub trait TestTrait2 {
680 const TEST_CONST: f64;
686 impl TestTrait2 for TestEnum {
687 const TEST_CONST: f64 = 42.0;
689 impl TestTrait for TestEnum {
690 const TEST_CONST: u8 = 42;
694 use test_mod::TestTrait2;
696 test_mod::TestEnum::TEST_CONST$0;
704 cov_mark::check!(qualify_path_trait_method);
709 pub trait TestTrait {
710 fn test_method(&self);
712 pub struct TestStruct {}
713 impl TestTrait for TestStruct {
714 fn test_method(&self) {}
719 let test_struct = test_mod::TestStruct {};
720 test_struct.test_meth$0od()
725 pub trait TestTrait {
726 fn test_method(&self);
728 pub struct TestStruct {}
729 impl TestTrait for TestStruct {
730 fn test_method(&self) {}
735 let test_struct = test_mod::TestStruct {};
736 test_mod::TestTrait::test_method(&test_struct)
743 fn trait_method_multi_params() {
748 pub trait TestTrait {
749 fn test_method(&self, test: i32);
751 pub struct TestStruct {}
752 impl TestTrait for TestStruct {
753 fn test_method(&self, test: i32) {}
758 let test_struct = test_mod::TestStruct {};
759 test_struct.test_meth$0od(42)
764 pub trait TestTrait {
765 fn test_method(&self, test: i32);
767 pub struct TestStruct {}
768 impl TestTrait for TestStruct {
769 fn test_method(&self, test: i32) {}
774 let test_struct = test_mod::TestStruct {};
775 test_mod::TestTrait::test_method(&test_struct, 42)
782 fn trait_method_consume() {
787 pub trait TestTrait {
788 fn test_method(self);
790 pub struct TestStruct {}
791 impl TestTrait for TestStruct {
792 fn test_method(self) {}
797 let test_struct = test_mod::TestStruct {};
798 test_struct.test_meth$0od()
803 pub trait TestTrait {
804 fn test_method(self);
806 pub struct TestStruct {}
807 impl TestTrait for TestStruct {
808 fn test_method(self) {}
813 let test_struct = test_mod::TestStruct {};
814 test_mod::TestTrait::test_method(test_struct)
821 fn trait_method_cross_crate() {
825 //- /main.rs crate:main deps:dep
827 let test_struct = dep::test_mod::TestStruct {};
828 test_struct.test_meth$0od()
830 //- /dep.rs crate:dep
832 pub trait TestTrait {
833 fn test_method(&self);
835 pub struct TestStruct {}
836 impl TestTrait for TestStruct {
837 fn test_method(&self) {}
843 let test_struct = dep::test_mod::TestStruct {};
844 dep::test_mod::TestTrait::test_method(&test_struct)
851 fn assoc_fn_cross_crate() {
855 //- /main.rs crate:main deps:dep
857 dep::test_mod::TestStruct::test_func$0tion
859 //- /dep.rs crate:dep
861 pub trait TestTrait {
864 pub struct TestStruct {}
865 impl TestTrait for TestStruct {
866 fn test_function() {}
872 <dep::test_mod::TestStruct as dep::test_mod::TestTrait>::test_function
879 fn assoc_const_cross_crate() {
883 //- /main.rs crate:main deps:dep
885 dep::test_mod::TestStruct::CONST$0
887 //- /dep.rs crate:dep
889 pub trait TestTrait {
892 pub struct TestStruct {}
893 impl TestTrait for TestStruct {
894 const CONST: bool = true;
900 <dep::test_mod::TestStruct as dep::test_mod::TestTrait>::CONST
907 fn assoc_fn_as_method_cross_crate() {
908 check_assist_not_applicable(
911 //- /main.rs crate:main deps:dep
913 let test_struct = dep::test_mod::TestStruct {};
914 test_struct.test_func$0tion()
916 //- /dep.rs crate:dep
918 pub trait TestTrait {
921 pub struct TestStruct {}
922 impl TestTrait for TestStruct {
923 fn test_function() {}
931 fn private_trait_cross_crate() {
932 check_assist_not_applicable(
935 //- /main.rs crate:main deps:dep
937 let test_struct = dep::test_mod::TestStruct {};
938 test_struct.test_meth$0od()
940 //- /dep.rs crate:dep
943 fn test_method(&self);
945 pub struct TestStruct {}
946 impl TestTrait for TestStruct {
947 fn test_method(&self) {}
955 fn not_applicable_for_imported_trait_for_method() {
956 check_assist_not_applicable(
960 pub trait TestTrait {
961 fn test_method(&self);
963 pub trait TestTrait2 {
964 fn test_method(&self);
970 impl TestTrait2 for TestEnum {
971 fn test_method(&self) {}
973 impl TestTrait for TestEnum {
974 fn test_method(&self) {}
978 use test_mod::TestTrait2;
980 let one = test_mod::TestEnum::One;
992 //- /lib.rs crate:dep
995 //- /main.rs crate:main deps:dep
1009 fn whole_segment() {
1010 // Tests that only imports whose last segment matches the identifier get suggested.
1014 //- /lib.rs crate:dep
1016 pub trait Display {}
1019 pub fn panic_fmt() {}
1021 //- /main.rs crate:main deps:dep
1024 impl f$0mt::Display for S {}
1029 impl dep::fmt::Display for S {}
1035 fn macro_generated() {
1036 // Tests that macro-generated items are suggested from external crates.
1040 //- /lib.rs crate:dep
1049 //- /main.rs crate:main deps:dep
1064 // Tests that differently cased names don't interfere and we only suggest the matching one.
1068 //- /lib.rs crate:dep
1072 //- /main.rs crate:main deps:dep
1086 fn keep_generic_annotations() {
1090 //- /lib.rs crate:dep
1091 pub mod generic { pub struct Thing<'a, T>(&'a T); }
1093 //- /main.rs crate:main deps:dep
1094 fn foo() -> Thin$0g<'static, ()> {}
1099 fn foo() -> dep::generic::Thing<'static, ()> {}
1107 fn keep_generic_annotations_leading_colon() {
1111 //- /lib.rs crate:dep
1112 pub mod generic { pub struct Thing<'a, T>(&'a T); }
1114 //- /main.rs crate:main deps:dep
1115 fn foo() -> Thin$0g::<'static, ()> {}
1120 fn foo() -> dep::generic::Thing::<'static, ()> {}
1128 fn associated_struct_const_generic() {
1133 pub struct TestStruct<T> {}
1134 impl<T> TestStruct<T> {
1135 const TEST_CONST: u8 = 42;
1140 TestStruct::<()>::TEST_CONST$0
1145 pub struct TestStruct<T> {}
1146 impl<T> TestStruct<T> {
1147 const TEST_CONST: u8 = 42;
1152 test_mod::TestStruct::<()>::TEST_CONST
1159 fn associated_trait_const_generic() {
1164 pub trait TestTrait {
1165 const TEST_CONST: u8;
1167 pub struct TestStruct<T> {}
1168 impl<T> TestTrait for TestStruct<T> {
1169 const TEST_CONST: u8 = 42;
1174 test_mod::TestStruct::<()>::TEST_CONST$0
1179 pub trait TestTrait {
1180 const TEST_CONST: u8;
1182 pub struct TestStruct<T> {}
1183 impl<T> TestTrait for TestStruct<T> {
1184 const TEST_CONST: u8 = 42;
1189 <test_mod::TestStruct::<()> as test_mod::TestTrait>::TEST_CONST
1196 fn trait_method_generic() {
1201 pub trait TestTrait {
1202 fn test_method<T>(&self);
1204 pub struct TestStruct {}
1205 impl TestTrait for TestStruct {
1206 fn test_method<T>(&self) {}
1211 let test_struct = test_mod::TestStruct {};
1212 test_struct.test_meth$0od::<()>()
1217 pub trait TestTrait {
1218 fn test_method<T>(&self);
1220 pub struct TestStruct {}
1221 impl TestTrait for TestStruct {
1222 fn test_method<T>(&self) {}
1227 let test_struct = test_mod::TestStruct {};
1228 test_mod::TestTrait::test_method::<()>(&test_struct)
1235 fn works_in_derives() {
1241 #[rustc_builtin_macro]
1249 #[rustc_builtin_macro]
1252 #[derive(foo::Copy)]
1259 fn works_in_use_start() {
1282 fn not_applicable_in_non_start_use() {
1283 check_assist_not_applicable(