Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix extract_function with await #9314

Merged
merged 3 commits into from
Jun 21, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions crates/ide_assists/src/handlers/extract_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 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, 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, 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),
Expand Down Expand Up @@ -954,7 +959,12 @@ fn scope_for_fn_insertion_node(node: &SyntaxNode, anchor: Anchor) -> Option<Synt
last_ancestor
}

fn format_replacement(ctx: &AssistContext, fun: &Function, indent: IndentLevel) -> String {
fn format_replacement(
ctx: &AssistContext,
fun: &Function,
indent: IndentLevel,
body_contains_await: bool,
) -> String {
let ret_ty = fun.return_type(ctx);

let args = fun.params.iter().map(|param| param.to_arg(ctx));
Expand Down Expand Up @@ -994,6 +1004,9 @@ fn format_replacement(ctx: &AssistContext, fun: &Function, indent: IndentLevel)
}
}
format_to!(buf, "{}", expr);
if body_contains_await {
buf.push_str(".await");
}
if fun.ret_ty.is_unit()
&& (!fun.vars_defined_in_body_and_outlive.is_empty() || !expr.is_block_like())
{
Expand Down Expand Up @@ -1122,12 +1135,13 @@ fn format_function(
fun: &Function,
old_indent: IndentLevel,
new_indent: IndentLevel,
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 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),
Expand Down Expand Up @@ -3681,7 +3695,7 @@ async fn some_function() {
"#,
r#"
fn main() {
fun_name();
fun_name().await;
}

async fn $0fun_name() {
Expand Down Expand Up @@ -3710,7 +3724,7 @@ async fn some_function() {
"#,
r#"
fn main() {
fun_name();
fun_name().await;
}

async fn $0fun_name() {
Expand Down