@@ -6,46 +6,46 @@ use syntax::{
6
6
ted, AstNode ,
7
7
} ;
8
8
9
- use crate :: { utils :: get_methods , AssistContext , AssistId , AssistKind , Assists } ;
9
+ use crate :: { AssistContext , AssistId , AssistKind , Assists } ;
10
10
11
- // Assist: reorder_impl
11
+ // Assist: reorder_impl_items
12
12
//
13
- // Reorder the methods of an `impl Trait`. The methods will be ordered
13
+ // Reorder the items of an `impl Trait`. The items will be ordered
14
14
// in the same order as in the trait definition.
15
15
//
16
16
// ```
17
17
// trait Foo {
18
- // fn a() {}
19
- // fn b() {}
20
- // fn c() {}
18
+ // type A;
19
+ // const B: u8;
20
+ // fn c();
21
21
// }
22
22
//
23
23
// struct Bar;
24
24
// $0impl Foo for Bar {
25
- // fn b() {}
25
+ // const B: u8 = 17;
26
26
// fn c() {}
27
- // fn a() {}
27
+ // type A = String;
28
28
// }
29
29
// ```
30
30
// ->
31
31
// ```
32
32
// trait Foo {
33
- // fn a() {}
34
- // fn b() {}
35
- // fn c() {}
33
+ // type A;
34
+ // const B: u8;
35
+ // fn c();
36
36
// }
37
37
//
38
38
// struct Bar;
39
39
// impl Foo for Bar {
40
- // fn a() {}
41
- // fn b() {}
40
+ // type A = String;
41
+ // const B: u8 = 17;
42
42
// fn c() {}
43
43
// }
44
44
// ```
45
- pub ( crate ) fn reorder_impl ( acc : & mut Assists , ctx : & AssistContext ) -> Option < ( ) > {
45
+ pub ( crate ) fn reorder_impl_items ( acc : & mut Assists , ctx : & AssistContext ) -> Option < ( ) > {
46
46
let impl_ast = ctx. find_node_at_offset :: < ast:: Impl > ( ) ?;
47
47
let items = impl_ast. assoc_item_list ( ) ?;
48
- let methods = get_methods ( & items) ;
48
+ let assoc_items = items. assoc_items ( ) . collect :: < Vec < _ > > ( ) ;
49
49
50
50
let path = impl_ast
51
51
. trait_ ( )
@@ -55,48 +55,53 @@ pub(crate) fn reorder_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
55
55
} ) ?
56
56
. path ( ) ?;
57
57
58
- let ranks = compute_method_ranks ( & path, ctx) ?;
59
- let sorted: Vec < _ > = methods
58
+ let ranks = compute_item_ranks ( & path, ctx) ?;
59
+ let sorted: Vec < _ > = assoc_items
60
60
. iter ( )
61
61
. cloned ( )
62
- . sorted_by_key ( |f| {
63
- f. name ( ) . and_then ( |n| ranks. get ( & n. to_string ( ) ) . copied ( ) ) . unwrap_or ( usize:: max_value ( ) )
62
+ . sorted_by_key ( |i| {
63
+ let name = match i {
64
+ ast:: AssocItem :: Const ( c) => c. name ( ) ,
65
+ ast:: AssocItem :: Fn ( f) => f. name ( ) ,
66
+ ast:: AssocItem :: TypeAlias ( t) => t. name ( ) ,
67
+ ast:: AssocItem :: MacroCall ( _) => None ,
68
+ } ;
69
+
70
+ name. and_then ( |n| ranks. get ( & n. to_string ( ) ) . copied ( ) ) . unwrap_or ( usize:: max_value ( ) )
64
71
} )
65
72
. collect ( ) ;
66
73
67
74
// Don't edit already sorted methods:
68
- if methods == sorted {
75
+ if assoc_items == sorted {
69
76
cov_mark:: hit!( not_applicable_if_sorted) ;
70
77
return None ;
71
78
}
72
79
73
80
let target = items. syntax ( ) . text_range ( ) ;
74
81
acc. add (
75
- AssistId ( "reorder_impl " , AssistKind :: RefactorRewrite ) ,
76
- "Sort methods by trait definition" ,
82
+ AssistId ( "reorder_impl_items " , AssistKind :: RefactorRewrite ) ,
83
+ "Sort items by trait definition" ,
77
84
target,
78
85
|builder| {
79
- let methods = methods. into_iter ( ) . map ( |fn_| builder. make_mut ( fn_) ) . collect :: < Vec < _ > > ( ) ;
80
- methods
86
+ let assoc_items =
87
+ assoc_items. into_iter ( ) . map ( |item| builder. make_mut ( item) ) . collect :: < Vec < _ > > ( ) ;
88
+ assoc_items
81
89
. into_iter ( )
82
90
. zip ( sorted)
83
91
. for_each ( |( old, new) | ted:: replace ( old. syntax ( ) , new. clone_for_update ( ) . syntax ( ) ) ) ;
84
92
} ,
85
93
)
86
94
}
87
95
88
- fn compute_method_ranks ( path : & ast:: Path , ctx : & AssistContext ) -> Option < FxHashMap < String , usize > > {
96
+ fn compute_item_ranks ( path : & ast:: Path , ctx : & AssistContext ) -> Option < FxHashMap < String , usize > > {
89
97
let td = trait_definition ( path, & ctx. sema ) ?;
90
98
91
99
Some (
92
100
td. items ( ctx. db ( ) )
93
101
. iter ( )
94
- . flat_map ( |i| match i {
95
- hir:: AssocItem :: Function ( f) => Some ( f) ,
96
- _ => None ,
97
- } )
102
+ . flat_map ( |i| i. name ( ctx. db ( ) ) )
98
103
. enumerate ( )
99
- . map ( |( idx, func ) | ( func . name ( ctx . db ( ) ) . to_string ( ) , idx) )
104
+ . map ( |( idx, name ) | ( name. to_string ( ) , idx) )
100
105
. collect ( ) ,
101
106
)
102
107
}
@@ -118,15 +123,19 @@ mod tests {
118
123
fn not_applicable_if_sorted ( ) {
119
124
cov_mark:: check!( not_applicable_if_sorted) ;
120
125
check_assist_not_applicable (
121
- reorder_impl ,
126
+ reorder_impl_items ,
122
127
r#"
123
128
trait Bar {
129
+ type T;
130
+ const C: ();
124
131
fn a() {}
125
132
fn z() {}
126
133
fn b() {}
127
134
}
128
135
struct Foo;
129
136
$0impl Bar for Foo {
137
+ type T = ();
138
+ const C: () = ();
130
139
fn a() {}
131
140
fn z() {}
132
141
fn b() {}
@@ -135,10 +144,49 @@ $0impl Bar for Foo {
135
144
)
136
145
}
137
146
147
+ #[ test]
148
+ fn reorder_impl_trait_functions ( ) {
149
+ check_assist (
150
+ reorder_impl_items,
151
+ r#"
152
+ trait Bar {
153
+ fn a() {}
154
+ fn c() {}
155
+ fn b() {}
156
+ fn d() {}
157
+ }
158
+
159
+ struct Foo;
160
+ $0impl Bar for Foo {
161
+ fn d() {}
162
+ fn b() {}
163
+ fn c() {}
164
+ fn a() {}
165
+ }
166
+ "# ,
167
+ r#"
168
+ trait Bar {
169
+ fn a() {}
170
+ fn c() {}
171
+ fn b() {}
172
+ fn d() {}
173
+ }
174
+
175
+ struct Foo;
176
+ impl Bar for Foo {
177
+ fn a() {}
178
+ fn c() {}
179
+ fn b() {}
180
+ fn d() {}
181
+ }
182
+ "# ,
183
+ )
184
+ }
185
+
138
186
#[ test]
139
187
fn not_applicable_if_empty ( ) {
140
188
check_assist_not_applicable (
141
- reorder_impl ,
189
+ reorder_impl_items ,
142
190
r#"
143
191
trait Bar {};
144
192
struct Foo;
@@ -148,69 +196,85 @@ $0impl Bar for Foo {}
148
196
}
149
197
150
198
#[ test]
151
- fn reorder_impl_trait_functions ( ) {
199
+ fn reorder_impl_trait_items ( ) {
152
200
check_assist (
153
- reorder_impl ,
201
+ reorder_impl_items ,
154
202
r#"
155
203
trait Bar {
156
204
fn a() {}
205
+ type T0;
157
206
fn c() {}
207
+ const C1: ();
158
208
fn b() {}
209
+ type T1;
159
210
fn d() {}
211
+ const C0: ();
160
212
}
161
213
162
214
struct Foo;
163
215
$0impl Bar for Foo {
216
+ type T1 = ();
164
217
fn d() {}
165
218
fn b() {}
166
219
fn c() {}
220
+ const C1: () = ();
167
221
fn a() {}
222
+ type T0 = ();
223
+ const C0: () = ();
168
224
}
169
225
"# ,
170
226
r#"
171
227
trait Bar {
172
228
fn a() {}
229
+ type T0;
173
230
fn c() {}
231
+ const C1: ();
174
232
fn b() {}
233
+ type T1;
175
234
fn d() {}
235
+ const C0: ();
176
236
}
177
237
178
238
struct Foo;
179
239
impl Bar for Foo {
180
240
fn a() {}
241
+ type T0 = ();
181
242
fn c() {}
243
+ const C1: () = ();
182
244
fn b() {}
245
+ type T1 = ();
183
246
fn d() {}
247
+ const C0: () = ();
184
248
}
185
249
"# ,
186
250
)
187
251
}
188
252
189
253
#[ test]
190
- fn reorder_impl_trait_methods_uneven_ident_lengths ( ) {
254
+ fn reorder_impl_trait_items_uneven_ident_lengths ( ) {
191
255
check_assist (
192
- reorder_impl ,
256
+ reorder_impl_items ,
193
257
r#"
194
258
trait Bar {
195
- fn foo(&mut self) {}
196
- fn fooo(&mut self) {}
259
+ type Foo;
260
+ type Fooo;
197
261
}
198
262
199
263
struct Foo;
200
264
impl Bar for Foo {
201
- fn fooo(&mut self) {}
202
- fn foo(&mut self) {$0}
265
+ type Fooo = ();
266
+ type Foo = ();$0
203
267
}"# ,
204
268
r#"
205
269
trait Bar {
206
- fn foo(&mut self) {}
207
- fn fooo(&mut self) {}
270
+ type Foo;
271
+ type Fooo;
208
272
}
209
273
210
274
struct Foo;
211
275
impl Bar for Foo {
212
- fn foo(&mut self) {}
213
- fn fooo(&mut self) {}
276
+ type Foo = ();
277
+ type Fooo = ();
214
278
}"# ,
215
279
)
216
280
}
0 commit comments