refactor(assist/add_return_type): avoid threading needs_whitespace

This commit is contained in:
Skyler Rain Ross 2021-12-02 10:46:07 -08:00
parent e031267602
commit 5b59a5eca8
No known key found for this signature in database
GPG Key ID: F0FA59ACC62ACF43

View File

@ -16,7 +16,7 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
// fn foo() -> i32 { 42i32 }
// ```
pub(crate) fn add_return_type(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let (fn_type, tail_expr, builder_edit_pos, needs_whitespace) = extract_tail(ctx)?;
let (fn_type, tail_expr, builder_edit_pos) = extract_tail(ctx)?;
let module = ctx.sema.scope(tail_expr.syntax()).module()?;
let ty = ctx.sema.type_of_expr(&tail_expr)?.adjusted();
if ty.is_unit() {
@ -32,14 +32,13 @@ pub(crate) fn add_return_type(acc: &mut Assists, ctx: &AssistContext) -> Option<
},
tail_expr.syntax().text_range(),
|builder| {
let preceeding_whitespace = if needs_whitespace { " " } else { "" };
match builder_edit_pos {
InsertOrReplace::Insert(insert_pos) => {
InsertOrReplace::Insert(insert_pos, needs_whitespace) => {
let preceeding_whitespace = if needs_whitespace { " " } else { "" };
builder.insert(insert_pos, &format!("{}-> {} ", preceeding_whitespace, ty))
}
InsertOrReplace::Replace(text_range) => {
builder.replace(text_range, &format!("{}-> {}", preceeding_whitespace, ty))
builder.replace(text_range, &format!("-> {}", ty))
}
}
if let FnType::Closure { wrap_expr: true } = fn_type {
@ -52,7 +51,7 @@ pub(crate) fn add_return_type(acc: &mut Assists, ctx: &AssistContext) -> Option<
}
enum InsertOrReplace {
Insert(TextSize),
Insert(TextSize, bool),
Replace(TextRange),
}
@ -61,13 +60,13 @@ enum InsertOrReplace {
fn ret_ty_to_action(
ret_ty: Option<ast::RetType>,
insert_after: SyntaxToken,
) -> Option<(InsertOrReplace, bool)> {
) -> Option<InsertOrReplace> {
match ret_ty {
Some(ret_ty) => match ret_ty.ty() {
Some(ast::Type::InferType(_)) | None => {
cov_mark::hit!(existing_infer_ret_type);
cov_mark::hit!(existing_infer_ret_type_closure);
Some((InsertOrReplace::Replace(ret_ty.syntax().text_range()), false))
Some(InsertOrReplace::Replace(ret_ty.syntax().text_range()))
}
_ => {
cov_mark::hit!(existing_ret_type);
@ -84,7 +83,7 @@ fn ret_ty_to_action(
_ => (insert_after_pos, true),
};
Some((InsertOrReplace::Insert(insert_pos), needs_whitespace))
Some(InsertOrReplace::Insert(insert_pos, needs_whitespace))
}
}
}
@ -94,13 +93,13 @@ enum FnType {
Closure { wrap_expr: bool },
}
fn extract_tail(ctx: &AssistContext) -> Option<(FnType, ast::Expr, InsertOrReplace, bool)> {
let (fn_type, tail_expr, return_type_range, action, needs_whitespace) =
fn extract_tail(ctx: &AssistContext) -> Option<(FnType, ast::Expr, InsertOrReplace)> {
let (fn_type, tail_expr, return_type_range, action) =
if let Some(closure) = ctx.find_node_at_offset::<ast::ClosureExpr>() {
let rpipe = closure.param_list()?.syntax().last_token()?;
let rpipe_pos = rpipe.text_range().end();
let (action, needs_whitespace) = ret_ty_to_action(closure.ret_type(), rpipe)?;
let action = ret_ty_to_action(closure.ret_type(), rpipe)?;
let body = closure.body()?;
let body_start = body.syntax().first_token()?.text_range().start();
@ -110,13 +109,13 @@ fn extract_tail(ctx: &AssistContext) -> Option<(FnType, ast::Expr, InsertOrRepla
};
let ret_range = TextRange::new(rpipe_pos, body_start);
(FnType::Closure { wrap_expr }, tail_expr, ret_range, action, needs_whitespace)
(FnType::Closure { wrap_expr }, tail_expr, ret_range, action)
} else {
let func = ctx.find_node_at_offset::<ast::Fn>()?;
let rparen = func.param_list()?.r_paren_token()?;
let rparen_pos = rparen.text_range().end();
let (action, needs_whitespace) = ret_ty_to_action(func.ret_type(), rparen)?;
let action = ret_ty_to_action(func.ret_type(), rparen)?;
let body = func.body()?;
let stmt_list = body.stmt_list()?;
@ -124,7 +123,7 @@ fn extract_tail(ctx: &AssistContext) -> Option<(FnType, ast::Expr, InsertOrRepla
let ret_range_end = stmt_list.l_curly_token()?.text_range().start();
let ret_range = TextRange::new(rparen_pos, ret_range_end);
(FnType::Function, tail_expr, ret_range, action, needs_whitespace)
(FnType::Function, tail_expr, ret_range, action)
};
let range = ctx.selection_trimmed();
if return_type_range.contains_range(range) {
@ -136,7 +135,7 @@ fn extract_tail(ctx: &AssistContext) -> Option<(FnType, ast::Expr, InsertOrRepla
} else {
return None;
}
Some((fn_type, tail_expr, action, needs_whitespace))
Some((fn_type, tail_expr, action))
}
#[cfg(test)]