Deunwrap extract_function

This commit is contained in:
Ali Bektas 2023-08-10 01:00:12 +02:00
parent 326f37ef1f
commit 35e0d800f0

View file

@ -103,22 +103,19 @@ pub(crate) fn extract_function(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
let scope = ImportScope::find_insert_use_container(&node, &ctx.sema)?; let scope = ImportScope::find_insert_use_container(&node, &ctx.sema)?;
acc.add(
AssistId("extract_function", crate::AssistKind::RefactorExtract),
"Extract into function",
target_range,
move |builder| {
let outliving_locals: Vec<_> = ret_values.collect(); let outliving_locals: Vec<_> = ret_values.collect();
if stdx::never!(!outliving_locals.is_empty() && !ret_ty.is_unit()) { if stdx::never!(!outliving_locals.is_empty() && !ret_ty.is_unit()) {
// We should not have variables that outlive body if we have expression block // We should not have variables that outlive body if we have expression block
return; return None;
} }
let params = let params = body.extracted_function_params(ctx, &container_info, locals_used.iter().copied());
body.extracted_function_params(ctx, &container_info, locals_used.iter().copied());
let name = make_function_name(&semantics_scope); let name = make_function_name(&semantics_scope);
let has_impl_wrapper =
insert_after.ancestors().any(|a| a.kind() == SyntaxKind::IMPL && a != insert_after);
let fun = Function { let fun = Function {
name, name,
self_param, self_param,
@ -134,19 +131,21 @@ pub(crate) fn extract_function(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
let new_indent = IndentLevel::from_node(&insert_after); let new_indent = IndentLevel::from_node(&insert_after);
let old_indent = fun.body.indent_level(); let old_indent = fun.body.indent_level();
builder.replace(target_range, make_call(ctx, &fun, old_indent));
let has_impl_wrapper =
insert_after.ancestors().any(|a| a.kind() == SyntaxKind::IMPL && a != insert_after);
let fn_def = match fun.self_param_adt(ctx) { let fn_def = match fun.self_param_adt(ctx) {
Some(adt) if anchor == Anchor::Method && !has_impl_wrapper => { Some(adt) if anchor == Anchor::Method && !has_impl_wrapper => {
let fn_def = format_function(ctx, module, &fun, old_indent, new_indent + 1); let fn_def = format_function(ctx, module, &fun, old_indent, new_indent + 1)?;
generate_impl_text(&adt, &fn_def).replace("{\n\n", "{") generate_impl_text(&adt, &fn_def).replace("{\n\n", "{")
} }
_ => format_function(ctx, module, &fun, old_indent, new_indent), _ => format_function(ctx, module, &fun, old_indent, new_indent)?,
}; };
acc.add(
AssistId("extract_function", crate::AssistKind::RefactorExtract),
"Extract into function",
target_range,
move |builder| {
builder.replace(target_range, make_call(ctx, &fun, old_indent));
if fn_def.contains("ControlFlow") { if fn_def.contains("ControlFlow") {
let scope = match scope { let scope = match scope {
ImportScope::File(it) => ImportScope::File(builder.make_mut(it)), ImportScope::File(it) => ImportScope::File(builder.make_mut(it)),
@ -1501,13 +1500,13 @@ fn format_function(
fun: &Function, fun: &Function,
old_indent: IndentLevel, old_indent: IndentLevel,
new_indent: IndentLevel, new_indent: IndentLevel,
) -> String { ) -> Option<String> {
let mut fn_def = String::new(); let mut fn_def = String::new();
let fun_name = &fun.name; let fun_name = &fun.name;
let params = fun.make_param_list(ctx, module); let params = fun.make_param_list(ctx, module);
let ret_ty = fun.make_ret_ty(ctx, module); let ret_ty = fun.make_ret_ty(ctx, module);
let body = make_body(ctx, old_indent, new_indent, fun); let body = make_body(ctx, old_indent, new_indent, fun)?;
let const_kw = if fun.mods.is_const { "const " } else { "" }; let const_kw = if fun.mods.is_const { "const " } else { "" };
let async_kw = if fun.control_flow.is_async { "async " } else { "" }; let async_kw = if fun.control_flow.is_async { "async " } else { "" };
let unsafe_kw = if fun.control_flow.is_unsafe { "unsafe " } else { "" }; let unsafe_kw = if fun.control_flow.is_unsafe { "unsafe " } else { "" };
@ -1535,7 +1534,7 @@ fn format_function(
format_to!(fn_def, " {body}"); format_to!(fn_def, " {body}");
fn_def Some(fn_def)
} }
fn make_generic_params_and_where_clause( fn make_generic_params_and_where_clause(
@ -1721,14 +1720,14 @@ fn make_body(
old_indent: IndentLevel, old_indent: IndentLevel,
new_indent: IndentLevel, new_indent: IndentLevel,
fun: &Function, fun: &Function,
) -> ast::BlockExpr { ) -> Option<ast::BlockExpr> {
let ret_ty = fun.return_type(ctx); let ret_ty = fun.return_type(ctx);
let handler = FlowHandler::from_ret_ty(fun, &ret_ty); let handler = FlowHandler::from_ret_ty(fun, &ret_ty);
let block = match &fun.body { let block = match &fun.body {
FunctionBody::Expr(expr) => { FunctionBody::Expr(expr) => {
let expr = rewrite_body_segment(ctx, &fun.params, &handler, expr.syntax()); let expr = rewrite_body_segment(ctx, &fun.params, &handler, expr.syntax())?;
let expr = ast::Expr::cast(expr).unwrap(); let expr = ast::Expr::cast(expr)?;
match expr { match expr {
ast::Expr::BlockExpr(block) => { ast::Expr::BlockExpr(block) => {
// If the extracted expression is itself a block, there is no need to wrap it inside another block. // If the extracted expression is itself a block, there is no need to wrap it inside another block.
@ -1749,12 +1748,16 @@ fn make_body(
.children_with_tokens() .children_with_tokens()
.filter(|it| text_range.contains_range(it.text_range())) .filter(|it| text_range.contains_range(it.text_range()))
.map(|it| match &it { .map(|it| match &it {
syntax::NodeOrToken::Node(n) => syntax::NodeOrToken::Node( syntax::NodeOrToken::Node(n) => {
rewrite_body_segment(ctx, &fun.params, &handler, n), let bs = rewrite_body_segment(ctx, &fun.params, &handler, n);
), match bs {
_ => it, Some(n) => Some(syntax::NodeOrToken::Node(n)),
None => None,
}
}
_ => Some(it),
}) })
.collect(); .collect::<Option<_>>()?;
let mut tail_expr = match &elements.last() { let mut tail_expr = match &elements.last() {
Some(syntax::NodeOrToken::Node(node)) if ast::Expr::can_cast(node.kind()) => { Some(syntax::NodeOrToken::Node(node)) if ast::Expr::can_cast(node.kind()) => {
@ -1838,7 +1841,7 @@ fn make_body(
}), }),
}; };
block.indent(new_indent) Some(block.indent(new_indent))
} }
fn map_tail_expr(block: ast::BlockExpr, f: impl FnOnce(ast::Expr) -> ast::Expr) -> ast::BlockExpr { fn map_tail_expr(block: ast::BlockExpr, f: impl FnOnce(ast::Expr) -> ast::Expr) -> ast::BlockExpr {
@ -1896,14 +1899,18 @@ fn rewrite_body_segment(
params: &[Param], params: &[Param],
handler: &FlowHandler, handler: &FlowHandler,
syntax: &SyntaxNode, syntax: &SyntaxNode,
) -> SyntaxNode { ) -> Option<SyntaxNode> {
let syntax = fix_param_usages(ctx, params, syntax); let syntax = fix_param_usages(ctx, params, syntax)?;
update_external_control_flow(handler, &syntax); update_external_control_flow(handler, &syntax);
syntax Some(syntax)
} }
/// change all usages to account for added `&`/`&mut` for some params /// change all usages to account for added `&`/`&mut` for some params
fn fix_param_usages(ctx: &AssistContext<'_>, params: &[Param], syntax: &SyntaxNode) -> SyntaxNode { fn fix_param_usages(
ctx: &AssistContext<'_>,
params: &[Param],
syntax: &SyntaxNode,
) -> Option<SyntaxNode> {
let mut usages_for_param: Vec<(&Param, Vec<ast::Expr>)> = Vec::new(); let mut usages_for_param: Vec<(&Param, Vec<ast::Expr>)> = Vec::new();
let tm = TreeMutator::new(syntax); let tm = TreeMutator::new(syntax);
@ -1934,12 +1941,12 @@ fn fix_param_usages(ctx: &AssistContext<'_>, params: &[Param], syntax: &SyntaxNo
Some(ast::Expr::RefExpr(node)) Some(ast::Expr::RefExpr(node))
if param.kind() == ParamKind::MutRef && node.mut_token().is_some() => if param.kind() == ParamKind::MutRef && node.mut_token().is_some() =>
{ {
ted::replace(node.syntax(), node.expr().unwrap().syntax()); ted::replace(node.syntax(), node.expr()?.syntax());
} }
Some(ast::Expr::RefExpr(node)) Some(ast::Expr::RefExpr(node))
if param.kind() == ParamKind::SharedRef && node.mut_token().is_none() => if param.kind() == ParamKind::SharedRef && node.mut_token().is_none() =>
{ {
ted::replace(node.syntax(), node.expr().unwrap().syntax()); ted::replace(node.syntax(), node.expr()?.syntax());
} }
Some(_) | None => { Some(_) | None => {
let p = &make::expr_prefix(T![*], usage.clone()).clone_for_update(); let p = &make::expr_prefix(T![*], usage.clone()).clone_for_update();
@ -1949,7 +1956,7 @@ fn fix_param_usages(ctx: &AssistContext<'_>, params: &[Param], syntax: &SyntaxNo
} }
} }
res Some(res)
} }
fn update_external_control_flow(handler: &FlowHandler, syntax: &SyntaxNode) { fn update_external_control_flow(handler: &FlowHandler, syntax: &SyntaxNode) {