2 use itertools::Itertools;
3 use rustc_hash::FxHashMap;
5 use syntax::{ast, ted, AstNode};
7 use crate::{AssistContext, AssistId, AssistKind, Assists};
9 // Assist: reorder_fields
11 // Reorder the fields of record literals and record patterns in the same order as in
15 // struct Foo {foo: i32, bar: i32};
16 // const test: Foo = $0Foo {bar: 0, foo: 1}
20 // struct Foo {foo: i32, bar: i32};
21 // const test: Foo = Foo {foo: 1, bar: 0}
23 pub(crate) fn reorder_fields(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
25 .find_node_at_offset::<ast::RecordExpr>()
27 .or_else(|| ctx.find_node_at_offset::<ast::RecordPat>().map(Either::Right))?;
29 let path = record.as_ref().either(|it| it.path(), |it| it.path())?;
30 let ranks = compute_fields_ranks(&path, ctx)?;
31 let get_rank_of_field =
32 |of: Option<_>| *ranks.get(&of.unwrap_or_default()).unwrap_or(&usize::MAX);
34 let field_list = match &record {
35 Either::Left(it) => Either::Left(it.record_expr_field_list()?),
36 Either::Right(it) => Either::Right(it.record_pat_field_list()?),
38 let fields = match field_list {
39 Either::Left(it) => Either::Left((
41 .sorted_unstable_by_key(|field| {
42 get_rank_of_field(field.field_name().map(|it| it.to_string()))
47 Either::Right(it) => Either::Right((
49 .sorted_unstable_by_key(|field| {
50 get_rank_of_field(field.field_name().map(|it| it.to_string()))
57 let is_sorted = fields.as_ref().either(
58 |(sorted, field_list)| field_list.fields().zip(sorted).all(|(a, b)| a == *b),
59 |(sorted, field_list)| field_list.fields().zip(sorted).all(|(a, b)| a == *b),
62 cov_mark::hit!(reorder_sorted_fields);
65 let target = record.as_ref().either(AstNode::syntax, AstNode::syntax).text_range();
67 AssistId("reorder_fields", AssistKind::RefactorRewrite),
68 "Reorder record fields",
70 |builder| match fields {
71 Either::Left((sorted, field_list)) => {
72 replace(builder.make_mut(field_list).fields(), sorted)
74 Either::Right((sorted, field_list)) => {
75 replace(builder.make_mut(field_list).fields(), sorted)
81 fn replace<T: AstNode + PartialEq>(
82 fields: impl Iterator<Item = T>,
83 sorted_fields: impl IntoIterator<Item = T>,
85 fields.zip(sorted_fields).for_each(|(field, sorted_field)| {
86 ted::replace(field.syntax(), sorted_field.syntax().clone_for_update())
90 fn compute_fields_ranks(path: &ast::Path, ctx: &AssistContext) -> Option<FxHashMap<String, usize>> {
91 let strukt = match ctx.sema.resolve_path(path) {
92 Some(hir::PathResolution::Def(hir::ModuleDef::Adt(hir::Adt::Struct(it)))) => it,
100 .map(|(idx, field)| (field.name(ctx.db()).to_string(), idx))
108 use crate::tests::{check_assist, check_assist_not_applicable};
113 fn reorder_sorted_fields() {
114 cov_mark::check!(reorder_sorted_fields);
115 check_assist_not_applicable(
118 struct Foo { foo: i32, bar: i32 }
119 const test: Foo = $0Foo { foo: 0, bar: 0 };
125 fn trivial_empty_fields() {
126 check_assist_not_applicable(
130 const test: Foo = $0Foo {};
136 fn reorder_struct_fields() {
140 struct Foo { foo: i32, bar: i32 }
141 const test: Foo = $0Foo { bar: 0, foo: 1 };
144 struct Foo { foo: i32, bar: i32 }
145 const test: Foo = Foo { foo: 1, bar: 0 };
150 fn reorder_struct_pattern() {
154 struct Foo { foo: i64, bar: i64, baz: i64 }
158 $0Foo { baz: 0, ref mut bar, .. } => (),
164 struct Foo { foo: i64, bar: i64, baz: i64 }
168 Foo { ref mut bar, baz: 0, .. } => (),
177 fn reorder_with_extra_field() {
181 struct Foo { foo: String, bar: String }
185 let foo = String::new();
188 extra: "Extra field",
195 struct Foo { foo: String, bar: String }
199 let foo = String::new();
203 extra: "Extra field",