diff options
Diffstat (limited to 'crates/assists/src/handlers/qualify_path.rs')
-rw-r--r-- | crates/assists/src/handlers/qualify_path.rs | 116 |
1 files changed, 62 insertions, 54 deletions
diff --git a/crates/assists/src/handlers/qualify_path.rs b/crates/assists/src/handlers/qualify_path.rs index 98cb09214..a7d9fd4dc 100644 --- a/crates/assists/src/handlers/qualify_path.rs +++ b/crates/assists/src/handlers/qualify_path.rs | |||
@@ -1,7 +1,10 @@ | |||
1 | use std::iter; | 1 | use std::iter; |
2 | 2 | ||
3 | use hir::AsName; | 3 | use hir::AsName; |
4 | use ide_db::helpers::mod_path_to_ast; | 4 | use ide_db::helpers::{ |
5 | import_assets::{ImportAssets, ImportCandidate}, | ||
6 | mod_path_to_ast, | ||
7 | }; | ||
5 | use ide_db::RootDatabase; | 8 | use ide_db::RootDatabase; |
6 | use syntax::{ | 9 | use syntax::{ |
7 | ast, | 10 | ast, |
@@ -12,7 +15,6 @@ use test_utils::mark; | |||
12 | 15 | ||
13 | use crate::{ | 16 | use crate::{ |
14 | assist_context::{AssistContext, Assists}, | 17 | assist_context::{AssistContext, Assists}, |
15 | utils::import_assets::{ImportAssets, ImportCandidate}, | ||
16 | AssistId, AssistKind, GroupLabel, | 18 | AssistId, AssistKind, GroupLabel, |
17 | }; | 19 | }; |
18 | 20 | ||
@@ -22,7 +24,7 @@ use crate::{ | |||
22 | // | 24 | // |
23 | // ``` | 25 | // ``` |
24 | // fn main() { | 26 | // fn main() { |
25 | // let map = HashMap<|>::new(); | 27 | // let map = HashMap$0::new(); |
26 | // } | 28 | // } |
27 | // # pub mod std { pub mod collections { pub struct HashMap { } } } | 29 | // # pub mod std { pub mod collections { pub struct HashMap { } } } |
28 | // ``` | 30 | // ``` |
@@ -53,17 +55,18 @@ pub(crate) fn qualify_path(acc: &mut Assists, ctx: &AssistContext) -> Option<()> | |||
53 | let range = ctx.sema.original_range(import_assets.syntax_under_caret()).range; | 55 | let range = ctx.sema.original_range(import_assets.syntax_under_caret()).range; |
54 | 56 | ||
55 | let qualify_candidate = match candidate { | 57 | let qualify_candidate = match candidate { |
56 | ImportCandidate::QualifierStart(_) => { | 58 | ImportCandidate::Path(candidate) => { |
57 | mark::hit!(qualify_path_qualifier_start); | 59 | if candidate.qualifier.is_some() { |
58 | let path = ast::Path::cast(import_assets.syntax_under_caret().clone())?; | 60 | mark::hit!(qualify_path_qualifier_start); |
59 | let (prev_segment, segment) = (path.qualifier()?.segment()?, path.segment()?); | 61 | let path = ast::Path::cast(import_assets.syntax_under_caret().clone())?; |
60 | QualifyCandidate::QualifierStart(segment, prev_segment.generic_arg_list()) | 62 | let (prev_segment, segment) = (path.qualifier()?.segment()?, path.segment()?); |
61 | } | 63 | QualifyCandidate::QualifierStart(segment, prev_segment.generic_arg_list()) |
62 | ImportCandidate::UnqualifiedName(_) => { | 64 | } else { |
63 | mark::hit!(qualify_path_unqualified_name); | 65 | mark::hit!(qualify_path_unqualified_name); |
64 | let path = ast::Path::cast(import_assets.syntax_under_caret().clone())?; | 66 | let path = ast::Path::cast(import_assets.syntax_under_caret().clone())?; |
65 | let generics = path.segment()?.generic_arg_list(); | 67 | let generics = path.segment()?.generic_arg_list(); |
66 | QualifyCandidate::UnqualifiedName(generics) | 68 | QualifyCandidate::UnqualifiedName(generics) |
69 | } | ||
67 | } | 70 | } |
68 | ImportCandidate::TraitAssocItem(_) => { | 71 | ImportCandidate::TraitAssocItem(_) => { |
69 | mark::hit!(qualify_path_trait_assoc_item); | 72 | mark::hit!(qualify_path_trait_assoc_item); |
@@ -186,7 +189,7 @@ fn item_as_trait(item: hir::ItemInNs) -> Option<hir::Trait> { | |||
186 | 189 | ||
187 | fn group_label(candidate: &ImportCandidate) -> GroupLabel { | 190 | fn group_label(candidate: &ImportCandidate) -> GroupLabel { |
188 | let name = match candidate { | 191 | let name = match candidate { |
189 | ImportCandidate::UnqualifiedName(it) | ImportCandidate::QualifierStart(it) => &it.name, | 192 | ImportCandidate::Path(it) => &it.name, |
190 | ImportCandidate::TraitAssocItem(it) | ImportCandidate::TraitMethod(it) => &it.name, | 193 | ImportCandidate::TraitAssocItem(it) | ImportCandidate::TraitMethod(it) => &it.name, |
191 | }; | 194 | }; |
192 | GroupLabel(format!("Qualify {}", name)) | 195 | GroupLabel(format!("Qualify {}", name)) |
@@ -194,8 +197,13 @@ fn group_label(candidate: &ImportCandidate) -> GroupLabel { | |||
194 | 197 | ||
195 | fn label(candidate: &ImportCandidate, import: &hir::ModPath) -> String { | 198 | fn label(candidate: &ImportCandidate, import: &hir::ModPath) -> String { |
196 | match candidate { | 199 | match candidate { |
197 | ImportCandidate::UnqualifiedName(_) => format!("Qualify as `{}`", &import), | 200 | ImportCandidate::Path(candidate) => { |
198 | ImportCandidate::QualifierStart(_) => format!("Qualify with `{}`", &import), | 201 | if candidate.qualifier.is_some() { |
202 | format!("Qualify with `{}`", &import) | ||
203 | } else { | ||
204 | format!("Qualify as `{}`", &import) | ||
205 | } | ||
206 | } | ||
199 | ImportCandidate::TraitAssocItem(_) => format!("Qualify `{}`", &import), | 207 | ImportCandidate::TraitAssocItem(_) => format!("Qualify `{}`", &import), |
200 | ImportCandidate::TraitMethod(_) => format!("Qualify with cast as `{}`", &import), | 208 | ImportCandidate::TraitMethod(_) => format!("Qualify with cast as `{}`", &import), |
201 | } | 209 | } |
@@ -221,7 +229,7 @@ mod tests { | |||
221 | 229 | ||
222 | use std::fmt; | 230 | use std::fmt; |
223 | 231 | ||
224 | <|>Formatter | 232 | $0Formatter |
225 | ", | 233 | ", |
226 | r" | 234 | r" |
227 | mod std { | 235 | mod std { |
@@ -242,7 +250,7 @@ mod tests { | |||
242 | check_assist( | 250 | check_assist( |
243 | qualify_path, | 251 | qualify_path, |
244 | r" | 252 | r" |
245 | <|>PubStruct | 253 | $0PubStruct |
246 | 254 | ||
247 | pub mod PubMod { | 255 | pub mod PubMod { |
248 | pub struct PubStruct; | 256 | pub struct PubStruct; |
@@ -266,7 +274,7 @@ mod tests { | |||
266 | macro_rules! foo { | 274 | macro_rules! foo { |
267 | ($i:ident) => { fn foo(a: $i) {} } | 275 | ($i:ident) => { fn foo(a: $i) {} } |
268 | } | 276 | } |
269 | foo!(Pub<|>Struct); | 277 | foo!(Pub$0Struct); |
270 | 278 | ||
271 | pub mod PubMod { | 279 | pub mod PubMod { |
272 | pub struct PubStruct; | 280 | pub struct PubStruct; |
@@ -290,7 +298,7 @@ mod tests { | |||
290 | check_assist( | 298 | check_assist( |
291 | qualify_path, | 299 | qualify_path, |
292 | r" | 300 | r" |
293 | PubSt<|>ruct | 301 | PubSt$0ruct |
294 | 302 | ||
295 | pub mod PubMod1 { | 303 | pub mod PubMod1 { |
296 | pub struct PubStruct; | 304 | pub struct PubStruct; |
@@ -325,7 +333,7 @@ mod tests { | |||
325 | r" | 333 | r" |
326 | use PubMod::PubStruct; | 334 | use PubMod::PubStruct; |
327 | 335 | ||
328 | PubStruct<|> | 336 | PubStruct$0 |
329 | 337 | ||
330 | pub mod PubMod { | 338 | pub mod PubMod { |
331 | pub struct PubStruct; | 339 | pub struct PubStruct; |
@@ -339,7 +347,7 @@ mod tests { | |||
339 | check_assist_not_applicable( | 347 | check_assist_not_applicable( |
340 | qualify_path, | 348 | qualify_path, |
341 | r" | 349 | r" |
342 | PrivateStruct<|> | 350 | PrivateStruct$0 |
343 | 351 | ||
344 | pub mod PubMod { | 352 | pub mod PubMod { |
345 | struct PrivateStruct; | 353 | struct PrivateStruct; |
@@ -353,7 +361,7 @@ mod tests { | |||
353 | check_assist_not_applicable( | 361 | check_assist_not_applicable( |
354 | qualify_path, | 362 | qualify_path, |
355 | " | 363 | " |
356 | PubStruct<|>", | 364 | PubStruct$0", |
357 | ); | 365 | ); |
358 | } | 366 | } |
359 | 367 | ||
@@ -362,7 +370,7 @@ mod tests { | |||
362 | check_assist_not_applicable( | 370 | check_assist_not_applicable( |
363 | qualify_path, | 371 | qualify_path, |
364 | r" | 372 | r" |
365 | use PubStruct<|>; | 373 | use PubStruct$0; |
366 | 374 | ||
367 | pub mod PubMod { | 375 | pub mod PubMod { |
368 | pub struct PubStruct; | 376 | pub struct PubStruct; |
@@ -375,7 +383,7 @@ mod tests { | |||
375 | check_assist( | 383 | check_assist( |
376 | qualify_path, | 384 | qualify_path, |
377 | r" | 385 | r" |
378 | test_function<|> | 386 | test_function$0 |
379 | 387 | ||
380 | pub mod PubMod { | 388 | pub mod PubMod { |
381 | pub fn test_function() {}; | 389 | pub fn test_function() {}; |
@@ -404,7 +412,7 @@ macro_rules! foo { | |||
404 | 412 | ||
405 | //- /main.rs crate:main deps:crate_with_macro | 413 | //- /main.rs crate:main deps:crate_with_macro |
406 | fn main() { | 414 | fn main() { |
407 | foo<|> | 415 | foo$0 |
408 | } | 416 | } |
409 | ", | 417 | ", |
410 | r" | 418 | r" |
@@ -421,7 +429,7 @@ fn main() { | |||
421 | qualify_path, | 429 | qualify_path, |
422 | r" | 430 | r" |
423 | struct AssistInfo { | 431 | struct AssistInfo { |
424 | group_label: Option<<|>GroupLabel>, | 432 | group_label: Option<$0GroupLabel>, |
425 | } | 433 | } |
426 | 434 | ||
427 | mod m { pub struct GroupLabel; } | 435 | mod m { pub struct GroupLabel; } |
@@ -445,7 +453,7 @@ fn main() { | |||
445 | 453 | ||
446 | use mod1::mod2; | 454 | use mod1::mod2; |
447 | fn main() { | 455 | fn main() { |
448 | mod2::mod3::TestStruct<|> | 456 | mod2::mod3::TestStruct$0 |
449 | } | 457 | } |
450 | ", | 458 | ", |
451 | ); | 459 | ); |
@@ -462,7 +470,7 @@ fn main() { | |||
462 | 470 | ||
463 | use test_mod::test_function; | 471 | use test_mod::test_function; |
464 | fn main() { | 472 | fn main() { |
465 | test_function<|> | 473 | test_function$0 |
466 | } | 474 | } |
467 | ", | 475 | ", |
468 | ); | 476 | ); |
@@ -481,7 +489,7 @@ fn main() { | |||
481 | } | 489 | } |
482 | 490 | ||
483 | fn main() { | 491 | fn main() { |
484 | TestStruct::test_function<|> | 492 | TestStruct::test_function$0 |
485 | } | 493 | } |
486 | ", | 494 | ", |
487 | r" | 495 | r" |
@@ -513,7 +521,7 @@ fn main() { | |||
513 | } | 521 | } |
514 | 522 | ||
515 | fn main() { | 523 | fn main() { |
516 | TestStruct::TEST_CONST<|> | 524 | TestStruct::TEST_CONST$0 |
517 | } | 525 | } |
518 | ", | 526 | ", |
519 | r" | 527 | r" |
@@ -547,7 +555,7 @@ fn main() { | |||
547 | } | 555 | } |
548 | 556 | ||
549 | fn main() { | 557 | fn main() { |
550 | test_mod::TestStruct::test_function<|> | 558 | test_mod::TestStruct::test_function$0 |
551 | } | 559 | } |
552 | ", | 560 | ", |
553 | r" | 561 | r" |
@@ -594,7 +602,7 @@ fn main() { | |||
594 | 602 | ||
595 | use test_mod::TestTrait2; | 603 | use test_mod::TestTrait2; |
596 | fn main() { | 604 | fn main() { |
597 | test_mod::TestEnum::test_function<|>; | 605 | test_mod::TestEnum::test_function$0; |
598 | } | 606 | } |
599 | ", | 607 | ", |
600 | ) | 608 | ) |
@@ -617,7 +625,7 @@ fn main() { | |||
617 | } | 625 | } |
618 | 626 | ||
619 | fn main() { | 627 | fn main() { |
620 | test_mod::TestStruct::TEST_CONST<|> | 628 | test_mod::TestStruct::TEST_CONST$0 |
621 | } | 629 | } |
622 | ", | 630 | ", |
623 | r" | 631 | r" |
@@ -664,7 +672,7 @@ fn main() { | |||
664 | 672 | ||
665 | use test_mod::TestTrait2; | 673 | use test_mod::TestTrait2; |
666 | fn main() { | 674 | fn main() { |
667 | test_mod::TestEnum::TEST_CONST<|>; | 675 | test_mod::TestEnum::TEST_CONST$0; |
668 | } | 676 | } |
669 | ", | 677 | ", |
670 | ) | 678 | ) |
@@ -688,7 +696,7 @@ fn main() { | |||
688 | 696 | ||
689 | fn main() { | 697 | fn main() { |
690 | let test_struct = test_mod::TestStruct {}; | 698 | let test_struct = test_mod::TestStruct {}; |
691 | test_struct.test_meth<|>od() | 699 | test_struct.test_meth$0od() |
692 | } | 700 | } |
693 | ", | 701 | ", |
694 | r" | 702 | r" |
@@ -727,7 +735,7 @@ fn main() { | |||
727 | 735 | ||
728 | fn main() { | 736 | fn main() { |
729 | let test_struct = test_mod::TestStruct {}; | 737 | let test_struct = test_mod::TestStruct {}; |
730 | test_struct.test_meth<|>od(42) | 738 | test_struct.test_meth$0od(42) |
731 | } | 739 | } |
732 | ", | 740 | ", |
733 | r" | 741 | r" |
@@ -766,7 +774,7 @@ fn main() { | |||
766 | 774 | ||
767 | fn main() { | 775 | fn main() { |
768 | let test_struct = test_mod::TestStruct {}; | 776 | let test_struct = test_mod::TestStruct {}; |
769 | test_struct.test_meth<|>od() | 777 | test_struct.test_meth$0od() |
770 | } | 778 | } |
771 | ", | 779 | ", |
772 | r" | 780 | r" |
@@ -796,7 +804,7 @@ fn main() { | |||
796 | //- /main.rs crate:main deps:dep | 804 | //- /main.rs crate:main deps:dep |
797 | fn main() { | 805 | fn main() { |
798 | let test_struct = dep::test_mod::TestStruct {}; | 806 | let test_struct = dep::test_mod::TestStruct {}; |
799 | test_struct.test_meth<|>od() | 807 | test_struct.test_meth$0od() |
800 | } | 808 | } |
801 | //- /dep.rs crate:dep | 809 | //- /dep.rs crate:dep |
802 | pub mod test_mod { | 810 | pub mod test_mod { |
@@ -825,7 +833,7 @@ fn main() { | |||
825 | r" | 833 | r" |
826 | //- /main.rs crate:main deps:dep | 834 | //- /main.rs crate:main deps:dep |
827 | fn main() { | 835 | fn main() { |
828 | dep::test_mod::TestStruct::test_func<|>tion | 836 | dep::test_mod::TestStruct::test_func$0tion |
829 | } | 837 | } |
830 | //- /dep.rs crate:dep | 838 | //- /dep.rs crate:dep |
831 | pub mod test_mod { | 839 | pub mod test_mod { |
@@ -853,7 +861,7 @@ fn main() { | |||
853 | r" | 861 | r" |
854 | //- /main.rs crate:main deps:dep | 862 | //- /main.rs crate:main deps:dep |
855 | fn main() { | 863 | fn main() { |
856 | dep::test_mod::TestStruct::CONST<|> | 864 | dep::test_mod::TestStruct::CONST$0 |
857 | } | 865 | } |
858 | //- /dep.rs crate:dep | 866 | //- /dep.rs crate:dep |
859 | pub mod test_mod { | 867 | pub mod test_mod { |
@@ -882,7 +890,7 @@ fn main() { | |||
882 | //- /main.rs crate:main deps:dep | 890 | //- /main.rs crate:main deps:dep |
883 | fn main() { | 891 | fn main() { |
884 | let test_struct = dep::test_mod::TestStruct {}; | 892 | let test_struct = dep::test_mod::TestStruct {}; |
885 | test_struct.test_func<|>tion() | 893 | test_struct.test_func$0tion() |
886 | } | 894 | } |
887 | //- /dep.rs crate:dep | 895 | //- /dep.rs crate:dep |
888 | pub mod test_mod { | 896 | pub mod test_mod { |
@@ -906,7 +914,7 @@ fn main() { | |||
906 | //- /main.rs crate:main deps:dep | 914 | //- /main.rs crate:main deps:dep |
907 | fn main() { | 915 | fn main() { |
908 | let test_struct = dep::test_mod::TestStruct {}; | 916 | let test_struct = dep::test_mod::TestStruct {}; |
909 | test_struct.test_meth<|>od() | 917 | test_struct.test_meth$0od() |
910 | } | 918 | } |
911 | //- /dep.rs crate:dep | 919 | //- /dep.rs crate:dep |
912 | pub mod test_mod { | 920 | pub mod test_mod { |
@@ -949,7 +957,7 @@ fn main() { | |||
949 | use test_mod::TestTrait2; | 957 | use test_mod::TestTrait2; |
950 | fn main() { | 958 | fn main() { |
951 | let one = test_mod::TestEnum::One; | 959 | let one = test_mod::TestEnum::One; |
952 | one.test<|>_method(); | 960 | one.test$0_method(); |
953 | } | 961 | } |
954 | ", | 962 | ", |
955 | ) | 963 | ) |
@@ -965,7 +973,7 @@ pub struct Struct; | |||
965 | 973 | ||
966 | //- /main.rs crate:main deps:dep | 974 | //- /main.rs crate:main deps:dep |
967 | fn main() { | 975 | fn main() { |
968 | Struct<|> | 976 | Struct$0 |
969 | } | 977 | } |
970 | ", | 978 | ", |
971 | r" | 979 | r" |
@@ -992,7 +1000,7 @@ pub fn panic_fmt() {} | |||
992 | //- /main.rs crate:main deps:dep | 1000 | //- /main.rs crate:main deps:dep |
993 | struct S; | 1001 | struct S; |
994 | 1002 | ||
995 | impl f<|>mt::Display for S {} | 1003 | impl f$0mt::Display for S {} |
996 | ", | 1004 | ", |
997 | r" | 1005 | r" |
998 | struct S; | 1006 | struct S; |
@@ -1019,7 +1027,7 @@ mac!(); | |||
1019 | 1027 | ||
1020 | //- /main.rs crate:main deps:dep | 1028 | //- /main.rs crate:main deps:dep |
1021 | fn main() { | 1029 | fn main() { |
1022 | Cheese<|>; | 1030 | Cheese$0; |
1023 | } | 1031 | } |
1024 | ", | 1032 | ", |
1025 | r" | 1033 | r" |
@@ -1042,7 +1050,7 @@ pub struct fmt; | |||
1042 | 1050 | ||
1043 | //- /main.rs crate:main deps:dep | 1051 | //- /main.rs crate:main deps:dep |
1044 | fn main() { | 1052 | fn main() { |
1045 | FMT<|>; | 1053 | FMT$0; |
1046 | } | 1054 | } |
1047 | ", | 1055 | ", |
1048 | r" | 1056 | r" |
@@ -1062,7 +1070,7 @@ fn main() { | |||
1062 | pub mod generic { pub struct Thing<'a, T>(&'a T); } | 1070 | pub mod generic { pub struct Thing<'a, T>(&'a T); } |
1063 | 1071 | ||
1064 | //- /main.rs crate:main deps:dep | 1072 | //- /main.rs crate:main deps:dep |
1065 | fn foo() -> Thin<|>g<'static, ()> {} | 1073 | fn foo() -> Thin$0g<'static, ()> {} |
1066 | 1074 | ||
1067 | fn main() {} | 1075 | fn main() {} |
1068 | ", | 1076 | ", |
@@ -1083,7 +1091,7 @@ fn main() {} | |||
1083 | pub mod generic { pub struct Thing<'a, T>(&'a T); } | 1091 | pub mod generic { pub struct Thing<'a, T>(&'a T); } |
1084 | 1092 | ||
1085 | //- /main.rs crate:main deps:dep | 1093 | //- /main.rs crate:main deps:dep |
1086 | fn foo() -> Thin<|>g::<'static, ()> {} | 1094 | fn foo() -> Thin$0g::<'static, ()> {} |
1087 | 1095 | ||
1088 | fn main() {} | 1096 | fn main() {} |
1089 | ", | 1097 | ", |
@@ -1108,7 +1116,7 @@ fn main() {} | |||
1108 | } | 1116 | } |
1109 | 1117 | ||
1110 | fn main() { | 1118 | fn main() { |
1111 | TestStruct::<()>::TEST_CONST<|> | 1119 | TestStruct::<()>::TEST_CONST$0 |
1112 | } | 1120 | } |
1113 | ", | 1121 | ", |
1114 | r" | 1122 | r" |
@@ -1142,7 +1150,7 @@ fn main() {} | |||
1142 | } | 1150 | } |
1143 | 1151 | ||
1144 | fn main() { | 1152 | fn main() { |
1145 | test_mod::TestStruct::<()>::TEST_CONST<|> | 1153 | test_mod::TestStruct::<()>::TEST_CONST$0 |
1146 | } | 1154 | } |
1147 | ", | 1155 | ", |
1148 | r" | 1156 | r" |
@@ -1180,7 +1188,7 @@ fn main() {} | |||
1180 | 1188 | ||
1181 | fn main() { | 1189 | fn main() { |
1182 | let test_struct = test_mod::TestStruct {}; | 1190 | let test_struct = test_mod::TestStruct {}; |
1183 | test_struct.test_meth<|>od::<()>() | 1191 | test_struct.test_meth$0od::<()>() |
1184 | } | 1192 | } |
1185 | ", | 1193 | ", |
1186 | r" | 1194 | r" |