From 643f3931f781940c0ee6c49be9f933f344d796a3 Mon Sep 17 00:00:00 2001 From: vsrs Date: Sat, 31 Jul 2021 05:15:28 +0300 Subject: [PATCH 01/11] Sort trait and impl methods --- .../ide_assists/src/handlers/reorder_impl.rs | 15 +- crates/ide_assists/src/handlers/sort_items.rs | 149 ++++++++++++++++++ crates/ide_assists/src/lib.rs | 2 + crates/ide_assists/src/utils.rs | 11 ++ 4 files changed, 164 insertions(+), 13 deletions(-) create mode 100644 crates/ide_assists/src/handlers/sort_items.rs diff --git a/crates/ide_assists/src/handlers/reorder_impl.rs b/crates/ide_assists/src/handlers/reorder_impl.rs index 5a6a9f158ea9..0ede55a6d7c0 100644 --- a/crates/ide_assists/src/handlers/reorder_impl.rs +++ b/crates/ide_assists/src/handlers/reorder_impl.rs @@ -8,7 +8,7 @@ use syntax::{ ted, AstNode, }; -use crate::{AssistContext, AssistId, AssistKind, Assists}; +use crate::{AssistContext, AssistId, AssistKind, Assists, utils::get_methods}; // Assist: reorder_impl // @@ -76,7 +76,7 @@ pub(crate) fn reorder_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<()> let target = items.syntax().text_range(); acc.add( AssistId("reorder_impl", AssistKind::RefactorRewrite), - "Sort methods", + "Sort methods by trait definition", target, |builder| { let methods = methods.into_iter().map(|fn_| builder.make_mut(fn_)).collect::>(); @@ -111,17 +111,6 @@ fn trait_definition(path: &ast::Path, sema: &Semantics) -> Option< } } -fn get_methods(items: &ast::AssocItemList) -> Vec { - items - .assoc_items() - .flat_map(|i| match i { - ast::AssocItem::Fn(f) => Some(f), - _ => None, - }) - .filter(|f| f.name().is_some()) - .collect() -} - #[cfg(test)] mod tests { use crate::tests::{check_assist, check_assist_not_applicable}; diff --git a/crates/ide_assists/src/handlers/sort_items.rs b/crates/ide_assists/src/handlers/sort_items.rs new file mode 100644 index 000000000000..d86b263c1279 --- /dev/null +++ b/crates/ide_assists/src/handlers/sort_items.rs @@ -0,0 +1,149 @@ +use std::cmp::Ordering; + +use hir::known::Option; +use itertools::Itertools; + +use syntax::{ + ast::{self, NameOwner}, + ted, AstNode, +}; + +use crate::{utils::get_methods, AssistContext, AssistId, AssistKind, Assists}; + +// Assist: sort_items +// +pub(crate) fn sort_items(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { + if let Some(trait_ast) = ctx.find_node_at_offset::() { + sort_methods_assist(acc, trait_ast.assoc_item_list()?) + } else if let Some(impl_ast) = ctx.find_node_at_offset::() { + sort_methods_assist(acc, impl_ast.assoc_item_list()?) + } else { + None + } +} + +fn sort_methods_assist(acc: &mut Assists, item_list: ast::AssocItemList) -> Option<()> { + let methods = get_methods(&item_list); + let sorted = sort_by_name(&methods); + + if methods == sorted { + cov_mark::hit!(not_applicable_if_sorted); + return None; + } + + acc.add( + AssistId("sort_items", AssistKind::RefactorRewrite), + "Sort methods alphabetically", + item_list.syntax().text_range(), + |builder| { + let methods = methods.into_iter().map(|fn_| builder.make_mut(fn_)).collect::>(); + methods + .into_iter() + .zip(sorted) + .for_each(|(old, new)| ted::replace(old.syntax(), new.clone_for_update().syntax())); + }, + ) +} + +fn sort_by_name(initial: &[T]) -> Vec { + 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_trait_sorted() { + cov_mark::check!(not_applicable_if_sorted); + + check_assist_not_applicable( + sort_items, + r#" +t$0rait Bar { + fn a() {} + fn b() {} + fn c() {} +} + "#, + ) + } + + #[test] + fn not_applicable_if_impl_sorted() { + cov_mark::check!(not_applicable_if_sorted); + + check_assist_not_applicable( + sort_items, + r#" +struct Bar; +$0impl Bar { + fn a() {} + fn b() {} + fn c() {} +} + "#, + ) + } + + #[test] + fn sort_trait() { + check_assist( + sort_items, + r#" +$0trait Bar { + fn a() {} + fn c() {} + fn z() {} + fn b() {} +} + "#, + r#" +trait Bar { + fn a() {} + fn b() {} + fn c() {} + fn z() {} +} + "#, + ) + } + + #[test] + fn sort_impl() { + check_assist( + sort_items, + r#" +struct Bar; +$0impl Bar { + fn c() {} + fn a() {} + fn z() {} + fn d() {} +} + "#, + r#" +struct Bar; +impl Bar { + fn a() {} + fn c() {} + fn d() {} + fn z() {} +} + "#, + ) + } +} diff --git a/crates/ide_assists/src/lib.rs b/crates/ide_assists/src/lib.rs index 14bf565e5656..75cf26806191 100644 --- a/crates/ide_assists/src/lib.rs +++ b/crates/ide_assists/src/lib.rs @@ -110,6 +110,7 @@ mod handlers { mod replace_qualified_name_with_use; mod replace_string_with_char; mod split_import; + mod sort_items; mod toggle_ignore; mod unmerge_use; mod unwrap_block; @@ -181,6 +182,7 @@ mod handlers { replace_impl_trait_with_generic::replace_impl_trait_with_generic, replace_let_with_if_let::replace_let_with_if_let, replace_qualified_name_with_use::replace_qualified_name_with_use, + sort_items::sort_items, split_import::split_import, toggle_ignore::toggle_ignore, unmerge_use::unmerge_use, diff --git a/crates/ide_assists/src/utils.rs b/crates/ide_assists/src/utils.rs index 81463745a461..7ebe7df1d8fe 100644 --- a/crates/ide_assists/src/utils.rs +++ b/crates/ide_assists/src/utils.rs @@ -516,3 +516,14 @@ fn ty_ctor(ty: &String, ctor: &str) -> Option { let res = ty.to_string().strip_prefix(ctor)?.strip_prefix('<')?.strip_suffix('>')?.to_string(); Some(res) } + +pub(crate) fn get_methods(items: &ast::AssocItemList) -> Vec { + items + .assoc_items() + .flat_map(|i| match i { + ast::AssocItem::Fn(f) => Some(f), + _ => None, + }) + .filter(|f| f.name().is_some()) + .collect() +} From 0694f18636a67117e142a6be9d6e51e0143dc3c0 Mon Sep 17 00:00:00 2001 From: vsrs Date: Sat, 31 Jul 2021 05:50:15 +0300 Subject: [PATCH 02/11] Sort struct fields --- crates/ide_assists/src/handlers/sort_items.rs | 120 +++++++++++++++++- 1 file changed, 117 insertions(+), 3 deletions(-) diff --git a/crates/ide_assists/src/handlers/sort_items.rs b/crates/ide_assists/src/handlers/sort_items.rs index d86b263c1279..c2144e1ad437 100644 --- a/crates/ide_assists/src/handlers/sort_items.rs +++ b/crates/ide_assists/src/handlers/sort_items.rs @@ -14,15 +14,17 @@ use crate::{utils::get_methods, AssistContext, AssistId, AssistKind, Assists}; // pub(crate) fn sort_items(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { if let Some(trait_ast) = ctx.find_node_at_offset::() { - sort_methods_assist(acc, trait_ast.assoc_item_list()?) + add_sort_methods_assist(acc, trait_ast.assoc_item_list()?) } else if let Some(impl_ast) = ctx.find_node_at_offset::() { - sort_methods_assist(acc, impl_ast.assoc_item_list()?) + add_sort_methods_assist(acc, impl_ast.assoc_item_list()?) + } else if let Some(struct_ast) = ctx.find_node_at_offset::() { + add_sort_fields_assist(acc, struct_ast.field_list()?) } else { None } } -fn sort_methods_assist(acc: &mut Assists, item_list: ast::AssocItemList) -> Option<()> { +fn add_sort_methods_assist(acc: &mut Assists, item_list: ast::AssocItemList) -> Option<()> { let methods = get_methods(&item_list); let sorted = sort_by_name(&methods); @@ -45,6 +47,36 @@ fn sort_methods_assist(acc: &mut Assists, item_list: ast::AssocItemList) -> Opti ) } +fn add_sort_fields_assist(acc: &mut Assists, field_list: ast::FieldList) -> Option<()> { + fn record_fields(field_list: &ast::FieldList) -> Option> { + match field_list { + ast::FieldList::RecordFieldList(it) => Some(it.fields().collect()), + ast::FieldList::TupleFieldList(_) => None, + } + } + + let fields = record_fields(&field_list)?; + let sorted = sort_by_name(&fields); + + if fields == sorted { + cov_mark::hit!(not_applicable_if_sorted); + return None; + } + + acc.add( + AssistId("sort_items", AssistKind::RefactorRewrite), + "Sort methods alphabetically", + field_list.syntax().text_range(), + |builder| { + let methods = fields.into_iter().map(|fn_| builder.make_mut(fn_)).collect::>(); + methods + .into_iter() + .zip(sorted) + .for_each(|(old, new)| ted::replace(old.syntax(), new.clone_for_update().syntax())); + }, + ) +} + fn sort_by_name(initial: &[T]) -> Vec { initial .iter() @@ -99,6 +131,22 @@ $0impl Bar { ) } + #[test] + fn not_applicable_if_struct_sorted() { + cov_mark::check!(not_applicable_if_sorted); + + check_assist_not_applicable( + sort_items, + r#" +$0struct Bar { + a: u32, + b: u8, + c: u64, +} + "#, + ) + } + #[test] fn sort_trait() { check_assist( @@ -146,4 +194,70 @@ impl Bar { "#, ) } + + #[test] + fn sort_struct() { + check_assist( + sort_items, + r#" +$0struct Bar { + b: u8, + a: u32, + c: u64, +} + "#, + r#" +struct Bar { + a: u32, + b: u8, + c: u64, +} + "#, + ) + } + + #[test] + fn sort_generic_struct_with_lifetime() { + check_assist( + sort_items, + r#" +$0struct Bar<'a, T> { + 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 { + aaa: u8, + a: usize, + b: u8, +} + "#, + r#" +struct Bar { + a: usize, + aaa: u8, + b: u8, +} + "#, + ) + } + } From b31bf74c13ccbd7f07dd74f03fe2dce29f058d44 Mon Sep 17 00:00:00 2001 From: vsrs Date: Sat, 31 Jul 2021 05:52:03 +0300 Subject: [PATCH 03/11] cargo fmt --- crates/ide_assists/src/handlers/reorder_impl.rs | 2 +- crates/ide_assists/src/handlers/sort_items.rs | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/ide_assists/src/handlers/reorder_impl.rs b/crates/ide_assists/src/handlers/reorder_impl.rs index 0ede55a6d7c0..17d419cee649 100644 --- a/crates/ide_assists/src/handlers/reorder_impl.rs +++ b/crates/ide_assists/src/handlers/reorder_impl.rs @@ -8,7 +8,7 @@ use syntax::{ ted, AstNode, }; -use crate::{AssistContext, AssistId, AssistKind, Assists, utils::get_methods}; +use crate::{utils::get_methods, AssistContext, AssistId, AssistKind, Assists}; // Assist: reorder_impl // diff --git a/crates/ide_assists/src/handlers/sort_items.rs b/crates/ide_assists/src/handlers/sort_items.rs index c2144e1ad437..83d42812c12c 100644 --- a/crates/ide_assists/src/handlers/sort_items.rs +++ b/crates/ide_assists/src/handlers/sort_items.rs @@ -259,5 +259,4 @@ struct Bar { "#, ) } - } From 2c7e99b2099175bb7b141f67ba2779c80a78a6a1 Mon Sep 17 00:00:00 2001 From: vsrs Date: Sat, 31 Jul 2021 06:15:19 +0300 Subject: [PATCH 04/11] Sort unions --- crates/ide_assists/src/handlers/sort_items.rs | 118 ++++++++++++------ 1 file changed, 83 insertions(+), 35 deletions(-) diff --git a/crates/ide_assists/src/handlers/sort_items.rs b/crates/ide_assists/src/handlers/sort_items.rs index 83d42812c12c..d264ce6eeb97 100644 --- a/crates/ide_assists/src/handlers/sort_items.rs +++ b/crates/ide_assists/src/handlers/sort_items.rs @@ -1,11 +1,10 @@ use std::cmp::Ordering; -use hir::known::Option; use itertools::Itertools; use syntax::{ ast::{self, NameOwner}, - ted, AstNode, + ted, AstNode, TextRange, }; use crate::{utils::get_methods, AssistContext, AssistId, AssistKind, Assists}; @@ -18,12 +17,45 @@ pub(crate) fn sort_items(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { } else if let Some(impl_ast) = ctx.find_node_at_offset::() { add_sort_methods_assist(acc, impl_ast.assoc_item_list()?) } else if let Some(struct_ast) = ctx.find_node_at_offset::() { - add_sort_fields_assist(acc, struct_ast.field_list()?) + match struct_ast.field_list() { + Some(ast::FieldList::RecordFieldList(it)) => add_sort_fields_assist(acc, it), + _ => None, + } + } else if let Some(union_ast) = ctx.find_node_at_offset::() { + add_sort_fields_assist(acc, union_ast.record_field_list()?) } else { None } } +trait AddRewrite { + fn add_rewrite( + &mut self, + label: &str, + old: Vec, + new: Vec, + target: TextRange, + ) -> Option<()>; +} + +impl AddRewrite for Assists { + fn add_rewrite( + &mut self, + label: &str, + old: Vec, + new: Vec, + target: TextRange, + ) -> Option<()> { + self.add(AssistId("sort_items", AssistKind::RefactorRewrite), label, target, |builder| { + let mutable: Vec<_> = old.into_iter().map(|it| builder.make_mut(it)).collect(); + mutable + .into_iter() + .zip(new) + .for_each(|(old, new)| ted::replace(old.syntax(), new.clone_for_update().syntax())); + }) + } +} + fn add_sort_methods_assist(acc: &mut Assists, item_list: ast::AssocItemList) -> Option<()> { let methods = get_methods(&item_list); let sorted = sort_by_name(&methods); @@ -33,29 +65,14 @@ fn add_sort_methods_assist(acc: &mut Assists, item_list: ast::AssocItemList) -> return None; } - acc.add( - AssistId("sort_items", AssistKind::RefactorRewrite), - "Sort methods alphabetically", - item_list.syntax().text_range(), - |builder| { - let methods = methods.into_iter().map(|fn_| builder.make_mut(fn_)).collect::>(); - methods - .into_iter() - .zip(sorted) - .for_each(|(old, new)| ted::replace(old.syntax(), new.clone_for_update().syntax())); - }, - ) + acc.add_rewrite("Sort methods alphabetically", methods, sorted, item_list.syntax().text_range()) } -fn add_sort_fields_assist(acc: &mut Assists, field_list: ast::FieldList) -> Option<()> { - fn record_fields(field_list: &ast::FieldList) -> Option> { - match field_list { - ast::FieldList::RecordFieldList(it) => Some(it.fields().collect()), - ast::FieldList::TupleFieldList(_) => None, - } - } - - let fields = record_fields(&field_list)?; +fn add_sort_fields_assist( + acc: &mut Assists, + record_field_list: ast::RecordFieldList, +) -> Option<()> { + let fields: Vec<_> = record_field_list.fields().collect(); let sorted = sort_by_name(&fields); if fields == sorted { @@ -63,17 +80,11 @@ fn add_sort_fields_assist(acc: &mut Assists, field_list: ast::FieldList) -> Opti return None; } - acc.add( - AssistId("sort_items", AssistKind::RefactorRewrite), - "Sort methods alphabetically", - field_list.syntax().text_range(), - |builder| { - let methods = fields.into_iter().map(|fn_| builder.make_mut(fn_)).collect::>(); - methods - .into_iter() - .zip(sorted) - .for_each(|(old, new)| ted::replace(old.syntax(), new.clone_for_update().syntax())); - }, + acc.add_rewrite( + "Sort fields alphabetically", + fields, + sorted, + record_field_list.syntax().text_range(), ) } @@ -147,6 +158,22 @@ $0struct Bar { ) } + #[test] + fn not_applicable_if_union_sorted() { + cov_mark::check!(not_applicable_if_sorted); + + check_assist_not_applicable( + sort_items, + r#" +$0union Bar { + a: u32, + b: u8, + c: u64, +} + "#, + ) + } + #[test] fn sort_trait() { check_assist( @@ -255,6 +282,27 @@ struct Bar { a: usize, aaa: u8, b: u8, +} + "#, + ) + } + + #[test] + fn sort_union() { + check_assist( + sort_items, + r#" +$0union Bar { + b: u8, + a: u32, + c: u64, +} + "#, + r#" +union Bar { + a: u32, + b: u8, + c: u64, } "#, ) From 6f8888cfe70007878deb1e38e58c299e69e8e259 Mon Sep 17 00:00:00 2001 From: vsrs Date: Sat, 31 Jul 2021 06:37:18 +0300 Subject: [PATCH 05/11] Sort enums --- crates/ide_assists/src/handlers/sort_items.rs | 138 +++++++++++++++++- 1 file changed, 131 insertions(+), 7 deletions(-) diff --git a/crates/ide_assists/src/handlers/sort_items.rs b/crates/ide_assists/src/handlers/sort_items.rs index d264ce6eeb97..7f8eeec01a74 100644 --- a/crates/ide_assists/src/handlers/sort_items.rs +++ b/crates/ide_assists/src/handlers/sort_items.rs @@ -19,10 +19,15 @@ pub(crate) fn sort_items(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { } else if let Some(struct_ast) = ctx.find_node_at_offset::() { match struct_ast.field_list() { Some(ast::FieldList::RecordFieldList(it)) => add_sort_fields_assist(acc, it), - _ => None, + _ => { + cov_mark::hit!(not_applicable_if_sorted_or_empty_or_single); + None + } } } else if let Some(union_ast) = ctx.find_node_at_offset::() { add_sort_fields_assist(acc, union_ast.record_field_list()?) + } else if let Some(enum_ast) = ctx.find_node_at_offset::() { + add_sort_variants_assist(acc, enum_ast.variant_list()?) } else { None } @@ -61,7 +66,7 @@ fn add_sort_methods_assist(acc: &mut Assists, item_list: ast::AssocItemList) -> let sorted = sort_by_name(&methods); if methods == sorted { - cov_mark::hit!(not_applicable_if_sorted); + cov_mark::hit!(not_applicable_if_sorted_or_empty_or_single); return None; } @@ -76,7 +81,7 @@ fn add_sort_fields_assist( let sorted = sort_by_name(&fields); if fields == sorted { - cov_mark::hit!(not_applicable_if_sorted); + cov_mark::hit!(not_applicable_if_sorted_or_empty_or_single); return None; } @@ -88,6 +93,23 @@ fn add_sort_fields_assist( ) } +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(), + ) +} + fn sort_by_name(initial: &[T]) -> Vec { initial .iter() @@ -109,9 +131,72 @@ mod tests { use super::*; + #[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 { +} + "#, + ) + } + + #[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 { +} + "#, + ) + } + + #[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; + "#, + ) + } + + #[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 { }; + "#, + ) + } + + #[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 {}; + "#, + ) + } + #[test] fn not_applicable_if_trait_sorted() { - cov_mark::check!(not_applicable_if_sorted); + cov_mark::check!(not_applicable_if_sorted_or_empty_or_single); check_assist_not_applicable( sort_items, @@ -127,7 +212,7 @@ t$0rait Bar { #[test] fn not_applicable_if_impl_sorted() { - cov_mark::check!(not_applicable_if_sorted); + cov_mark::check!(not_applicable_if_sorted_or_empty_or_single); check_assist_not_applicable( sort_items, @@ -144,7 +229,7 @@ $0impl Bar { #[test] fn not_applicable_if_struct_sorted() { - cov_mark::check!(not_applicable_if_sorted); + cov_mark::check!(not_applicable_if_sorted_or_empty_or_single); check_assist_not_applicable( sort_items, @@ -160,7 +245,7 @@ $0struct Bar { #[test] fn not_applicable_if_union_sorted() { - cov_mark::check!(not_applicable_if_sorted); + cov_mark::check!(not_applicable_if_sorted_or_empty_or_single); check_assist_not_applicable( sort_items, @@ -174,6 +259,22 @@ $0union Bar { ) } + #[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 { + a, + b, + c, +} + "#, + ) + } + #[test] fn sort_trait() { check_assist( @@ -303,6 +404,29 @@ union Bar { a: u32, b: u8, c: u64, +} + "#, + ) + } + + #[test] + fn sort_enum() { + check_assist( + sort_items, + r#" +$0enum Bar { + 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}, } "#, ) From 4fa66f1e1eb24a30d298c2f78efa402fbc92e662 Mon Sep 17 00:00:00 2001 From: vsrs Date: Sat, 31 Jul 2021 07:20:07 +0300 Subject: [PATCH 06/11] Sort struct enum variant fields --- crates/ide_assists/src/handlers/sort_items.rs | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/crates/ide_assists/src/handlers/sort_items.rs b/crates/ide_assists/src/handlers/sort_items.rs index 7f8eeec01a74..be6fc51c4751 100644 --- a/crates/ide_assists/src/handlers/sort_items.rs +++ b/crates/ide_assists/src/handlers/sort_items.rs @@ -26,6 +26,10 @@ pub(crate) fn sort_items(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { } } else if let Some(union_ast) = ctx.find_node_at_offset::() { add_sort_fields_assist(acc, union_ast.record_field_list()?) + } else if let Some(enum_struct_variant_ast) = ctx.find_node_at_offset::() + { + // should be above enum and below struct + add_sort_fields_assist(acc, enum_struct_variant_ast) } else if let Some(enum_ast) = ctx.find_node_at_offset::() { add_sort_variants_assist(acc, enum_ast.variant_list()?) } else { @@ -427,6 +431,29 @@ enum Bar { b = 14, c(u32, usize), d{ first: u32, second: usize}, +} + "#, + ) + } + + #[test] + fn sort_struct_enum_variant() { + check_assist( + sort_items, + r#" +enum Bar { + d$0{ second: usize, first: u32 }, + b = 14, + a, + c(u32, usize), +} + "#, + r#" +enum Bar { + d{ first: u32, second: usize }, + b = 14, + a, + c(u32, usize), } "#, ) From 580e5e277a33a9bc0337eb3b7bebe411446dc24c Mon Sep 17 00:00:00 2001 From: vsrs Date: Sat, 31 Jul 2021 07:34:54 +0300 Subject: [PATCH 07/11] Some comment tests --- crates/ide_assists/src/handlers/sort_items.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/crates/ide_assists/src/handlers/sort_items.rs b/crates/ide_assists/src/handlers/sort_items.rs index be6fc51c4751..7ceadd06c003 100644 --- a/crates/ide_assists/src/handlers/sort_items.rs +++ b/crates/ide_assists/src/handlers/sort_items.rs @@ -285,7 +285,11 @@ $0enum Bar { sort_items, r#" $0trait Bar { - fn a() {} + fn a() { + + } + + // comment for c fn c() {} fn z() {} fn b() {} @@ -293,8 +297,12 @@ $0trait Bar { "#, r#" trait Bar { - fn a() {} + fn a() { + + } + fn b() {} + // comment for c fn c() {} fn z() {} } @@ -311,6 +319,9 @@ struct Bar; $0impl Bar { fn c() {} fn a() {} + /// long + /// doc + /// comment fn z() {} fn d() {} } @@ -321,6 +332,9 @@ impl Bar { fn a() {} fn c() {} fn d() {} + /// long + /// doc + /// comment fn z() {} } "#, From 0088f84c8841d8dcd392c89367c06baa5aee0736 Mon Sep 17 00:00:00 2001 From: vsrs Date: Sat, 31 Jul 2021 09:52:02 +0300 Subject: [PATCH 08/11] Allow several samples in "// Assist:" comments. --- .../src/handlers/introduce_named_lifetime.rs | 4 +- .../src/handlers/reorder_fields.rs | 1 - .../ide_assists/src/handlers/reorder_impl.rs | 1 - crates/ide_assists/src/handlers/sort_items.rs | 75 ++++++++++++++- crates/ide_assists/src/tests/generated.rs | 92 +++++++++++++++++++ crates/ide_assists/src/tests/sourcegen.rs | 92 +++++++++++-------- 6 files changed, 223 insertions(+), 42 deletions(-) diff --git a/crates/ide_assists/src/handlers/introduce_named_lifetime.rs b/crates/ide_assists/src/handlers/introduce_named_lifetime.rs index 16f8f9d70ba6..aa32c698a2b0 100644 --- a/crates/ide_assists/src/handlers/introduce_named_lifetime.rs +++ b/crates/ide_assists/src/handlers/introduce_named_lifetime.rs @@ -33,9 +33,9 @@ static ASSIST_LABEL: &str = "Introduce named lifetime"; // } // } // ``` -// FIXME: How can we handle renaming any one of multiple anonymous lifetimes? -// FIXME: should also add support for the case fun(f: &Foo) -> &$0Foo pub(crate) fn introduce_named_lifetime(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { + // FIXME: How can we handle renaming any one of multiple anonymous lifetimes? + // FIXME: should also add support for the case fun(f: &Foo) -> &$0Foo let lifetime = ctx.find_node_at_offset::().filter(|lifetime| lifetime.text() == "'_")?; let lifetime_loc = lifetime.lifetime_ident_token()?.text_range(); diff --git a/crates/ide_assists/src/handlers/reorder_fields.rs b/crates/ide_assists/src/handlers/reorder_fields.rs index f6a926042c13..cd4eb7c15e90 100644 --- a/crates/ide_assists/src/handlers/reorder_fields.rs +++ b/crates/ide_assists/src/handlers/reorder_fields.rs @@ -20,7 +20,6 @@ use crate::{AssistContext, AssistId, AssistKind, Assists}; // 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::() diff --git a/crates/ide_assists/src/handlers/reorder_impl.rs b/crates/ide_assists/src/handlers/reorder_impl.rs index 17d419cee649..d398373c3413 100644 --- a/crates/ide_assists/src/handlers/reorder_impl.rs +++ b/crates/ide_assists/src/handlers/reorder_impl.rs @@ -44,7 +44,6 @@ use crate::{utils::get_methods, AssistContext, AssistId, AssistKind, Assists}; // fn c() {} // } // ``` -// pub(crate) fn reorder_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { let impl_ast = ctx.find_node_at_offset::()?; let items = impl_ast.assoc_item_list()?; diff --git a/crates/ide_assists/src/handlers/sort_items.rs b/crates/ide_assists/src/handlers/sort_items.rs index 7ceadd06c003..75e501d3dd3f 100644 --- a/crates/ide_assists/src/handlers/sort_items.rs +++ b/crates/ide_assists/src/handlers/sort_items.rs @@ -11,6 +11,77 @@ use crate::{utils::get_methods, AssistContext, AssistId, AssistKind, Assists}; // Assist: sort_items // +// Sorts item members alphabetically: fields, enum variants and methods. +// +// ``` +// struct $0Foo { second: u32, first: String } +// ``` +// -> +// ``` +// struct Foo { first: String, second: u32 } +// ``` +// --- +// ``` +// trait $0Bar { +// fn second(&self) -> u32; +// fn first(&self) -> String; +// } +// ``` +// -> +// ``` +// trait Bar { +// fn first(&self) -> String; +// fn second(&self) -> u32; +// } +// ``` +// --- +// ``` +// struct Baz; +// impl $0Baz { +// 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: +// +// ``` +// en$0um Animal { +// Dog(String, f64), +// Cat { weight: f64, name: String }, +// } +// ``` +// -> +// ``` +// enum Animal { +// // variants sorted +// Cat { weight: f64, name: String }, +// Dog(String, f64), +// } +// ``` +// and sorting a single enum struct variant: +// +// ``` +// enum Animal { +// Dog(String, f64), +// Cat {$0 weight: f64, name: String }, +// } +// ``` +// -> +// ``` +// enum Animal { +// Dog(String, f64), +// Cat { name: String, weight: f64 }, // Cat fields sorted +// } +// ``` pub(crate) fn sort_items(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { if let Some(trait_ast) = ctx.find_node_at_offset::() { add_sort_methods_assist(acc, trait_ast.assoc_item_list()?) @@ -155,7 +226,7 @@ t$0rait Bar { check_assist_not_applicable( sort_items, r#" -struct Bar; +struct Bar; $0impl Bar { } "#, @@ -221,7 +292,7 @@ t$0rait Bar { check_assist_not_applicable( sort_items, r#" -struct Bar; +struct Bar; $0impl Bar { fn a() {} fn b() {} diff --git a/crates/ide_assists/src/tests/generated.rs b/crates/ide_assists/src/tests/generated.rs index ebf312aa3f8e..7ed89ce88a17 100644 --- a/crates/ide_assists/src/tests/generated.rs +++ b/crates/ide_assists/src/tests/generated.rs @@ -1523,6 +1523,98 @@ fn main() { ) } +#[test] +fn doctest_sort_items() { + check_doc_test( + "sort_items", + r#####" +struct $0Foo { second: u32, first: String } +"#####, + r#####" +struct Foo { first: String, second: u32 } +"#####, + ) +} + +#[test] +fn doctest_sort_items_1() { + check_doc_test( + "sort_items_1", + r#####" +trait $0Bar { + fn second(&self) -> u32; + fn first(&self) -> String; +} +"#####, + r#####" +trait Bar { + fn first(&self) -> String; + fn second(&self) -> u32; +} +"#####, + ) +} + +#[test] +fn doctest_sort_items_2() { + check_doc_test( + "sort_items_2", + r#####" +struct Baz; +impl $0Baz { + fn second(&self) -> u32; + fn first(&self) -> String; +} +"#####, + r#####" +struct Baz; +impl Baz { + fn first(&self) -> String; + fn second(&self) -> u32; +} +"#####, + ) +} + +#[test] +fn doctest_sort_items_3() { + check_doc_test( + "sort_items_3", + r#####" +en$0um Animal { + Dog(String, f64), + Cat { weight: f64, name: String }, +} +"#####, + r#####" +enum Animal { + // variants sorted + Cat { weight: f64, name: String }, + Dog(String, f64), +} +"#####, + ) +} + +#[test] +fn doctest_sort_items_4() { + check_doc_test( + "sort_items_4", + r#####" +enum Animal { + Dog(String, f64), + Cat {$0 weight: f64, name: String }, +} +"#####, + r#####" +enum Animal { + Dog(String, f64), + Cat { name: String, weight: f64 }, // Cat fields sorted +} +"#####, + ) +} + #[test] fn doctest_split_import() { check_doc_test( diff --git a/crates/ide_assists/src/tests/sourcegen.rs b/crates/ide_assists/src/tests/sourcegen.rs index bf29c5e536f2..46c71048dd99 100644 --- a/crates/ide_assists/src/tests/sourcegen.rs +++ b/crates/ide_assists/src/tests/sourcegen.rs @@ -16,7 +16,9 @@ use super::check_doc_test; " .to_string(); for assist in assists.iter() { - let test = format!( + for (idx, section) in assist.sections.iter().enumerate() { + let id = if idx == 0 { assist.id.clone() } else { format!("{}_{}", &assist.id, idx)}; + let test = format!( r######" #[test] fn doctest_{}() {{ @@ -27,13 +29,14 @@ r#####" {}"#####) }} "######, - assist.id, - assist.id, - reveal_hash_comments(&assist.before), - reveal_hash_comments(&assist.after) - ); - - buf.push_str(&test) + &id, + &id, + reveal_hash_comments(§ion.before), + reveal_hash_comments(§ion.after) + ); + + buf.push_str(&test) + } } let buf = sourcegen::add_preamble("sourcegen_assists_docs", sourcegen::reformat(buf)); sourcegen::ensure_file_contents( @@ -55,14 +58,17 @@ r#####" fs::write(dst, contents).unwrap(); } } +#[derive(Debug)]struct Section { + doc: String, + before: String, + after: String, +} #[derive(Debug)] struct Assist { id: String, location: sourcegen::Location, - doc: String, - before: String, - after: String, + sections: Vec
} impl Assist { @@ -89,22 +95,28 @@ impl Assist { "invalid assist id: {:?}", id ); - let mut lines = block.contents.iter(); - - let doc = take_until(lines.by_ref(), "```").trim().to_string(); - assert!( - doc.chars().next().unwrap().is_ascii_uppercase() && doc.ends_with('.'), - "\n\n{}: assist docs should be proper sentences, with capitalization and a full stop at the end.\n\n{}\n\n", - id, doc, - ); - - let before = take_until(lines.by_ref(), "```"); - - assert_eq!(lines.next().unwrap().as_str(), "->"); - assert_eq!(lines.next().unwrap().as_str(), "```"); - let after = take_until(lines.by_ref(), "```"); + let mut lines = block.contents.iter().peekable(); let location = sourcegen::Location { file: path.to_path_buf(), line: block.line }; - acc.push(Assist { id, location, doc, before, after }) + let mut assist = Assist { id, location, sections: Vec::new() }; + + while lines.peek().is_some() { + let doc = take_until(lines.by_ref(), "```").trim().to_string(); + assert!( + (doc.chars().next().unwrap().is_ascii_uppercase() && doc.ends_with('.')) || assist.sections.len() > 0, + "\n\n{}: assist docs should be proper sentences, with capitalization and a full stop at the end.\n\n{}\n\n", + &assist.id, doc, + ); + + let before = take_until(lines.by_ref(), "```"); + + assert_eq!(lines.next().unwrap().as_str(), "->"); + assert_eq!(lines.next().unwrap().as_str(), "```"); + let after = take_until(lines.by_ref(), "```"); + + assist.sections.push(Section{doc, before, after}); + } + + acc.push(assist) } } @@ -123,13 +135,20 @@ impl Assist { impl fmt::Display for Assist { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let before = self.before.replace("$0", "┃"); // Unicode pseudo-graphics bar - let after = self.after.replace("$0", "┃"); - writeln!( + let _ = writeln!( f, "[discrete]\n=== `{}` -**Source:** {} +**Source:** {}", + self.id, + self.location, + ); + for section in &self.sections { + let before = section.before.replace("$0", "┃"); // Unicode pseudo-graphics bar + let after = section.after.replace("$0", "┃"); + let _= writeln!( + f, +" {} .Before @@ -139,12 +158,13 @@ impl fmt::Display for Assist { .After ```rust {}```", - self.id, - self.location, - self.doc, - hide_hash_comments(&before), - hide_hash_comments(&after) - ) + section.doc, + hide_hash_comments(&before), + hide_hash_comments(&after) + ); + } + + Ok(()) } } From e71b239d377135bf940c574865f409e40200333c Mon Sep 17 00:00:00 2001 From: vsrs Date: Sat, 31 Jul 2021 10:18:38 +0300 Subject: [PATCH 09/11] fix generated tests --- crates/ide_assists/src/handlers/sort_items.rs | 3 +- crates/ide_assists/src/tests/generated.rs | 11 +++--- crates/ide_assists/src/tests/sourcegen.rs | 35 ++++++++++--------- 3 files changed, 24 insertions(+), 25 deletions(-) diff --git a/crates/ide_assists/src/handlers/sort_items.rs b/crates/ide_assists/src/handlers/sort_items.rs index 75e501d3dd3f..3a3afa7e07dc 100644 --- a/crates/ide_assists/src/handlers/sort_items.rs +++ b/crates/ide_assists/src/handlers/sort_items.rs @@ -62,7 +62,6 @@ use crate::{utils::get_methods, AssistContext, AssistId, AssistKind, Assists}; // -> // ``` // enum Animal { -// // variants sorted // Cat { weight: f64, name: String }, // Dog(String, f64), // } @@ -79,7 +78,7 @@ use crate::{utils::get_methods, AssistContext, AssistId, AssistKind, Assists}; // ``` // enum Animal { // Dog(String, f64), -// Cat { name: String, weight: f64 }, // Cat fields sorted +// Cat { name: String, weight: f64 }, // } // ``` pub(crate) fn sort_items(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { diff --git a/crates/ide_assists/src/tests/generated.rs b/crates/ide_assists/src/tests/generated.rs index 7ed89ce88a17..1d9d14d94164 100644 --- a/crates/ide_assists/src/tests/generated.rs +++ b/crates/ide_assists/src/tests/generated.rs @@ -1539,7 +1539,7 @@ struct Foo { first: String, second: u32 } #[test] fn doctest_sort_items_1() { check_doc_test( - "sort_items_1", + "sort_items", r#####" trait $0Bar { fn second(&self) -> u32; @@ -1558,7 +1558,7 @@ trait Bar { #[test] fn doctest_sort_items_2() { check_doc_test( - "sort_items_2", + "sort_items", r#####" struct Baz; impl $0Baz { @@ -1579,7 +1579,7 @@ impl Baz { #[test] fn doctest_sort_items_3() { check_doc_test( - "sort_items_3", + "sort_items", r#####" en$0um Animal { Dog(String, f64), @@ -1588,7 +1588,6 @@ en$0um Animal { "#####, r#####" enum Animal { - // variants sorted Cat { weight: f64, name: String }, Dog(String, f64), } @@ -1599,7 +1598,7 @@ enum Animal { #[test] fn doctest_sort_items_4() { check_doc_test( - "sort_items_4", + "sort_items", r#####" enum Animal { Dog(String, f64), @@ -1609,7 +1608,7 @@ enum Animal { r#####" enum Animal { Dog(String, f64), - Cat { name: String, weight: f64 }, // Cat fields sorted + Cat { name: String, weight: f64 }, } "#####, ) diff --git a/crates/ide_assists/src/tests/sourcegen.rs b/crates/ide_assists/src/tests/sourcegen.rs index 46c71048dd99..2af1de66fadb 100644 --- a/crates/ide_assists/src/tests/sourcegen.rs +++ b/crates/ide_assists/src/tests/sourcegen.rs @@ -17,9 +17,10 @@ use super::check_doc_test; .to_string(); for assist in assists.iter() { for (idx, section) in assist.sections.iter().enumerate() { - let id = if idx == 0 { assist.id.clone() } else { format!("{}_{}", &assist.id, idx)}; + let test_id = + if idx == 0 { assist.id.clone() } else { format!("{}_{}", &assist.id, idx) }; let test = format!( - r######" + r######" #[test] fn doctest_{}() {{ check_doc_test( @@ -29,13 +30,13 @@ r#####" {}"#####) }} "######, - &id, - &id, + &test_id, + &assist.id, reveal_hash_comments(§ion.before), reveal_hash_comments(§ion.after) ); - - buf.push_str(&test) + + buf.push_str(&test) } } let buf = sourcegen::add_preamble("sourcegen_assists_docs", sourcegen::reformat(buf)); @@ -58,7 +59,8 @@ r#####" fs::write(dst, contents).unwrap(); } } -#[derive(Debug)]struct Section { +#[derive(Debug)] +struct Section { doc: String, before: String, after: String, @@ -68,7 +70,7 @@ r#####" struct Assist { id: String, location: sourcegen::Location, - sections: Vec
+ sections: Vec
, } impl Assist { @@ -106,14 +108,14 @@ impl Assist { "\n\n{}: assist docs should be proper sentences, with capitalization and a full stop at the end.\n\n{}\n\n", &assist.id, doc, ); - + let before = take_until(lines.by_ref(), "```"); - + assert_eq!(lines.next().unwrap().as_str(), "->"); assert_eq!(lines.next().unwrap().as_str(), "```"); let after = take_until(lines.by_ref(), "```"); - assist.sections.push(Section{doc, before, after}); + assist.sections.push(Section { doc, before, after }); } acc.push(assist) @@ -139,16 +141,15 @@ impl fmt::Display for Assist { f, "[discrete]\n=== `{}` **Source:** {}", - self.id, - self.location, - ); + self.id, self.location, + ); for section in &self.sections { let before = section.before.replace("$0", "┃"); // Unicode pseudo-graphics bar let after = section.after.replace("$0", "┃"); - let _= writeln!( + let _ = writeln!( f, -" + " {} .Before @@ -161,7 +162,7 @@ impl fmt::Display for Assist { section.doc, hide_hash_comments(&before), hide_hash_comments(&after) - ); + ); } Ok(()) From 6031dee145ef2e31f11460d7bc38075411ff5738 Mon Sep 17 00:00:00 2001 From: vsrs Date: Mon, 2 Aug 2021 19:58:41 +0300 Subject: [PATCH 10/11] Enable assist only if an item is selected --- crates/ide_assists/src/handlers/sort_items.rs | 56 +++++++++++++------ 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/crates/ide_assists/src/handlers/sort_items.rs b/crates/ide_assists/src/handlers/sort_items.rs index 3a3afa7e07dc..f35165a0fca6 100644 --- a/crates/ide_assists/src/handlers/sort_items.rs +++ b/crates/ide_assists/src/handlers/sort_items.rs @@ -82,6 +82,11 @@ use crate::{utils::get_methods, AssistContext, AssistId, AssistKind, Assists}; // } // ``` pub(crate) fn sort_items(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { + if ctx.frange.range.is_empty() { + cov_mark::hit!(not_applicable_if_no_selection); + return None; + } + if let Some(trait_ast) = ctx.find_node_at_offset::() { add_sort_methods_assist(acc, trait_ast.assoc_item_list()?) } else if let Some(impl_ast) = ctx.find_node_at_offset::() { @@ -205,6 +210,21 @@ mod tests { use super::*; + #[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_trait_empty() { cov_mark::check!(not_applicable_if_sorted_or_empty_or_single); @@ -212,7 +232,7 @@ mod tests { check_assist_not_applicable( sort_items, r#" -t$0rait Bar { +t$0rait Bar$0 { } "#, ) @@ -226,7 +246,7 @@ t$0rait Bar { sort_items, r#" struct Bar; -$0impl Bar { +$0impl Bar$0 { } "#, ) @@ -239,7 +259,7 @@ $0impl Bar { check_assist_not_applicable( sort_items, r#" -$0struct Bar; +$0struct Bar$0 ; "#, ) } @@ -251,7 +271,7 @@ $0struct Bar; check_assist_not_applicable( sort_items, r#" -$0struct Bar { }; +$0struct Bar$0 { }; "#, ) } @@ -263,7 +283,7 @@ $0struct Bar { }; check_assist_not_applicable( sort_items, r#" -$0enum ZeroVariants {}; +$0enum ZeroVariants$0 {}; "#, ) } @@ -275,7 +295,7 @@ $0enum ZeroVariants {}; check_assist_not_applicable( sort_items, r#" -t$0rait Bar { +t$0rait Bar$0 { fn a() {} fn b() {} fn c() {} @@ -292,7 +312,7 @@ t$0rait Bar { sort_items, r#" struct Bar; -$0impl Bar { +$0impl Bar$0 { fn a() {} fn b() {} fn c() {} @@ -308,7 +328,7 @@ $0impl Bar { check_assist_not_applicable( sort_items, r#" -$0struct Bar { +$0struct Bar$0 { a: u32, b: u8, c: u64, @@ -324,7 +344,7 @@ $0struct Bar { check_assist_not_applicable( sort_items, r#" -$0union Bar { +$0union Bar$0 { a: u32, b: u8, c: u64, @@ -340,7 +360,7 @@ $0union Bar { check_assist_not_applicable( sort_items, r#" -$0enum Bar { +$0enum Bar$0 { a, b, c, @@ -354,7 +374,7 @@ $0enum Bar { check_assist( sort_items, r#" -$0trait Bar { +$0trait Bar$0 { fn a() { } @@ -386,7 +406,7 @@ trait Bar { sort_items, r#" struct Bar; -$0impl Bar { +$0impl Bar$0 { fn c() {} fn a() {} /// long @@ -416,7 +436,7 @@ impl Bar { check_assist( sort_items, r#" -$0struct Bar { +$0struct Bar$0 { b: u8, a: u32, c: u64, @@ -437,7 +457,7 @@ struct Bar { check_assist( sort_items, r#" -$0struct Bar<'a, T> { +$0struct Bar<'a,$0 T> { d: &'a str, b: u8, a: T, @@ -460,7 +480,7 @@ struct Bar<'a, T> { check_assist( sort_items, r#" -$0struct Bar { +$0struct Bar $0{ aaa: u8, a: usize, b: u8, @@ -481,7 +501,7 @@ struct Bar { check_assist( sort_items, r#" -$0union Bar { +$0union Bar$0 { b: u8, a: u32, c: u64, @@ -502,7 +522,7 @@ union Bar { check_assist( sort_items, r#" -$0enum Bar { +$0enum Bar $0{ d{ first: u32, second: usize}, b = 14, a, @@ -526,7 +546,7 @@ enum Bar { sort_items, r#" enum Bar { - d$0{ second: usize, first: u32 }, + d$0{ second: usize, first: u32 }$0, b = 14, a, c(u32, usize), From cc33b0dc82c119434967aa9609e6a7a08e38cd14 Mon Sep 17 00:00:00 2001 From: vsrs Date: Mon, 2 Aug 2021 20:22:45 +0300 Subject: [PATCH 11/11] Update the docs --- crates/ide_assists/src/handlers/sort_items.rs | 10 +++++----- crates/ide_assists/src/tests/generated.rs | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/crates/ide_assists/src/handlers/sort_items.rs b/crates/ide_assists/src/handlers/sort_items.rs index f35165a0fca6..2ca4a19ec409 100644 --- a/crates/ide_assists/src/handlers/sort_items.rs +++ b/crates/ide_assists/src/handlers/sort_items.rs @@ -14,7 +14,7 @@ use crate::{utils::get_methods, AssistContext, AssistId, AssistKind, Assists}; // Sorts item members alphabetically: fields, enum variants and methods. // // ``` -// struct $0Foo { second: u32, first: String } +// struct $0Foo$0 { second: u32, first: String } // ``` // -> // ``` @@ -22,7 +22,7 @@ use crate::{utils::get_methods, AssistContext, AssistId, AssistKind, Assists}; // ``` // --- // ``` -// trait $0Bar { +// trait $0Bar$0 { // fn second(&self) -> u32; // fn first(&self) -> String; // } @@ -37,7 +37,7 @@ use crate::{utils::get_methods, AssistContext, AssistId, AssistKind, Assists}; // --- // ``` // struct Baz; -// impl $0Baz { +// impl $0Baz$0 { // fn second(&self) -> u32; // fn first(&self) -> String; // } @@ -54,7 +54,7 @@ use crate::{utils::get_methods, AssistContext, AssistId, AssistKind, Assists}; // There is a difference between sorting enum variants: // // ``` -// en$0um Animal { +// enum $0Animal$0 { // Dog(String, f64), // Cat { weight: f64, name: String }, // } @@ -71,7 +71,7 @@ use crate::{utils::get_methods, AssistContext, AssistId, AssistKind, Assists}; // ``` // enum Animal { // Dog(String, f64), -// Cat {$0 weight: f64, name: String }, +// Cat $0{ weight: f64, name: String }$0, // } // ``` // -> diff --git a/crates/ide_assists/src/tests/generated.rs b/crates/ide_assists/src/tests/generated.rs index 1d9d14d94164..2a652f150100 100644 --- a/crates/ide_assists/src/tests/generated.rs +++ b/crates/ide_assists/src/tests/generated.rs @@ -1528,7 +1528,7 @@ fn doctest_sort_items() { check_doc_test( "sort_items", r#####" -struct $0Foo { second: u32, first: String } +struct $0Foo$0 { second: u32, first: String } "#####, r#####" struct Foo { first: String, second: u32 } @@ -1541,7 +1541,7 @@ fn doctest_sort_items_1() { check_doc_test( "sort_items", r#####" -trait $0Bar { +trait $0Bar$0 { fn second(&self) -> u32; fn first(&self) -> String; } @@ -1561,7 +1561,7 @@ fn doctest_sort_items_2() { "sort_items", r#####" struct Baz; -impl $0Baz { +impl $0Baz$0 { fn second(&self) -> u32; fn first(&self) -> String; } @@ -1581,7 +1581,7 @@ fn doctest_sort_items_3() { check_doc_test( "sort_items", r#####" -en$0um Animal { +enum $0Animal$0 { Dog(String, f64), Cat { weight: f64, name: String }, } @@ -1602,7 +1602,7 @@ fn doctest_sort_items_4() { r#####" enum Animal { Dog(String, f64), - Cat {$0 weight: f64, name: String }, + Cat $0{ weight: f64, name: String }$0, } "#####, r#####"