10001: Sort enum variant r=Veykril a=vsrs

A small fix to the problem noted by `@lnicola` :

> ![sort-fields](https://user-images.githubusercontent.com/308347/129513196-4ffc7937-be58-44d4-9ec7-ba8745dcb460.gif)
> 
> (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 <vit@conrlab.com>
This commit is contained in:
bors[bot] 2021-08-23 22:19:50 +00:00 committed by GitHub
commit ced65f77c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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#"
@ -558,6 +564,23 @@ enum Bar {
b = 14,
a,
c(u32, usize),
}
"#,
)
}
#[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 },
}
"#,
)