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

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

1251 lines
26 KiB
Rust
Raw Normal View History

use hir::{InFile, ModuleDef};
use ide_db::{
2022-03-06 12:01:30 -06:00
helpers::mod_path_to_ast, imports::import_assets::NameToImport, items_locator,
syntax_helpers::insert_whitespace_into_node::insert_ws_into,
2021-11-24 09:21:29 -06:00
};
2020-08-12 08:04:06 -05:00
use itertools::Itertools;
2021-02-13 14:51:48 -06:00
use syntax::{
ast::{self, AstNode, HasName},
2021-11-24 09:21:29 -06:00
SyntaxKind::WHITESPACE,
2021-02-13 14:51:48 -06:00
};
2021-02-13 14:51:48 -06:00
use crate::{
assist_context::{AssistContext, Assists, SourceChangeBuilder},
2021-02-13 14:51:48 -06:00
utils::{
2021-08-10 05:21:48 -05:00
add_trait_assoc_items_to_impl, filter_assoc_items, gen_trait_fn_body,
generate_trait_impl_text, render_snippet, Cursor, DefaultMethods,
2021-02-13 14:51:48 -06:00
},
AssistId, AssistKind,
};
2020-03-19 06:36:33 -05:00
2020-11-09 06:07:18 -06:00
// Assist: replace_derive_with_manual_impl
//
2020-11-09 06:07:18 -06:00
// Converts a `derive` impl into a manual one.
//
// ```
// # //- minicore: derive
2020-11-09 06:07:18 -06:00
// # trait Debug { fn fmt(&self, f: &mut Formatter) -> Result<()>; }
2021-01-06 14:15:48 -06:00
// #[derive(Deb$0ug, Display)]
// struct S;
// ```
// ->
// ```
2020-11-09 06:07:18 -06:00
// # trait Debug { fn fmt(&self, f: &mut Formatter) -> Result<()>; }
// #[derive(Display)]
// struct S;
//
// impl Debug for S {
2021-08-08 06:09:50 -05:00
// $0fn fmt(&self, f: &mut Formatter) -> Result<()> {
2021-08-08 06:50:49 -05:00
// f.debug_struct("S").finish()
2020-11-09 06:07:18 -06:00
// }
// }
// ```
2020-11-09 06:07:18 -06:00
pub(crate) fn replace_derive_with_manual_impl(
acc: &mut Assists,
2022-07-20 08:02:08 -05:00
ctx: &AssistContext<'_>,
2020-11-09 06:07:18 -06:00
) -> Option<()> {
let attr = ctx.find_node_at_offset_with_descend::<ast::Attr>()?;
let path = attr.path()?;
let hir_file = ctx.sema.hir_file_for(attr.syntax());
if !hir_file.is_derive_attr_pseudo_expansion(ctx.db()) {
2021-01-19 16:56:11 -06:00
return None;
}
let InFile { file_id, value } = hir_file.call_node(ctx.db())?;
if file_id.is_macro() {
// FIXME: make this work in macro files
return None;
}
// collect the derive paths from the #[derive] expansion
let current_derives = ctx
.sema
.parse_or_expand(hir_file)?
.descendants()
.filter_map(ast::Attr::cast)
.filter_map(|attr| attr.path())
.collect::<Vec<_>>();
let adt = value.parent().and_then(ast::Adt::cast)?;
let attr = ast::Attr::cast(value)?;
let args = attr.token_tree()?;
let current_module = ctx.sema.scope(adt.syntax())?.module();
let current_crate = current_module.krate();
2021-03-20 15:54:04 -05:00
let found_traits = items_locator::items_with_name(
&ctx.sema,
current_crate,
NameToImport::exact_case_sensitive(path.segments().last()?.to_string()),
items_locator::AssocItemSearch::Exclude,
2021-07-10 15:49:17 -05:00
Some(items_locator::DEFAULT_QUERY_SEARCH_LIMIT.inner()),
)
.filter_map(|item| match item.as_module_def()? {
ModuleDef::Trait(trait_) => Some(trait_),
_ => None,
})
.flat_map(|trait_| {
current_module
.find_use_path(ctx.sema.db, hir::ModuleDef::Trait(trait_))
.as_ref()
.map(mod_path_to_ast)
.zip(Some(trait_))
});
let mut no_traits_found = true;
2021-11-24 09:21:29 -06:00
for (replace_trait_path, trait_) in found_traits.inspect(|_| no_traits_found = false) {
add_assist(
acc,
ctx,
&attr,
&current_derives,
&args,
&path,
2021-11-24 09:21:29 -06:00
&replace_trait_path,
Some(trait_),
&adt,
)?;
}
if no_traits_found {
add_assist(acc, ctx, &attr, &current_derives, &args, &path, &path, None, &adt)?;
}
Some(())
}
2020-01-14 08:07:28 -06:00
fn add_assist(
acc: &mut Assists,
2022-07-20 08:02:08 -05:00
ctx: &AssistContext<'_>,
attr: &ast::Attr,
2021-11-24 09:21:29 -06:00
old_derives: &[ast::Path],
old_tree: &ast::TokenTree,
old_trait_path: &ast::Path,
replace_trait_path: &ast::Path,
trait_: Option<hir::Trait>,
adt: &ast::Adt,
) -> Option<()> {
let target = attr.syntax().text_range();
2021-04-19 10:11:30 -05:00
let annotated_name = adt.name()?;
2021-11-24 09:21:29 -06:00
let label = format!("Convert to manual `impl {} for {}`", replace_trait_path, annotated_name);
2020-05-17 07:21:24 -05:00
2020-11-09 06:07:18 -06:00
acc.add(
AssistId("replace_derive_with_manual_impl", AssistKind::Refactor),
label,
target,
|builder| {
2021-04-19 10:11:30 -05:00
let insert_pos = adt.syntax().text_range().end();
2020-11-09 06:07:18 -06:00
let impl_def_with_items =
2021-11-24 09:21:29 -06:00
impl_def_from_trait(&ctx.sema, adt, &annotated_name, trait_, replace_trait_path);
update_attribute(builder, old_derives, old_tree, old_trait_path, attr);
let trait_path = replace_trait_path.to_string();
2020-11-09 06:07:18 -06:00
match (ctx.config.snippet_cap, impl_def_with_items) {
2021-02-13 14:51:48 -06:00
(None, _) => {
builder.insert(insert_pos, generate_trait_impl_text(adt, &trait_path, ""))
}
2020-11-09 06:07:18 -06:00
(Some(cap), None) => builder.insert_snippet(
cap,
insert_pos,
generate_trait_impl_text(adt, &trait_path, " $0"),
2020-11-09 06:07:18 -06:00
),
(Some(cap), Some((impl_def, first_assoc_item))) => {
let mut cursor = Cursor::Before(first_assoc_item.syntax());
let placeholder;
if let ast::AssocItem::Fn(ref func) = first_assoc_item {
if let Some(m) = func.syntax().descendants().find_map(ast::MacroCall::cast)
{
if m.syntax().text() == "todo!()" {
placeholder = m;
cursor = Cursor::Replace(placeholder.syntax());
}
}
}
2020-11-09 06:07:18 -06:00
builder.insert_snippet(
cap,
insert_pos,
format!("\n\n{}", render_snippet(cap, impl_def.syntax(), cursor)),
)
}
};
},
)
}
fn impl_def_from_trait(
2022-07-20 08:02:08 -05:00
sema: &hir::Semantics<'_, ide_db::RootDatabase>,
2021-08-08 06:09:50 -05:00
adt: &ast::Adt,
annotated_name: &ast::Name,
trait_: Option<hir::Trait>,
trait_path: &ast::Path,
) -> Option<(ast::Impl, ast::AssocItem)> {
let trait_ = trait_?;
let target_scope = sema.scope(annotated_name.syntax())?;
let trait_items = filter_assoc_items(sema, &trait_.items(sema.db), DefaultMethods::No);
if trait_items.is_empty() {
return None;
}
let impl_def = {
use syntax::ast::Impl;
let text = generate_trait_impl_text(adt, trait_path.to_string().as_str(), "");
let parse = syntax::SourceFile::parse(&text);
let node = match parse.tree().syntax().descendants().find_map(Impl::cast) {
Some(it) => it,
None => {
panic!(
"Failed to make ast node `{}` from text {}",
std::any::type_name::<Impl>(),
text
)
}
};
let node = node.clone_subtree();
assert_eq!(node.syntax().text_range().start(), 0.into());
node
};
let trait_items = trait_items
.into_iter()
.map(|it| {
if sema.hir_file_for(it.syntax()).is_macro() {
if let Some(it) = ast::AssocItem::cast(insert_ws_into(it.syntax().clone())) {
return it;
}
}
it.clone_for_update()
})
.collect();
let (impl_def, first_assoc_item) =
add_trait_assoc_items_to_impl(sema, trait_items, trait_, impl_def, target_scope);
2021-08-08 06:09:50 -05:00
2021-08-08 08:56:26 -05:00
// Generate a default `impl` function body for the derived trait.
2021-08-08 17:29:38 -05:00
if let ast::AssocItem::Fn(ref func) = first_assoc_item {
2021-08-10 05:21:48 -05:00
let _ = gen_trait_fn_body(func, trait_path, adt);
2021-08-08 17:29:38 -05:00
};
Some((impl_def, first_assoc_item))
}
fn update_attribute(
builder: &mut SourceChangeBuilder,
2021-11-24 09:21:29 -06:00
old_derives: &[ast::Path],
old_tree: &ast::TokenTree,
old_trait_path: &ast::Path,
attr: &ast::Attr,
) {
2021-11-24 09:21:29 -06:00
let new_derives = old_derives
.iter()
.filter(|t| t.to_string() != old_trait_path.to_string())
2021-01-19 16:56:11 -06:00
.collect::<Vec<_>>();
2021-11-24 09:21:29 -06:00
let has_more_derives = !new_derives.is_empty();
if has_more_derives {
2021-11-24 09:21:29 -06:00
let new_derives = format!("({})", new_derives.iter().format(", "));
builder.replace(old_tree.syntax().text_range(), new_derives);
} else {
let attr_range = attr.syntax().text_range();
builder.delete(attr_range);
if let Some(line_break_range) = attr
.syntax()
.next_sibling_or_token()
.filter(|t| t.kind() == WHITESPACE)
.map(|t| t.text_range())
{
builder.delete(line_break_range);
}
}
}
#[cfg(test)]
mod tests {
2020-05-06 03:16:55 -05:00
use crate::tests::{check_assist, check_assist_not_applicable};
use super::*;
#[test]
2021-08-08 07:08:02 -05:00
fn add_custom_impl_debug_record_struct() {
check_assist(
2020-11-09 06:07:18 -06:00
replace_derive_with_manual_impl,
2021-04-20 09:26:07 -05:00
r#"
//- minicore: fmt, derive
2021-01-06 14:15:48 -06:00
#[derive(Debu$0g)]
struct Foo {
bar: String,
}
2021-04-20 09:26:07 -05:00
"#,
r#"
struct Foo {
bar: String,
}
2021-08-08 11:58:42 -05:00
impl core::fmt::Debug for Foo {
$0fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
2021-08-01 13:46:23 -05:00
f.debug_struct("Foo").field("bar", &self.bar).finish()
}
}
2021-08-08 07:08:02 -05:00
"#,
)
}
#[test]
fn add_custom_impl_debug_tuple_struct() {
check_assist(
replace_derive_with_manual_impl,
r#"
//- minicore: fmt, derive
2021-08-08 07:08:02 -05:00
#[derive(Debu$0g)]
struct Foo(String, usize);
"#,
2021-08-08 11:58:42 -05:00
r#"struct Foo(String, usize);
2021-08-08 07:08:02 -05:00
2021-08-08 11:58:42 -05:00
impl core::fmt::Debug for Foo {
$0fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
2021-08-08 07:08:02 -05:00
f.debug_tuple("Foo").field(&self.0).field(&self.1).finish()
}
}
"#,
)
}
#[test]
fn add_custom_impl_debug_empty_struct() {
check_assist(
replace_derive_with_manual_impl,
r#"
//- minicore: fmt, derive
2021-08-08 07:08:02 -05:00
#[derive(Debu$0g)]
struct Foo;
"#,
r#"
struct Foo;
2021-08-08 11:58:42 -05:00
impl core::fmt::Debug for Foo {
$0fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
2021-08-08 07:08:02 -05:00
f.debug_struct("Foo").finish()
}
}
2021-08-08 08:24:04 -05:00
"#,
)
}
#[test]
fn add_custom_impl_debug_enum() {
check_assist(
replace_derive_with_manual_impl,
r#"
//- minicore: fmt, derive
2021-08-08 08:24:04 -05:00
#[derive(Debu$0g)]
enum Foo {
Bar,
Baz,
}
"#,
r#"
enum Foo {
Bar,
Baz,
}
2021-08-08 11:58:42 -05:00
impl core::fmt::Debug for Foo {
$0fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
2021-08-08 08:24:04 -05:00
match self {
2021-08-08 08:32:11 -05:00
Self::Bar => write!(f, "Bar"),
Self::Baz => write!(f, "Baz"),
2021-08-08 08:24:04 -05:00
}
}
}
2021-08-16 05:58:06 -05:00
"#,
)
}
#[test]
fn add_custom_impl_debug_tuple_enum() {
check_assist(
replace_derive_with_manual_impl,
r#"
//- minicore: fmt, derive
2021-08-16 05:58:06 -05:00
#[derive(Debu$0g)]
enum Foo {
Bar(usize, usize),
Baz,
}
"#,
r#"
enum Foo {
Bar(usize, usize),
Baz,
}
impl core::fmt::Debug for Foo {
$0fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
2021-08-16 10:39:08 -05:00
Self::Bar(arg0, arg1) => f.debug_tuple("Bar").field(arg0).field(arg1).finish(),
2021-08-16 05:58:06 -05:00
Self::Baz => write!(f, "Baz"),
}
}
}
"#,
)
}
#[test]
fn add_custom_impl_debug_record_enum() {
check_assist(
replace_derive_with_manual_impl,
r#"
//- minicore: fmt, derive
2021-08-16 05:58:06 -05:00
#[derive(Debu$0g)]
enum Foo {
Bar {
baz: usize,
qux: usize,
},
Baz,
}
"#,
r#"
enum Foo {
Bar {
baz: usize,
qux: usize,
},
Baz,
}
impl core::fmt::Debug for Foo {
$0fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Bar { baz, qux } => f.debug_struct("Bar").field("baz", baz).field("qux", qux).finish(),
Self::Baz => write!(f, "Baz"),
}
}
}
"#,
)
}
#[test]
fn add_custom_impl_default_record_struct() {
check_assist(
replace_derive_with_manual_impl,
r#"
//- minicore: default, derive
#[derive(Defau$0lt)]
struct Foo {
foo: usize,
}
"#,
r#"
struct Foo {
foo: usize,
}
impl Default for Foo {
$0fn default() -> Self {
Self { foo: Default::default() }
}
}
"#,
)
}
#[test]
fn add_custom_impl_default_tuple_struct() {
check_assist(
replace_derive_with_manual_impl,
r#"
//- minicore: default, derive
#[derive(Defau$0lt)]
struct Foo(usize);
"#,
r#"
struct Foo(usize);
impl Default for Foo {
$0fn default() -> Self {
Self(Default::default())
}
}
2021-08-09 10:28:06 -05:00
"#,
)
}
#[test]
fn add_custom_impl_default_empty_struct() {
check_assist(
replace_derive_with_manual_impl,
r#"
//- minicore: default, derive
2021-08-09 10:28:06 -05:00
#[derive(Defau$0lt)]
struct Foo;
"#,
r#"
struct Foo;
impl Default for Foo {
$0fn default() -> Self {
Self { }
}
}
2021-08-10 04:59:41 -05:00
"#,
)
}
2021-08-10 05:13:30 -05:00
#[test]
fn add_custom_impl_hash_record_struct() {
check_assist(
replace_derive_with_manual_impl,
r#"
//- minicore: hash, derive
2021-08-10 05:13:30 -05:00
#[derive(Has$0h)]
struct Foo {
bin: usize,
bar: usize,
}
"#,
r#"
struct Foo {
bin: usize,
bar: usize,
}
impl core::hash::Hash for Foo {
$0fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
self.bin.hash(state);
self.bar.hash(state);
}
}
"#,
)
}
#[test]
fn add_custom_impl_hash_tuple_struct() {
check_assist(
replace_derive_with_manual_impl,
r#"
//- minicore: hash, derive
2021-08-10 05:13:30 -05:00
#[derive(Has$0h)]
struct Foo(usize, usize);
"#,
r#"
struct Foo(usize, usize);
impl core::hash::Hash for Foo {
$0fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
self.0.hash(state);
self.1.hash(state);
}
}
"#,
)
}
2021-08-10 04:59:41 -05:00
#[test]
fn add_custom_impl_hash_enum() {
check_assist(
replace_derive_with_manual_impl,
r#"
//- minicore: hash, derive
2021-08-10 04:59:41 -05:00
#[derive(Has$0h)]
enum Foo {
Bar,
Baz,
}
"#,
r#"
enum Foo {
Bar,
Baz,
}
impl core::hash::Hash for Foo {
$0fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
core::mem::discriminant(self).hash(state);
}
}
2021-08-10 06:20:24 -05:00
"#,
)
}
#[test]
fn add_custom_impl_clone_record_struct() {
check_assist(
replace_derive_with_manual_impl,
r#"
//- minicore: clone, derive
2021-08-10 06:20:24 -05:00
#[derive(Clo$0ne)]
struct Foo {
bin: usize,
bar: usize,
}
"#,
r#"
struct Foo {
bin: usize,
bar: usize,
}
impl Clone for Foo {
$0fn clone(&self) -> Self {
Self { bin: self.bin.clone(), bar: self.bar.clone() }
2021-08-10 06:20:24 -05:00
}
}
"#,
)
}
#[test]
fn add_custom_impl_clone_tuple_struct() {
check_assist(
replace_derive_with_manual_impl,
r#"
//- minicore: clone, derive
2021-08-10 06:20:24 -05:00
#[derive(Clo$0ne)]
struct Foo(usize, usize);
"#,
r#"
struct Foo(usize, usize);
impl Clone for Foo {
$0fn clone(&self) -> Self {
Self(self.0.clone(), self.1.clone())
}
}
"#,
)
}
#[test]
fn add_custom_impl_clone_empty_struct() {
check_assist(
replace_derive_with_manual_impl,
r#"
//- minicore: clone, derive
#[derive(Clo$0ne)]
struct Foo;
"#,
r#"
struct Foo;
impl Clone for Foo {
$0fn clone(&self) -> Self {
Self { }
}
}
"#,
)
}
2021-08-10 06:20:24 -05:00
#[test]
fn add_custom_impl_clone_enum() {
check_assist(
replace_derive_with_manual_impl,
r#"
//- minicore: clone, derive
2021-08-10 06:20:24 -05:00
#[derive(Clo$0ne)]
enum Foo {
Bar,
Baz,
}
"#,
r#"
enum Foo {
Bar,
Baz,
}
impl Clone for Foo {
$0fn clone(&self) -> Self {
match self {
Self::Bar => Self::Bar,
Self::Baz => Self::Baz,
}
}
}
"#,
)
}
#[test]
fn add_custom_impl_clone_tuple_enum() {
check_assist(
replace_derive_with_manual_impl,
r#"
//- minicore: clone, derive
2021-08-10 06:20:24 -05:00
#[derive(Clo$0ne)]
enum Foo {
Bar(String),
2021-08-10 06:20:24 -05:00
Baz,
}
"#,
r#"
enum Foo {
Bar(String),
Baz,
}
impl Clone for Foo {
$0fn clone(&self) -> Self {
match self {
2021-08-10 08:07:13 -05:00
Self::Bar(arg0) => Self::Bar(arg0.clone()),
2021-08-10 06:20:24 -05:00
Self::Baz => Self::Baz,
}
}
}
2021-08-10 07:43:20 -05:00
"#,
)
}
#[test]
fn add_custom_impl_clone_record_enum() {
check_assist(
replace_derive_with_manual_impl,
r#"
//- minicore: clone, derive
2021-08-10 07:43:20 -05:00
#[derive(Clo$0ne)]
enum Foo {
Bar {
bin: String,
},
Baz,
}
"#,
r#"
enum Foo {
Bar {
bin: String,
},
Baz,
}
impl Clone for Foo {
$0fn clone(&self) -> Self {
match self {
Self::Bar { bin } => Self::Bar { bin: bin.clone() },
Self::Baz => Self::Baz,
}
}
}
2021-08-10 12:48:50 -05:00
"#,
)
}
#[test]
fn add_custom_impl_partial_ord_record_struct() {
check_assist(
replace_derive_with_manual_impl,
r#"
//- minicore: ord, derive
#[derive(Partial$0Ord)]
2021-10-12 10:44:57 -05:00
struct Foo {
bin: usize,
}
"#,
r#"
struct Foo {
bin: usize,
}
impl PartialOrd for Foo {
$0fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
2021-10-18 05:43:53 -05:00
self.bin.partial_cmp(&other.bin)
2021-10-12 10:44:57 -05:00
}
}
"#,
)
}
#[test]
fn add_custom_impl_partial_ord_record_struct_multi_field() {
check_assist(
replace_derive_with_manual_impl,
r#"
//- minicore: ord, derive
2021-10-12 10:44:57 -05:00
#[derive(Partial$0Ord)]
struct Foo {
bin: usize,
bar: usize,
baz: usize,
}
"#,
r#"
struct Foo {
bin: usize,
bar: usize,
baz: usize,
}
impl PartialOrd for Foo {
$0fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
match self.bin.partial_cmp(&other.bin) {
2021-10-18 07:45:24 -05:00
Some(core::cmp::Ordering::Equal) => {}
ord => return ord,
}
match self.bar.partial_cmp(&other.bar) {
2021-10-18 07:45:24 -05:00
Some(core::cmp::Ordering::Equal) => {}
ord => return ord,
}
self.baz.partial_cmp(&other.baz)
}
}
"#,
)
}
#[test]
fn add_custom_impl_partial_ord_tuple_struct() {
check_assist(
replace_derive_with_manual_impl,
r#"
//- minicore: ord, derive
#[derive(Partial$0Ord)]
struct Foo(usize, usize, usize);
"#,
r#"
struct Foo(usize, usize, usize);
impl PartialOrd for Foo {
$0fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
match self.0.partial_cmp(&other.0) {
2021-10-18 07:45:24 -05:00
Some(core::cmp::Ordering::Equal) => {}
ord => return ord,
}
match self.1.partial_cmp(&other.1) {
2021-10-18 07:45:24 -05:00
Some(core::cmp::Ordering::Equal) => {}
ord => return ord,
}
self.2.partial_cmp(&other.2)
2021-10-12 08:07:57 -05:00
}
}
"#,
)
}
2021-08-10 12:48:50 -05:00
#[test]
fn add_custom_impl_partial_eq_record_struct() {
check_assist(
replace_derive_with_manual_impl,
r#"
//- minicore: eq, derive
2021-08-10 12:48:50 -05:00
#[derive(Partial$0Eq)]
struct Foo {
bin: usize,
bar: usize,
}
"#,
r#"
struct Foo {
bin: usize,
bar: usize,
}
impl PartialEq for Foo {
$0fn eq(&self, other: &Self) -> bool {
self.bin == other.bin && self.bar == other.bar
}
}
"#,
)
}
#[test]
fn add_custom_impl_partial_eq_tuple_struct() {
check_assist(
replace_derive_with_manual_impl,
r#"
//- minicore: eq, derive
2021-08-10 12:48:50 -05:00
#[derive(Partial$0Eq)]
struct Foo(usize, usize);
"#,
r#"
struct Foo(usize, usize);
impl PartialEq for Foo {
$0fn eq(&self, other: &Self) -> bool {
self.0 == other.0 && self.1 == other.1
}
}
"#,
)
}
#[test]
fn add_custom_impl_partial_eq_empty_struct() {
check_assist(
replace_derive_with_manual_impl,
r#"
//- minicore: eq, derive
2021-08-10 12:48:50 -05:00
#[derive(Partial$0Eq)]
struct Foo;
"#,
r#"
struct Foo;
impl PartialEq for Foo {
$0fn eq(&self, other: &Self) -> bool {
true
}
}
"#,
)
}
#[test]
fn add_custom_impl_partial_eq_enum() {
check_assist(
replace_derive_with_manual_impl,
r#"
//- minicore: eq, derive
2021-08-10 12:48:50 -05:00
#[derive(Partial$0Eq)]
enum Foo {
Bar,
Baz,
}
"#,
r#"
enum Foo {
Bar,
Baz,
}
impl PartialEq for Foo {
$0fn eq(&self, other: &Self) -> bool {
2021-08-10 14:05:23 -05:00
core::mem::discriminant(self) == core::mem::discriminant(other)
2021-08-10 12:48:50 -05:00
}
}
"#,
)
}
#[test]
fn add_custom_impl_partial_eq_tuple_enum() {
check_assist(
replace_derive_with_manual_impl,
r#"
//- minicore: eq, derive
2021-08-10 12:48:50 -05:00
#[derive(Partial$0Eq)]
enum Foo {
Bar(String),
Baz,
}
"#,
r#"
enum Foo {
Bar(String),
Baz,
}
impl PartialEq for Foo {
$0fn eq(&self, other: &Self) -> bool {
2021-08-11 13:33:13 -05:00
match (self, other) {
(Self::Bar(l0), Self::Bar(r0)) => l0 == r0,
_ => core::mem::discriminant(self) == core::mem::discriminant(other),
2021-08-10 12:48:50 -05:00
}
}
}
"#,
)
}
#[test]
fn add_custom_impl_partial_eq_record_enum() {
check_assist(
replace_derive_with_manual_impl,
r#"
//- minicore: eq, derive
2021-08-10 12:48:50 -05:00
#[derive(Partial$0Eq)]
enum Foo {
Bar {
bin: String,
},
Baz {
qux: String,
2021-08-10 12:48:50 -05:00
fez: String,
},
Qux {},
Bin,
2021-08-10 12:48:50 -05:00
}
"#,
r#"
enum Foo {
Bar {
bin: String,
},
Baz {
qux: String,
2021-08-10 12:48:50 -05:00
fez: String,
2021-08-10 15:08:14 -05:00
},
Qux {},
Bin,
2021-08-10 12:48:50 -05:00
}
impl PartialEq for Foo {
$0fn eq(&self, other: &Self) -> bool {
2021-08-11 13:33:13 -05:00
match (self, other) {
(Self::Bar { bin: l_bin }, Self::Bar { bin: r_bin }) => l_bin == r_bin,
(Self::Baz { qux: l_qux, fez: l_fez }, Self::Baz { qux: r_qux, fez: r_fez }) => l_qux == r_qux && l_fez == r_fez,
_ => core::mem::discriminant(self) == core::mem::discriminant(other),
2021-08-10 12:48:50 -05:00
}
}
}
2021-04-20 09:26:07 -05:00
"#,
)
}
#[test]
fn add_custom_impl_all() {
check_assist(
2020-11-09 06:07:18 -06:00
replace_derive_with_manual_impl,
2021-04-20 09:26:07 -05:00
r#"
//- minicore: derive
mod foo {
pub trait Bar {
type Qux;
const Baz: usize = 42;
const Fez: usize;
fn foo();
fn bar() {}
}
}
2021-01-06 14:15:48 -06:00
#[derive($0Bar)]
struct Foo {
bar: String,
}
2021-04-20 09:26:07 -05:00
"#,
r#"
mod foo {
pub trait Bar {
type Qux;
const Baz: usize = 42;
const Fez: usize;
fn foo();
fn bar() {}
}
}
struct Foo {
bar: String,
}
impl foo::Bar for Foo {
$0type Qux;
const Baz: usize = 42;
const Fez: usize;
fn foo() {
todo!()
}
}
2021-04-20 09:26:07 -05:00
"#,
)
}
#[test]
fn add_custom_impl_for_unique_input_unknown() {
check_assist(
2020-11-09 06:07:18 -06:00
replace_derive_with_manual_impl,
2021-04-20 09:26:07 -05:00
r#"
//- minicore: derive
2021-01-06 14:15:48 -06:00
#[derive(Debu$0g)]
struct Foo {
bar: String,
}
2021-04-20 09:26:07 -05:00
"#,
r#"
struct Foo {
bar: String,
}
impl Debug for Foo {
2020-05-17 07:21:24 -05:00
$0
}
2021-04-20 09:26:07 -05:00
"#,
)
}
#[test]
fn add_custom_impl_for_with_visibility_modifier() {
check_assist(
2020-11-09 06:07:18 -06:00
replace_derive_with_manual_impl,
2021-04-20 09:26:07 -05:00
r#"
//- minicore: derive
2021-01-06 14:15:48 -06:00
#[derive(Debug$0)]
pub struct Foo {
bar: String,
}
2021-04-20 09:26:07 -05:00
"#,
r#"
pub struct Foo {
bar: String,
}
impl Debug for Foo {
2020-05-17 07:21:24 -05:00
$0
}
2021-04-20 09:26:07 -05:00
"#,
)
}
#[test]
fn add_custom_impl_when_multiple_inputs() {
check_assist(
2020-11-09 06:07:18 -06:00
replace_derive_with_manual_impl,
2021-04-20 09:26:07 -05:00
r#"
//- minicore: derive
2021-01-06 14:15:48 -06:00
#[derive(Display, Debug$0, Serialize)]
struct Foo {}
2021-04-20 09:26:07 -05:00
"#,
r#"
#[derive(Display, Serialize)]
struct Foo {}
impl Debug for Foo {
2020-05-17 07:21:24 -05:00
$0
}
2021-04-20 09:26:07 -05:00
"#,
)
}
2021-11-01 11:47:27 -05:00
#[test]
fn add_custom_impl_default_generic_record_struct() {
check_assist(
replace_derive_with_manual_impl,
r#"
//- minicore: default, derive
2021-11-01 11:47:27 -05:00
#[derive(Defau$0lt)]
struct Foo<T, U> {
foo: T,
bar: U,
}
"#,
r#"
struct Foo<T, U> {
foo: T,
bar: U,
}
impl<T, U> Default for Foo<T, U> {
$0fn default() -> Self {
Self { foo: Default::default(), bar: Default::default() }
}
}
"#,
)
}
#[test]
2021-11-11 02:46:29 -06:00
fn add_custom_impl_clone_generic_tuple_struct_with_bounds() {
2021-11-01 11:47:27 -05:00
check_assist(
replace_derive_with_manual_impl,
r#"
//- minicore: clone, derive
2021-11-01 11:47:27 -05:00
#[derive(Clo$0ne)]
2021-11-11 02:46:29 -06:00
struct Foo<T: Clone>(T, usize);
2021-11-01 11:47:27 -05:00
"#,
r#"
2021-11-11 02:46:29 -06:00
struct Foo<T: Clone>(T, usize);
2021-11-01 11:47:27 -05:00
2021-11-11 02:46:29 -06:00
impl<T: Clone> Clone for Foo<T> {
2021-11-01 11:47:27 -05:00
$0fn clone(&self) -> Self {
Self(self.0.clone(), self.1.clone())
}
}
"#,
)
}
#[test]
fn test_ignore_derive_macro_without_input() {
check_assist_not_applicable(
2020-11-09 06:07:18 -06:00
replace_derive_with_manual_impl,
2021-04-20 09:26:07 -05:00
r#"
//- minicore: derive
2021-01-06 14:15:48 -06:00
#[derive($0)]
struct Foo {}
2021-04-20 09:26:07 -05:00
"#,
)
}
#[test]
fn test_ignore_if_cursor_on_param() {
check_assist_not_applicable(
2020-11-09 06:07:18 -06:00
replace_derive_with_manual_impl,
2021-04-20 09:26:07 -05:00
r#"
//- minicore: derive, fmt
2021-01-06 14:15:48 -06:00
#[derive$0(Debug)]
struct Foo {}
2021-04-20 09:26:07 -05:00
"#,
);
check_assist_not_applicable(
2020-11-09 06:07:18 -06:00
replace_derive_with_manual_impl,
2021-04-20 09:26:07 -05:00
r#"
//- minicore: derive, fmt
2021-01-06 14:15:48 -06:00
#[derive(Debug)$0]
struct Foo {}
2021-04-20 09:26:07 -05:00
"#,
)
}
#[test]
fn test_ignore_if_not_derive() {
check_assist_not_applicable(
2020-11-09 06:07:18 -06:00
replace_derive_with_manual_impl,
2021-04-20 09:26:07 -05:00
r#"
//- minicore: derive
2021-01-06 14:15:48 -06:00
#[allow(non_camel_$0case_types)]
struct Foo {}
2021-04-20 09:26:07 -05:00
"#,
)
}
#[test]
fn works_at_start_of_file() {
check_assist_not_applicable(
replace_derive_with_manual_impl,
r#"
//- minicore: derive, fmt
$0#[derive(Debug)]
struct S;
"#,
);
}
2021-11-24 09:21:29 -06:00
#[test]
fn add_custom_impl_keep_path() {
check_assist(
replace_derive_with_manual_impl,
r#"
//- minicore: clone, derive
2021-11-24 09:21:29 -06:00
#[derive(std::fmt::Debug, Clo$0ne)]
pub struct Foo;
"#,
r#"
#[derive(std::fmt::Debug)]
pub struct Foo;
impl Clone for Foo {
$0fn clone(&self) -> Self {
Self { }
}
}
"#,
)
}
#[test]
fn add_custom_impl_replace_path() {
check_assist(
replace_derive_with_manual_impl,
r#"
//- minicore: fmt, derive
2021-11-24 09:21:29 -06:00
#[derive(core::fmt::Deb$0ug, Clone)]
pub struct Foo;
"#,
r#"
#[derive(Clone)]
pub struct Foo;
impl core::fmt::Debug for Foo {
$0fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Foo").finish()
}
}
"#,
)
}
}