@@ -10,59 +10,9 @@ use crate::{
10
10
AssistId , AssistKind ,
11
11
} ;
12
12
13
- // Assist: inline_method
13
+ // Assist: inline_call
14
14
//
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.
66
16
//
67
17
// ```
68
18
// fn add(a: u32, b: u32) -> u32 { a + b }
@@ -81,32 +31,40 @@ pub(crate) fn inline_method(acc: &mut Assists, ctx: &AssistContext) -> Option<()
81
31
// };
82
32
// }
83
33
// ```
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)
103
62
}
104
63
105
64
pub ( crate ) fn inline_ (
106
65
acc : & mut Assists ,
107
66
ctx : & AssistContext ,
108
- assist_id : & ' static str ,
109
- label : & str ,
67
+ label : String ,
110
68
function : hir:: Function ,
111
69
arg_list : Vec < ast:: Expr > ,
112
70
expr : ast:: Expr ,
@@ -133,7 +91,7 @@ pub(crate) fn inline_(
133
91
if arg_list. len ( ) != params. len ( ) {
134
92
// Can't inline the function because they've passed the wrong number of
135
93
// arguments to this function
136
- cov_mark:: hit!( inline_function_incorrect_number_of_arguments ) ;
94
+ cov_mark:: hit!( inline_call_incorrect_number_of_arguments ) ;
137
95
return None ;
138
96
}
139
97
@@ -142,11 +100,12 @@ pub(crate) fn inline_(
142
100
let body = function_source. body ( ) ?;
143
101
144
102
acc. add (
145
- AssistId ( assist_id , AssistKind :: RefactorInline ) ,
103
+ AssistId ( "inline_call" , AssistKind :: RefactorInline ) ,
146
104
label,
147
105
expr. syntax ( ) . text_range ( ) ,
148
106
|builder| {
149
107
// FIXME: emit type ascriptions when a coercion happens?
108
+ // FIXME: dont create locals when its not required
150
109
let statements = new_bindings
151
110
. map ( |( pattern, value) | make:: let_stmt ( pattern, Some ( value) ) . into ( ) )
152
111
. chain ( body. statements ( ) ) ;
@@ -184,7 +143,7 @@ mod tests {
184
143
#[ test]
185
144
fn no_args_or_return_value_gets_inlined_without_block ( ) {
186
145
check_assist (
187
- inline_function ,
146
+ inline_call ,
188
147
r#"
189
148
fn foo() { println!("Hello, World!"); }
190
149
fn main() {
@@ -205,7 +164,7 @@ fn main() {
205
164
#[ test]
206
165
fn args_with_side_effects ( ) {
207
166
check_assist (
208
- inline_function ,
167
+ inline_call ,
209
168
r#"
210
169
fn foo(name: String) { println!("Hello, {}!", name); }
211
170
fn main() {
@@ -226,9 +185,9 @@ fn main() {
226
185
227
186
#[ test]
228
187
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 ) ;
230
189
check_assist_not_applicable (
231
- inline_function ,
190
+ inline_call ,
232
191
r#"
233
192
fn add(a: u32, b: u32) -> u32 { a + b }
234
193
fn main() { let x = add$0(42); }
@@ -239,7 +198,7 @@ fn main() { let x = add$0(42); }
239
198
#[ test]
240
199
fn function_with_multiple_statements ( ) {
241
200
check_assist (
242
- inline_function ,
201
+ inline_call ,
243
202
r#"
244
203
fn foo(a: u32, b: u32) -> u32 {
245
204
let x = a + b;
@@ -274,7 +233,7 @@ fn main() {
274
233
#[ test]
275
234
fn function_with_self_param ( ) {
276
235
check_assist (
277
- inline_function ,
236
+ inline_call ,
278
237
r#"
279
238
struct Foo(u32);
280
239
@@ -311,7 +270,7 @@ fn main() {
311
270
#[ test]
312
271
fn method_by_val ( ) {
313
272
check_assist (
314
- inline_method ,
273
+ inline_call ,
315
274
r#"
316
275
struct Foo(u32);
317
276
@@ -348,7 +307,7 @@ fn main() {
348
307
#[ test]
349
308
fn method_by_ref ( ) {
350
309
check_assist (
351
- inline_method ,
310
+ inline_call ,
352
311
r#"
353
312
struct Foo(u32);
354
313
@@ -385,7 +344,7 @@ fn main() {
385
344
#[ test]
386
345
fn method_by_ref_mut ( ) {
387
346
check_assist (
388
- inline_method ,
347
+ inline_call ,
389
348
r#"
390
349
struct Foo(u32);
391
350
0 commit comments