3 use itertools::Itertools;
4 use syntax::{ast, ted, AstNode};
6 use crate::{AssistContext, AssistId, AssistKind, Assists};
8 // Assist: reorder_fields
10 // Reorder the fields of record literals and record patterns in the same order as in
14 // struct Foo {foo: i32, bar: i32};
15 // const test: Foo = $0Foo {bar: 0, foo: 1}
19 // struct Foo {foo: i32, bar: i32};
20 // const test: Foo = Foo {foo: 1, bar: 0}
22 pub(crate) fn reorder_fields(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
24 .find_node_at_offset::<ast::RecordExpr>()
26 .or_else(|| ctx.find_node_at_offset::<ast::RecordPat>().map(Either::Right))?;
28 let path = record.as_ref().either(|it| it.path(), |it| it.path())?;
29 let ranks = compute_fields_ranks(&path, ctx)?;
30 let get_rank_of_field =
31 |of: Option<_>| *ranks.get(&of.unwrap_or_default()).unwrap_or(&usize::MAX);
33 let field_list = match &record {
34 Either::Left(it) => Either::Left(it.record_expr_field_list()?),
35 Either::Right(it) => Either::Right(it.record_pat_field_list()?),
37 let fields = match field_list {
38 Either::Left(it) => Either::Left((
40 .sorted_unstable_by_key(|field| {
41 get_rank_of_field(field.field_name().map(|it| it.to_string()))
46 Either::Right(it) => Either::Right((
48 .sorted_unstable_by_key(|field| {
49 get_rank_of_field(field.field_name().map(|it| it.to_string()))
56 let is_sorted = fields.as_ref().either(
57 |(sorted, field_list)| field_list.fields().zip(sorted).all(|(a, b)| a == *b),
58 |(sorted, field_list)| field_list.fields().zip(sorted).all(|(a, b)| a == *b),
61 cov_mark::hit!(reorder_sorted_fields);
64 let target = record.as_ref().either(AstNode::syntax, AstNode::syntax).text_range();
66 AssistId("reorder_fields", AssistKind::RefactorRewrite),
67 "Reorder record fields",
69 |builder| match fields {
70 Either::Left((sorted, field_list)) => {
71 replace(builder.make_mut(field_list).fields(), sorted)
73 Either::Right((sorted, field_list)) => {
74 replace(builder.make_mut(field_list).fields(), sorted)
80 fn replace<T: AstNode + PartialEq>(
81 fields: impl Iterator<Item = T>,
82 sorted_fields: impl IntoIterator<Item = T>,
84 fields.zip(sorted_fields).for_each(|(field, sorted_field)| {
85 ted::replace(field.syntax(), sorted_field.syntax().clone_for_update())
89 fn compute_fields_ranks(
91 ctx: &AssistContext<'_>,
92 ) -> Option<FxHashMap<String, usize>> {
93 let strukt = match ctx.sema.resolve_path(path) {
94 Some(hir::PathResolution::Def(hir::ModuleDef::Adt(hir::Adt::Struct(it)))) => it,
102 .map(|(idx, field)| (field.name(ctx.db()).to_string(), idx))
110 use crate::tests::{check_assist, check_assist_not_applicable};
115 fn reorder_sorted_fields() {
116 cov_mark::check!(reorder_sorted_fields);
117 check_assist_not_applicable(
120 struct Foo { foo: i32, bar: i32 }
121 const test: Foo = $0Foo { foo: 0, bar: 0 };
127 fn trivial_empty_fields() {
128 check_assist_not_applicable(
132 const test: Foo = $0Foo {};
138 fn reorder_struct_fields() {
142 struct Foo { foo: i32, bar: i32 }
143 const test: Foo = $0Foo { bar: 0, foo: 1 };
146 struct Foo { foo: i32, bar: i32 }
147 const test: Foo = Foo { foo: 1, bar: 0 };
152 fn reorder_struct_pattern() {
156 struct Foo { foo: i64, bar: i64, baz: i64 }
160 $0Foo { baz: 0, ref mut bar, .. } => (),
166 struct Foo { foo: i64, bar: i64, baz: i64 }
170 Foo { ref mut bar, baz: 0, .. } => (),
179 fn reorder_with_extra_field() {
183 struct Foo { foo: String, bar: String }
187 let foo = String::new();
190 extra: "Extra field",
197 struct Foo { foo: String, bar: String }
201 let foo = String::new();
205 extra: "Extra field",