@@ -9,13 +9,12 @@ use syntax::{
9
9
Direction , InsertPosition , SyntaxElement , SyntaxNode , T ,
10
10
} ;
11
11
12
- use crate :: assist_context:: AssistContext ;
13
12
use test_utils:: mark;
14
13
15
14
/// Determines the containing syntax node in which to insert a `use` statement affecting `position`.
16
15
pub ( crate ) fn find_insert_use_container (
17
16
position : & SyntaxNode ,
18
- ctx : & AssistContext ,
17
+ ctx : & crate :: assist_context :: AssistContext ,
19
18
) -> Option < Either < ast:: ItemList , ast:: SourceFile > > {
20
19
ctx. sema . ancestors_with_macros ( position. clone ( ) ) . find_map ( |n| {
21
20
if let Some ( module) = ast:: Module :: cast ( n. clone ( ) ) {
@@ -25,19 +24,9 @@ pub(crate) fn find_insert_use_container(
25
24
} )
26
25
}
27
26
28
- pub ( crate ) fn insert_use_statement (
29
- // Ideally the position of the cursor, used to
30
- position : & SyntaxNode ,
31
- path_to_import : & str ,
32
- ctx : & crate :: assist_context:: AssistContext ,
33
- builder : & mut text_edit:: TextEditBuilder ,
34
- ) {
35
- insert_use ( position. clone ( ) , make:: path_from_text ( path_to_import) , Some ( MergeBehaviour :: Full ) ) ;
36
- }
37
-
38
27
/// Insert an import path into the given file/node. A `merge` value of none indicates that no import merging is allowed to occur.
39
28
pub fn insert_use (
40
- where_ : SyntaxNode ,
29
+ where_ : & SyntaxNode ,
41
30
path : ast:: Path ,
42
31
merge : Option < MergeBehaviour > ,
43
32
) -> SyntaxNode {
@@ -49,42 +38,36 @@ pub fn insert_use(
49
38
let to_delete: SyntaxElement = existing_use. syntax ( ) . clone ( ) . into ( ) ;
50
39
let to_delete = to_delete. clone ( ) ..=to_delete;
51
40
let to_insert = iter:: once ( merged. syntax ( ) . clone ( ) . into ( ) ) ;
52
- return algo:: replace_children ( & where_, to_delete, to_insert) ;
41
+ return algo:: replace_children ( where_, to_delete, to_insert) ;
53
42
}
54
43
}
55
44
}
56
45
57
46
// either we weren't allowed to merge or there is no import that fits the merge conditions
58
47
// so look for the place we have to insert to
59
- let ( insert_position, add_blank) = find_insert_position ( & where_, path) ;
48
+ let ( insert_position, add_blank) = find_insert_position ( where_, path) ;
60
49
61
50
let to_insert: Vec < SyntaxElement > = {
62
51
let mut buf = Vec :: new ( ) ;
63
52
64
53
match add_blank {
65
54
AddBlankLine :: Before => buf. push ( make:: tokens:: single_newline ( ) . into ( ) ) ,
66
- AddBlankLine :: BeforeTwice => {
67
- buf. push ( make:: tokens:: single_newline ( ) . into ( ) ) ;
68
- buf. push ( make:: tokens:: single_newline ( ) . into ( ) ) ;
69
- }
55
+ AddBlankLine :: BeforeTwice => buf. push ( make:: tokens:: blank_line ( ) . into ( ) ) ,
70
56
_ => ( ) ,
71
57
}
72
58
73
59
buf. push ( use_item. syntax ( ) . clone ( ) . into ( ) ) ;
74
60
75
61
match add_blank {
76
62
AddBlankLine :: After => buf. push ( make:: tokens:: single_newline ( ) . into ( ) ) ,
77
- AddBlankLine :: AfterTwice => {
78
- buf. push ( make:: tokens:: single_newline ( ) . into ( ) ) ;
79
- buf. push ( make:: tokens:: single_newline ( ) . into ( ) ) ;
80
- }
63
+ AddBlankLine :: AfterTwice => buf. push ( make:: tokens:: blank_line ( ) . into ( ) ) ,
81
64
_ => ( ) ,
82
65
}
83
66
84
67
buf
85
68
} ;
86
69
87
- algo:: insert_children ( & where_, insert_position, to_insert)
70
+ algo:: insert_children ( where_, insert_position, to_insert)
88
71
}
89
72
90
73
fn try_merge_imports (
@@ -613,7 +596,7 @@ use foo::bar;",
613
596
. find_map ( ast:: Path :: cast)
614
597
. unwrap ( ) ;
615
598
616
- let result = insert_use ( file, path, mb) . to_string ( ) ;
599
+ let result = insert_use ( & file, path, mb) . to_string ( ) ;
617
600
assert_eq_text ! ( & result, ra_fixture_after) ;
618
601
}
619
602
0 commit comments