From cd1ef8de1829d7ec17015f5cd02b476cd0d1d889 Mon Sep 17 00:00:00 2001 From: Daiki Ihara Date: Thu, 17 Jun 2021 23:54:49 +0900 Subject: Fix extract_function with await --- crates/ide_assists/src/handlers/extract_function.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/ide_assists/src/handlers/extract_function.rs b/crates/ide_assists/src/handlers/extract_function.rs index ac7f0959b..62edc4850 100644 --- a/crates/ide_assists/src/handlers/extract_function.rs +++ b/crates/ide_assists/src/handlers/extract_function.rs @@ -994,6 +994,9 @@ fn format_replacement(ctx: &AssistContext, fun: &Function, indent: IndentLevel) } } format_to!(buf, "{}", expr); + if body_contains_await(&fun.body) { + buf.push_str(".await"); + } if fun.ret_ty.is_unit() && (!fun.vars_defined_in_body_and_outlive.is_empty() || !expr.is_block_like()) { @@ -3681,7 +3684,7 @@ async fn some_function() { "#, r#" fn main() { - fun_name(); + fun_name().await; } async fn $0fun_name() { @@ -3710,7 +3713,7 @@ async fn some_function() { "#, r#" fn main() { - fun_name(); + fun_name().await; } async fn $0fun_name() { -- cgit v1.2.3 From 7a04f72220736025820adb7ac5c9b2ce41e4a4c2 Mon Sep 17 00:00:00 2001 From: Daiki Ihara Date: Mon, 21 Jun 2021 18:42:25 +0900 Subject: Fix pointed out --- crates/ide_assists/src/handlers/extract_function.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/crates/ide_assists/src/handlers/extract_function.rs b/crates/ide_assists/src/handlers/extract_function.rs index 62edc4850..6a9a84da0 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 let new_indent = IndentLevel::from_node(&insert_after); let old_indent = fun.body.indent_level(); + let is_body_contains_await = body_contains_await(&fun.body); - builder.replace(target_range, format_replacement(ctx, &fun, old_indent)); + builder.replace( + target_range, + format_replacement(ctx, &fun, old_indent, is_body_contains_await), + ); - let fn_def = format_function(ctx, module, &fun, old_indent, new_indent); + let fn_def = + format_function(ctx, module, &fun, old_indent, new_indent, is_body_contains_await); let insert_offset = insert_after.text_range().end(); match ctx.config.snippet_cap { 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 String { +fn format_replacement( + ctx: &AssistContext, + fun: &Function, + indent: IndentLevel, + is_body_contains_await: bool, +) -> String { let ret_ty = fun.return_type(ctx); let args = fun.params.iter().map(|param| param.to_arg(ctx)); @@ -994,7 +1004,7 @@ fn format_replacement(ctx: &AssistContext, fun: &Function, indent: IndentLevel) } } format_to!(buf, "{}", expr); - if body_contains_await(&fun.body) { + if is_body_contains_await { buf.push_str(".await"); } if fun.ret_ty.is_unit() @@ -1125,12 +1135,13 @@ fn format_function( fun: &Function, old_indent: IndentLevel, new_indent: IndentLevel, + is_body_contains_await: bool, ) -> String { let mut fn_def = String::new(); let params = make_param_list(ctx, module, fun); let ret_ty = make_ret_ty(ctx, module, fun); let body = make_body(ctx, old_indent, new_indent, fun); - let async_kw = if body_contains_await(&fun.body) { "async " } else { "" }; + let async_kw = if is_body_contains_await { "async " } else { "" }; match ctx.config.snippet_cap { Some(_) => format_to!(fn_def, "\n\n{}{}fn $0{}{}", new_indent, async_kw, fun.name, params), None => format_to!(fn_def, "\n\n{}{}fn {}{}", new_indent, async_kw, fun.name, params), -- cgit v1.2.3 From 873aa904f253abc60ebb5b7f8ce4412f3c984604 Mon Sep 17 00:00:00 2001 From: Daiki Ihara Date: Mon, 21 Jun 2021 22:47:39 +0900 Subject: Fix var name --- crates/ide_assists/src/handlers/extract_function.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/ide_assists/src/handlers/extract_function.rs b/crates/ide_assists/src/handlers/extract_function.rs index 6a9a84da0..870d4f665 100644 --- a/crates/ide_assists/src/handlers/extract_function.rs +++ b/crates/ide_assists/src/handlers/extract_function.rs @@ -109,15 +109,15 @@ pub(crate) fn extract_function(acc: &mut Assists, ctx: &AssistContext) -> Option let new_indent = IndentLevel::from_node(&insert_after); let old_indent = fun.body.indent_level(); - let is_body_contains_await = body_contains_await(&fun.body); + let body_contains_await = body_contains_await(&fun.body); builder.replace( target_range, - format_replacement(ctx, &fun, old_indent, is_body_contains_await), + format_replacement(ctx, &fun, old_indent, body_contains_await), ); let fn_def = - format_function(ctx, module, &fun, old_indent, new_indent, is_body_contains_await); + format_function(ctx, module, &fun, old_indent, new_indent, body_contains_await); let insert_offset = insert_after.text_range().end(); match ctx.config.snippet_cap { Some(cap) => builder.insert_snippet(cap, insert_offset, fn_def), @@ -963,7 +963,7 @@ fn format_replacement( ctx: &AssistContext, fun: &Function, indent: IndentLevel, - is_body_contains_await: bool, + body_contains_await: bool, ) -> String { let ret_ty = fun.return_type(ctx); @@ -1004,7 +1004,7 @@ fn format_replacement( } } format_to!(buf, "{}", expr); - if is_body_contains_await { + if body_contains_await { buf.push_str(".await"); } if fun.ret_ty.is_unit() @@ -1135,13 +1135,13 @@ fn format_function( fun: &Function, old_indent: IndentLevel, new_indent: IndentLevel, - is_body_contains_await: bool, + body_contains_await: bool, ) -> String { let mut fn_def = String::new(); let params = make_param_list(ctx, module, fun); let ret_ty = make_ret_ty(ctx, module, fun); let body = make_body(ctx, old_indent, new_indent, fun); - let async_kw = if is_body_contains_await { "async " } else { "" }; + let async_kw = if body_contains_await { "async " } else { "" }; match ctx.config.snippet_cap { Some(_) => format_to!(fn_def, "\n\n{}{}fn $0{}{}", new_indent, async_kw, fun.name, params), None => format_to!(fn_def, "\n\n{}{}fn {}{}", new_indent, async_kw, fun.name, params), -- cgit v1.2.3