2019-09-30 03:58:53 -05:00
|
|
|
//! FIXME: write short doc here
|
|
|
|
|
2019-07-20 12:04:34 -05:00
|
|
|
use format_buf::format;
|
2019-02-03 12:26:35 -06:00
|
|
|
use hir::db::HirDatabase;
|
2019-07-04 15:05:17 -05:00
|
|
|
use join_to_string::join;
|
2019-01-03 06:08:32 -06:00
|
|
|
use ra_syntax::{
|
2019-03-30 05:25:53 -05:00
|
|
|
ast::{self, AstNode, NameOwner, TypeParamsOwner},
|
2019-01-03 06:08:32 -06:00
|
|
|
TextUnit,
|
|
|
|
};
|
|
|
|
|
2019-07-04 15:05:17 -05:00
|
|
|
use crate::{Assist, AssistCtx, AssistId};
|
2019-01-03 06:08:32 -06:00
|
|
|
|
2019-02-11 11:07:21 -06:00
|
|
|
pub(crate) fn add_impl(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
2019-01-03 09:59:17 -06:00
|
|
|
let nominal = ctx.node_at_offset::<ast::NominalDef>()?;
|
2019-01-03 06:08:32 -06:00
|
|
|
let name = nominal.name()?;
|
2019-02-24 04:53:35 -06:00
|
|
|
ctx.add_action(AssistId("add_impl"), "add impl", |edit| {
|
2019-07-20 04:58:27 -05:00
|
|
|
edit.target(nominal.syntax().text_range());
|
2019-01-03 06:08:32 -06:00
|
|
|
let type_params = nominal.type_param_list();
|
2019-07-20 04:58:27 -05:00
|
|
|
let start_offset = nominal.syntax().text_range().end();
|
2019-01-03 06:08:32 -06:00
|
|
|
let mut buf = String::new();
|
|
|
|
buf.push_str("\n\nimpl");
|
2019-07-19 03:24:41 -05:00
|
|
|
if let Some(type_params) = &type_params {
|
2019-07-20 12:04:34 -05:00
|
|
|
format!(buf, "{}", type_params.syntax());
|
2019-01-03 06:08:32 -06:00
|
|
|
}
|
|
|
|
buf.push_str(" ");
|
|
|
|
buf.push_str(name.text().as_str());
|
|
|
|
if let Some(type_params) = type_params {
|
2019-03-30 05:25:53 -05:00
|
|
|
let lifetime_params = type_params
|
|
|
|
.lifetime_params()
|
|
|
|
.filter_map(|it| it.lifetime_token())
|
2019-07-19 03:24:41 -05:00
|
|
|
.map(|it| it.text().clone());
|
2019-02-08 05:49:43 -06:00
|
|
|
let type_params =
|
2019-07-19 03:24:41 -05:00
|
|
|
type_params.type_params().filter_map(|it| it.name()).map(|it| it.text().clone());
|
2019-02-08 05:49:43 -06:00
|
|
|
join(lifetime_params.chain(type_params)).surround_with("<", ">").to_buf(&mut buf);
|
2019-01-03 06:08:32 -06:00
|
|
|
}
|
|
|
|
buf.push_str(" {\n");
|
2019-01-03 09:59:17 -06:00
|
|
|
edit.set_cursor(start_offset + TextUnit::of_str(&buf));
|
2019-01-03 06:08:32 -06:00
|
|
|
buf.push_str("\n}");
|
|
|
|
edit.insert(start_offset, buf);
|
2019-02-11 11:07:21 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
ctx.build()
|
2019-01-03 06:08:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-02-08 17:34:05 -06:00
|
|
|
use crate::helpers::{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
|
|
|
}
|