diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ide_assists/src/handlers/extract_function.rs | 65 |
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 | ||
645 | fn 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 | |||
3582 | async fn some_function() { | ||
3583 | |||
3584 | } | ||
3585 | "#, | ||
3586 | r#" | ||
3587 | fn main() { | ||
3588 | fun_name(); | ||
3589 | } | ||
3590 | |||
3591 | async fn $0fun_name() { | ||
3592 | some_function().await; | ||
3593 | } | ||
3594 | |||
3595 | async 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 | |||
3610 | async fn some_function() { | ||
3611 | |||
3612 | } | ||
3613 | "#, | ||
3614 | r#" | ||
3615 | fn main() { | ||
3616 | fun_name(); | ||
3617 | } | ||
3618 | |||
3619 | async fn $0fun_name() { | ||
3620 | function_call("a", some_function().await); | ||
3621 | } | ||
3622 | |||
3623 | async fn some_function() { | ||
3624 | |||
3625 | } | ||
3626 | "#, | ||
3627 | ); | ||
3628 | } | ||
3568 | } | 3629 | } |