2020-06-29 15:07:52 +00:00
|
|
|
use rustc_hash::FxHashMap;
|
2020-04-09 22:35:43 +00:00
|
|
|
|
2021-03-23 16:59:33 +00:00
|
|
|
use syntax::{algo, ast, match_ast, AstNode, SyntaxKind::*, SyntaxNode};
|
2020-05-06 16:45:35 +00:00
|
|
|
|
2020-06-28 22:36:05 +00:00
|
|
|
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
2020-04-09 22:35:43 +00:00
|
|
|
|
2020-04-11 17:39:10 +00:00
|
|
|
// Assist: reorder_fields
|
|
|
|
//
|
|
|
|
// Reorder the fields of record literals and record patterns in the same order as in
|
|
|
|
// the definition.
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// struct Foo {foo: i32, bar: i32};
|
2021-01-06 20:15:48 +00:00
|
|
|
// const test: Foo = $0Foo {bar: 0, foo: 1}
|
2020-04-11 17:39:10 +00:00
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// struct Foo {foo: i32, bar: i32};
|
2020-04-11 18:32:48 +00:00
|
|
|
// const test: Foo = Foo {foo: 1, bar: 0}
|
2020-04-11 17:39:10 +00:00
|
|
|
// ```
|
|
|
|
//
|
2020-05-06 16:45:35 +00:00
|
|
|
pub(crate) fn reorder_fields(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
2021-03-23 16:59:33 +00:00
|
|
|
let record = ctx
|
|
|
|
.find_node_at_offset::<ast::RecordExpr>()
|
|
|
|
.map(|it| it.syntax().clone())
|
|
|
|
.or_else(|| ctx.find_node_at_offset::<ast::RecordPat>().map(|it| it.syntax().clone()))?;
|
2020-04-09 22:35:43 +00:00
|
|
|
|
2021-03-23 16:59:33 +00:00
|
|
|
let path = record.children().find_map(ast::Path::cast)?;
|
2020-04-09 22:35:43 +00:00
|
|
|
|
2020-04-11 15:04:25 +00:00
|
|
|
let ranks = compute_fields_ranks(&path, &ctx)?;
|
2020-04-09 22:35:43 +00:00
|
|
|
|
2021-03-23 16:59:33 +00:00
|
|
|
let fields: Vec<SyntaxNode> = {
|
|
|
|
let field_kind = match record.kind() {
|
|
|
|
RECORD_EXPR => RECORD_EXPR_FIELD,
|
|
|
|
RECORD_PAT => RECORD_PAT_FIELD,
|
|
|
|
_ => {
|
|
|
|
stdx::never!();
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
record.children().flat_map(|n| n.children()).filter(|n| n.kind() == field_kind).collect()
|
|
|
|
};
|
|
|
|
|
|
|
|
let sorted_fields = {
|
|
|
|
let mut fields = fields.clone();
|
|
|
|
fields.sort_by_key(|node| *ranks.get(&get_field_name(node)).unwrap_or(&usize::max_value()));
|
|
|
|
fields
|
|
|
|
};
|
2020-04-09 22:35:43 +00:00
|
|
|
|
|
|
|
if sorted_fields == fields {
|
2021-03-08 20:19:44 +00:00
|
|
|
cov_mark::hit!(reorder_sorted_fields);
|
2020-04-09 22:35:43 +00:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2021-03-23 16:59:33 +00:00
|
|
|
let target = record.text_range();
|
2020-06-28 22:36:05 +00:00
|
|
|
acc.add(
|
2020-07-02 21:48:35 +00:00
|
|
|
AssistId("reorder_fields", AssistKind::RefactorRewrite),
|
2020-06-28 22:36:05 +00:00
|
|
|
"Reorder record fields",
|
|
|
|
target,
|
|
|
|
|edit| {
|
2020-11-08 22:22:11 +00:00
|
|
|
let mut rewriter = algo::SyntaxRewriter::default();
|
2020-06-28 22:36:05 +00:00
|
|
|
for (old, new) in fields.iter().zip(&sorted_fields) {
|
2020-11-08 22:22:11 +00:00
|
|
|
rewriter.replace(old, new);
|
2020-06-28 22:36:05 +00:00
|
|
|
}
|
2020-11-08 22:22:11 +00:00
|
|
|
edit.rewrite(rewriter);
|
2020-06-28 22:36:05 +00:00
|
|
|
},
|
|
|
|
)
|
2020-04-09 22:35:43 +00:00
|
|
|
}
|
|
|
|
|
2020-04-11 15:04:25 +00:00
|
|
|
fn get_field_name(node: &SyntaxNode) -> String {
|
2020-04-11 21:33:17 +00:00
|
|
|
let res = match_ast! {
|
|
|
|
match node {
|
2020-07-30 14:21:30 +00:00
|
|
|
ast::RecordExprField(field) => field.field_name().map(|it| it.to_string()),
|
2020-07-31 17:54:16 +00:00
|
|
|
ast::RecordPatField(field) => field.field_name().map(|it| it.to_string()),
|
2020-04-11 21:33:17 +00:00
|
|
|
_ => None,
|
2020-04-11 15:04:25 +00:00
|
|
|
}
|
2020-04-11 21:33:17 +00:00
|
|
|
};
|
|
|
|
res.unwrap_or_default()
|
2020-04-11 15:04:25 +00:00
|
|
|
}
|
|
|
|
|
2021-03-23 16:59:33 +00:00
|
|
|
fn compute_fields_ranks(path: &ast::Path, ctx: &AssistContext) -> Option<FxHashMap<String, usize>> {
|
|
|
|
let strukt = match ctx.sema.resolve_path(path) {
|
|
|
|
Some(hir::PathResolution::Def(hir::ModuleDef::Adt(hir::Adt::Struct(it)))) => it,
|
|
|
|
_ => return None,
|
|
|
|
};
|
2020-04-09 22:35:43 +00:00
|
|
|
|
2021-03-23 16:59:33 +00:00
|
|
|
let res = strukt
|
|
|
|
.fields(ctx.db())
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(idx, field)| (field.name(ctx.db()).to_string(), idx))
|
|
|
|
.collect();
|
2020-04-09 22:35:43 +00:00
|
|
|
|
2021-03-23 16:59:33 +00:00
|
|
|
Some(res)
|
2020-04-09 22:35:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-05-06 08:16:55 +00:00
|
|
|
use crate::tests::{check_assist, check_assist_not_applicable};
|
2020-04-09 22:35:43 +00:00
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
2020-12-15 08:51:40 +00:00
|
|
|
fn reorder_sorted_fields() {
|
2021-03-08 20:19:44 +00:00
|
|
|
cov_mark::check!(reorder_sorted_fields);
|
2020-04-09 22:35:43 +00:00
|
|
|
check_assist_not_applicable(
|
|
|
|
reorder_fields,
|
|
|
|
r#"
|
2021-03-23 16:59:33 +00:00
|
|
|
struct Foo { foo: i32, bar: i32 }
|
2021-01-06 20:15:48 +00:00
|
|
|
const test: Foo = $0Foo { foo: 0, bar: 0 };
|
2020-12-15 08:51:40 +00:00
|
|
|
"#,
|
2020-04-09 22:35:43 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn trivial_empty_fields() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
reorder_fields,
|
|
|
|
r#"
|
2021-03-23 16:59:33 +00:00
|
|
|
struct Foo {}
|
|
|
|
const test: Foo = $0Foo {};
|
2020-12-15 08:51:40 +00:00
|
|
|
"#,
|
2020-04-09 22:35:43 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn reorder_struct_fields() {
|
|
|
|
check_assist(
|
|
|
|
reorder_fields,
|
|
|
|
r#"
|
2021-03-23 16:59:33 +00:00
|
|
|
struct Foo { foo: i32, bar: i32 }
|
|
|
|
const test: Foo = $0Foo { bar: 0, foo: 1 };
|
2020-12-15 08:51:40 +00:00
|
|
|
"#,
|
2020-04-09 22:35:43 +00:00
|
|
|
r#"
|
2021-03-23 16:59:33 +00:00
|
|
|
struct Foo { foo: i32, bar: i32 }
|
|
|
|
const test: Foo = Foo { foo: 1, bar: 0 };
|
2020-12-15 08:51:40 +00:00
|
|
|
"#,
|
2020-04-09 22:35:43 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn reorder_struct_pattern() {
|
|
|
|
check_assist(
|
|
|
|
reorder_fields,
|
|
|
|
r#"
|
2020-12-15 08:51:40 +00:00
|
|
|
struct Foo { foo: i64, bar: i64, baz: i64 }
|
2020-04-09 22:35:43 +00:00
|
|
|
|
2020-12-15 08:51:40 +00:00
|
|
|
fn f(f: Foo) -> {
|
|
|
|
match f {
|
2021-01-06 20:15:48 +00:00
|
|
|
$0Foo { baz: 0, ref mut bar, .. } => (),
|
2020-12-15 08:51:40 +00:00
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2020-04-09 22:35:43 +00:00
|
|
|
r#"
|
2020-12-15 08:51:40 +00:00
|
|
|
struct Foo { foo: i64, bar: i64, baz: i64 }
|
2020-04-09 22:35:43 +00:00
|
|
|
|
2020-12-15 08:51:40 +00:00
|
|
|
fn f(f: Foo) -> {
|
|
|
|
match f {
|
|
|
|
Foo { ref mut bar, baz: 0, .. } => (),
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2020-04-09 22:35:43 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn reorder_with_extra_field() {
|
|
|
|
check_assist(
|
|
|
|
reorder_fields,
|
|
|
|
r#"
|
2021-03-23 16:59:33 +00:00
|
|
|
struct Foo { foo: String, bar: String }
|
2020-04-09 22:35:43 +00:00
|
|
|
|
2020-12-15 08:51:40 +00:00
|
|
|
impl Foo {
|
|
|
|
fn new() -> Foo {
|
|
|
|
let foo = String::new();
|
2021-01-06 20:15:48 +00:00
|
|
|
$0Foo {
|
2020-12-15 08:51:40 +00:00
|
|
|
bar: foo.clone(),
|
|
|
|
extra: "Extra field",
|
|
|
|
foo,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2020-04-09 22:35:43 +00:00
|
|
|
r#"
|
2021-03-23 16:59:33 +00:00
|
|
|
struct Foo { foo: String, bar: String }
|
2020-04-09 22:35:43 +00:00
|
|
|
|
2020-12-15 08:51:40 +00:00
|
|
|
impl Foo {
|
|
|
|
fn new() -> Foo {
|
|
|
|
let foo = String::new();
|
|
|
|
Foo {
|
|
|
|
foo,
|
|
|
|
bar: foo.clone(),
|
|
|
|
extra: "Extra field",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2020-04-09 22:35:43 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|