Deunwrap inline call

This commit is contained in:
Ali Bektas 2023-08-10 01:18:05 +02:00
parent 326f37ef1f
commit 5683df2965

View file

@ -116,7 +116,8 @@ pub(crate) fn inline_into_callers(acc: &mut Assists, ctx: &AssistContext<'_>) ->
.into_iter() .into_iter()
.map(|(call_info, mut_node)| { .map(|(call_info, mut_node)| {
let replacement = let replacement =
inline(&ctx.sema, def_file, function, &func_body, &params, &call_info); inline(&ctx.sema, def_file, function, &func_body, &params, &call_info)
.unwrap();
ted::replace(mut_node, replacement.syntax()); ted::replace(mut_node, replacement.syntax());
}) })
.count(); .count();
@ -218,13 +219,12 @@ pub(crate) fn inline_call(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<
} }
let syntax = call_info.node.syntax().clone(); let syntax = call_info.node.syntax().clone();
let replacement = inline(&ctx.sema, file_id, function, &fn_body, &params, &call_info)?;
acc.add( acc.add(
AssistId("inline_call", AssistKind::RefactorInline), AssistId("inline_call", AssistKind::RefactorInline),
label, label,
syntax.text_range(), syntax.text_range(),
|builder| { |builder| {
let replacement = inline(&ctx.sema, file_id, function, &fn_body, &params, &call_info);
builder.replace_ast( builder.replace_ast(
match call_info.node { match call_info.node {
ast::CallableExpr::Call(it) => ast::Expr::CallExpr(it), ast::CallableExpr::Call(it) => ast::Expr::CallExpr(it),
@ -305,7 +305,7 @@ fn inline(
fn_body: &ast::BlockExpr, fn_body: &ast::BlockExpr,
params: &[(ast::Pat, Option<ast::Type>, hir::Param)], params: &[(ast::Pat, Option<ast::Type>, hir::Param)],
CallInfo { node, arguments, generic_arg_list }: &CallInfo, CallInfo { node, arguments, generic_arg_list }: &CallInfo,
) -> ast::Expr { ) -> Option<ast::Expr> {
let mut body = if sema.hir_file_for(fn_body.syntax()).is_macro() { let mut body = if sema.hir_file_for(fn_body.syntax()).is_macro() {
cov_mark::hit!(inline_call_defined_in_macro); cov_mark::hit!(inline_call_defined_in_macro);
if let Some(body) = ast::BlockExpr::cast(insert_ws_into(fn_body.syntax().clone())) { if let Some(body) = ast::BlockExpr::cast(insert_ws_into(fn_body.syntax().clone())) {
@ -363,16 +363,17 @@ fn inline(
.collect(); .collect();
if function.self_param(sema.db).is_some() { if function.self_param(sema.db).is_some() {
let this = || make::name_ref("this").syntax().clone_for_update().first_token().unwrap(); let this = || make::name_ref("this").syntax().clone_for_update().first_token();
if let Some(self_local) = params[0].2.as_local(sema.db) { if let Some(self_local) = params[0].2.as_local(sema.db) {
usages_for_locals(self_local) let usages = usages_for_locals(self_local).filter_map(
.filter_map(|FileReference { name, range, .. }| match name { |FileReference { name, range, .. }| match name {
ast::NameLike::NameRef(_) => Some(body.syntax().covering_element(range)), ast::NameLike::NameRef(_) => Some(body.syntax().covering_element(range)),
_ => None, _ => None,
}) },
.for_each(|it| { );
ted::replace(it, &this()); for usage in usages {
}) ted::replace(usage, &this()?);
}
} }
} }
@ -470,7 +471,7 @@ fn inline(
} }
} else if let Some(stmt_list) = body.stmt_list() { } else if let Some(stmt_list) = body.stmt_list() {
ted::insert_all( ted::insert_all(
ted::Position::after(stmt_list.l_curly_token().unwrap()), ted::Position::after(stmt_list.l_curly_token()?),
let_stmts.into_iter().map(|stmt| stmt.syntax().clone().into()).collect(), let_stmts.into_iter().map(|stmt| stmt.syntax().clone().into()).collect(),
); );
} }
@ -481,7 +482,7 @@ fn inline(
}; };
body.reindent_to(original_indentation); body.reindent_to(original_indentation);
match body.tail_expr() { Some(match body.tail_expr() {
Some(expr) if !is_async_fn && body.statements().next().is_none() => expr, Some(expr) if !is_async_fn && body.statements().next().is_none() => expr,
_ => match node _ => match node
.syntax() .syntax()
@ -494,7 +495,7 @@ fn inline(
} }
_ => ast::Expr::BlockExpr(body), _ => ast::Expr::BlockExpr(body),
}, },
} })
} }
fn path_expr_as_record_field(usage: &PathExpr) -> Option<ast::RecordExprField> { fn path_expr_as_record_field(usage: &PathExpr) -> Option<ast::RecordExprField> {