Skip to content

Commit 6f8888c

Browse files
committed
Sort enums
1 parent 2c7e99b commit 6f8888c

File tree

1 file changed

+131
-7
lines changed

1 file changed

+131
-7
lines changed

crates/ide_assists/src/handlers/sort_items.rs

Lines changed: 131 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,15 @@ pub(crate) fn sort_items(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
1919
} else if let Some(struct_ast) = ctx.find_node_at_offset::<ast::Struct>() {
2020
match struct_ast.field_list() {
2121
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+
}
2326
}
2427
} else if let Some(union_ast) = ctx.find_node_at_offset::<ast::Union>() {
2528
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()?)
2631
} else {
2732
None
2833
}
@@ -61,7 +66,7 @@ fn add_sort_methods_assist(acc: &mut Assists, item_list: ast::AssocItemList) ->
6166
let sorted = sort_by_name(&methods);
6267

6368
if methods == sorted {
64-
cov_mark::hit!(not_applicable_if_sorted);
69+
cov_mark::hit!(not_applicable_if_sorted_or_empty_or_single);
6570
return None;
6671
}
6772

@@ -76,7 +81,7 @@ fn add_sort_fields_assist(
7681
let sorted = sort_by_name(&fields);
7782

7883
if fields == sorted {
79-
cov_mark::hit!(not_applicable_if_sorted);
84+
cov_mark::hit!(not_applicable_if_sorted_or_empty_or_single);
8085
return None;
8186
}
8287

@@ -88,6 +93,23 @@ fn add_sort_fields_assist(
8893
)
8994
}
9095

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+
91113
fn sort_by_name<T: NameOwner + Clone>(initial: &[T]) -> Vec<T> {
92114
initial
93115
.iter()
@@ -109,9 +131,72 @@ mod tests {
109131

110132
use super::*;
111133

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+
112197
#[test]
113198
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);
115200

116201
check_assist_not_applicable(
117202
sort_items,
@@ -127,7 +212,7 @@ t$0rait Bar {
127212

128213
#[test]
129214
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);
131216

132217
check_assist_not_applicable(
133218
sort_items,
@@ -144,7 +229,7 @@ $0impl Bar {
144229

145230
#[test]
146231
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);
148233

149234
check_assist_not_applicable(
150235
sort_items,
@@ -160,7 +245,7 @@ $0struct Bar {
160245

161246
#[test]
162247
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);
164249

165250
check_assist_not_applicable(
166251
sort_items,
@@ -174,6 +259,22 @@ $0union Bar {
174259
)
175260
}
176261

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+
177278
#[test]
178279
fn sort_trait() {
179280
check_assist(
@@ -303,6 +404,29 @@ union Bar {
303404
a: u32,
304405
b: u8,
305406
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},
306430
}
307431
"#,
308432
)

0 commit comments

Comments
 (0)