Transform paths before make::fn_

This commit is contained in:
roife 2024-01-06 20:09:51 +08:00
parent 872951d2d9
commit ba952e65ff

View file

@ -107,31 +107,48 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
|edit| { |edit| {
// Create the function // Create the function
let method_source = match ctx.sema.source(method) { let method_source = match ctx.sema.source(method) {
Some(source) => source.value, Some(source) => {
let v = source.value.clone_for_update();
let source_scope = ctx.sema.scope(v.syntax());
let target_scope = ctx.sema.scope(strukt.syntax());
if let (Some(s), Some(t)) = (source_scope, target_scope) {
PathTransform::generic_transformation(&t, &s).apply(v.syntax());
}
v
}
None => return, None => return,
}; };
let vis = method_source.visibility(); let vis = method_source.visibility();
let fn_name = make::name(&name);
let params =
method_source.param_list().unwrap_or_else(|| make::param_list(None, []));
let type_params = method_source.generic_param_list();
let arg_list = match method_source.param_list() {
Some(list) => convert_param_list_to_arg_list(list),
None => make::arg_list([]),
};
let tail_expr = make::expr_method_call(field, make::name_ref(&name), arg_list);
let ret_type = method_source.ret_type();
let is_async = method_source.async_token().is_some(); let is_async = method_source.async_token().is_some();
let is_const = method_source.const_token().is_some(); let is_const = method_source.const_token().is_some();
let is_unsafe = method_source.unsafe_token().is_some(); let is_unsafe = method_source.unsafe_token().is_some();
let fn_name = make::name(&name);
let type_params = method_source.generic_param_list();
let where_clause = method_source.where_clause();
let params =
method_source.param_list().unwrap_or_else(|| make::param_list(None, []));
// compute the `body`
let arg_list = method_source
.param_list()
.map(|list| convert_param_list_to_arg_list(list))
.unwrap_or_else(|| make::arg_list([]));
let tail_expr = make::expr_method_call(field, make::name_ref(&name), arg_list);
let tail_expr_finished = let tail_expr_finished =
if is_async { make::expr_await(tail_expr) } else { tail_expr }; if is_async { make::expr_await(tail_expr) } else { tail_expr };
let body = make::block_expr([], Some(tail_expr_finished)); let body = make::block_expr([], Some(tail_expr_finished));
let ret_type = method_source.ret_type();
let f = make::fn_( let f = make::fn_(
vis, vis,
fn_name, fn_name,
type_params, type_params,
method_source.where_clause(), where_clause,
params, params,
body, body,
ret_type, ret_type,
@ -184,12 +201,6 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
let assoc_items = impl_def.get_or_create_assoc_item_list(); let assoc_items = impl_def.get_or_create_assoc_item_list();
assoc_items.add_item(f.clone().into()); assoc_items.add_item(f.clone().into());
if let Some((target, source)) =
ctx.sema.scope(strukt.syntax()).zip(ctx.sema.scope(method_source.syntax()))
{
PathTransform::generic_transformation(&target, &source).apply(f.syntax());
}
if let Some(cap) = ctx.config.snippet_cap { if let Some(cap) = ctx.config.snippet_cap {
edit.add_tabstop_before(cap, f) edit.add_tabstop_before(cap, f)
} }