aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorJamie Cunliffe <[email protected]>2021-04-24 18:01:36 +0100
committerJamie Cunliffe <[email protected]>2021-05-17 18:17:29 +0100
commit74f8fe77632b992e6b28e98b6027d4e8ffd29398 (patch)
tree01ee63eaae45c14e90b62bc341a38ce4176a7d1f /crates
parent459b0332d3a4905fa9bc93c484538e74d191c80b (diff)
Extract function assist will add async if required
The extract function assist will check for an AWAIT_EXPR in the body and if found, will add async to the generated function.
Diffstat (limited to 'crates')
-rw-r--r--crates/ide_assists/src/handlers/extract_function.rs65
1 files changed, 63 insertions, 2 deletions
diff --git a/crates/ide_assists/src/handlers/extract_function.rs b/crates/ide_assists/src/handlers/extract_function.rs
index 6311afc1f..a2dba915c 100644
--- a/crates/ide_assists/src/handlers/extract_function.rs
+++ b/crates/ide_assists/src/handlers/extract_function.rs
@@ -642,6 +642,10 @@ fn vars_used_in_body(ctx: &AssistContext, body: &FunctionBody) -> Vec<Local> {
642 .collect() 642 .collect()
643} 643}
644 644
645fn body_contains_await(body: &FunctionBody) -> bool {
646 body.descendants().any(|d| matches!(d.kind(), SyntaxKind::AWAIT_EXPR))
647}
648
645/// find `self` param, that was not defined inside `body` 649/// find `self` param, that was not defined inside `body`
646/// 650///
647/// It should skip `self` params from impls inside `body` 651/// It should skip `self` params from impls inside `body`
@@ -1123,9 +1127,10 @@ fn format_function(
1123 let params = make_param_list(ctx, module, fun); 1127 let params = make_param_list(ctx, module, fun);
1124 let ret_ty = make_ret_ty(ctx, module, fun); 1128 let ret_ty = make_ret_ty(ctx, module, fun);
1125 let body = make_body(ctx, old_indent, new_indent, fun); 1129 let body = make_body(ctx, old_indent, new_indent, fun);
1130 let async_kw = if body_contains_await(&fun.body) { "async " } else { "" };
1126 match ctx.config.snippet_cap { 1131 match ctx.config.snippet_cap {
1127 Some(_) => format_to!(fn_def, "\n\n{}fn $0{}{}", new_indent, fun.name, params), 1132 Some(_) => format_to!(fn_def, "\n\n{}{}fn $0{}{}", new_indent, async_kw, fun.name, params),
1128 None => format_to!(fn_def, "\n\n{}fn {}{}", new_indent, fun.name, params), 1133 None => format_to!(fn_def, "\n\n{}{}fn {}{}", new_indent, async_kw, fun.name, params),
1129 } 1134 }
1130 if let Some(ret_ty) = ret_ty { 1135 if let Some(ret_ty) = ret_ty {
1131 format_to!(fn_def, " {}", ret_ty); 1136 format_to!(fn_def, " {}", ret_ty);
@@ -3565,4 +3570,60 @@ fn $0fun_name(n: i32) -> i32 {
3565}", 3570}",
3566 ); 3571 );
3567 } 3572 }
3573
3574 #[test]
3575 fn extract_with_await() {
3576 check_assist(
3577 extract_function,
3578 r#"fn main() {
3579 $0some_function().await;$0
3580}
3581
3582async fn some_function() {
3583
3584}
3585"#,
3586 r#"
3587fn main() {
3588 fun_name();
3589}
3590
3591async fn $0fun_name() {
3592 some_function().await;
3593}
3594
3595async fn some_function() {
3596
3597}
3598"#,
3599 );
3600 }
3601
3602 #[test]
3603 fn extract_with_await_in_args() {
3604 check_assist(
3605 extract_function,
3606 r#"fn main() {
3607 $0function_call("a", some_function().await);$0
3608}
3609
3610async fn some_function() {
3611
3612}
3613"#,
3614 r#"
3615fn main() {
3616 fun_name();
3617}
3618
3619async fn $0fun_name() {
3620 function_call("a", some_function().await);
3621}
3622
3623async fn some_function() {
3624
3625}
3626"#,
3627 );
3628 }
3568} 3629}