1
- use syntax:: ast:: { self , AstNode } ;
1
+ use syntax:: {
2
+ ast:: { self , edit:: IndentLevel , AstNode } ,
3
+ match_ast,
4
+ } ;
2
5
3
6
use crate :: { AssistContext , AssistId , AssistKind , Assists } ;
4
7
@@ -25,12 +28,15 @@ pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext) -> Opti
25
28
}
26
29
27
30
let node = ctx. find_node_at_range :: < ast:: Type > ( ) ?;
28
- let insert = ctx
29
- . find_node_at_offset :: < ast:: Impl > ( )
30
- . map ( |imp| imp. syntax ( ) . clone ( ) )
31
- . or_else ( || ctx. find_node_at_offset :: < ast:: Item > ( ) . map ( |item| item. syntax ( ) . clone ( ) ) ) ?
32
- . text_range ( )
33
- . start ( ) ;
31
+ let item = ctx. find_node_at_offset :: < ast:: Item > ( ) ?;
32
+ let insert = match_ast ! {
33
+ match ( item. syntax( ) . parent( ) ?) {
34
+ ast:: AssocItemList ( it) => it. syntax( ) . parent( ) ?. clone( ) ,
35
+ _ => item. syntax( ) . clone( ) ,
36
+ }
37
+ } ;
38
+ let indent = IndentLevel :: from_node ( & insert) ;
39
+ let insert = insert. text_range ( ) . start ( ) ;
34
40
let target = node. syntax ( ) . text_range ( ) ;
35
41
36
42
acc. add (
@@ -42,10 +48,14 @@ pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext) -> Opti
42
48
builder. replace ( target, "Type" ) ;
43
49
match ctx. config . snippet_cap {
44
50
Some ( cap) => {
45
- builder. insert_snippet ( cap, insert, format ! ( "type $0Type = {};\n \n " , node) ) ;
51
+ builder. insert_snippet (
52
+ cap,
53
+ insert,
54
+ format ! ( "type $0Type = {};\n \n {}" , node, indent) ,
55
+ ) ;
46
56
}
47
57
None => {
48
- builder. insert ( insert, format ! ( "type Type = {};\n \n " , node) ) ;
58
+ builder. insert ( insert, format ! ( "type Type = {};\n \n {} " , node, indent ) ) ;
49
59
}
50
60
}
51
61
} ,
@@ -153,9 +163,9 @@ struct S {
153
163
}
154
164
155
165
#[ test]
156
- fn extract_from_impl ( ) {
157
- // When invoked in an impl, extracted type alias should be placed next to the impl, not
158
- // inside.
166
+ fn extract_from_impl_or_trait ( ) {
167
+ // When invoked in an impl/trait , extracted type alias should be placed next to the
168
+ // impl/trait, not inside.
159
169
check_assist (
160
170
extract_type_alias,
161
171
r#"
@@ -167,6 +177,40 @@ impl S {
167
177
type $0Type = (u8, u8);
168
178
169
179
impl S {
180
+ fn f() -> Type {}
181
+ }
182
+ "# ,
183
+ ) ;
184
+ check_assist (
185
+ extract_type_alias,
186
+ r#"
187
+ trait Tr {
188
+ fn f() -> $0(u8, u8)$0 {}
189
+ }
190
+ "# ,
191
+ r#"
192
+ type $0Type = (u8, u8);
193
+
194
+ trait Tr {
195
+ fn f() -> Type {}
196
+ }
197
+ "# ,
198
+ ) ;
199
+ }
200
+
201
+ #[ test]
202
+ fn indentation ( ) {
203
+ check_assist (
204
+ extract_type_alias,
205
+ r#"
206
+ mod m {
207
+ fn f() -> $0u8$0 {}
208
+ }
209
+ "# ,
210
+ r#"
211
+ mod m {
212
+ type $0Type = u8;
213
+
170
214
fn f() -> Type {}
171
215
}
172
216
"# ,
0 commit comments