@@ -12,10 +12,10 @@ use crate::{
12
12
AssistId ,
13
13
} ;
14
14
15
- /// This function produces sequence of text edits into edit
16
- /// to import the target path in the most appropriate scope given
17
- /// the cursor position
18
- pub fn auto_import_text_edit (
15
+ /// Creates and inserts a use statement for the given path to import.
16
+ /// The use statement is inserted in the scope most appropriate to the
17
+ /// the cursor position given, additionally merged with the existing use imports.
18
+ pub fn insert_use_statement (
19
19
// Ideally the position of the cursor, used to
20
20
position : & SyntaxNode ,
21
21
// The statement to use as anchor (last resort)
@@ -37,9 +37,9 @@ pub fn auto_import_text_edit(
37
37
}
38
38
}
39
39
40
- // Assist: add_import
40
+ // Assist: replace_qualified_name_with_use
41
41
//
42
- // Adds a use statement for a given fully-qualified path .
42
+ // Adds a use statement for a given fully-qualified name .
43
43
//
44
44
// ```
45
45
// fn process(map: std::collections::<|>HashMap<String, String>) {}
@@ -50,7 +50,7 @@ pub fn auto_import_text_edit(
50
50
//
51
51
// fn process(map: HashMap<String, String>) {}
52
52
// ```
53
- pub ( crate ) fn add_import ( ctx : AssistCtx ) -> Option < Assist > {
53
+ pub ( crate ) fn replace_qualified_name_with_use ( ctx : AssistCtx ) -> Option < Assist > {
54
54
let path: ast:: Path = ctx. find_node_at_offset ( ) ?;
55
55
// We don't want to mess with use statements
56
56
if path. syntax ( ) . ancestors ( ) . find_map ( ast:: UseItem :: cast) . is_some ( ) {
@@ -72,9 +72,13 @@ pub(crate) fn add_import(ctx: AssistCtx) -> Option<Assist> {
72
72
}
73
73
} ;
74
74
75
- ctx. add_assist ( AssistId ( "add_import" ) , format ! ( "Import {}" , fmt_segments( & segments) ) , |edit| {
76
- apply_auto_import ( & position, & path, & segments, edit. text_edit_builder ( ) ) ;
77
- } )
75
+ ctx. add_assist (
76
+ AssistId ( "replace_qualified_name_with_use" ) ,
77
+ "Replace qualified path with use" ,
78
+ |edit| {
79
+ replace_with_use ( & position, & path, & segments, edit. text_edit_builder ( ) ) ;
80
+ } ,
81
+ )
78
82
}
79
83
80
84
fn collect_path_segments_raw (
@@ -107,12 +111,6 @@ fn collect_path_segments_raw(
107
111
Some ( segments. len ( ) - oldlen)
108
112
}
109
113
110
- fn fmt_segments ( segments : & [ SmolStr ] ) -> String {
111
- let mut buf = String :: new ( ) ;
112
- fmt_segments_raw ( segments, & mut buf) ;
113
- buf
114
- }
115
-
116
114
fn fmt_segments_raw ( segments : & [ SmolStr ] , buf : & mut String ) {
117
115
let mut iter = segments. iter ( ) ;
118
116
if let Some ( s) = iter. next ( ) {
@@ -558,7 +556,7 @@ fn make_assist_add_nested_import(
558
556
}
559
557
}
560
558
561
- fn apply_auto_import (
559
+ fn replace_with_use (
562
560
container : & SyntaxNode ,
563
561
path : & ast:: Path ,
564
562
target : & [ SmolStr ] ,
@@ -567,7 +565,7 @@ fn apply_auto_import(
567
565
let action = best_action_for_target ( container. clone ( ) , path. syntax ( ) . clone ( ) , target) ;
568
566
make_assist ( & action, target, edit) ;
569
567
if let Some ( last) = path. segment ( ) {
570
- // Here we are assuming the assist will provide a correct use statement
568
+ // Here we are assuming the assist will provide a correct use statement
571
569
// so we can delete the path qualifier
572
570
edit. delete ( TextRange :: from_to (
573
571
path. syntax ( ) . text_range ( ) . start ( ) ,
@@ -603,9 +601,9 @@ mod tests {
603
601
use super :: * ;
604
602
605
603
#[ test]
606
- fn test_auto_import_add_use_no_anchor ( ) {
604
+ fn test_replace_add_use_no_anchor ( ) {
607
605
check_assist (
608
- add_import ,
606
+ replace_qualified_name_with_use ,
609
607
"
610
608
std::fmt::Debug<|>
611
609
" ,
@@ -617,9 +615,9 @@ Debug<|>
617
615
) ;
618
616
}
619
617
#[ test]
620
- fn test_auto_import_add_use_no_anchor_with_item_below ( ) {
618
+ fn test_replace_add_use_no_anchor_with_item_below ( ) {
621
619
check_assist (
622
- add_import ,
620
+ replace_qualified_name_with_use ,
623
621
"
624
622
std::fmt::Debug<|>
625
623
@@ -638,9 +636,9 @@ fn main() {
638
636
}
639
637
640
638
#[ test]
641
- fn test_auto_import_add_use_no_anchor_with_item_above ( ) {
639
+ fn test_replace_add_use_no_anchor_with_item_above ( ) {
642
640
check_assist (
643
- add_import ,
641
+ replace_qualified_name_with_use ,
644
642
"
645
643
fn main() {
646
644
}
@@ -659,9 +657,9 @@ Debug<|>
659
657
}
660
658
661
659
#[ test]
662
- fn test_auto_import_add_use_no_anchor_2seg ( ) {
660
+ fn test_replace_add_use_no_anchor_2seg ( ) {
663
661
check_assist (
664
- add_import ,
662
+ replace_qualified_name_with_use ,
665
663
"
666
664
std::fmt<|>::Debug
667
665
" ,
@@ -674,9 +672,9 @@ fmt<|>::Debug
674
672
}
675
673
676
674
#[ test]
677
- fn test_auto_import_add_use ( ) {
675
+ fn test_replace_add_use ( ) {
678
676
check_assist (
679
- add_import ,
677
+ replace_qualified_name_with_use ,
680
678
"
681
679
use stdx;
682
680
@@ -694,9 +692,9 @@ impl Debug<|> for Foo {
694
692
}
695
693
696
694
#[ test]
697
- fn test_auto_import_file_use_other_anchor ( ) {
695
+ fn test_replace_file_use_other_anchor ( ) {
698
696
check_assist (
699
- add_import ,
697
+ replace_qualified_name_with_use ,
700
698
"
701
699
impl std::fmt::Debug<|> for Foo {
702
700
}
@@ -711,9 +709,9 @@ impl Debug<|> for Foo {
711
709
}
712
710
713
711
#[ test]
714
- fn test_auto_import_add_use_other_anchor_indent ( ) {
712
+ fn test_replace_add_use_other_anchor_indent ( ) {
715
713
check_assist (
716
- add_import ,
714
+ replace_qualified_name_with_use ,
717
715
"
718
716
impl std::fmt::Debug<|> for Foo {
719
717
}
@@ -728,9 +726,9 @@ impl Debug<|> for Foo {
728
726
}
729
727
730
728
#[ test]
731
- fn test_auto_import_split_different ( ) {
729
+ fn test_replace_split_different ( ) {
732
730
check_assist (
733
- add_import ,
731
+ replace_qualified_name_with_use ,
734
732
"
735
733
use std::fmt;
736
734
@@ -747,9 +745,9 @@ impl io<|> for Foo {
747
745
}
748
746
749
747
#[ test]
750
- fn test_auto_import_split_self_for_use ( ) {
748
+ fn test_replace_split_self_for_use ( ) {
751
749
check_assist (
752
- add_import ,
750
+ replace_qualified_name_with_use ,
753
751
"
754
752
use std::fmt;
755
753
@@ -766,9 +764,9 @@ impl Debug<|> for Foo {
766
764
}
767
765
768
766
#[ test]
769
- fn test_auto_import_split_self_for_target ( ) {
767
+ fn test_replace_split_self_for_target ( ) {
770
768
check_assist (
771
- add_import ,
769
+ replace_qualified_name_with_use ,
772
770
"
773
771
use std::fmt::Debug;
774
772
@@ -785,9 +783,9 @@ impl fmt<|> for Foo {
785
783
}
786
784
787
785
#[ test]
788
- fn test_auto_import_add_to_nested_self_nested ( ) {
786
+ fn test_replace_add_to_nested_self_nested ( ) {
789
787
check_assist (
790
- add_import ,
788
+ replace_qualified_name_with_use ,
791
789
"
792
790
use std::fmt::{Debug, nested::{Display}};
793
791
@@ -804,9 +802,9 @@ impl nested<|> for Foo {
804
802
}
805
803
806
804
#[ test]
807
- fn test_auto_import_add_to_nested_self_already_included ( ) {
805
+ fn test_replace_add_to_nested_self_already_included ( ) {
808
806
check_assist (
809
- add_import ,
807
+ replace_qualified_name_with_use ,
810
808
"
811
809
use std::fmt::{Debug, nested::{self, Display}};
812
810
@@ -823,9 +821,9 @@ impl nested<|> for Foo {
823
821
}
824
822
825
823
#[ test]
826
- fn test_auto_import_add_to_nested_nested ( ) {
824
+ fn test_replace_add_to_nested_nested ( ) {
827
825
check_assist (
828
- add_import ,
826
+ replace_qualified_name_with_use ,
829
827
"
830
828
use std::fmt::{Debug, nested::{Display}};
831
829
@@ -842,9 +840,9 @@ impl Debug<|> for Foo {
842
840
}
843
841
844
842
#[ test]
845
- fn test_auto_import_split_common_target_longer ( ) {
843
+ fn test_replace_split_common_target_longer ( ) {
846
844
check_assist (
847
- add_import ,
845
+ replace_qualified_name_with_use ,
848
846
"
849
847
use std::fmt::Debug;
850
848
@@ -861,9 +859,9 @@ impl Display<|> for Foo {
861
859
}
862
860
863
861
#[ test]
864
- fn test_auto_import_split_common_use_longer ( ) {
862
+ fn test_replace_split_common_use_longer ( ) {
865
863
check_assist (
866
- add_import ,
864
+ replace_qualified_name_with_use ,
867
865
"
868
866
use std::fmt::nested::Debug;
869
867
@@ -880,9 +878,9 @@ impl Display<|> for Foo {
880
878
}
881
879
882
880
#[ test]
883
- fn test_auto_import_use_nested_import ( ) {
881
+ fn test_replace_use_nested_import ( ) {
884
882
check_assist (
885
- add_import ,
883
+ replace_qualified_name_with_use ,
886
884
"
887
885
use crate::{
888
886
ty::{Substs, Ty},
@@ -903,9 +901,9 @@ fn foo() { lower<|>::trait_env() }
903
901
}
904
902
905
903
#[ test]
906
- fn test_auto_import_alias ( ) {
904
+ fn test_replace_alias ( ) {
907
905
check_assist (
908
- add_import ,
906
+ replace_qualified_name_with_use ,
909
907
"
910
908
use std::fmt as foo;
911
909
@@ -922,9 +920,9 @@ impl Debug<|> for Foo {
922
920
}
923
921
924
922
#[ test]
925
- fn test_auto_import_not_applicable_one_segment ( ) {
923
+ fn test_replace_not_applicable_one_segment ( ) {
926
924
check_assist_not_applicable (
927
- add_import ,
925
+ replace_qualified_name_with_use ,
928
926
"
929
927
impl foo<|> for Foo {
930
928
}
@@ -933,19 +931,19 @@ impl foo<|> for Foo {
933
931
}
934
932
935
933
#[ test]
936
- fn test_auto_import_not_applicable_in_use ( ) {
934
+ fn test_replace_not_applicable_in_use ( ) {
937
935
check_assist_not_applicable (
938
- add_import ,
936
+ replace_qualified_name_with_use ,
939
937
"
940
938
use std::fmt<|>;
941
939
" ,
942
940
) ;
943
941
}
944
942
945
943
#[ test]
946
- fn test_auto_import_add_use_no_anchor_in_mod_mod ( ) {
944
+ fn test_replace_add_use_no_anchor_in_mod_mod ( ) {
947
945
check_assist (
948
- add_import ,
946
+ replace_qualified_name_with_use ,
949
947
"
950
948
mod foo {
951
949
mod bar {
0 commit comments