mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-11 02:42:33 +00:00
refactor(assist/add_return_type): avoid threading needs_whitespace
This commit is contained in:
parent
e031267602
commit
5b59a5eca8
1 changed files with 15 additions and 16 deletions
|
@ -16,7 +16,7 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
|
||||||
// fn foo() -> i32 { 42i32 }
|
// fn foo() -> i32 { 42i32 }
|
||||||
// ```
|
// ```
|
||||||
pub(crate) fn add_return_type(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
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 module = ctx.sema.scope(tail_expr.syntax()).module()?;
|
||||||
let ty = ctx.sema.type_of_expr(&tail_expr)?.adjusted();
|
let ty = ctx.sema.type_of_expr(&tail_expr)?.adjusted();
|
||||||
if ty.is_unit() {
|
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(),
|
tail_expr.syntax().text_range(),
|
||||||
|builder| {
|
|builder| {
|
||||||
let preceeding_whitespace = if needs_whitespace { " " } else { "" };
|
|
||||||
|
|
||||||
match builder_edit_pos {
|
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))
|
builder.insert(insert_pos, &format!("{}-> {} ", preceeding_whitespace, ty))
|
||||||
}
|
}
|
||||||
InsertOrReplace::Replace(text_range) => {
|
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 {
|
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 {
|
enum InsertOrReplace {
|
||||||
Insert(TextSize),
|
Insert(TextSize, bool),
|
||||||
Replace(TextRange),
|
Replace(TextRange),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,13 +60,13 @@ enum InsertOrReplace {
|
||||||
fn ret_ty_to_action(
|
fn ret_ty_to_action(
|
||||||
ret_ty: Option<ast::RetType>,
|
ret_ty: Option<ast::RetType>,
|
||||||
insert_after: SyntaxToken,
|
insert_after: SyntaxToken,
|
||||||
) -> Option<(InsertOrReplace, bool)> {
|
) -> Option<InsertOrReplace> {
|
||||||
match ret_ty {
|
match ret_ty {
|
||||||
Some(ret_ty) => match ret_ty.ty() {
|
Some(ret_ty) => match ret_ty.ty() {
|
||||||
Some(ast::Type::InferType(_)) | None => {
|
Some(ast::Type::InferType(_)) | None => {
|
||||||
cov_mark::hit!(existing_infer_ret_type);
|
cov_mark::hit!(existing_infer_ret_type);
|
||||||
cov_mark::hit!(existing_infer_ret_type_closure);
|
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);
|
cov_mark::hit!(existing_ret_type);
|
||||||
|
@ -84,7 +83,7 @@ fn ret_ty_to_action(
|
||||||
_ => (insert_after_pos, true),
|
_ => (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 },
|
Closure { wrap_expr: bool },
|
||||||
}
|
}
|
||||||
|
|
||||||
fn extract_tail(ctx: &AssistContext) -> Option<(FnType, ast::Expr, InsertOrReplace, bool)> {
|
fn extract_tail(ctx: &AssistContext) -> Option<(FnType, ast::Expr, InsertOrReplace)> {
|
||||||
let (fn_type, tail_expr, return_type_range, action, needs_whitespace) =
|
let (fn_type, tail_expr, return_type_range, action) =
|
||||||
if let Some(closure) = ctx.find_node_at_offset::<ast::ClosureExpr>() {
|
if let Some(closure) = ctx.find_node_at_offset::<ast::ClosureExpr>() {
|
||||||
let rpipe = closure.param_list()?.syntax().last_token()?;
|
let rpipe = closure.param_list()?.syntax().last_token()?;
|
||||||
let rpipe_pos = rpipe.text_range().end();
|
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 = closure.body()?;
|
||||||
let body_start = body.syntax().first_token()?.text_range().start();
|
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);
|
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 {
|
} else {
|
||||||
let func = ctx.find_node_at_offset::<ast::Fn>()?;
|
let func = ctx.find_node_at_offset::<ast::Fn>()?;
|
||||||
|
|
||||||
let rparen = func.param_list()?.r_paren_token()?;
|
let rparen = func.param_list()?.r_paren_token()?;
|
||||||
let rparen_pos = rparen.text_range().end();
|
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 body = func.body()?;
|
||||||
let stmt_list = body.stmt_list()?;
|
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_end = stmt_list.l_curly_token()?.text_range().start();
|
||||||
let ret_range = TextRange::new(rparen_pos, ret_range_end);
|
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();
|
let range = ctx.selection_trimmed();
|
||||||
if return_type_range.contains_range(range) {
|
if return_type_range.contains_range(range) {
|
||||||
|
@ -136,7 +135,7 @@ fn extract_tail(ctx: &AssistContext) -> Option<(FnType, ast::Expr, InsertOrRepla
|
||||||
} else {
|
} else {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
Some((fn_type, tail_expr, action, needs_whitespace))
|
Some((fn_type, tail_expr, action))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue