@@ -44,8 +44,8 @@ use crate::{
44
44
pub ( crate ) fn generate_function ( acc : & mut Assists , ctx : & AssistContext ) -> Option < ( ) > {
45
45
let path_expr: ast:: PathExpr = ctx. find_node_at_offset ( ) ?;
46
46
let call = path_expr. syntax ( ) . parent ( ) . and_then ( ast:: CallExpr :: cast) ?;
47
- let path = path_expr. path ( ) ?;
48
47
48
+ let path = path_expr. path ( ) ?;
49
49
if ctx. sema . resolve_path ( & path) . is_some ( ) {
50
50
// The function call already resolves, no need to add a function
51
51
return None ;
@@ -60,8 +60,8 @@ pub(crate) fn generate_function(acc: &mut Assists, ctx: &AssistContext) -> Optio
60
60
} ;
61
61
62
62
let function_builder = FunctionBuilder :: from_call ( ctx, & call, & path, target_module) ?;
63
-
64
63
let target = call. syntax ( ) . text_range ( ) ;
64
+
65
65
acc. add (
66
66
AssistId ( "generate_function" , AssistKind :: Generate ) ,
67
67
format ! ( "Generate `{}` function" , function_builder. fn_name) ,
@@ -109,6 +109,7 @@ struct FunctionBuilder {
109
109
should_render_snippet : bool ,
110
110
file : FileId ,
111
111
needs_pub : bool ,
112
+ is_async : bool ,
112
113
}
113
114
114
115
impl FunctionBuilder {
@@ -135,6 +136,9 @@ impl FunctionBuilder {
135
136
let fn_name = fn_name ( path) ?;
136
137
let ( type_params, params) = fn_args ( ctx, target_module, call) ?;
137
138
139
+ let await_expr = call. syntax ( ) . parent ( ) . and_then ( ast:: AwaitExpr :: cast) ;
140
+ let is_async = await_expr. is_some ( ) ;
141
+
138
142
// should_render_snippet intends to express a rough level of confidence about
139
143
// the correctness of the return type.
140
144
//
@@ -171,6 +175,7 @@ impl FunctionBuilder {
171
175
should_render_snippet,
172
176
file,
173
177
needs_pub,
178
+ is_async,
174
179
} )
175
180
}
176
181
@@ -185,6 +190,7 @@ impl FunctionBuilder {
185
190
self . params ,
186
191
fn_body,
187
192
Some ( self . ret_type ) ,
193
+ self . is_async ,
188
194
) ;
189
195
let leading_ws;
190
196
let trailing_ws;
@@ -1159,4 +1165,25 @@ impl Foo {
1159
1165
"# ,
1160
1166
)
1161
1167
}
1168
+
1169
+ #[ test]
1170
+ fn create_function_with_async ( ) {
1171
+ check_assist (
1172
+ generate_function,
1173
+ r"
1174
+ fn foo() {
1175
+ $0bar(42).await();
1176
+ }
1177
+ " ,
1178
+ r"
1179
+ fn foo() {
1180
+ bar(42).await();
1181
+ }
1182
+
1183
+ async fn bar(arg: i32) ${0:-> ()} {
1184
+ todo!()
1185
+ }
1186
+ " ,
1187
+ )
1188
+ }
1162
1189
}
0 commit comments