Skip to content

Commit 57f119b

Browse files
committed
fix: Adding async keyword when await is present in generate_function assist
1 parent 80f193e commit 57f119b

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

crates/ide_assists/src/handlers/generate_function.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ use crate::{
4444
pub(crate) fn generate_function(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
4545
let path_expr: ast::PathExpr = ctx.find_node_at_offset()?;
4646
let call = path_expr.syntax().parent().and_then(ast::CallExpr::cast)?;
47-
let path = path_expr.path()?;
4847

48+
let path = path_expr.path()?;
4949
if ctx.sema.resolve_path(&path).is_some() {
5050
// The function call already resolves, no need to add a function
5151
return None;
@@ -60,8 +60,8 @@ pub(crate) fn generate_function(acc: &mut Assists, ctx: &AssistContext) -> Optio
6060
};
6161

6262
let function_builder = FunctionBuilder::from_call(ctx, &call, &path, target_module)?;
63-
6463
let target = call.syntax().text_range();
64+
6565
acc.add(
6666
AssistId("generate_function", AssistKind::Generate),
6767
format!("Generate `{}` function", function_builder.fn_name),
@@ -109,6 +109,7 @@ struct FunctionBuilder {
109109
should_render_snippet: bool,
110110
file: FileId,
111111
needs_pub: bool,
112+
is_async: bool,
112113
}
113114

114115
impl FunctionBuilder {
@@ -135,6 +136,9 @@ impl FunctionBuilder {
135136
let fn_name = fn_name(path)?;
136137
let (type_params, params) = fn_args(ctx, target_module, call)?;
137138

139+
let await_expr = call.syntax().parent().and_then(ast::AwaitExpr::cast);
140+
let is_async = await_expr.is_some();
141+
138142
// should_render_snippet intends to express a rough level of confidence about
139143
// the correctness of the return type.
140144
//
@@ -171,6 +175,7 @@ impl FunctionBuilder {
171175
should_render_snippet,
172176
file,
173177
needs_pub,
178+
is_async,
174179
})
175180
}
176181

@@ -185,6 +190,7 @@ impl FunctionBuilder {
185190
self.params,
186191
fn_body,
187192
Some(self.ret_type),
193+
self.is_async,
188194
);
189195
let leading_ws;
190196
let trailing_ws;
@@ -1159,4 +1165,25 @@ impl Foo {
11591165
"#,
11601166
)
11611167
}
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+
}
11621189
}

crates/syntax/src/ast/make.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,7 @@ pub fn fn_(
587587
params: ast::ParamList,
588588
body: ast::BlockExpr,
589589
ret_type: Option<ast::RetType>,
590+
is_async: bool,
590591
) -> ast::Fn {
591592
let type_params =
592593
if let Some(type_params) = type_params { format!("<{}>", type_params) } else { "".into() };
@@ -596,9 +597,11 @@ pub fn fn_(
596597
Some(it) => format!("{} ", it),
597598
};
598599

600+
let async_literal = if is_async { "async " } else { "" };
601+
599602
ast_from_text(&format!(
600-
"{}fn {}{}{} {}{}",
601-
visibility, fn_name, type_params, params, ret_type, body
603+
"{}{}fn {}{}{} {}{}",
604+
visibility, async_literal, fn_name, type_params, params, ret_type, body
602605
))
603606
}
604607

0 commit comments

Comments
 (0)