mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-27 20:35:09 +00:00
Merge #10001
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:
commit
ced65f77c4
1 changed files with 31 additions and 8 deletions
|
@ -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>() {
|
} else if let Some(impl_ast) = ctx.find_node_at_offset::<ast::Impl>() {
|
||||||
add_sort_methods_assist(acc, impl_ast.assoc_item_list()?)
|
add_sort_methods_assist(acc, impl_ast.assoc_item_list()?)
|
||||||
} else if let Some(struct_ast) = ctx.find_node_at_offset::<ast::Struct>() {
|
} else if let Some(struct_ast) = ctx.find_node_at_offset::<ast::Struct>() {
|
||||||
match struct_ast.field_list() {
|
add_sort_field_list_assist(acc, 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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if let Some(union_ast) = ctx.find_node_at_offset::<ast::Union>() {
|
} else if let Some(union_ast) = ctx.find_node_at_offset::<ast::Union>() {
|
||||||
add_sort_fields_assist(acc, union_ast.record_field_list()?)
|
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>()
|
} else if let Some(enum_struct_variant_ast) = ctx.find_node_at_offset::<ast::RecordFieldList>()
|
||||||
{
|
{
|
||||||
// should be above enum and below struct
|
// 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<()> {
|
fn add_sort_methods_assist(acc: &mut Assists, item_list: ast::AssocItemList) -> Option<()> {
|
||||||
let methods = get_methods(&item_list);
|
let methods = get_methods(&item_list);
|
||||||
let sorted = sort_by_name(&methods);
|
let sorted = sort_by_name(&methods);
|
||||||
|
@ -541,7 +547,7 @@ enum Bar {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn sort_struct_enum_variant() {
|
fn sort_struct_enum_variant_fields() {
|
||||||
check_assist(
|
check_assist(
|
||||||
sort_items,
|
sort_items,
|
||||||
r#"
|
r#"
|
||||||
|
@ -558,6 +564,23 @@ enum Bar {
|
||||||
b = 14,
|
b = 14,
|
||||||
a,
|
a,
|
||||||
c(u32, usize),
|
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 },
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue