Deunwrap inline call v2

This commit is contained in:
Ali Bektas 2023-09-09 14:35:26 +02:00
parent 5683df2965
commit 68d24b69d4

View file

@ -117,7 +117,7 @@ pub(crate) fn inline_into_callers(acc: &mut Assists, ctx: &AssistContext<'_>) ->
.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(); .expect("inline() should return an Expr");
ted::replace(mut_node, replacement.syntax()); ted::replace(mut_node, replacement.syntax());
}) })
.count(); .count();
@ -363,17 +363,23 @@ 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(); let this = || {
make::name_ref("this")
.syntax()
.clone_for_update()
.first_token()
.expect("NameRef should have had a 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) {
let usages = usages_for_locals(self_local).filter_map( usages_for_locals(self_local)
|FileReference { name, range, .. }| match name { .filter_map(|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,
}, })
); .into_iter()
for usage in usages { .for_each(|usage| {
ted::replace(usage, &this()?); ted::replace(usage, &this());
} });
} }
} }
@ -471,7 +477,9 @@ 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()?), ted::Position::after(
stmt_list.l_curly_token().expect("L_CURLY for StatementList is missing."),
),
let_stmts.into_iter().map(|stmt| stmt.syntax().clone().into()).collect(), let_stmts.into_iter().map(|stmt| stmt.syntax().clone().into()).collect(),
); );
} }