rust/crates/assists/src/handlers/generate_impl.rs

152 lines
4.2 KiB
Rust
Raw Normal View History

2020-08-12 08:04:06 -05:00
use itertools::Itertools;
use stdx::format_to;
2021-01-19 16:56:11 -06:00
use syntax::{
ast::{self, AstNode, AttrsOwner, GenericParamsOwner, NameOwner},
SmolStr,
};
2019-01-03 06:08:32 -06:00
2020-06-28 17:36:05 -05:00
use crate::{AssistContext, AssistId, AssistKind, Assists};
2019-01-03 06:08:32 -06:00
2020-07-03 11:15:03 -05:00
// Assist: generate_impl
2019-10-26 09:27:47 -05:00
//
2019-10-26 11:08:13 -05:00
// Adds a new inherent impl for a type.
2019-10-26 09:27:47 -05:00
//
2019-10-25 15:38:15 -05:00
// ```
// struct Ctx<T: Clone> {
2021-01-06 14:15:48 -06:00
// data: T,$0
2019-10-25 15:38:15 -05:00
// }
// ```
// ->
// ```
// struct Ctx<T: Clone> {
2020-05-17 07:21:24 -05:00
// data: T,
2019-10-25 15:38:15 -05:00
// }
//
// impl<T: Clone> Ctx<T> {
2020-05-17 07:21:24 -05:00
// $0
2019-10-25 15:38:15 -05:00
// }
// ```
2020-07-03 11:15:03 -05:00
pub(crate) fn generate_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
2020-07-29 12:22:15 -05:00
let nominal = ctx.find_node_at_offset::<ast::AdtDef>()?;
2019-01-03 06:08:32 -06:00
let name = nominal.name()?;
let target = nominal.syntax().text_range();
2020-06-28 17:36:05 -05:00
acc.add(
2020-07-03 12:14:42 -05:00
AssistId("generate_impl", AssistKind::Generate),
2020-07-03 11:15:03 -05:00
format!("Generate impl for `{}`", name),
2020-06-28 17:36:05 -05:00
target,
|edit| {
let type_params = nominal.generic_param_list();
2020-06-28 17:36:05 -05:00
let start_offset = nominal.syntax().text_range().end();
let mut buf = String::new();
buf.push_str("\n\n");
nominal
.attrs()
.filter(|attr| {
attr.as_simple_call().map(|(name, _arg)| name == "cfg").unwrap_or(false)
})
.for_each(|attr| buf.push_str(format!("{}\n", attr.to_string()).as_str()));
buf.push_str("impl");
2020-06-28 17:36:05 -05:00
if let Some(type_params) = &type_params {
format_to!(buf, "{}", type_params.syntax());
}
buf.push_str(" ");
2021-01-19 16:56:11 -06:00
buf.push_str(name.text());
2020-06-28 17:36:05 -05:00
if let Some(type_params) = type_params {
let lifetime_params = type_params
.lifetime_params()
2020-12-15 12:23:51 -06:00
.filter_map(|it| it.lifetime())
2021-01-19 16:56:11 -06:00
.map(|it| SmolStr::from(it.text()));
2020-06-28 17:36:05 -05:00
let type_params = type_params
.type_params()
.filter_map(|it| it.name())
2021-01-19 16:56:11 -06:00
.map(|it| SmolStr::from(it.text()));
2020-08-12 08:04:06 -05:00
let generic_params = lifetime_params.chain(type_params).format(", ");
2020-06-28 17:36:05 -05:00
format_to!(buf, "<{}>", generic_params)
2020-05-17 07:21:24 -05:00
}
2020-06-28 17:36:05 -05:00
match ctx.config.snippet_cap {
Some(cap) => {
buf.push_str(" {\n $0\n}");
edit.insert_snippet(cap, start_offset, buf);
}
None => {
buf.push_str(" {\n}");
edit.insert(start_offset, buf);
}
2020-05-17 07:21:24 -05:00
}
2020-06-28 17:36:05 -05:00
},
)
2019-01-03 06:08:32 -06:00
}
#[cfg(test)]
mod tests {
2020-05-06 03:16:55 -05:00
use crate::tests::{check_assist, check_assist_target};
2019-01-03 06:08:32 -06:00
2020-05-17 07:21:24 -05:00
use super::*;
2019-01-03 06:08:32 -06:00
#[test]
fn test_add_impl() {
2019-01-03 09:59:17 -06:00
check_assist(
2020-07-03 11:15:03 -05:00
generate_impl,
2021-01-06 14:15:48 -06:00
"struct Foo {$0}\n",
2020-07-03 11:15:03 -05:00
"struct Foo {}\n\nimpl Foo {\n $0\n}\n",
);
check_assist(
generate_impl,
2021-01-06 14:15:48 -06:00
"struct Foo<T: Clone> {$0}",
2020-05-17 07:21:24 -05:00
"struct Foo<T: Clone> {}\n\nimpl<T: Clone> Foo<T> {\n $0\n}",
2019-01-03 06:08:32 -06:00
);
2019-01-03 09:59:17 -06:00
check_assist(
2020-07-03 11:15:03 -05:00
generate_impl,
2021-01-06 14:15:48 -06:00
"struct Foo<'a, T: Foo<'a>> {$0}",
2020-05-17 07:21:24 -05:00
"struct Foo<'a, T: Foo<'a>> {}\n\nimpl<'a, T: Foo<'a>> Foo<'a, T> {\n $0\n}",
2019-01-03 06:08:32 -06:00
);
check_assist(
generate_impl,
r#"
#[cfg(feature = "foo")]
2021-01-06 14:15:48 -06:00
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"))]
2021-01-06 14:15:48 -06:00
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
}"#,
);
2019-01-03 06:08:32 -06:00
}
2019-02-08 17:34:05 -06:00
#[test]
fn add_impl_target() {
check_assist_target(
2020-07-03 11:15:03 -05:00
generate_impl,
2019-02-08 17:34:05 -06:00
"
struct SomeThingIrrelevant;
/// Has a lifetime parameter
2021-01-06 14:15:48 -06:00
struct Foo<'a, T: Foo<'a>> {$0}
2019-02-08 17:34:05 -06:00
struct EvenMoreIrrelevant;
",
"/// Has a lifetime parameter
struct Foo<'a, T: Foo<'a>> {}",
);
}
2019-01-03 06:08:32 -06:00
}