Unnamed repository; edit this file 'description' to name the repository.
Merge #10001
10001: Sort enum variant r=Veykril a=vsrs
A small fix to the problem noted by `@lnicola` :
> 
>
> (note the slight inconsistency here: to sort the variants of `Animal` I have to select the enum name, but to sort the fields of `Cat` I have to select the fields themselves)
Co-authored-by: vsrs <[email protected]>
| -rw-r--r-- | crates/ide_assists/src/handlers/sort_items.rs | 39 |
1 files changed, 31 insertions, 8 deletions
diff --git a/crates/ide_assists/src/handlers/sort_items.rs b/crates/ide_assists/src/handlers/sort_items.rs index 775bd367de..f318c048b1 100644 --- a/crates/ide_assists/src/handlers/sort_items.rs +++ b/crates/ide_assists/src/handlers/sort_items.rs @@ -92,15 +92,11 @@ pub(crate) fn sort_items(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { } else if let Some(impl_ast) = ctx.find_node_at_offset::<ast::Impl>() { add_sort_methods_assist(acc, impl_ast.assoc_item_list()?) } else if let Some(struct_ast) = ctx.find_node_at_offset::<ast::Struct>() { - match struct_ast.field_list() { - Some(ast::FieldList::RecordFieldList(it)) => add_sort_fields_assist(acc, it), - _ => { - cov_mark::hit!(not_applicable_if_sorted_or_empty_or_single); - None - } - } + add_sort_field_list_assist(acc, struct_ast.field_list()) } else if let Some(union_ast) = ctx.find_node_at_offset::<ast::Union>() { add_sort_fields_assist(acc, union_ast.record_field_list()?) + } else if let Some(variant_ast) = ctx.find_node_at_offset::<ast::Variant>() { + add_sort_field_list_assist(acc, variant_ast.field_list()) } else if let Some(enum_struct_variant_ast) = ctx.find_node_at_offset::<ast::RecordFieldList>() { // should be above enum and below struct @@ -140,6 +136,16 @@ impl AddRewrite for Assists { } } +fn add_sort_field_list_assist(acc: &mut Assists, field_list: Option<ast::FieldList>) -> Option<()> { + match field_list { + Some(ast::FieldList::RecordFieldList(it)) => add_sort_fields_assist(acc, it), + _ => { + cov_mark::hit!(not_applicable_if_sorted_or_empty_or_single); + None + } + } +} + fn add_sort_methods_assist(acc: &mut Assists, item_list: ast::AssocItemList) -> Option<()> { let methods = get_methods(&item_list); let sorted = sort_by_name(&methods); @@ -541,7 +547,7 @@ enum Bar { } #[test] - fn sort_struct_enum_variant() { + fn sort_struct_enum_variant_fields() { check_assist( sort_items, r#" @@ -562,4 +568,21 @@ enum Bar { "#, ) } + + #[test] + fn sort_struct_enum_variant() { + check_assist( + sort_items, + r#" +enum Bar { + $0d$0{ second: usize, first: u32 }, +} + "#, + r#" +enum Bar { + d{ first: u32, second: usize }, +} + "#, + ) + } } |