Skip to content

Add ide-assist: generate_impl_trait for generate_impl #19938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
274 changes: 271 additions & 3 deletions crates/ide-assists/src/handlers/generate_impl.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
use syntax::{
ast::{self, AstNode, HasName, edit_in_place::Indent, make},
ast::{self, AstNode, HasGenericParams, HasName, edit_in_place::Indent, make},
ted,
};

use crate::{AssistContext, AssistId, Assists, utils};
use crate::{
AssistContext, AssistId, Assists,
utils::{self, DefaultMethods, IgnoreAssocItems},
};

fn insert_impl(impl_: ast::Impl, nominal: &ast::Adt) {
fn insert_impl(impl_: ast::Impl, nominal: &impl Indent) {
let indent = nominal.indent_level();

impl_.indent(indent);
ted::insert_all_raw(
ted::Position::after(nominal.syntax()),
vec![
Expand Down Expand Up @@ -113,6 +118,101 @@ pub(crate) fn generate_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>) ->
)
}

// Assist: generate_impl_trait
//
// Adds this trait impl for a type.
//
// ```
// trait $0Foo {
// fn foo(&self) -> i32;
// }
// ```
// ->
// ```
// trait Foo {
// fn foo(&self) -> i32;
// }
//
// impl Foo for ${1:_} {
// $0fn foo(&self) -> i32 {
// todo!()
// }
// }
// ```
pub(crate) fn generate_impl_trait(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
let name = ctx.find_node_at_offset::<ast::Name>()?;
let trait_ = ast::Trait::cast(name.syntax().parent()?)?;
let target_scope = ctx.sema.scope(trait_.syntax())?;
let hir_trait = ctx.sema.to_def(&trait_)?;

let target = trait_.syntax().text_range();
acc.add(
AssistId::generate("generate_impl_trait"),
format!("Generate `{name}` impl for type"),
target,
|edit| {
let holder_arg = ast::GenericArg::TypeArg(make::type_arg(make::ty_placeholder()));
let missing_items = utils::filter_assoc_items(
&ctx.sema,
&hir_trait.items(ctx.db()),
DefaultMethods::No,
IgnoreAssocItems::DocHiddenAttrPresent,
);
let impl_ = make::impl_trait(
trait_.unsafe_token().is_some(),
None,
trait_.generic_param_list().map(|list| {
make::generic_arg_list(list.generic_params().map(|_| holder_arg.clone()))
}),
None,
None,
false,
make::ty(&name.text()),
make::ty_placeholder(),
None,
None,
None,
)
.clone_for_update();

let trait_ = edit.make_mut(trait_);

if !missing_items.is_empty() {
utils::add_trait_assoc_items_to_impl(
&ctx.sema,
ctx.config,
&missing_items,
hir_trait,
&impl_,
&target_scope,
);
}

if let Some(cap) = ctx.config.snippet_cap {
if let Some(generics) = impl_.trait_().and_then(|it| it.generic_arg_list()) {
for generic in generics.generic_args() {
edit.add_placeholder_snippet(cap, generic);
}
}

if let Some(ty) = impl_.self_ty() {
edit.add_placeholder_snippet(cap, ty);
}

if let Some(item) = impl_.assoc_item_list().and_then(|it| it.assoc_items().next()) {
edit.add_tabstop_before(cap, item);
} else if let Some(l_curly) =
impl_.assoc_item_list().and_then(|it| it.l_curly_token())
{
edit.add_tabstop_after_token(cap, l_curly);
}
}

insert_impl(impl_, &trait_);
},
)
}

#[cfg(test)]
mod tests {
use crate::tests::{check_assist, check_assist_target};
Expand Down Expand Up @@ -485,4 +585,172 @@ mod tests {
"#,
);
}

#[test]
fn test_add_impl_trait() {
check_assist(
generate_impl_trait,
r#"
trait $0Foo {
fn foo(&self) -> i32;

fn bar(&self) -> i32 {
self.foo()
}
}
"#,
r#"
trait Foo {
fn foo(&self) -> i32;

fn bar(&self) -> i32 {
self.foo()
}
}

impl Foo for ${1:_} {
$0fn foo(&self) -> i32 {
todo!()
}
}
"#,
);
}

#[test]
fn test_add_impl_trait_use_generic() {
check_assist(
generate_impl_trait,
r#"
trait $0Foo<T> {
fn foo(&self) -> T;

fn bar(&self) -> T {
self.foo()
}
}
"#,
r#"
trait Foo<T> {
fn foo(&self) -> T;

fn bar(&self) -> T {
self.foo()
}
}

impl Foo<${1:_}> for ${2:_} {
$0fn foo(&self) -> _ {
todo!()
}
}
"#,
);
check_assist(
generate_impl_trait,
r#"
trait $0Foo<T, U> {
fn foo(&self) -> T;

fn bar(&self) -> T {
self.foo()
}
}
"#,
r#"
trait Foo<T, U> {
fn foo(&self) -> T;

fn bar(&self) -> T {
self.foo()
}
}

impl Foo<${1:_}, ${2:_}> for ${3:_} {
$0fn foo(&self) -> _ {
todo!()
}
}
"#,
);
}

#[test]
fn test_add_impl_trait_docs() {
check_assist(
generate_impl_trait,
r#"
/// foo
trait $0Foo {
/// foo method
fn foo(&self) -> i32;

fn bar(&self) -> i32 {
self.foo()
}
}
"#,
r#"
/// foo
trait Foo {
/// foo method
fn foo(&self) -> i32;

fn bar(&self) -> i32 {
self.foo()
}
}

impl Foo for ${1:_} {
$0fn foo(&self) -> i32 {
todo!()
}
}
"#,
);
}

#[test]
fn test_add_impl_trait_assoc_types() {
check_assist(
generate_impl_trait,
r#"
trait $0Foo {
type Output;

fn foo(&self) -> Self::Output;
}
"#,
r#"
trait Foo {
type Output;

fn foo(&self) -> Self::Output;
}

impl Foo for ${1:_} {
$0type Output;

fn foo(&self) -> Self::Output {
todo!()
}
}
"#,
);
}

#[test]
fn test_add_impl_trait_empty() {
check_assist(
generate_impl_trait,
r#"
trait $0Foo {}
"#,
r#"
trait Foo {}

impl Foo for ${1:_} {$0}
"#,
);
}
}
1 change: 1 addition & 0 deletions crates/ide-assists/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ mod handlers {
generate_function::generate_function,
generate_impl::generate_impl,
generate_impl::generate_trait_impl,
generate_impl::generate_impl_trait,
generate_is_empty_from_len::generate_is_empty_from_len,
generate_mut_trait_impl::generate_mut_trait_impl,
generate_new::generate_new,
Expand Down
23 changes: 23 additions & 0 deletions crates/ide-assists/src/tests/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1880,6 +1880,29 @@ impl<T: Clone> Ctx<T> {$0}
)
}

#[test]
fn doctest_generate_impl_trait() {
check_doc_test(
"generate_impl_trait",
r#####"
trait $0Foo {
fn foo(&self) -> i32;
}
"#####,
r#####"
trait Foo {
fn foo(&self) -> i32;
}

impl Foo for ${1:_} {
$0fn foo(&self) -> i32 {
todo!()
}
}
"#####,
)
}

#[test]
fn doctest_generate_is_empty_from_len() {
check_doc_test(
Expand Down