2020-11-28 08:30:39 -06:00
|
|
|
use ide_db::helpers::mod_path_to_ast;
|
2020-11-03 16:29:53 -06:00
|
|
|
use ide_db::imports_locator;
|
2020-08-12 08:04:06 -05:00
|
|
|
use itertools::Itertools;
|
2020-08-12 11:26:51 -05:00
|
|
|
use syntax::{
|
2020-11-03 16:29:53 -06:00
|
|
|
ast::{self, make, AstNode},
|
2021-01-19 16:56:11 -06:00
|
|
|
Direction,
|
2019-10-15 13:29:20 -05:00
|
|
|
SyntaxKind::{IDENT, WHITESPACE},
|
2020-11-05 16:34:50 -06:00
|
|
|
TextSize,
|
2019-10-15 13:29:20 -05:00
|
|
|
};
|
|
|
|
|
2020-05-06 11:45:35 -05:00
|
|
|
use crate::{
|
2020-11-03 16:29:53 -06:00
|
|
|
assist_context::{AssistBuilder, AssistContext, Assists},
|
2020-11-05 16:34:50 -06:00
|
|
|
utils::{
|
2020-11-24 15:25:13 -06:00
|
|
|
add_trait_assoc_items_to_impl, filter_assoc_items, render_snippet, Cursor, DefaultMethods,
|
2020-11-05 16:34:50 -06:00
|
|
|
},
|
2020-06-28 17:36:05 -05:00
|
|
|
AssistId, AssistKind,
|
2020-05-06 11:45:35 -05:00
|
|
|
};
|
2020-03-19 06:36:33 -05:00
|
|
|
|
2020-11-09 06:07:18 -06:00
|
|
|
// Assist: replace_derive_with_manual_impl
|
2019-10-19 06:19:06 -05:00
|
|
|
//
|
2020-11-09 06:07:18 -06:00
|
|
|
// Converts a `derive` impl into a manual one.
|
2019-10-19 06:19:06 -05:00
|
|
|
//
|
|
|
|
// ```
|
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)]
|
2019-10-19 06:19:06 -05:00
|
|
|
// struct S;
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
2020-11-09 06:07:18 -06:00
|
|
|
// # trait Debug { fn fmt(&self, f: &mut Formatter) -> Result<()>; }
|
2019-10-19 06:19:06 -05:00
|
|
|
// #[derive(Display)]
|
|
|
|
// struct S;
|
|
|
|
//
|
|
|
|
// impl Debug for S {
|
2020-11-09 06:07:18 -06:00
|
|
|
// fn fmt(&self, f: &mut Formatter) -> Result<()> {
|
|
|
|
// ${0:todo!()}
|
|
|
|
// }
|
2019-10-19 06:19:06 -05:00
|
|
|
// }
|
|
|
|
// ```
|
2020-11-09 06:07:18 -06:00
|
|
|
pub(crate) fn replace_derive_with_manual_impl(
|
|
|
|
acc: &mut Assists,
|
|
|
|
ctx: &AssistContext,
|
|
|
|
) -> Option<()> {
|
2020-07-30 13:16:04 -05:00
|
|
|
let attr = ctx.find_node_at_offset::<ast::Attr>()?;
|
2019-10-15 13:29:20 -05:00
|
|
|
|
2021-01-19 16:56:11 -06:00
|
|
|
let has_derive = attr
|
2019-10-15 13:29:20 -05:00
|
|
|
.syntax()
|
|
|
|
.descendants_with_tokens()
|
|
|
|
.filter(|t| t.kind() == IDENT)
|
2020-11-03 16:29:53 -06:00
|
|
|
.find_map(syntax::NodeOrToken::into_token)
|
2021-01-19 16:56:11 -06:00
|
|
|
.filter(|t| t.text() == "derive")
|
|
|
|
.is_some();
|
|
|
|
if !has_derive {
|
|
|
|
return None;
|
|
|
|
}
|
2019-10-15 13:29:20 -05:00
|
|
|
|
2021-01-19 16:56:11 -06:00
|
|
|
let trait_token = ctx.token_at_offset().find(|t| t.kind() == IDENT && t.text() != "derive")?;
|
2020-11-03 16:29:53 -06:00
|
|
|
let trait_path = make::path_unqualified(make::path_segment(make::name_ref(trait_token.text())));
|
2019-10-15 13:29:20 -05:00
|
|
|
|
2020-11-05 16:34:50 -06:00
|
|
|
let annotated_name = attr.syntax().siblings(Direction::Next).find_map(ast::Name::cast)?;
|
|
|
|
let insert_pos = annotated_name.syntax().parent()?.text_range().end();
|
2020-11-03 16:29:53 -06:00
|
|
|
|
2020-11-05 16:34:50 -06:00
|
|
|
let current_module = ctx.sema.scope(annotated_name.syntax()).module()?;
|
2020-11-03 16:29:53 -06:00
|
|
|
let current_crate = current_module.krate();
|
|
|
|
|
2020-12-29 06:35:49 -06:00
|
|
|
let found_traits = imports_locator::find_exact_imports(
|
|
|
|
&ctx.sema,
|
|
|
|
current_crate,
|
|
|
|
trait_token.text().to_string(),
|
|
|
|
)
|
|
|
|
.filter_map(|candidate: either::Either<hir::ModuleDef, hir::MacroDef>| match candidate {
|
|
|
|
either::Either::Left(hir::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_))
|
|
|
|
});
|
2020-11-03 16:29:53 -06:00
|
|
|
|
|
|
|
let mut no_traits_found = true;
|
2020-11-05 16:34:50 -06:00
|
|
|
for (trait_path, trait_) in found_traits.inspect(|_| no_traits_found = false) {
|
|
|
|
add_assist(acc, ctx, &attr, &trait_path, Some(trait_), &annotated_name, insert_pos)?;
|
2020-11-03 16:29:53 -06:00
|
|
|
}
|
|
|
|
if no_traits_found {
|
2020-11-05 16:34:50 -06:00
|
|
|
add_assist(acc, ctx, &attr, &trait_path, None, &annotated_name, insert_pos)?;
|
2020-11-03 16:29:53 -06:00
|
|
|
}
|
|
|
|
Some(())
|
|
|
|
}
|
2020-01-14 08:07:28 -06:00
|
|
|
|
2020-11-03 16:29:53 -06:00
|
|
|
fn add_assist(
|
|
|
|
acc: &mut Assists,
|
2020-11-05 16:34:50 -06:00
|
|
|
ctx: &AssistContext,
|
2020-11-03 16:29:53 -06:00
|
|
|
attr: &ast::Attr,
|
|
|
|
trait_path: &ast::Path,
|
2020-11-05 16:34:50 -06:00
|
|
|
trait_: Option<hir::Trait>,
|
|
|
|
annotated_name: &ast::Name,
|
2020-11-03 16:29:53 -06:00
|
|
|
insert_pos: TextSize,
|
|
|
|
) -> Option<()> {
|
2020-05-06 05:51:28 -05:00
|
|
|
let target = attr.syntax().text_range();
|
2020-11-03 16:29:53 -06:00
|
|
|
let input = attr.token_tree()?;
|
2020-11-09 06:07:18 -06:00
|
|
|
let label = format!("Convert to manual `impl {} for {}`", trait_path, annotated_name);
|
2020-11-03 16:29:53 -06:00
|
|
|
let trait_name = trait_path.segment().and_then(|seg| seg.name_ref())?;
|
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| {
|
|
|
|
let impl_def_with_items =
|
|
|
|
impl_def_from_trait(&ctx.sema, annotated_name, trait_, trait_path);
|
|
|
|
update_attribute(builder, &input, &trait_name, &attr);
|
|
|
|
match (ctx.config.snippet_cap, impl_def_with_items) {
|
|
|
|
(None, _) => builder.insert(
|
|
|
|
insert_pos,
|
|
|
|
format!("\n\nimpl {} for {} {{\n\n}}", trait_path, annotated_name),
|
|
|
|
),
|
|
|
|
(Some(cap), None) => builder.insert_snippet(
|
|
|
|
cap,
|
|
|
|
insert_pos,
|
|
|
|
format!("\n\nimpl {} for {} {{\n $0\n}}", trait_path, annotated_name),
|
|
|
|
),
|
|
|
|
(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-05 16:34:50 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-09 06:07:18 -06:00
|
|
|
builder.insert_snippet(
|
|
|
|
cap,
|
|
|
|
insert_pos,
|
|
|
|
format!("\n\n{}", render_snippet(cap, impl_def.syntax(), cursor)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
)
|
2019-10-15 13:29:20 -05:00
|
|
|
}
|
|
|
|
|
2020-11-05 16:34:50 -06:00
|
|
|
fn impl_def_from_trait(
|
|
|
|
sema: &hir::Semantics<ide_db::RootDatabase>,
|
|
|
|
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.db, &trait_.items(sema.db), DefaultMethods::No);
|
|
|
|
if trait_items.is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let impl_def = make::impl_trait(
|
|
|
|
trait_path.clone(),
|
|
|
|
make::path_unqualified(make::path_segment(make::name_ref(annotated_name.text()))),
|
|
|
|
);
|
|
|
|
let (impl_def, first_assoc_item) =
|
|
|
|
add_trait_assoc_items_to_impl(sema, trait_items, trait_, impl_def, target_scope);
|
|
|
|
Some((impl_def, first_assoc_item))
|
|
|
|
}
|
|
|
|
|
2020-11-03 16:29:53 -06:00
|
|
|
fn update_attribute(
|
|
|
|
builder: &mut AssistBuilder,
|
|
|
|
input: &ast::TokenTree,
|
|
|
|
trait_name: &ast::NameRef,
|
|
|
|
attr: &ast::Attr,
|
|
|
|
) {
|
|
|
|
let new_attr_input = input
|
|
|
|
.syntax()
|
|
|
|
.descendants_with_tokens()
|
|
|
|
.filter(|t| t.kind() == IDENT)
|
2021-01-19 16:56:11 -06:00
|
|
|
.filter_map(|t| t.into_token().map(|t| t.text().to_string()))
|
2020-11-03 16:29:53 -06:00
|
|
|
.filter(|t| t != trait_name.text())
|
2021-01-19 16:56:11 -06:00
|
|
|
.collect::<Vec<_>>();
|
2020-11-03 16:29:53 -06:00
|
|
|
let has_more_derives = !new_attr_input.is_empty();
|
|
|
|
|
|
|
|
if has_more_derives {
|
|
|
|
let new_attr_input = format!("({})", new_attr_input.iter().format(", "));
|
|
|
|
builder.replace(input.syntax().text_range(), new_attr_input);
|
|
|
|
} else {
|
|
|
|
let attr_range = attr.syntax().text_range();
|
|
|
|
builder.delete(attr_range);
|
|
|
|
|
2020-11-05 16:34:50 -06:00
|
|
|
if let Some(line_break_range) = attr
|
2020-11-03 16:29:53 -06:00
|
|
|
.syntax()
|
|
|
|
.next_sibling_or_token()
|
|
|
|
.filter(|t| t.kind() == WHITESPACE)
|
|
|
|
.map(|t| t.text_range())
|
2020-11-05 16:34:50 -06:00
|
|
|
{
|
|
|
|
builder.delete(line_break_range);
|
|
|
|
}
|
2020-11-03 16:29:53 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-15 13:29:20 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-05-06 03:16:55 -05:00
|
|
|
use crate::tests::{check_assist, check_assist_not_applicable};
|
2019-10-15 13:29:20 -05:00
|
|
|
|
2020-03-28 05:01:25 -05:00
|
|
|
use super::*;
|
|
|
|
|
2020-11-03 16:29:53 -06:00
|
|
|
#[test]
|
2020-11-05 16:34:50 -06:00
|
|
|
fn add_custom_impl_debug() {
|
2020-11-03 16:29:53 -06:00
|
|
|
check_assist(
|
2020-11-09 06:07:18 -06:00
|
|
|
replace_derive_with_manual_impl,
|
2020-11-03 16:29:53 -06:00
|
|
|
"
|
|
|
|
mod fmt {
|
2020-11-05 16:34:50 -06:00
|
|
|
pub struct Error;
|
|
|
|
pub type Result = Result<(), Error>;
|
|
|
|
pub struct Formatter<'a>;
|
|
|
|
pub trait Debug {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> Result;
|
|
|
|
}
|
2020-11-03 16:29:53 -06:00
|
|
|
}
|
|
|
|
|
2021-01-06 14:15:48 -06:00
|
|
|
#[derive(Debu$0g)]
|
2020-11-03 16:29:53 -06:00
|
|
|
struct Foo {
|
|
|
|
bar: String,
|
|
|
|
}
|
|
|
|
",
|
|
|
|
"
|
|
|
|
mod fmt {
|
2020-11-05 16:34:50 -06:00
|
|
|
pub struct Error;
|
|
|
|
pub type Result = Result<(), Error>;
|
|
|
|
pub struct Formatter<'a>;
|
|
|
|
pub trait Debug {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> Result;
|
|
|
|
}
|
2020-11-03 16:29:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Foo {
|
|
|
|
bar: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for Foo {
|
2020-11-05 16:34:50 -06:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
${0:todo!()}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn add_custom_impl_all() {
|
|
|
|
check_assist(
|
2020-11-09 06:07:18 -06:00
|
|
|
replace_derive_with_manual_impl,
|
2020-11-05 16:34:50 -06:00
|
|
|
"
|
|
|
|
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)]
|
2020-11-05 16:34:50 -06:00
|
|
|
struct Foo {
|
|
|
|
bar: String,
|
|
|
|
}
|
|
|
|
",
|
|
|
|
"
|
|
|
|
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!()
|
|
|
|
}
|
2020-11-03 16:29:53 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
2019-10-15 13:29:20 -05:00
|
|
|
#[test]
|
|
|
|
fn add_custom_impl_for_unique_input() {
|
|
|
|
check_assist(
|
2020-11-09 06:07:18 -06:00
|
|
|
replace_derive_with_manual_impl,
|
2019-10-15 13:29:20 -05:00
|
|
|
"
|
2021-01-06 14:15:48 -06:00
|
|
|
#[derive(Debu$0g)]
|
2019-10-15 13:29:20 -05:00
|
|
|
struct Foo {
|
|
|
|
bar: String,
|
|
|
|
}
|
|
|
|
",
|
|
|
|
"
|
|
|
|
struct Foo {
|
|
|
|
bar: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Debug for Foo {
|
2020-05-17 07:21:24 -05:00
|
|
|
$0
|
2019-10-15 13:29:20 -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,
|
2019-10-15 13:29:20 -05:00
|
|
|
"
|
2021-01-06 14:15:48 -06:00
|
|
|
#[derive(Debug$0)]
|
2019-10-15 13:29:20 -05:00
|
|
|
pub struct Foo {
|
|
|
|
bar: String,
|
|
|
|
}
|
|
|
|
",
|
|
|
|
"
|
|
|
|
pub struct Foo {
|
|
|
|
bar: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Debug for Foo {
|
2020-05-17 07:21:24 -05:00
|
|
|
$0
|
2019-10-15 13:29:20 -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,
|
2019-10-15 13:29:20 -05:00
|
|
|
"
|
2021-01-06 14:15:48 -06:00
|
|
|
#[derive(Display, Debug$0, Serialize)]
|
2019-10-15 13:29:20 -05:00
|
|
|
struct Foo {}
|
|
|
|
",
|
|
|
|
"
|
|
|
|
#[derive(Display, Serialize)]
|
|
|
|
struct Foo {}
|
|
|
|
|
|
|
|
impl Debug for Foo {
|
2020-05-17 07:21:24 -05:00
|
|
|
$0
|
2019-10-15 13:29:20 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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,
|
2019-10-15 13:29:20 -05:00
|
|
|
"
|
2021-01-06 14:15:48 -06:00
|
|
|
#[derive($0)]
|
2019-10-15 13:29:20 -05:00
|
|
|
struct Foo {}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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,
|
2019-10-15 13:29:20 -05:00
|
|
|
"
|
2021-01-06 14:15:48 -06:00
|
|
|
#[derive$0(Debug)]
|
2019-10-15 13:29:20 -05:00
|
|
|
struct Foo {}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
|
|
|
check_assist_not_applicable(
|
2020-11-09 06:07:18 -06:00
|
|
|
replace_derive_with_manual_impl,
|
2019-10-15 13:29:20 -05:00
|
|
|
"
|
2021-01-06 14:15:48 -06:00
|
|
|
#[derive(Debug)$0]
|
2019-10-15 13:29:20 -05:00
|
|
|
struct Foo {}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ignore_if_not_derive() {
|
|
|
|
check_assist_not_applicable(
|
2020-11-09 06:07:18 -06:00
|
|
|
replace_derive_with_manual_impl,
|
2019-10-15 13:29:20 -05:00
|
|
|
"
|
2021-01-06 14:15:48 -06:00
|
|
|
#[allow(non_camel_$0case_types)]
|
2019-10-15 13:29:20 -05:00
|
|
|
struct Foo {}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|