1
- use syntax:: ast:: { self , AstNode } ;
1
+ use syntax:: {
2
+ ast:: { self , AstNode } ,
3
+ match_ast,
4
+ } ;
2
5
3
6
use crate :: { AssistContext , AssistId , AssistKind , Assists } ;
4
7
@@ -25,12 +28,13 @@ 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( ) ?. text_range( ) . start( ) ,
35
+ _ => item. syntax( ) . text_range( ) . start( ) ,
36
+ }
37
+ } ;
34
38
let target = node. syntax ( ) . text_range ( ) ;
35
39
36
40
acc. add (
@@ -153,9 +157,9 @@ struct S {
153
157
}
154
158
155
159
#[ 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.
160
+ fn extract_from_impl_or_trait ( ) {
161
+ // When invoked in an impl/trait , extracted type alias should be placed next to the
162
+ // impl/trait, not inside.
159
163
check_assist (
160
164
extract_type_alias,
161
165
r#"
@@ -168,6 +172,21 @@ type $0Type = (u8, u8);
168
172
169
173
impl S {
170
174
fn f() -> Type {}
175
+ }
176
+ "# ,
177
+ ) ;
178
+ check_assist (
179
+ extract_type_alias,
180
+ r#"
181
+ trait Tr {
182
+ fn f() -> $0(u8, u8)$0 {}
183
+ }
184
+ "# ,
185
+ r#"
186
+ type $0Type = (u8, u8);
187
+
188
+ trait Tr {
189
+ fn f() -> Type {}
171
190
}
172
191
"# ,
173
192
) ;
0 commit comments