1
1
use super :: * ;
2
2
3
3
use hir:: PrefixKind ;
4
- use test_utils:: assert_eq_text;
4
+ use test_utils:: { assert_eq_text, extract_range_or_offset, CURSOR_MARKER } ;
5
+
6
+ #[ test]
7
+ fn respects_cfg_attr_fn ( ) {
8
+ check (
9
+ r"bar::Bar" ,
10
+ r#"
11
+ #[cfg(test)]
12
+ fn foo() {$0}
13
+ "# ,
14
+ r#"
15
+ #[cfg(test)]
16
+ fn foo() {
17
+ use bar::Bar;
18
+ }
19
+ "# ,
20
+ ImportGranularity :: Crate ,
21
+ ) ;
22
+ }
23
+
24
+ #[ test]
25
+ fn respects_cfg_attr_const ( ) {
26
+ check (
27
+ r"bar::Bar" ,
28
+ r#"
29
+ #[cfg(test)]
30
+ const FOO: Bar = {$0};
31
+ "# ,
32
+ r#"
33
+ #[cfg(test)]
34
+ const FOO: Bar = {
35
+ use bar::Bar;
36
+ };
37
+ "# ,
38
+ ImportGranularity :: Crate ,
39
+ ) ;
40
+ }
5
41
6
42
#[ test]
7
43
fn insert_skips_lone_glob_imports ( ) {
@@ -15,15 +51,13 @@ use foo::bar::*;
15
51
use foo::baz::A;
16
52
" ,
17
53
ImportGranularity :: Crate ,
18
- false ,
19
- false ,
20
54
) ;
21
55
}
22
56
23
57
#[ test]
24
58
fn insert_not_group ( ) {
25
59
cov_mark:: check!( insert_no_grouping_last) ;
26
- check (
60
+ check_with_config (
27
61
"use external_crate2::bar::A" ,
28
62
r"
29
63
use std::bar::B;
@@ -38,24 +72,32 @@ use crate::bar::A;
38
72
use self::bar::A;
39
73
use super::bar::A;
40
74
use external_crate2::bar::A;" ,
41
- ImportGranularity :: Item ,
42
- false ,
43
- false ,
75
+ & InsertUseConfig {
76
+ granularity : ImportGranularity :: Item ,
77
+ enforce_granularity : true ,
78
+ prefix_kind : PrefixKind :: Plain ,
79
+ group : false ,
80
+ skip_glob_imports : true ,
81
+ } ,
44
82
) ;
45
83
}
46
84
47
85
#[ test]
48
86
fn insert_not_group_empty ( ) {
49
87
cov_mark:: check!( insert_no_grouping_last2) ;
50
- check (
88
+ check_with_config (
51
89
"use external_crate2::bar::A" ,
52
90
r"" ,
53
91
r"use external_crate2::bar::A;
54
92
55
93
" ,
56
- ImportGranularity :: Item ,
57
- false ,
58
- false ,
94
+ & InsertUseConfig {
95
+ granularity : ImportGranularity :: Item ,
96
+ enforce_granularity : true ,
97
+ prefix_kind : PrefixKind :: Plain ,
98
+ group : false ,
99
+ skip_glob_imports : true ,
100
+ } ,
59
101
) ;
60
102
}
61
103
@@ -294,13 +336,15 @@ fn insert_empty_module() {
294
336
cov_mark:: check!( insert_group_empty_module) ;
295
337
check (
296
338
"foo::bar" ,
297
- "mod x {}" ,
298
- r"{
339
+ r"
340
+ mod x {$0}
341
+ " ,
342
+ r"
343
+ mod x {
299
344
use foo::bar;
300
- }" ,
345
+ }
346
+ " ,
301
347
ImportGranularity :: Item ,
302
- true ,
303
- true ,
304
348
)
305
349
}
306
350
@@ -555,7 +599,6 @@ fn merge_mod_into_glob() {
555
599
"token::TokenKind" ,
556
600
r"use token::TokenKind::*;" ,
557
601
r"use token::TokenKind::{*, self};" ,
558
- false ,
559
602
& InsertUseConfig {
560
603
granularity : ImportGranularity :: Crate ,
561
604
enforce_granularity : true ,
@@ -573,7 +616,6 @@ fn merge_self_glob() {
573
616
"self" ,
574
617
r"use self::*;" ,
575
618
r"use self::{*, self};" ,
576
- false ,
577
619
& InsertUseConfig {
578
620
granularity : ImportGranularity :: Crate ,
579
621
enforce_granularity : true ,
@@ -798,14 +840,20 @@ fn check_with_config(
798
840
path : & str ,
799
841
ra_fixture_before : & str ,
800
842
ra_fixture_after : & str ,
801
- module : bool ,
802
843
config : & InsertUseConfig ,
803
844
) {
804
- let mut syntax = ast:: SourceFile :: parse ( ra_fixture_before) . tree ( ) . syntax ( ) . clone ( ) ;
805
- if module {
806
- syntax = syntax. descendants ( ) . find_map ( ast:: Module :: cast) . unwrap ( ) . syntax ( ) . clone ( ) ;
807
- }
808
- let file = super :: ImportScope :: from ( syntax. clone_for_update ( ) ) . unwrap ( ) ;
845
+ let ( text, pos) = if ra_fixture_before. contains ( CURSOR_MARKER ) {
846
+ let ( range_or_offset, text) = extract_range_or_offset ( ra_fixture_before) ;
847
+ ( text, Some ( range_or_offset) )
848
+ } else {
849
+ ( ra_fixture_before. to_owned ( ) , None )
850
+ } ;
851
+ let syntax = ast:: SourceFile :: parse ( & text) . tree ( ) . syntax ( ) . clone_for_update ( ) ;
852
+ let file = pos
853
+ . and_then ( |pos| syntax. token_at_offset ( pos. expect_offset ( ) ) . next ( ) ?. parent ( ) )
854
+ . and_then ( |it| super :: ImportScope :: find_insert_use_container ( & it) )
855
+ . or_else ( || super :: ImportScope :: from ( syntax) )
856
+ . unwrap ( ) ;
809
857
let path = ast:: SourceFile :: parse ( & format ! ( "use {};" , path) )
810
858
. tree ( )
811
859
. syntax ( )
@@ -814,7 +862,7 @@ fn check_with_config(
814
862
. unwrap ( ) ;
815
863
816
864
insert_use ( & file, path, config) ;
817
- let result = file. as_syntax_node ( ) . to_string ( ) ;
865
+ let result = file. as_syntax_node ( ) . ancestors ( ) . last ( ) . unwrap ( ) . to_string ( ) ;
818
866
assert_eq_text ! ( ra_fixture_after, & result) ;
819
867
}
820
868
@@ -823,34 +871,31 @@ fn check(
823
871
ra_fixture_before : & str ,
824
872
ra_fixture_after : & str ,
825
873
granularity : ImportGranularity ,
826
- module : bool ,
827
- group : bool ,
828
874
) {
829
875
check_with_config (
830
876
path,
831
877
ra_fixture_before,
832
878
ra_fixture_after,
833
- module,
834
879
& InsertUseConfig {
835
880
granularity,
836
881
enforce_granularity : true ,
837
882
prefix_kind : PrefixKind :: Plain ,
838
- group,
883
+ group : true ,
839
884
skip_glob_imports : true ,
840
885
} ,
841
886
)
842
887
}
843
888
844
889
fn check_crate ( path : & str , ra_fixture_before : & str , ra_fixture_after : & str ) {
845
- check ( path, ra_fixture_before, ra_fixture_after, ImportGranularity :: Crate , false , true )
890
+ check ( path, ra_fixture_before, ra_fixture_after, ImportGranularity :: Crate )
846
891
}
847
892
848
893
fn check_module ( path : & str , ra_fixture_before : & str , ra_fixture_after : & str ) {
849
- check ( path, ra_fixture_before, ra_fixture_after, ImportGranularity :: Module , false , true )
894
+ check ( path, ra_fixture_before, ra_fixture_after, ImportGranularity :: Module )
850
895
}
851
896
852
897
fn check_none ( path : & str , ra_fixture_before : & str , ra_fixture_after : & str ) {
853
- check ( path, ra_fixture_before, ra_fixture_after, ImportGranularity :: Item , false , true )
898
+ check ( path, ra_fixture_before, ra_fixture_after, ImportGranularity :: Item )
854
899
}
855
900
856
901
fn check_merge_only_fail ( ra_fixture0 : & str , ra_fixture1 : & str , mb : MergeBehavior ) {
0 commit comments