From e4756cb4f6e66097638b9d101589358976be2ba8 Mon Sep 17 00:00:00 2001 From: Chetan Khilosiya Date: Tue, 23 Feb 2021 00:17:48 +0530 Subject: 7526: Rename crate assists to ide_assists. --- crates/ide_assists/src/handlers/generate_impl.rs | 166 +++++++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 crates/ide_assists/src/handlers/generate_impl.rs (limited to 'crates/ide_assists/src/handlers/generate_impl.rs') diff --git a/crates/ide_assists/src/handlers/generate_impl.rs b/crates/ide_assists/src/handlers/generate_impl.rs new file mode 100644 index 000000000..a8e3c4fc2 --- /dev/null +++ b/crates/ide_assists/src/handlers/generate_impl.rs @@ -0,0 +1,166 @@ +use syntax::ast::{self, AstNode, NameOwner}; + +use crate::{utils::generate_impl_text, AssistContext, AssistId, AssistKind, Assists}; + +// Assist: generate_impl +// +// Adds a new inherent impl for a type. +// +// ``` +// struct Ctx { +// data: T,$0 +// } +// ``` +// -> +// ``` +// struct Ctx { +// data: T, +// } +// +// impl Ctx { +// $0 +// } +// ``` +pub(crate) fn generate_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { + let nominal = ctx.find_node_at_offset::()?; + let name = nominal.name()?; + let target = nominal.syntax().text_range(); + + acc.add( + AssistId("generate_impl", AssistKind::Generate), + format!("Generate impl for `{}`", name), + target, + |edit| { + let start_offset = nominal.syntax().text_range().end(); + match ctx.config.snippet_cap { + Some(cap) => { + let snippet = generate_impl_text(&nominal, " $0"); + edit.insert_snippet(cap, start_offset, snippet); + } + None => { + let snippet = generate_impl_text(&nominal, ""); + edit.insert(start_offset, snippet); + } + } + }, + ) +} + +#[cfg(test)] +mod tests { + use crate::tests::{check_assist, check_assist_target}; + + use super::*; + + #[test] + fn test_add_impl() { + check_assist( + generate_impl, + "struct Foo {$0}\n", + "struct Foo {}\n\nimpl Foo {\n $0\n}\n", + ); + check_assist( + generate_impl, + "struct Foo {$0}", + "struct Foo {}\n\nimpl Foo {\n $0\n}", + ); + check_assist( + generate_impl, + "struct Foo<'a, T: Foo<'a>> {$0}", + "struct Foo<'a, T: Foo<'a>> {}\n\nimpl<'a, T: Foo<'a>> Foo<'a, T> {\n $0\n}", + ); + check_assist( + generate_impl, + r#" + #[cfg(feature = "foo")] + struct Foo<'a, T: Foo<'a>> {$0}"#, + r#" + #[cfg(feature = "foo")] + struct Foo<'a, T: Foo<'a>> {} + + #[cfg(feature = "foo")] + impl<'a, T: Foo<'a>> Foo<'a, T> { + $0 + }"#, + ); + + check_assist( + generate_impl, + r#" + #[cfg(not(feature = "foo"))] + struct Foo<'a, T: Foo<'a>> {$0}"#, + r#" + #[cfg(not(feature = "foo"))] + struct Foo<'a, T: Foo<'a>> {} + + #[cfg(not(feature = "foo"))] + impl<'a, T: Foo<'a>> Foo<'a, T> { + $0 + }"#, + ); + + check_assist( + generate_impl, + r#" + struct Defaulted {}$0"#, + r#" + struct Defaulted {} + + impl Defaulted { + $0 + }"#, + ); + + check_assist( + generate_impl, + r#" + struct Defaulted<'a, 'b: 'a, T: Debug + Clone + 'a + 'b = String> {}$0"#, + r#" + struct Defaulted<'a, 'b: 'a, T: Debug + Clone + 'a + 'b = String> {} + + impl<'a, 'b: 'a, T: Debug + Clone + 'a + 'b> Defaulted<'a, 'b, T> { + $0 + }"#, + ); + + check_assist( + generate_impl, + r#"pub trait Trait {} +struct Struct$0 +where + T: Trait, +{ + inner: T, +}"#, + r#"pub trait Trait {} +struct Struct +where + T: Trait, +{ + inner: T, +} + +impl Struct +where + T: Trait, +{ + $0 +}"#, + ); + } + + #[test] + fn add_impl_target() { + check_assist_target( + generate_impl, + " +struct SomeThingIrrelevant; +/// Has a lifetime parameter +struct Foo<'a, T: Foo<'a>> {$0} +struct EvenMoreIrrelevant; +", + "/// Has a lifetime parameter +struct Foo<'a, T: Foo<'a>> {}", + ); + } +} -- cgit v1.2.3