rust/crates/ide-assists/src/handlers/sort_items.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

688 lines
13 KiB
Rust
Raw Normal View History

2021-07-30 21:15:28 -05:00
use std::cmp::Ordering;
use itertools::Itertools;
use syntax::{
2021-09-27 05:54:24 -05:00
ast::{self, HasName},
2021-07-30 22:15:19 -05:00
ted, AstNode, TextRange,
2021-07-30 21:15:28 -05:00
};
use crate::{utils::get_methods, AssistContext, AssistId, AssistKind, Assists};
// Assist: sort_items
//
// Sorts item members alphabetically: fields, enum variants and methods.
//
// ```
2021-08-02 12:22:45 -05:00
// struct $0Foo$0 { second: u32, first: String }
// ```
// ->
// ```
// struct Foo { first: String, second: u32 }
// ```
// ---
// ```
2021-08-02 12:22:45 -05:00
// trait $0Bar$0 {
// fn second(&self) -> u32;
// fn first(&self) -> String;
// }
// ```
// ->
// ```
// trait Bar {
// fn first(&self) -> String;
// fn second(&self) -> u32;
// }
// ```
// ---
// ```
// struct Baz;
2021-08-02 12:22:45 -05:00
// impl $0Baz$0 {
// fn second(&self) -> u32;
// fn first(&self) -> String;
// }
// ```
// ->
// ```
// struct Baz;
// impl Baz {
// fn first(&self) -> String;
// fn second(&self) -> u32;
// }
// ```
// ---
// There is a difference between sorting enum variants:
//
// ```
2021-08-02 12:22:45 -05:00
// enum $0Animal$0 {
// Dog(String, f64),
// Cat { weight: f64, name: String },
// }
// ```
// ->
// ```
// enum Animal {
// Cat { weight: f64, name: String },
// Dog(String, f64),
// }
// ```
// and sorting a single enum struct variant:
//
// ```
// enum Animal {
// Dog(String, f64),
2021-08-02 12:22:45 -05:00
// Cat $0{ weight: f64, name: String }$0,
// }
// ```
// ->
// ```
// enum Animal {
// Dog(String, f64),
2021-07-31 02:18:38 -05:00
// Cat { name: String, weight: f64 },
// }
// ```
2022-07-20 08:02:08 -05:00
pub(crate) fn sort_items(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
2021-10-13 07:39:37 -05:00
if ctx.has_empty_selection() {
cov_mark::hit!(not_applicable_if_no_selection);
return None;
}
if let Some(struct_ast) = ctx.find_node_at_offset::<ast::Struct>() {
2021-08-23 12:31:54 -05:00
add_sort_field_list_assist(acc, struct_ast.field_list())
2021-07-30 22:15:19 -05:00
} else if let Some(union_ast) = ctx.find_node_at_offset::<ast::Union>() {
add_sort_fields_assist(acc, union_ast.record_field_list()?)
2021-08-23 12:31:54 -05:00
} else if let Some(variant_ast) = ctx.find_node_at_offset::<ast::Variant>() {
add_sort_field_list_assist(acc, variant_ast.field_list())
2021-07-30 23:20:07 -05:00
} else if let Some(enum_struct_variant_ast) = ctx.find_node_at_offset::<ast::RecordFieldList>()
{
// should be above enum and below struct
add_sort_fields_assist(acc, enum_struct_variant_ast)
2021-07-30 22:37:18 -05:00
} else if let Some(enum_ast) = ctx.find_node_at_offset::<ast::Enum>() {
add_sort_variants_assist(acc, enum_ast.variant_list()?)
} else if let Some(trait_ast) = ctx.find_node_at_offset::<ast::Trait>() {
add_sort_methods_assist(acc, ctx, trait_ast.assoc_item_list()?)
} else if let Some(impl_ast) = ctx.find_node_at_offset::<ast::Impl>() {
add_sort_methods_assist(acc, ctx, impl_ast.assoc_item_list()?)
2021-07-30 21:15:28 -05:00
} else {
None
}
}
2021-07-30 22:15:19 -05:00
trait AddRewrite {
fn add_rewrite<T: AstNode>(
&mut self,
label: &str,
old: Vec<T>,
new: Vec<T>,
target: TextRange,
) -> Option<()>;
fn yeet() {}
2021-07-30 22:15:19 -05:00
}
impl AddRewrite for Assists {
fn yeet() {}
2021-07-30 22:15:19 -05:00
fn add_rewrite<T: AstNode>(
&mut self,
label: &str,
old: Vec<T>,
new: Vec<T>,
target: TextRange,
) -> Option<()> {
self.add(AssistId("sort_items", AssistKind::RefactorRewrite), label, target, |builder| {
let mutable: Vec<T> = old.into_iter().map(|it| builder.make_mut(it)).collect();
2021-07-30 22:15:19 -05:00
mutable
.into_iter()
.zip(new)
.for_each(|(old, new)| ted::replace(old.syntax(), new.clone_for_update().syntax()));
})
}
}
2021-08-23 12:31:54 -05:00
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,
ctx: &AssistContext<'_>,
item_list: ast::AssocItemList,
) -> Option<()> {
let selection = ctx.selection_trimmed();
// ignore assist if the selection intersects with an associated item.
if item_list.assoc_items().any(|item| item.syntax().text_range().intersect(selection).is_some())
{
return None;
}
2021-07-30 21:15:28 -05:00
let methods = get_methods(&item_list);
let sorted = sort_by_name(&methods);
if methods == sorted {
2021-07-30 22:37:18 -05:00
cov_mark::hit!(not_applicable_if_sorted_or_empty_or_single);
2021-07-30 21:15:28 -05:00
return None;
}
2021-07-30 22:15:19 -05:00
acc.add_rewrite("Sort methods alphabetically", methods, sorted, item_list.syntax().text_range())
2021-07-30 21:15:28 -05:00
}
2021-07-30 22:15:19 -05:00
fn add_sort_fields_assist(
acc: &mut Assists,
record_field_list: ast::RecordFieldList,
) -> Option<()> {
let fields: Vec<_> = record_field_list.fields().collect();
2021-07-30 21:50:15 -05:00
let sorted = sort_by_name(&fields);
if fields == sorted {
2021-07-30 22:37:18 -05:00
cov_mark::hit!(not_applicable_if_sorted_or_empty_or_single);
2021-07-30 21:50:15 -05:00
return None;
}
2021-07-30 22:15:19 -05:00
acc.add_rewrite(
"Sort fields alphabetically",
fields,
sorted,
record_field_list.syntax().text_range(),
2021-07-30 21:50:15 -05:00
)
}
2021-07-30 22:37:18 -05:00
fn add_sort_variants_assist(acc: &mut Assists, variant_list: ast::VariantList) -> Option<()> {
let variants: Vec<_> = variant_list.variants().collect();
let sorted = sort_by_name(&variants);
if variants == sorted {
cov_mark::hit!(not_applicable_if_sorted_or_empty_or_single);
return None;
}
acc.add_rewrite(
"Sort variants alphabetically",
variants,
sorted,
variant_list.syntax().text_range(),
)
}
2021-09-27 05:54:24 -05:00
fn sort_by_name<T: HasName + Clone>(initial: &[T]) -> Vec<T> {
2021-07-30 21:15:28 -05:00
initial
.iter()
.cloned()
.sorted_by(|a, b| match (a.name(), b.name()) {
(Some(a), Some(b)) => Ord::cmp(&a.to_string(), &b.to_string()),
// unexpected, but just in case
(None, None) => Ordering::Equal,
(None, Some(_)) => Ordering::Less,
(Some(_), None) => Ordering::Greater,
})
.collect()
}
#[cfg(test)]
mod tests {
use crate::tests::{check_assist, check_assist_not_applicable};
use super::*;
#[test]
fn not_applicable_if_selection_in_fn_body() {
check_assist_not_applicable(
sort_items,
r#"
struct S;
impl S {
fn func2() {
$0 bar $0
}
fn func() {}
}
"#,
)
}
#[test]
fn not_applicable_if_selection_at_associated_const() {
check_assist_not_applicable(
sort_items,
r#"
struct S;
impl S {
fn func2() {}
fn func() {}
const C: () = $0()$0;
}
"#,
)
}
#[test]
fn not_applicable_if_selection_overlaps_nodes() {
check_assist_not_applicable(
sort_items,
r#"
struct S;
impl $0S {
fn$0 func2() {}
fn func() {}
}
"#,
)
}
#[test]
fn not_applicable_if_no_selection() {
cov_mark::check!(not_applicable_if_no_selection);
check_assist_not_applicable(
sort_items,
r#"
t$0rait Bar {
fn b();
fn a();
}
"#,
)
}
#[test]
fn not_applicable_if_selection_in_trait_fn_body() {
check_assist_not_applicable(
sort_items,
r#"
trait Bar {
fn b() {
$0 hello $0
}
fn a();
}
"#,
)
}
2021-07-30 22:37:18 -05:00
#[test]
fn not_applicable_if_trait_empty() {
cov_mark::check!(not_applicable_if_sorted_or_empty_or_single);
check_assist_not_applicable(
sort_items,
r#"
t$0rait Bar$0 {
2021-07-30 22:37:18 -05:00
}
"#,
)
}
#[test]
fn not_applicable_if_impl_empty() {
cov_mark::check!(not_applicable_if_sorted_or_empty_or_single);
check_assist_not_applicable(
sort_items,
r#"
struct Bar;
$0impl Bar$0 {
2021-07-30 22:37:18 -05:00
}
"#,
)
}
#[test]
fn not_applicable_if_struct_empty() {
cov_mark::check!(not_applicable_if_sorted_or_empty_or_single);
check_assist_not_applicable(
sort_items,
r#"
$0struct Bar$0 ;
2021-07-30 22:37:18 -05:00
"#,
)
}
#[test]
fn not_applicable_if_struct_empty2() {
cov_mark::check!(not_applicable_if_sorted_or_empty_or_single);
check_assist_not_applicable(
sort_items,
r#"
$0struct Bar$0 { };
2021-07-30 22:37:18 -05:00
"#,
)
}
#[test]
fn not_applicable_if_enum_empty() {
cov_mark::check!(not_applicable_if_sorted_or_empty_or_single);
check_assist_not_applicable(
sort_items,
r#"
$0enum ZeroVariants$0 {};
2021-07-30 22:37:18 -05:00
"#,
)
}
2021-07-30 21:15:28 -05:00
#[test]
fn not_applicable_if_trait_sorted() {
2021-07-30 22:37:18 -05:00
cov_mark::check!(not_applicable_if_sorted_or_empty_or_single);
2021-07-30 21:15:28 -05:00
check_assist_not_applicable(
sort_items,
r#"
t$0rait Bar$0 {
2021-07-30 21:15:28 -05:00
fn a() {}
fn b() {}
fn c() {}
}
"#,
)
}
#[test]
fn not_applicable_if_impl_sorted() {
2021-07-30 22:37:18 -05:00
cov_mark::check!(not_applicable_if_sorted_or_empty_or_single);
2021-07-30 21:15:28 -05:00
check_assist_not_applicable(
sort_items,
r#"
struct Bar;
$0impl Bar$0 {
2021-07-30 21:15:28 -05:00
fn a() {}
fn b() {}
fn c() {}
}
"#,
)
}
2021-07-30 21:50:15 -05:00
#[test]
fn not_applicable_if_struct_sorted() {
2021-07-30 22:37:18 -05:00
cov_mark::check!(not_applicable_if_sorted_or_empty_or_single);
2021-07-30 21:50:15 -05:00
check_assist_not_applicable(
sort_items,
r#"
$0struct Bar$0 {
2021-07-30 21:50:15 -05:00
a: u32,
b: u8,
c: u64,
}
"#,
)
}
2021-07-30 22:15:19 -05:00
#[test]
fn not_applicable_if_union_sorted() {
2021-07-30 22:37:18 -05:00
cov_mark::check!(not_applicable_if_sorted_or_empty_or_single);
2021-07-30 22:15:19 -05:00
check_assist_not_applicable(
sort_items,
r#"
$0union Bar$0 {
2021-07-30 22:15:19 -05:00
a: u32,
b: u8,
c: u64,
}
"#,
)
}
2021-07-30 22:37:18 -05:00
#[test]
fn not_applicable_if_enum_sorted() {
cov_mark::check!(not_applicable_if_sorted_or_empty_or_single);
check_assist_not_applicable(
sort_items,
r#"
$0enum Bar$0 {
2021-07-30 22:37:18 -05:00
a,
b,
c,
}
"#,
)
}
2021-07-30 21:15:28 -05:00
#[test]
fn sort_trait() {
check_assist(
sort_items,
r#"
$0trait Bar$0 {
2021-07-30 23:34:54 -05:00
fn a() {
}
// comment for c
2021-07-30 21:15:28 -05:00
fn c() {}
fn z() {}
fn b() {}
}
"#,
r#"
trait Bar {
2021-07-30 23:34:54 -05:00
fn a() {
}
2021-07-30 21:15:28 -05:00
fn b() {}
2021-07-30 23:34:54 -05:00
// comment for c
2021-07-30 21:15:28 -05:00
fn c() {}
fn z() {}
}
"#,
)
}
#[test]
fn sort_impl() {
check_assist(
sort_items,
r#"
struct Bar;
$0impl Bar$0 {
2021-07-30 21:15:28 -05:00
fn c() {}
fn a() {}
2021-07-30 23:34:54 -05:00
/// long
/// doc
/// comment
2021-07-30 21:15:28 -05:00
fn z() {}
fn d() {}
}
"#,
r#"
struct Bar;
impl Bar {
fn a() {}
fn c() {}
fn d() {}
2021-07-30 23:34:54 -05:00
/// long
/// doc
/// comment
2021-07-30 21:15:28 -05:00
fn z() {}
}
"#,
)
}
2021-07-30 21:50:15 -05:00
#[test]
fn sort_struct() {
check_assist(
sort_items,
r#"
$0struct Bar$0 {
2021-07-30 21:50:15 -05:00
b: u8,
a: u32,
c: u64,
}
"#,
r#"
struct Bar {
a: u32,
b: u8,
c: u64,
}
"#,
)
}
#[test]
fn sort_struct_inside_a_function() {
check_assist(
sort_items,
r#"
fn hello() {
$0struct Bar$0 {
b: u8,
a: u32,
c: u64,
}
}
"#,
r#"
fn hello() {
struct Bar {
a: u32,
b: u8,
c: u64,
}
}
"#,
)
}
2021-07-30 21:50:15 -05:00
#[test]
fn sort_generic_struct_with_lifetime() {
check_assist(
sort_items,
r#"
$0struct Bar<'a,$0 T> {
2021-07-30 21:50:15 -05:00
d: &'a str,
b: u8,
a: T,
c: u64,
}
"#,
r#"
struct Bar<'a, T> {
a: T,
b: u8,
c: u64,
d: &'a str,
}
"#,
)
}
#[test]
fn sort_struct_fields_diff_len() {
check_assist(
sort_items,
r#"
$0struct Bar $0{
2021-07-30 21:50:15 -05:00
aaa: u8,
a: usize,
b: u8,
}
"#,
r#"
struct Bar {
a: usize,
aaa: u8,
b: u8,
2021-07-30 22:15:19 -05:00
}
"#,
)
}
#[test]
fn sort_union() {
check_assist(
sort_items,
r#"
$0union Bar$0 {
2021-07-30 22:15:19 -05:00
b: u8,
a: u32,
c: u64,
}
"#,
r#"
union Bar {
a: u32,
b: u8,
c: u64,
2021-07-30 22:37:18 -05:00
}
"#,
)
}
#[test]
fn sort_enum() {
check_assist(
sort_items,
r#"
$0enum Bar $0{
2021-07-30 22:37:18 -05:00
d{ first: u32, second: usize},
b = 14,
a,
c(u32, usize),
}
"#,
r#"
enum Bar {
a,
b = 14,
c(u32, usize),
d{ first: u32, second: usize},
2021-07-30 23:20:07 -05:00
}
"#,
)
}
#[test]
2021-08-23 12:31:54 -05:00
fn sort_struct_enum_variant_fields() {
2021-07-30 23:20:07 -05:00
check_assist(
sort_items,
r#"
enum Bar {
d$0{ second: usize, first: u32 }$0,
2021-07-30 23:20:07 -05:00
b = 14,
a,
c(u32, usize),
}
"#,
r#"
enum Bar {
d{ first: u32, second: usize },
b = 14,
a,
c(u32, usize),
2021-08-23 12:31:54 -05:00
}
"#,
)
}
#[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 },
2021-07-30 21:50:15 -05:00
}
"#,
)
}
2021-07-30 21:15:28 -05:00
}