@@ -19,10 +19,15 @@ pub(crate) fn sort_items(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
19
19
} else if let Some ( struct_ast) = ctx. find_node_at_offset :: < ast:: Struct > ( ) {
20
20
match struct_ast. field_list ( ) {
21
21
Some ( ast:: FieldList :: RecordFieldList ( it) ) => add_sort_fields_assist ( acc, it) ,
22
- _ => None ,
22
+ _ => {
23
+ cov_mark:: hit!( not_applicable_if_sorted_or_empty_or_single) ;
24
+ None
25
+ }
23
26
}
24
27
} else if let Some ( union_ast) = ctx. find_node_at_offset :: < ast:: Union > ( ) {
25
28
add_sort_fields_assist ( acc, union_ast. record_field_list ( ) ?)
29
+ } else if let Some ( enum_ast) = ctx. find_node_at_offset :: < ast:: Enum > ( ) {
30
+ add_sort_variants_assist ( acc, enum_ast. variant_list ( ) ?)
26
31
} else {
27
32
None
28
33
}
@@ -61,7 +66,7 @@ fn add_sort_methods_assist(acc: &mut Assists, item_list: ast::AssocItemList) ->
61
66
let sorted = sort_by_name ( & methods) ;
62
67
63
68
if methods == sorted {
64
- cov_mark:: hit!( not_applicable_if_sorted ) ;
69
+ cov_mark:: hit!( not_applicable_if_sorted_or_empty_or_single ) ;
65
70
return None ;
66
71
}
67
72
@@ -76,7 +81,7 @@ fn add_sort_fields_assist(
76
81
let sorted = sort_by_name ( & fields) ;
77
82
78
83
if fields == sorted {
79
- cov_mark:: hit!( not_applicable_if_sorted ) ;
84
+ cov_mark:: hit!( not_applicable_if_sorted_or_empty_or_single ) ;
80
85
return None ;
81
86
}
82
87
@@ -88,6 +93,23 @@ fn add_sort_fields_assist(
88
93
)
89
94
}
90
95
96
+ fn add_sort_variants_assist ( acc : & mut Assists , variant_list : ast:: VariantList ) -> Option < ( ) > {
97
+ let variants: Vec < _ > = variant_list. variants ( ) . collect ( ) ;
98
+ let sorted = sort_by_name ( & variants) ;
99
+
100
+ if variants == sorted {
101
+ cov_mark:: hit!( not_applicable_if_sorted_or_empty_or_single) ;
102
+ return None ;
103
+ }
104
+
105
+ acc. add_rewrite (
106
+ "Sort variants alphabetically" ,
107
+ variants,
108
+ sorted,
109
+ variant_list. syntax ( ) . text_range ( ) ,
110
+ )
111
+ }
112
+
91
113
fn sort_by_name < T : NameOwner + Clone > ( initial : & [ T ] ) -> Vec < T > {
92
114
initial
93
115
. iter ( )
@@ -109,9 +131,72 @@ mod tests {
109
131
110
132
use super :: * ;
111
133
134
+ #[ test]
135
+ fn not_applicable_if_trait_empty ( ) {
136
+ cov_mark:: check!( not_applicable_if_sorted_or_empty_or_single) ;
137
+
138
+ check_assist_not_applicable (
139
+ sort_items,
140
+ r#"
141
+ t$0rait Bar {
142
+ }
143
+ "# ,
144
+ )
145
+ }
146
+
147
+ #[ test]
148
+ fn not_applicable_if_impl_empty ( ) {
149
+ cov_mark:: check!( not_applicable_if_sorted_or_empty_or_single) ;
150
+
151
+ check_assist_not_applicable (
152
+ sort_items,
153
+ r#"
154
+ struct Bar;
155
+ $0impl Bar {
156
+ }
157
+ "# ,
158
+ )
159
+ }
160
+
161
+ #[ test]
162
+ fn not_applicable_if_struct_empty ( ) {
163
+ cov_mark:: check!( not_applicable_if_sorted_or_empty_or_single) ;
164
+
165
+ check_assist_not_applicable (
166
+ sort_items,
167
+ r#"
168
+ $0struct Bar;
169
+ "# ,
170
+ )
171
+ }
172
+
173
+ #[ test]
174
+ fn not_applicable_if_struct_empty2 ( ) {
175
+ cov_mark:: check!( not_applicable_if_sorted_or_empty_or_single) ;
176
+
177
+ check_assist_not_applicable (
178
+ sort_items,
179
+ r#"
180
+ $0struct Bar { };
181
+ "# ,
182
+ )
183
+ }
184
+
185
+ #[ test]
186
+ fn not_applicable_if_enum_empty ( ) {
187
+ cov_mark:: check!( not_applicable_if_sorted_or_empty_or_single) ;
188
+
189
+ check_assist_not_applicable (
190
+ sort_items,
191
+ r#"
192
+ $0enum ZeroVariants {};
193
+ "# ,
194
+ )
195
+ }
196
+
112
197
#[ test]
113
198
fn not_applicable_if_trait_sorted ( ) {
114
- cov_mark:: check!( not_applicable_if_sorted ) ;
199
+ cov_mark:: check!( not_applicable_if_sorted_or_empty_or_single ) ;
115
200
116
201
check_assist_not_applicable (
117
202
sort_items,
@@ -127,7 +212,7 @@ t$0rait Bar {
127
212
128
213
#[ test]
129
214
fn not_applicable_if_impl_sorted ( ) {
130
- cov_mark:: check!( not_applicable_if_sorted ) ;
215
+ cov_mark:: check!( not_applicable_if_sorted_or_empty_or_single ) ;
131
216
132
217
check_assist_not_applicable (
133
218
sort_items,
@@ -144,7 +229,7 @@ $0impl Bar {
144
229
145
230
#[ test]
146
231
fn not_applicable_if_struct_sorted ( ) {
147
- cov_mark:: check!( not_applicable_if_sorted ) ;
232
+ cov_mark:: check!( not_applicable_if_sorted_or_empty_or_single ) ;
148
233
149
234
check_assist_not_applicable (
150
235
sort_items,
@@ -160,7 +245,7 @@ $0struct Bar {
160
245
161
246
#[ test]
162
247
fn not_applicable_if_union_sorted ( ) {
163
- cov_mark:: check!( not_applicable_if_sorted ) ;
248
+ cov_mark:: check!( not_applicable_if_sorted_or_empty_or_single ) ;
164
249
165
250
check_assist_not_applicable (
166
251
sort_items,
@@ -174,6 +259,22 @@ $0union Bar {
174
259
)
175
260
}
176
261
262
+ #[ test]
263
+ fn not_applicable_if_enum_sorted ( ) {
264
+ cov_mark:: check!( not_applicable_if_sorted_or_empty_or_single) ;
265
+
266
+ check_assist_not_applicable (
267
+ sort_items,
268
+ r#"
269
+ $0enum Bar {
270
+ a,
271
+ b,
272
+ c,
273
+ }
274
+ "# ,
275
+ )
276
+ }
277
+
177
278
#[ test]
178
279
fn sort_trait ( ) {
179
280
check_assist (
@@ -303,6 +404,29 @@ union Bar {
303
404
a: u32,
304
405
b: u8,
305
406
c: u64,
407
+ }
408
+ "# ,
409
+ )
410
+ }
411
+
412
+ #[ test]
413
+ fn sort_enum ( ) {
414
+ check_assist (
415
+ sort_items,
416
+ r#"
417
+ $0enum Bar {
418
+ d{ first: u32, second: usize},
419
+ b = 14,
420
+ a,
421
+ c(u32, usize),
422
+ }
423
+ "# ,
424
+ r#"
425
+ enum Bar {
426
+ a,
427
+ b = 14,
428
+ c(u32, usize),
429
+ d{ first: u32, second: usize},
306
430
}
307
431
"# ,
308
432
)
0 commit comments