1
1
use syntax:: {
2
- ast:: { self , AstNode } ,
2
+ ast:: { self , edit :: IndentLevel , AstNode } ,
3
3
match_ast,
4
4
} ;
5
5
@@ -31,10 +31,12 @@ pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext) -> Opti
31
31
let item = ctx. find_node_at_offset :: < ast:: Item > ( ) ?;
32
32
let insert = match_ast ! {
33
33
match ( item. syntax( ) . parent( ) ?) {
34
- ast:: AssocItemList ( it) => it. syntax( ) . parent( ) ?. text_range ( ) . start ( ) ,
35
- _ => item. syntax( ) . text_range ( ) . start ( ) ,
34
+ ast:: AssocItemList ( it) => it. syntax( ) . parent( ) ?. clone ( ) ,
35
+ _ => item. syntax( ) . clone ( ) ,
36
36
}
37
37
} ;
38
+ let indent = IndentLevel :: from_node ( & insert) ;
39
+ let insert = insert. text_range ( ) . start ( ) ;
38
40
let target = node. syntax ( ) . text_range ( ) ;
39
41
40
42
acc. add (
@@ -46,10 +48,14 @@ pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext) -> Opti
46
48
builder. replace ( target, "Type" ) ;
47
49
match ctx. config . snippet_cap {
48
50
Some ( cap) => {
49
- 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
+ ) ;
50
56
}
51
57
None => {
52
- builder. insert ( insert, format ! ( "type Type = {};\n \n " , node) ) ;
58
+ builder. insert ( insert, format ! ( "type Type = {};\n \n {} " , node, indent ) ) ;
53
59
}
54
60
}
55
61
} ,
@@ -191,4 +197,23 @@ trait Tr {
191
197
"# ,
192
198
) ;
193
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
+
214
+ fn f() -> Type {}
215
+ }
216
+ "# ,
217
+ ) ;
218
+ }
194
219
}
0 commit comments