diff options
Diffstat (limited to 'crates/ide_assists/src/handlers')
-rw-r--r-- | crates/ide_assists/src/handlers/extract_function.rs | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/crates/ide_assists/src/handlers/extract_function.rs b/crates/ide_assists/src/handlers/extract_function.rs index ac7f0959b..870d4f665 100644 --- a/crates/ide_assists/src/handlers/extract_function.rs +++ b/crates/ide_assists/src/handlers/extract_function.rs | |||
@@ -109,10 +109,15 @@ pub(crate) fn extract_function(acc: &mut Assists, ctx: &AssistContext) -> Option | |||
109 | 109 | ||
110 | let new_indent = IndentLevel::from_node(&insert_after); | 110 | let new_indent = IndentLevel::from_node(&insert_after); |
111 | let old_indent = fun.body.indent_level(); | 111 | let old_indent = fun.body.indent_level(); |
112 | let body_contains_await = body_contains_await(&fun.body); | ||
112 | 113 | ||
113 | builder.replace(target_range, format_replacement(ctx, &fun, old_indent)); | 114 | builder.replace( |
115 | target_range, | ||
116 | format_replacement(ctx, &fun, old_indent, body_contains_await), | ||
117 | ); | ||
114 | 118 | ||
115 | let fn_def = format_function(ctx, module, &fun, old_indent, new_indent); | 119 | let fn_def = |
120 | format_function(ctx, module, &fun, old_indent, new_indent, body_contains_await); | ||
116 | let insert_offset = insert_after.text_range().end(); | 121 | let insert_offset = insert_after.text_range().end(); |
117 | match ctx.config.snippet_cap { | 122 | match ctx.config.snippet_cap { |
118 | Some(cap) => builder.insert_snippet(cap, insert_offset, fn_def), | 123 | Some(cap) => builder.insert_snippet(cap, insert_offset, fn_def), |
@@ -954,7 +959,12 @@ fn scope_for_fn_insertion_node(node: &SyntaxNode, anchor: Anchor) -> Option<Synt | |||
954 | last_ancestor | 959 | last_ancestor |
955 | } | 960 | } |
956 | 961 | ||
957 | fn format_replacement(ctx: &AssistContext, fun: &Function, indent: IndentLevel) -> String { | 962 | fn format_replacement( |
963 | ctx: &AssistContext, | ||
964 | fun: &Function, | ||
965 | indent: IndentLevel, | ||
966 | body_contains_await: bool, | ||
967 | ) -> String { | ||
958 | let ret_ty = fun.return_type(ctx); | 968 | let ret_ty = fun.return_type(ctx); |
959 | 969 | ||
960 | let args = fun.params.iter().map(|param| param.to_arg(ctx)); | 970 | let args = fun.params.iter().map(|param| param.to_arg(ctx)); |
@@ -994,6 +1004,9 @@ fn format_replacement(ctx: &AssistContext, fun: &Function, indent: IndentLevel) | |||
994 | } | 1004 | } |
995 | } | 1005 | } |
996 | format_to!(buf, "{}", expr); | 1006 | format_to!(buf, "{}", expr); |
1007 | if body_contains_await { | ||
1008 | buf.push_str(".await"); | ||
1009 | } | ||
997 | if fun.ret_ty.is_unit() | 1010 | if fun.ret_ty.is_unit() |
998 | && (!fun.vars_defined_in_body_and_outlive.is_empty() || !expr.is_block_like()) | 1011 | && (!fun.vars_defined_in_body_and_outlive.is_empty() || !expr.is_block_like()) |
999 | { | 1012 | { |
@@ -1122,12 +1135,13 @@ fn format_function( | |||
1122 | fun: &Function, | 1135 | fun: &Function, |
1123 | old_indent: IndentLevel, | 1136 | old_indent: IndentLevel, |
1124 | new_indent: IndentLevel, | 1137 | new_indent: IndentLevel, |
1138 | body_contains_await: bool, | ||
1125 | ) -> String { | 1139 | ) -> String { |
1126 | let mut fn_def = String::new(); | 1140 | let mut fn_def = String::new(); |
1127 | let params = make_param_list(ctx, module, fun); | 1141 | let params = make_param_list(ctx, module, fun); |
1128 | let ret_ty = make_ret_ty(ctx, module, fun); | 1142 | let ret_ty = make_ret_ty(ctx, module, fun); |
1129 | let body = make_body(ctx, old_indent, new_indent, fun); | 1143 | let body = make_body(ctx, old_indent, new_indent, fun); |
1130 | let async_kw = if body_contains_await(&fun.body) { "async " } else { "" }; | 1144 | let async_kw = if body_contains_await { "async " } else { "" }; |
1131 | match ctx.config.snippet_cap { | 1145 | match ctx.config.snippet_cap { |
1132 | Some(_) => format_to!(fn_def, "\n\n{}{}fn $0{}{}", new_indent, async_kw, fun.name, params), | 1146 | Some(_) => format_to!(fn_def, "\n\n{}{}fn $0{}{}", new_indent, async_kw, fun.name, params), |
1133 | None => format_to!(fn_def, "\n\n{}{}fn {}{}", new_indent, async_kw, fun.name, params), | 1147 | None => format_to!(fn_def, "\n\n{}{}fn {}{}", new_indent, async_kw, fun.name, params), |
@@ -3681,7 +3695,7 @@ async fn some_function() { | |||
3681 | "#, | 3695 | "#, |
3682 | r#" | 3696 | r#" |
3683 | fn main() { | 3697 | fn main() { |
3684 | fun_name(); | 3698 | fun_name().await; |
3685 | } | 3699 | } |
3686 | 3700 | ||
3687 | async fn $0fun_name() { | 3701 | async fn $0fun_name() { |
@@ -3710,7 +3724,7 @@ async fn some_function() { | |||
3710 | "#, | 3724 | "#, |
3711 | r#" | 3725 | r#" |
3712 | fn main() { | 3726 | fn main() { |
3713 | fun_name(); | 3727 | fun_name().await; |
3714 | } | 3728 | } |
3715 | 3729 | ||
3716 | async fn $0fun_name() { | 3730 | async fn $0fun_name() { |