2019-01-03 06:08:32 -06:00
|
|
|
use ra_syntax::{
|
2020-04-10 04:49:13 -05:00
|
|
|
ast::{self, AstNode, NameOwner, TypeParamsOwner},
|
2020-04-24 16:40:41 -05:00
|
|
|
TextSize,
|
2019-01-03 06:08:32 -06:00
|
|
|
};
|
2020-03-28 05:01:25 -05:00
|
|
|
use stdx::{format_to, SepBy};
|
2019-01-03 06:08:32 -06:00
|
|
|
|
2019-07-04 15:05:17 -05:00
|
|
|
use crate::{Assist, AssistCtx, AssistId};
|
2019-01-03 06:08:32 -06:00
|
|
|
|
2019-10-25 15:38:15 -05:00
|
|
|
// Assist: add_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> {
|
|
|
|
// data: T,<|>
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// struct Ctx<T: Clone> {
|
|
|
|
// data: T,
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// impl<T: Clone> Ctx<T> {
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
// ```
|
2020-02-06 09:58:57 -06:00
|
|
|
pub(crate) fn add_impl(ctx: AssistCtx) -> Option<Assist> {
|
2019-10-27 03:48:40 -05:00
|
|
|
let nominal = ctx.find_node_at_offset::<ast::NominalDef>()?;
|
2019-01-03 06:08:32 -06:00
|
|
|
let name = nominal.name()?;
|
2020-05-06 05:51:28 -05:00
|
|
|
let target = nominal.syntax().text_range();
|
|
|
|
ctx.add_assist(
|
|
|
|
AssistId("add_impl"),
|
|
|
|
format!("Implement {}", name.text().as_str()),
|
|
|
|
target,
|
|
|
|
|edit| {
|
|
|
|
let type_params = nominal.type_param_list();
|
|
|
|
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-03-28 05:01:25 -05:00
|
|
|
|
2020-05-06 05:51:28 -05:00
|
|
|
let generic_params = lifetime_params.chain(type_params).sep_by(", ");
|
|
|
|
format_to!(buf, "<{}>", generic_params)
|
|
|
|
}
|
|
|
|
buf.push_str(" {\n");
|
|
|
|
edit.set_cursor(start_offset + TextSize::of(&buf));
|
|
|
|
buf.push_str("\n}");
|
|
|
|
edit.insert(start_offset, buf);
|
|
|
|
},
|
|
|
|
)
|
2019-01-03 06:08:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2020-05-06 03:16:55 -05:00
|
|
|
use crate::tests::{check_assist, check_assist_target};
|
2019-01-03 06:08:32 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_add_impl() {
|
2019-02-08 05:49:43 -06:00
|
|
|
check_assist(add_impl, "struct Foo {<|>}\n", "struct Foo {}\n\nimpl Foo {\n<|>\n}\n");
|
2019-01-03 09:59:17 -06:00
|
|
|
check_assist(
|
|
|
|
add_impl,
|
2019-01-03 06:08:32 -06:00
|
|
|
"struct Foo<T: Clone> {<|>}",
|
|
|
|
"struct Foo<T: Clone> {}\n\nimpl<T: Clone> Foo<T> {\n<|>\n}",
|
|
|
|
);
|
2019-01-03 09:59:17 -06:00
|
|
|
check_assist(
|
|
|
|
add_impl,
|
2019-01-03 06:08:32 -06:00
|
|
|
"struct Foo<'a, T: Foo<'a>> {<|>}",
|
|
|
|
"struct Foo<'a, T: Foo<'a>> {}\n\nimpl<'a, T: Foo<'a>> Foo<'a, T> {\n<|>\n}",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-02-08 17:34:05 -06:00
|
|
|
#[test]
|
|
|
|
fn add_impl_target() {
|
|
|
|
check_assist_target(
|
|
|
|
add_impl,
|
|
|
|
"
|
|
|
|
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 06:08:32 -06:00
|
|
|
}
|