Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide-assists/src/handlers/inline_call.rs')
| -rw-r--r-- | crates/ide-assists/src/handlers/inline_call.rs | 63 |
1 files changed, 31 insertions, 32 deletions
diff --git a/crates/ide-assists/src/handlers/inline_call.rs b/crates/ide-assists/src/handlers/inline_call.rs index 9e09f198fe..6f028e58d0 100644 --- a/crates/ide-assists/src/handlers/inline_call.rs +++ b/crates/ide-assists/src/handlers/inline_call.rs @@ -3,30 +3,32 @@ use std::collections::BTreeSet; use ast::make; use either::Either; use hir::{ + FileRange, PathResolution, Semantics, TypeInfo, db::{ExpandDatabase, HirDatabase}, - sym, FileRange, PathResolution, Semantics, TypeInfo, + sym, }; use ide_db::{ - base_db::CrateId, + EditionedFileId, RootDatabase, + base_db::Crate, defs::Definition, imports::insert_use::remove_path_if_in_use_stmt, path_transform::PathTransform, search::{FileReference, FileReferenceNode, SearchScope}, source_change::SourceChangeBuilder, syntax_helpers::{node_ext::expr_as_name_ref, prettify_macro_expansion}, - EditionedFileId, RootDatabase, }; -use itertools::{izip, Itertools}; +use itertools::{Itertools, izip}; use syntax::{ + AstNode, NodeOrToken, SyntaxKind, ast::{ - self, edit::IndentLevel, edit_in_place::Indent, HasArgList, HasGenericArgs, Pat, PathExpr, + self, HasArgList, HasGenericArgs, Pat, PathExpr, edit::IndentLevel, edit_in_place::Indent, }, - ted, AstNode, NodeOrToken, SyntaxKind, + ted, }; use crate::{ + AssistId, assist_context::{AssistContext, Assists}, - AssistId, AssistKind, }; // Assist: inline_into_callers @@ -69,6 +71,7 @@ use crate::{ // ``` pub(crate) fn inline_into_callers(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> { let def_file = ctx.file_id(); + let vfs_def_file = ctx.vfs_file_id(); let name = ctx.find_node_at_offset::<ast::Name>()?; let ast_func = name.syntax().parent().and_then(ast::Fn::cast)?; let func_body = ast_func.body()?; @@ -96,7 +99,7 @@ pub(crate) fn inline_into_callers(acc: &mut Assists, ctx: &AssistContext<'_>) -> } acc.add( - AssistId("inline_into_callers", AssistKind::RefactorInline), + AssistId::refactor_inline("inline_into_callers"), "Inline into all callers", name.syntax().text_range(), |builder| { @@ -104,7 +107,8 @@ pub(crate) fn inline_into_callers(acc: &mut Assists, ctx: &AssistContext<'_>) -> let current_file_usage = usages.references.remove(&def_file); let mut remove_def = true; - let mut inline_refs_for_file = |file_id, refs: Vec<FileReference>| { + let mut inline_refs_for_file = |file_id: EditionedFileId, refs: Vec<FileReference>| { + let file_id = file_id.file_id(ctx.db()); builder.edit_file(file_id); let call_krate = ctx.sema.file_to_module_def(file_id).map(|it| it.krate()); let count = refs.len(); @@ -141,7 +145,7 @@ pub(crate) fn inline_into_callers(acc: &mut Assists, ctx: &AssistContext<'_>) -> } match current_file_usage { Some(refs) => inline_refs_for_file(def_file, refs), - None => builder.edit_file(def_file), + None => builder.edit_file(vfs_def_file), } if remove_def { builder.delete(ast_func.syntax().text_range()); @@ -192,7 +196,7 @@ pub(crate) fn inline_call(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option< let name_ref: ast::NameRef = ctx.find_node_at_offset()?; let call_info = CallInfo::from_name_ref( name_ref.clone(), - ctx.sema.file_to_module_def(ctx.file_id())?.krate().into(), + ctx.sema.file_to_module_def(ctx.vfs_file_id())?.krate().into(), )?; let (function, label) = match &call_info.node { ast::CallableExpr::Call(call) => { @@ -230,32 +234,27 @@ pub(crate) fn inline_call(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option< } let syntax = call_info.node.syntax().clone(); - acc.add( - AssistId("inline_call", AssistKind::RefactorInline), - label, - syntax.text_range(), - |builder| { - let replacement = inline(&ctx.sema, file_id, function, &fn_body, ¶ms, &call_info); - builder.replace_ast( - match call_info.node { - ast::CallableExpr::Call(it) => ast::Expr::CallExpr(it), - ast::CallableExpr::MethodCall(it) => ast::Expr::MethodCallExpr(it), - }, - replacement, - ); - }, - ) + acc.add(AssistId::refactor_inline("inline_call"), label, syntax.text_range(), |builder| { + let replacement = inline(&ctx.sema, file_id, function, &fn_body, ¶ms, &call_info); + builder.replace_ast( + match call_info.node { + ast::CallableExpr::Call(it) => ast::Expr::CallExpr(it), + ast::CallableExpr::MethodCall(it) => ast::Expr::MethodCallExpr(it), + }, + replacement, + ); + }) } struct CallInfo { node: ast::CallableExpr, arguments: Vec<ast::Expr>, generic_arg_list: Option<ast::GenericArgList>, - krate: CrateId, + krate: Crate, } impl CallInfo { - fn from_name_ref(name_ref: ast::NameRef, krate: CrateId) -> Option<CallInfo> { + fn from_name_ref(name_ref: ast::NameRef, krate: Crate) -> Option<CallInfo> { let parent = name_ref.syntax().parent()?; if let Some(call) = ast::MethodCallExpr::cast(parent.clone()) { let receiver = call.receiver()?; @@ -452,7 +451,7 @@ fn inline( let ty = sema.type_of_expr(expr).filter(TypeInfo::has_adjustment).and(param_ty); - let is_self = param.name(sema.db).is_some_and(|name| name == sym::self_.clone()); + let is_self = param.name(sema.db).is_some_and(|name| name == sym::self_); if is_self { let mut this_pat = make::ident_pat(false, false, make::name("this")); @@ -515,7 +514,7 @@ fn inline( && usage.syntax().parent().and_then(ast::Expr::cast).is_some() => { cov_mark::hit!(inline_call_inline_closure); - let expr = make::expr_paren(expr.clone()); + let expr = make::expr_paren(expr.clone()).into(); inline_direct(usage, &expr); } // inline single use literals @@ -570,7 +569,7 @@ fn inline( let no_stmts = body.statements().next().is_none(); match body.tail_expr() { Some(expr) if matches!(expr, ast::Expr::ClosureExpr(_)) && no_stmts => { - make::expr_paren(expr).clone_for_update() + make::expr_paren(expr).clone_for_update().into() } Some(expr) if !is_async_fn && no_stmts => expr, _ => match node @@ -580,7 +579,7 @@ fn inline( .and_then(|bin_expr| bin_expr.lhs()) { Some(lhs) if lhs.syntax() == node.syntax() => { - make::expr_paren(ast::Expr::BlockExpr(body)).clone_for_update() + make::expr_paren(ast::Expr::BlockExpr(body)).clone_for_update().into() } _ => ast::Expr::BlockExpr(body), }, |