rust-analyzer/crates/ide_assists/src/handlers/generate_delegate.rs

222 lines
6.2 KiB
Rust
Raw Normal View History

2021-10-14 10:34:31 +00:00
use hir::{self, HasCrate, HasSource, HirDisplay};
2021-10-13 18:13:36 +00:00
use syntax::ast::{self, make, AstNode, HasName, HasVisibility};
2021-10-13 13:08:40 +00:00
use crate::{
2021-10-13 21:59:23 +00:00
utils::{find_struct_impl, render_snippet, Cursor},
2021-10-13 13:08:40 +00:00
AssistContext, AssistId, AssistKind, Assists, GroupLabel,
};
2021-10-14 10:34:31 +00:00
use syntax::ast::edit::AstNodeEdit;
2021-10-13 13:08:40 +00:00
// Assist: generate_setter
//
// Generate a setter method.
//
// ```
// struct Person {
// nam$0e: String,
// }
// ```
// ->
// ```
// struct Person {
// name: String,
// }
//
// impl Person {
// /// Set the person's name.
// fn set_name(&mut self, name: String) {
// self.name = name;
// }
// }
// ```
pub(crate) fn generate_delegate(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let strukt = ctx.find_node_at_offset::<ast::Struct>()?;
2021-10-13 21:59:23 +00:00
let strukt_name = strukt.name()?;
2021-10-13 13:08:40 +00:00
2021-10-13 21:59:23 +00:00
let field = ctx.find_node_at_offset::<ast::RecordField>()?;
2021-10-14 10:34:31 +00:00
let field_name = field.name()?;
2021-10-13 13:08:40 +00:00
let field_ty = field.ty()?;
let sema_field_ty = ctx.sema.resolve_type(&field_ty)?;
let krate = sema_field_ty.krate(ctx.db());
let mut methods = vec![];
sema_field_ty.iterate_assoc_items(ctx.db(), krate, |item| {
if let hir::AssocItem::Function(f) = item {
if f.self_param(ctx.db()).is_some() {
methods.push(f)
}
}
Some(())
});
let target = field_ty.syntax().text_range();
for method in methods {
let impl_def = find_struct_impl(
ctx,
&ast::Adt::Struct(strukt.clone()),
&method.name(ctx.db()).to_string(),
)?;
acc.add_group(
&GroupLabel("Generate delegate".to_owned()),
AssistId("generate_delegate", AssistKind::Generate),
format!("Generate a delegate method for '{}'", method.name(ctx.db())),
target,
|builder| {
2021-10-13 18:13:36 +00:00
// make function
2021-10-14 10:34:31 +00:00
let method_source = match method.source(ctx.db()) {
Some(source) => source.value,
None => return,
};
let method_name = method.name(ctx.db());
let vis = method_source.visibility();
2021-10-13 18:13:36 +00:00
let name = make::name(&method.name(ctx.db()).to_string());
let type_params = None;
2021-10-14 10:34:31 +00:00
let self_ty = method
.self_param(ctx.db())
.map(|s| s.source(ctx.db()).map(|s| s.value))
.flatten();
let params = make::param_list(self_ty, []);
let tail_expr = make::expr_method_call(
field_from_idents(["self", &field_name.to_string()]).unwrap(),
make::name_ref(&method_name.to_string()),
make::arg_list([]),
);
let body = make::block_expr([], Some(tail_expr));
2021-10-13 18:13:36 +00:00
let ret_type = &method.ret_type(ctx.db()).display(ctx.db()).to_string();
let ret_type = Some(make::ret_type(make::ty(ret_type)));
let is_async = false;
2021-10-14 10:34:31 +00:00
let f = make::fn_(vis, name, type_params, params, body, ret_type, is_async)
.indent(ast::edit::IndentLevel(1))
.clone_for_update();
2021-10-13 13:08:40 +00:00
2021-10-13 18:13:36 +00:00
let cursor = Cursor::Before(f.syntax());
2021-10-13 21:59:23 +00:00
let cap = ctx.config.snippet_cap.unwrap(); // FIXME.
2021-10-13 18:13:36 +00:00
2021-10-14 10:34:31 +00:00
// Create or update an impl block, and attach the function to it.
2021-10-13 21:59:23 +00:00
match impl_def {
Some(impl_def) => {
2021-10-14 10:34:31 +00:00
// Remember where in our source our `impl` block lives.
2021-10-13 21:59:23 +00:00
let impl_def = impl_def.clone_for_update();
let old_range = impl_def.syntax().text_range();
2021-10-14 10:34:31 +00:00
// Attach the function to the impl block
2021-10-13 21:59:23 +00:00
let assoc_items = impl_def.get_or_create_assoc_item_list();
assoc_items.add_item(f.clone().into());
2021-10-14 10:34:31 +00:00
// Update the impl block.
2021-10-13 21:59:23 +00:00
let snippet = render_snippet(cap, impl_def.syntax(), cursor);
builder.replace_snippet(cap, old_range, snippet);
}
None => {
2021-10-14 10:34:31 +00:00
// Attach the function to the impl block
2021-10-13 21:59:23 +00:00
let name = &strukt_name.to_string();
2021-10-14 10:34:31 +00:00
let impl_def = make::impl_(make::ext::ident_path(name)).clone_for_update();
2021-10-13 21:59:23 +00:00
let assoc_items = impl_def.get_or_create_assoc_item_list();
assoc_items.add_item(f.clone().into());
2021-10-14 10:34:31 +00:00
// Insert the impl block.
let offset = strukt.syntax().text_range().end();
2021-10-13 21:59:23 +00:00
let snippet = render_snippet(cap, impl_def.syntax(), cursor);
2021-10-14 10:34:31 +00:00
let snippet = format!("\n\n{}", snippet);
builder.insert_snippet(cap, offset, snippet);
2021-10-13 21:59:23 +00:00
}
}
2021-10-13 13:08:40 +00:00
},
)?;
}
Some(())
}
2021-10-14 10:34:31 +00:00
pub fn field_from_idents<'a>(
parts: impl std::iter::IntoIterator<Item = &'a str>,
) -> Option<ast::Expr> {
let mut iter = parts.into_iter();
let base = make::expr_path(make::ext::ident_path(iter.next()?));
let expr = iter.fold(base, |base, s| make::expr_field(base, s));
Some(expr)
}
2021-10-13 13:08:40 +00:00
#[cfg(test)]
mod tests {
use crate::tests::check_assist;
use super::*;
#[test]
2021-10-14 10:34:31 +00:00
fn test_generate_delegate_create_impl_block() {
check_assist(
generate_delegate,
r#"
struct Age(u8);
impl Age {
fn age(&self) -> u8 {
self.0
}
}
struct Person {
ag$0e: Age,
}"#,
r#"
struct Age(u8);
impl Age {
fn age(&self) -> u8 {
self.0
}
}
struct Person {
age: Age,
}
impl Person {
$0fn age(&self) -> u8 {
self.age.age()
}
}"#,
);
}
#[test]
fn test_generate_delegate_update_impl_block() {
2021-10-13 13:08:40 +00:00
check_assist(
generate_delegate,
r#"
struct Age(u8);
impl Age {
fn age(&self) -> u8 {
self.0
}
}
struct Person {
ag$0e: Age,
}
2021-10-13 21:59:23 +00:00
2021-10-14 10:34:31 +00:00
impl Person {}"#,
2021-10-13 13:08:40 +00:00
r#"
struct Age(u8);
impl Age {
2021-10-14 10:34:31 +00:00
fn age(&self) -> u8 {
2021-10-13 13:08:40 +00:00
self.0
}
}
struct Person {
age: Age,
}
impl Person {
2021-10-14 10:34:31 +00:00
$0fn age(&self) -> u8 {
self.age.age()
}
}"#,
);
}
2021-10-13 13:08:40 +00:00
self.age.age()
}
}"#,
);
}
}