mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-04 23:50:47 +00:00
move assists to a separate crate
This commit is contained in:
parent
736a55c97e
commit
0c5fd8f7cb
26 changed files with 580 additions and 578 deletions
67
crates/ra_assists/src/add_impl.rs
Normal file
67
crates/ra_assists/src/add_impl.rs
Normal file
|
@ -0,0 +1,67 @@
|
|||
use join_to_string::join;
|
||||
use hir::db::HirDatabase;
|
||||
use ra_syntax::{
|
||||
ast::{self, AstNode, AstToken, NameOwner, TypeParamsOwner},
|
||||
TextUnit,
|
||||
};
|
||||
|
||||
use crate::{AssistCtx, Assist};
|
||||
|
||||
pub(crate) fn add_impl(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
||||
let nominal = ctx.node_at_offset::<ast::NominalDef>()?;
|
||||
let name = nominal.name()?;
|
||||
ctx.build("add impl", |edit| {
|
||||
let type_params = nominal.type_param_list();
|
||||
let start_offset = nominal.syntax().range().end();
|
||||
let mut buf = String::new();
|
||||
buf.push_str("\n\nimpl");
|
||||
if let Some(type_params) = type_params {
|
||||
type_params.syntax().text().push_to(&mut buf);
|
||||
}
|
||||
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())
|
||||
.map(|it| it.text());
|
||||
let type_params = type_params
|
||||
.type_params()
|
||||
.filter_map(|it| it.name())
|
||||
.map(|it| it.text());
|
||||
join(lifetime_params.chain(type_params))
|
||||
.surround_with("<", ">")
|
||||
.to_buf(&mut buf);
|
||||
}
|
||||
buf.push_str(" {\n");
|
||||
edit.set_cursor(start_offset + TextUnit::of_str(&buf));
|
||||
buf.push_str("\n}");
|
||||
edit.insert(start_offset, buf);
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::helpers::check_assist;
|
||||
|
||||
#[test]
|
||||
fn test_add_impl() {
|
||||
check_assist(
|
||||
add_impl,
|
||||
"struct Foo {<|>}\n",
|
||||
"struct Foo {}\n\nimpl Foo {\n<|>\n}\n",
|
||||
);
|
||||
check_assist(
|
||||
add_impl,
|
||||
"struct Foo<T: Clone> {<|>}",
|
||||
"struct Foo<T: Clone> {}\n\nimpl<T: Clone> Foo<T> {\n<|>\n}",
|
||||
);
|
||||
check_assist(
|
||||
add_impl,
|
||||
"struct Foo<'a, T: Foo<'a>> {<|>}",
|
||||
"struct Foo<'a, T: Foo<'a>> {}\n\nimpl<'a, T: Foo<'a>> Foo<'a, T> {\n<|>\n}",
|
||||
);
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue