Skip to content

Commit 14e18bf

Browse files
committed
Merge the inline function/method assists into inline_call
1 parent 688398f commit 14e18bf

File tree

4 files changed

+48
-91
lines changed

4 files changed

+48
-91
lines changed

crates/ide_assists/src/handlers/inline_function.rs renamed to crates/ide_assists/src/handlers/inline_call.rs

Lines changed: 43 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -10,59 +10,9 @@ use crate::{
1010
AssistId, AssistKind,
1111
};
1212

13-
// Assist: inline_method
13+
// Assist: inline_call
1414
//
15-
// Inlines a method body.
16-
//
17-
// ```
18-
// struct Foo(u32);
19-
// impl Foo {
20-
// fn add(self, a: u32) -> Self {
21-
// Foo(self.0 + a)
22-
// }
23-
// }
24-
// fn main() {
25-
// let x = Foo(3).add$0(2);
26-
// }
27-
// ```
28-
// ->
29-
// ```
30-
// struct Foo(u32);
31-
// impl Foo {
32-
// fn add(self, a: u32) -> Self {
33-
// Foo(self.0 + a)
34-
// }
35-
// }
36-
// fn main() {
37-
// let x = {
38-
// let this = Foo(3);
39-
// let a = 2;
40-
// Foo(this.0 + a)
41-
// };
42-
// }
43-
// ```
44-
pub(crate) fn inline_method(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
45-
let name_ref: ast::NameRef = ctx.find_node_at_offset()?;
46-
let call = name_ref.syntax().parent().and_then(ast::MethodCallExpr::cast)?;
47-
let receiver = call.receiver()?;
48-
let function = ctx.sema.resolve_method_call(&call)?;
49-
let mut arguments = vec![receiver];
50-
arguments.extend(call.arg_list()?.args());
51-
52-
inline_(
53-
acc,
54-
ctx,
55-
"inline_method",
56-
&format!("Inline `{}`", name_ref),
57-
function,
58-
arguments,
59-
ast::Expr::MethodCallExpr(call),
60-
)
61-
}
62-
63-
// Assist: inline_function
64-
//
65-
// Inlines a function body.
15+
// Inlines a function or method body.
6616
//
6717
// ```
6818
// fn add(a: u32, b: u32) -> u32 { a + b }
@@ -81,32 +31,40 @@ pub(crate) fn inline_method(acc: &mut Assists, ctx: &AssistContext) -> Option<()
8131
// };
8232
// }
8333
// ```
84-
pub(crate) fn inline_function(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
85-
let path_expr: ast::PathExpr = ctx.find_node_at_offset()?;
86-
let call = path_expr.syntax().parent().and_then(ast::CallExpr::cast)?;
87-
let path = path_expr.path()?;
88-
89-
let function = match dbg!(ctx.sema.resolve_path(&path)?) {
90-
PathResolution::Def(hir::ModuleDef::Function(f))
91-
| PathResolution::AssocItem(hir::AssocItem::Function(f)) => f,
92-
_ => return None,
93-
};
94-
inline_(
95-
acc,
96-
ctx,
97-
"inline_function",
98-
&format!("Inline `{}`", path),
99-
function,
100-
call.arg_list()?.args().collect(),
101-
ast::Expr::CallExpr(call),
102-
)
34+
pub(crate) fn inline_call(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
35+
let (label, function, arguments, expr) =
36+
if let Some(path_expr) = ctx.find_node_at_offset::<ast::PathExpr>() {
37+
let call = path_expr.syntax().parent().and_then(ast::CallExpr::cast)?;
38+
let path = path_expr.path()?;
39+
40+
let function = match ctx.sema.resolve_path(&path)? {
41+
PathResolution::Def(hir::ModuleDef::Function(f))
42+
| PathResolution::AssocItem(hir::AssocItem::Function(f)) => f,
43+
_ => return None,
44+
};
45+
(
46+
format!("Inline `{}`", path),
47+
function,
48+
call.arg_list()?.args().collect(),
49+
ast::Expr::CallExpr(call),
50+
)
51+
} else {
52+
let name_ref: ast::NameRef = ctx.find_node_at_offset()?;
53+
let call = name_ref.syntax().parent().and_then(ast::MethodCallExpr::cast)?;
54+
let receiver = call.receiver()?;
55+
let function = ctx.sema.resolve_method_call(&call)?;
56+
let mut arguments = vec![receiver];
57+
arguments.extend(call.arg_list()?.args());
58+
(format!("Inline `{}`", name_ref), function, arguments, ast::Expr::MethodCallExpr(call))
59+
};
60+
61+
inline_(acc, ctx, label, function, arguments, expr)
10362
}
10463

10564
pub(crate) fn inline_(
10665
acc: &mut Assists,
10766
ctx: &AssistContext,
108-
assist_id: &'static str,
109-
label: &str,
67+
label: String,
11068
function: hir::Function,
11169
arg_list: Vec<ast::Expr>,
11270
expr: ast::Expr,
@@ -133,7 +91,7 @@ pub(crate) fn inline_(
13391
if arg_list.len() != params.len() {
13492
// Can't inline the function because they've passed the wrong number of
13593
// arguments to this function
136-
cov_mark::hit!(inline_function_incorrect_number_of_arguments);
94+
cov_mark::hit!(inline_call_incorrect_number_of_arguments);
13795
return None;
13896
}
13997

@@ -142,11 +100,12 @@ pub(crate) fn inline_(
142100
let body = function_source.body()?;
143101

144102
acc.add(
145-
AssistId(assist_id, AssistKind::RefactorInline),
103+
AssistId("inline_call", AssistKind::RefactorInline),
146104
label,
147105
expr.syntax().text_range(),
148106
|builder| {
149107
// FIXME: emit type ascriptions when a coercion happens?
108+
// FIXME: dont create locals when its not required
150109
let statements = new_bindings
151110
.map(|(pattern, value)| make::let_stmt(pattern, Some(value)).into())
152111
.chain(body.statements());
@@ -184,7 +143,7 @@ mod tests {
184143
#[test]
185144
fn no_args_or_return_value_gets_inlined_without_block() {
186145
check_assist(
187-
inline_function,
146+
inline_call,
188147
r#"
189148
fn foo() { println!("Hello, World!"); }
190149
fn main() {
@@ -205,7 +164,7 @@ fn main() {
205164
#[test]
206165
fn args_with_side_effects() {
207166
check_assist(
208-
inline_function,
167+
inline_call,
209168
r#"
210169
fn foo(name: String) { println!("Hello, {}!", name); }
211170
fn main() {
@@ -226,9 +185,9 @@ fn main() {
226185

227186
#[test]
228187
fn not_applicable_when_incorrect_number_of_parameters_are_provided() {
229-
cov_mark::check!(inline_function_incorrect_number_of_arguments);
188+
cov_mark::check!(inline_call_incorrect_number_of_arguments);
230189
check_assist_not_applicable(
231-
inline_function,
190+
inline_call,
232191
r#"
233192
fn add(a: u32, b: u32) -> u32 { a + b }
234193
fn main() { let x = add$0(42); }
@@ -239,7 +198,7 @@ fn main() { let x = add$0(42); }
239198
#[test]
240199
fn function_with_multiple_statements() {
241200
check_assist(
242-
inline_function,
201+
inline_call,
243202
r#"
244203
fn foo(a: u32, b: u32) -> u32 {
245204
let x = a + b;
@@ -274,7 +233,7 @@ fn main() {
274233
#[test]
275234
fn function_with_self_param() {
276235
check_assist(
277-
inline_function,
236+
inline_call,
278237
r#"
279238
struct Foo(u32);
280239
@@ -311,7 +270,7 @@ fn main() {
311270
#[test]
312271
fn method_by_val() {
313272
check_assist(
314-
inline_method,
273+
inline_call,
315274
r#"
316275
struct Foo(u32);
317276
@@ -348,7 +307,7 @@ fn main() {
348307
#[test]
349308
fn method_by_ref() {
350309
check_assist(
351-
inline_method,
310+
inline_call,
352311
r#"
353312
struct Foo(u32);
354313
@@ -385,7 +344,7 @@ fn main() {
385344
#[test]
386345
fn method_by_ref_mut() {
387346
check_assist(
388-
inline_method,
347+
inline_call,
389348
r#"
390349
struct Foo(u32);
391350

crates/ide_assists/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ mod handlers {
8585
mod generate_new;
8686
mod generate_setter;
8787
mod infer_function_return_type;
88-
mod inline_function;
88+
mod inline_call;
8989
mod inline_local_variable;
9090
mod introduce_named_lifetime;
9191
mod invert_if;
@@ -155,8 +155,7 @@ mod handlers {
155155
generate_new::generate_new,
156156
generate_setter::generate_setter,
157157
infer_function_return_type::infer_function_return_type,
158-
inline_function::inline_function,
159-
inline_function::inline_method,
158+
inline_call::inline_call,
160159
inline_local_variable::inline_local_variable,
161160
introduce_named_lifetime::introduce_named_lifetime,
162161
invert_if::invert_if,

crates/ide_assists/src/tests/generated.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -919,9 +919,9 @@ fn foo() -> i32 { 42i32 }
919919
}
920920

921921
#[test]
922-
fn doctest_inline_function() {
922+
fn doctest_inline_call() {
923923
check_doc_test(
924-
"inline_function",
924+
"inline_call",
925925
r#####"
926926
fn add(a: u32, b: u32) -> u32 { a + b }
927927
fn main() {

crates/syntax/src/ast/make.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,15 +338,14 @@ pub fn arg_list(args: impl IntoIterator<Item = ast::Expr>) -> ast::ArgList {
338338
}
339339

340340
pub fn ident_pat(ref_: bool, mut_: bool, name: ast::Name) -> ast::IdentPat {
341-
use std::fmt::Write as _;
342341
let mut s = String::from("fn f(");
343342
if ref_ {
344343
s.push_str("ref ");
345344
}
346345
if mut_ {
347346
s.push_str("mut ");
348347
}
349-
let _ = write!(s, "{}", name);
348+
format_to!(s, "{}", name);
350349
s.push_str(": ())");
351350
ast_from_text(&s)
352351
}

0 commit comments

Comments
 (0)