// struct Foo {foo: i32, bar: i32};
// const test: Foo = Foo {foo: 1, bar: 0}
// ```
-//
pub(crate) fn reorder_fields(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let record = ctx
.find_node_at_offset::<ast::RecordExpr>()
.or_else(|| ctx.find_node_at_offset::<ast::RecordPat>().map(Either::Right))?;
let path = record.as_ref().either(|it| it.path(), |it| it.path())?;
- let ranks = compute_fields_ranks(&path, &ctx)?;
+ let ranks = compute_fields_ranks(&path, ctx)?;
let get_rank_of_field =
|of: Option<_>| *ranks.get(&of.unwrap_or_default()).unwrap_or(&usize::MAX);
target,
|builder| match fields {
Either::Left((sorted, field_list)) => {
- replace(builder.make_ast_mut(field_list).fields(), sorted)
+ replace(builder.make_mut(field_list).fields(), sorted)
}
Either::Right((sorted, field_list)) => {
- replace(builder.make_ast_mut(field_list).fields(), sorted)
+ replace(builder.make_mut(field_list).fields(), sorted)
}
},
)
fields: impl Iterator<Item = T>,
sorted_fields: impl IntoIterator<Item = T>,
) {
- fields.zip(sorted_fields).filter(|(field, sorted)| field != sorted).for_each(
- |(field, sorted_field)| {
- ted::replace(field.syntax(), sorted_field.syntax().clone_for_update());
- },
- );
+ fields.zip(sorted_fields).for_each(|(field, sorted_field)| {
+ ted::replace(field.syntax(), sorted_field.syntax().clone_for_update())
+ });
}
fn compute_fields_ranks(path: &ast::Path, ctx: &AssistContext) -> Option<FxHashMap<String, usize>> {