Merge the inline function/method assists into inline_call

This commit is contained in:
Lukas Wirth 2021-07-03 17:13:56 +02:00
parent 688398febc
commit 14e18bfa38
4 changed files with 47 additions and 90 deletions

View file

@ -10,59 +10,9 @@ use crate::{
AssistId, AssistKind, AssistId, AssistKind,
}; };
// Assist: inline_method // Assist: inline_call
// //
// Inlines a method body. // Inlines a function or method body.
//
// ```
// struct Foo(u32);
// impl Foo {
// fn add(self, a: u32) -> Self {
// Foo(self.0 + a)
// }
// }
// fn main() {
// let x = Foo(3).add$0(2);
// }
// ```
// ->
// ```
// struct Foo(u32);
// impl Foo {
// fn add(self, a: u32) -> Self {
// Foo(self.0 + a)
// }
// }
// fn main() {
// let x = {
// let this = Foo(3);
// let a = 2;
// Foo(this.0 + a)
// };
// }
// ```
pub(crate) fn inline_method(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let name_ref: ast::NameRef = ctx.find_node_at_offset()?;
let call = name_ref.syntax().parent().and_then(ast::MethodCallExpr::cast)?;
let receiver = call.receiver()?;
let function = ctx.sema.resolve_method_call(&call)?;
let mut arguments = vec![receiver];
arguments.extend(call.arg_list()?.args());
inline_(
acc,
ctx,
"inline_method",
&format!("Inline `{}`", name_ref),
function,
arguments,
ast::Expr::MethodCallExpr(call),
)
}
// Assist: inline_function
//
// Inlines a function body.
// //
// ``` // ```
// fn add(a: u32, b: u32) -> u32 { a + b } // fn add(a: u32, b: u32) -> u32 { a + b }
@ -81,32 +31,40 @@ pub(crate) fn inline_method(acc: &mut Assists, ctx: &AssistContext) -> Option<()
// }; // };
// } // }
// ``` // ```
pub(crate) fn inline_function(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { pub(crate) fn inline_call(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let path_expr: ast::PathExpr = ctx.find_node_at_offset()?; let (label, function, arguments, expr) =
let call = path_expr.syntax().parent().and_then(ast::CallExpr::cast)?; if let Some(path_expr) = ctx.find_node_at_offset::<ast::PathExpr>() {
let path = path_expr.path()?; let call = path_expr.syntax().parent().and_then(ast::CallExpr::cast)?;
let path = path_expr.path()?;
let function = match dbg!(ctx.sema.resolve_path(&path)?) { let function = match ctx.sema.resolve_path(&path)? {
PathResolution::Def(hir::ModuleDef::Function(f)) PathResolution::Def(hir::ModuleDef::Function(f))
| PathResolution::AssocItem(hir::AssocItem::Function(f)) => f, | PathResolution::AssocItem(hir::AssocItem::Function(f)) => f,
_ => return None, _ => return None,
}; };
inline_( (
acc, format!("Inline `{}`", path),
ctx, function,
"inline_function", call.arg_list()?.args().collect(),
&format!("Inline `{}`", path), ast::Expr::CallExpr(call),
function, )
call.arg_list()?.args().collect(), } else {
ast::Expr::CallExpr(call), let name_ref: ast::NameRef = ctx.find_node_at_offset()?;
) let call = name_ref.syntax().parent().and_then(ast::MethodCallExpr::cast)?;
let receiver = call.receiver()?;
let function = ctx.sema.resolve_method_call(&call)?;
let mut arguments = vec![receiver];
arguments.extend(call.arg_list()?.args());
(format!("Inline `{}`", name_ref), function, arguments, ast::Expr::MethodCallExpr(call))
};
inline_(acc, ctx, label, function, arguments, expr)
} }
pub(crate) fn inline_( pub(crate) fn inline_(
acc: &mut Assists, acc: &mut Assists,
ctx: &AssistContext, ctx: &AssistContext,
assist_id: &'static str, label: String,
label: &str,
function: hir::Function, function: hir::Function,
arg_list: Vec<ast::Expr>, arg_list: Vec<ast::Expr>,
expr: ast::Expr, expr: ast::Expr,
@ -133,7 +91,7 @@ pub(crate) fn inline_(
if arg_list.len() != params.len() { if arg_list.len() != params.len() {
// Can't inline the function because they've passed the wrong number of // Can't inline the function because they've passed the wrong number of
// arguments to this function // arguments to this function
cov_mark::hit!(inline_function_incorrect_number_of_arguments); cov_mark::hit!(inline_call_incorrect_number_of_arguments);
return None; return None;
} }
@ -142,11 +100,12 @@ pub(crate) fn inline_(
let body = function_source.body()?; let body = function_source.body()?;
acc.add( acc.add(
AssistId(assist_id, AssistKind::RefactorInline), AssistId("inline_call", AssistKind::RefactorInline),
label, label,
expr.syntax().text_range(), expr.syntax().text_range(),
|builder| { |builder| {
// FIXME: emit type ascriptions when a coercion happens? // FIXME: emit type ascriptions when a coercion happens?
// FIXME: dont create locals when its not required
let statements = new_bindings let statements = new_bindings
.map(|(pattern, value)| make::let_stmt(pattern, Some(value)).into()) .map(|(pattern, value)| make::let_stmt(pattern, Some(value)).into())
.chain(body.statements()); .chain(body.statements());
@ -184,7 +143,7 @@ mod tests {
#[test] #[test]
fn no_args_or_return_value_gets_inlined_without_block() { fn no_args_or_return_value_gets_inlined_without_block() {
check_assist( check_assist(
inline_function, inline_call,
r#" r#"
fn foo() { println!("Hello, World!"); } fn foo() { println!("Hello, World!"); }
fn main() { fn main() {
@ -205,7 +164,7 @@ fn main() {
#[test] #[test]
fn args_with_side_effects() { fn args_with_side_effects() {
check_assist( check_assist(
inline_function, inline_call,
r#" r#"
fn foo(name: String) { println!("Hello, {}!", name); } fn foo(name: String) { println!("Hello, {}!", name); }
fn main() { fn main() {
@ -226,9 +185,9 @@ fn main() {
#[test] #[test]
fn not_applicable_when_incorrect_number_of_parameters_are_provided() { fn not_applicable_when_incorrect_number_of_parameters_are_provided() {
cov_mark::check!(inline_function_incorrect_number_of_arguments); cov_mark::check!(inline_call_incorrect_number_of_arguments);
check_assist_not_applicable( check_assist_not_applicable(
inline_function, inline_call,
r#" r#"
fn add(a: u32, b: u32) -> u32 { a + b } fn add(a: u32, b: u32) -> u32 { a + b }
fn main() { let x = add$0(42); } fn main() { let x = add$0(42); }
@ -239,7 +198,7 @@ fn main() { let x = add$0(42); }
#[test] #[test]
fn function_with_multiple_statements() { fn function_with_multiple_statements() {
check_assist( check_assist(
inline_function, inline_call,
r#" r#"
fn foo(a: u32, b: u32) -> u32 { fn foo(a: u32, b: u32) -> u32 {
let x = a + b; let x = a + b;
@ -274,7 +233,7 @@ fn main() {
#[test] #[test]
fn function_with_self_param() { fn function_with_self_param() {
check_assist( check_assist(
inline_function, inline_call,
r#" r#"
struct Foo(u32); struct Foo(u32);
@ -311,7 +270,7 @@ fn main() {
#[test] #[test]
fn method_by_val() { fn method_by_val() {
check_assist( check_assist(
inline_method, inline_call,
r#" r#"
struct Foo(u32); struct Foo(u32);
@ -348,7 +307,7 @@ fn main() {
#[test] #[test]
fn method_by_ref() { fn method_by_ref() {
check_assist( check_assist(
inline_method, inline_call,
r#" r#"
struct Foo(u32); struct Foo(u32);
@ -385,7 +344,7 @@ fn main() {
#[test] #[test]
fn method_by_ref_mut() { fn method_by_ref_mut() {
check_assist( check_assist(
inline_method, inline_call,
r#" r#"
struct Foo(u32); struct Foo(u32);

View file

@ -85,7 +85,7 @@ mod handlers {
mod generate_new; mod generate_new;
mod generate_setter; mod generate_setter;
mod infer_function_return_type; mod infer_function_return_type;
mod inline_function; mod inline_call;
mod inline_local_variable; mod inline_local_variable;
mod introduce_named_lifetime; mod introduce_named_lifetime;
mod invert_if; mod invert_if;
@ -155,8 +155,7 @@ mod handlers {
generate_new::generate_new, generate_new::generate_new,
generate_setter::generate_setter, generate_setter::generate_setter,
infer_function_return_type::infer_function_return_type, infer_function_return_type::infer_function_return_type,
inline_function::inline_function, inline_call::inline_call,
inline_function::inline_method,
inline_local_variable::inline_local_variable, inline_local_variable::inline_local_variable,
introduce_named_lifetime::introduce_named_lifetime, introduce_named_lifetime::introduce_named_lifetime,
invert_if::invert_if, invert_if::invert_if,

View file

@ -919,9 +919,9 @@ fn foo() -> i32 { 42i32 }
} }
#[test] #[test]
fn doctest_inline_function() { fn doctest_inline_call() {
check_doc_test( check_doc_test(
"inline_function", "inline_call",
r#####" r#####"
fn add(a: u32, b: u32) -> u32 { a + b } fn add(a: u32, b: u32) -> u32 { a + b }
fn main() { fn main() {

View file

@ -338,7 +338,6 @@ pub fn arg_list(args: impl IntoIterator<Item = ast::Expr>) -> ast::ArgList {
} }
pub fn ident_pat(ref_: bool, mut_: bool, name: ast::Name) -> ast::IdentPat { pub fn ident_pat(ref_: bool, mut_: bool, name: ast::Name) -> ast::IdentPat {
use std::fmt::Write as _;
let mut s = String::from("fn f("); let mut s = String::from("fn f(");
if ref_ { if ref_ {
s.push_str("ref "); s.push_str("ref ");
@ -346,7 +345,7 @@ pub fn ident_pat(ref_: bool, mut_: bool, name: ast::Name) -> ast::IdentPat {
if mut_ { if mut_ {
s.push_str("mut "); s.push_str("mut ");
} }
let _ = write!(s, "{}", name); format_to!(s, "{}", name);
s.push_str(": ())"); s.push_str(": ())");
ast_from_text(&s) ast_from_text(&s)
} }