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

110 lines
3.1 KiB
Rust
Raw Normal View History

use ra_syntax::ast::{self, AstNode, GenericParamsOwner, NameOwner};
use stdx::{format_to, SepBy};
2019-01-03 15:08:32 +03:00
2020-06-28 18:36:05 -04:00
use crate::{AssistContext, AssistId, AssistKind, Assists};
2019-01-03 15:08:32 +03:00
2020-07-03 18:15:03 +02:00
// Assist: generate_impl
2019-10-26 17:27:47 +03:00
//
2019-10-26 19:08:13 +03:00
// Adds a new inherent impl for a type.
2019-10-26 17:27:47 +03:00
//
2019-10-25 23:38:15 +03:00
// ```
// struct Ctx<T: Clone> {
2020-05-17 14:21:24 +02:00
// data: T,<|>
2019-10-25 23:38:15 +03:00
// }
// ```
// ->
// ```
// struct Ctx<T: Clone> {
2020-05-17 14:21:24 +02:00
// data: T,
2019-10-25 23:38:15 +03:00
// }
//
// impl<T: Clone> Ctx<T> {
2020-05-17 14:21:24 +02:00
// $0
2019-10-25 23:38:15 +03:00
// }
// ```
2020-07-03 18:15:03 +02:00
pub(crate) fn generate_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
2020-07-29 19:22:15 +02:00
let nominal = ctx.find_node_at_offset::<ast::AdtDef>()?;
2019-01-03 15:08:32 +03:00
let name = nominal.name()?;
let target = nominal.syntax().text_range();
2020-06-28 18:36:05 -04:00
acc.add(
2020-07-03 19:14:42 +02:00
AssistId("generate_impl", AssistKind::Generate),
2020-07-03 18:15:03 +02:00
format!("Generate impl for `{}`", name),
2020-06-28 18:36:05 -04:00
target,
|edit| {
let type_params = nominal.generic_param_list();
2020-06-28 18:36:05 -04:00
let start_offset = nominal.syntax().text_range().end();
let mut buf = String::new();
buf.push_str("\n\nimpl");
if let Some(type_params) = &type_params {
format_to!(buf, "{}", type_params.syntax());
}
buf.push_str(" ");
buf.push_str(name.text().as_str());
if let Some(type_params) = type_params {
let lifetime_params = type_params
.lifetime_params()
.filter_map(|it| it.lifetime_token())
.map(|it| it.text().clone());
let type_params = type_params
.type_params()
.filter_map(|it| it.name())
.map(|it| it.text().clone());
2020-06-28 18:36:05 -04:00
let generic_params = lifetime_params.chain(type_params).sep_by(", ");
format_to!(buf, "<{}>", generic_params)
2020-05-17 14:21:24 +02:00
}
2020-06-28 18:36:05 -04: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 14:21:24 +02:00
}
2020-06-28 18:36:05 -04:00
},
)
2019-01-03 15:08:32 +03:00
}
#[cfg(test)]
mod tests {
2020-05-06 10:16:55 +02:00
use crate::tests::{check_assist, check_assist_target};
2019-01-03 15:08:32 +03:00
2020-05-17 14:21:24 +02:00
use super::*;
2019-01-03 15:08:32 +03:00
#[test]
fn test_add_impl() {
2019-01-03 18:59:17 +03:00
check_assist(
2020-07-03 18:15:03 +02:00
generate_impl,
"struct Foo {<|>}\n",
"struct Foo {}\n\nimpl Foo {\n $0\n}\n",
);
check_assist(
generate_impl,
2019-01-03 15:08:32 +03:00
"struct Foo<T: Clone> {<|>}",
2020-05-17 14:21:24 +02:00
"struct Foo<T: Clone> {}\n\nimpl<T: Clone> Foo<T> {\n $0\n}",
2019-01-03 15:08:32 +03:00
);
2019-01-03 18:59:17 +03:00
check_assist(
2020-07-03 18:15:03 +02:00
generate_impl,
2019-01-03 15:08:32 +03:00
"struct Foo<'a, T: Foo<'a>> {<|>}",
2020-05-17 14:21:24 +02:00
"struct Foo<'a, T: Foo<'a>> {}\n\nimpl<'a, T: Foo<'a>> Foo<'a, T> {\n $0\n}",
2019-01-03 15:08:32 +03:00
);
}
2019-02-09 00:34:05 +01:00
#[test]
fn add_impl_target() {
check_assist_target(
2020-07-03 18:15:03 +02:00
generate_impl,
2019-02-09 00:34:05 +01:00
"
struct SomeThingIrrelevant;
/// Has a lifetime parameter
struct Foo<'a, T: Foo<'a>> {<|>}
struct EvenMoreIrrelevant;
",
"/// Has a lifetime parameter
struct Foo<'a, T: Foo<'a>> {}",
);
}
2019-01-03 15:08:32 +03:00
}