Skip to content

Commit 5b59a5e

Browse files
committed
refactor(assist/add_return_type): avoid threading needs_whitespace
1 parent e031267 commit 5b59a5e

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

crates/ide_assists/src/handlers/add_return_type.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
1616
// fn foo() -> i32 { 42i32 }
1717
// ```
1818
pub(crate) fn add_return_type(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
19-
let (fn_type, tail_expr, builder_edit_pos, needs_whitespace) = extract_tail(ctx)?;
19+
let (fn_type, tail_expr, builder_edit_pos) = extract_tail(ctx)?;
2020
let module = ctx.sema.scope(tail_expr.syntax()).module()?;
2121
let ty = ctx.sema.type_of_expr(&tail_expr)?.adjusted();
2222
if ty.is_unit() {
@@ -32,14 +32,13 @@ pub(crate) fn add_return_type(acc: &mut Assists, ctx: &AssistContext) -> Option<
3232
},
3333
tail_expr.syntax().text_range(),
3434
|builder| {
35-
let preceeding_whitespace = if needs_whitespace { " " } else { "" };
36-
3735
match builder_edit_pos {
38-
InsertOrReplace::Insert(insert_pos) => {
36+
InsertOrReplace::Insert(insert_pos, needs_whitespace) => {
37+
let preceeding_whitespace = if needs_whitespace { " " } else { "" };
3938
builder.insert(insert_pos, &format!("{}-> {} ", preceeding_whitespace, ty))
4039
}
4140
InsertOrReplace::Replace(text_range) => {
42-
builder.replace(text_range, &format!("{}-> {}", preceeding_whitespace, ty))
41+
builder.replace(text_range, &format!("-> {}", ty))
4342
}
4443
}
4544
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<
5251
}
5352

5453
enum InsertOrReplace {
55-
Insert(TextSize),
54+
Insert(TextSize, bool),
5655
Replace(TextRange),
5756
}
5857

@@ -61,13 +60,13 @@ enum InsertOrReplace {
6160
fn ret_ty_to_action(
6261
ret_ty: Option<ast::RetType>,
6362
insert_after: SyntaxToken,
64-
) -> Option<(InsertOrReplace, bool)> {
63+
) -> Option<InsertOrReplace> {
6564
match ret_ty {
6665
Some(ret_ty) => match ret_ty.ty() {
6766
Some(ast::Type::InferType(_)) | None => {
6867
cov_mark::hit!(existing_infer_ret_type);
6968
cov_mark::hit!(existing_infer_ret_type_closure);
70-
Some((InsertOrReplace::Replace(ret_ty.syntax().text_range()), false))
69+
Some(InsertOrReplace::Replace(ret_ty.syntax().text_range()))
7170
}
7271
_ => {
7372
cov_mark::hit!(existing_ret_type);
@@ -84,7 +83,7 @@ fn ret_ty_to_action(
8483
_ => (insert_after_pos, true),
8584
};
8685

87-
Some((InsertOrReplace::Insert(insert_pos), needs_whitespace))
86+
Some(InsertOrReplace::Insert(insert_pos, needs_whitespace))
8887
}
8988
}
9089
}
@@ -94,13 +93,13 @@ enum FnType {
9493
Closure { wrap_expr: bool },
9594
}
9695

97-
fn extract_tail(ctx: &AssistContext) -> Option<(FnType, ast::Expr, InsertOrReplace, bool)> {
98-
let (fn_type, tail_expr, return_type_range, action, needs_whitespace) =
96+
fn extract_tail(ctx: &AssistContext) -> Option<(FnType, ast::Expr, InsertOrReplace)> {
97+
let (fn_type, tail_expr, return_type_range, action) =
9998
if let Some(closure) = ctx.find_node_at_offset::<ast::ClosureExpr>() {
10099
let rpipe = closure.param_list()?.syntax().last_token()?;
101100
let rpipe_pos = rpipe.text_range().end();
102101

103-
let (action, needs_whitespace) = ret_ty_to_action(closure.ret_type(), rpipe)?;
102+
let action = ret_ty_to_action(closure.ret_type(), rpipe)?;
104103

105104
let body = closure.body()?;
106105
let body_start = body.syntax().first_token()?.text_range().start();
@@ -110,21 +109,21 @@ fn extract_tail(ctx: &AssistContext) -> Option<(FnType, ast::Expr, InsertOrRepla
110109
};
111110

112111
let ret_range = TextRange::new(rpipe_pos, body_start);
113-
(FnType::Closure { wrap_expr }, tail_expr, ret_range, action, needs_whitespace)
112+
(FnType::Closure { wrap_expr }, tail_expr, ret_range, action)
114113
} else {
115114
let func = ctx.find_node_at_offset::<ast::Fn>()?;
116115

117116
let rparen = func.param_list()?.r_paren_token()?;
118117
let rparen_pos = rparen.text_range().end();
119-
let (action, needs_whitespace) = ret_ty_to_action(func.ret_type(), rparen)?;
118+
let action = ret_ty_to_action(func.ret_type(), rparen)?;
120119

121120
let body = func.body()?;
122121
let stmt_list = body.stmt_list()?;
123122
let tail_expr = stmt_list.tail_expr()?;
124123

125124
let ret_range_end = stmt_list.l_curly_token()?.text_range().start();
126125
let ret_range = TextRange::new(rparen_pos, ret_range_end);
127-
(FnType::Function, tail_expr, ret_range, action, needs_whitespace)
126+
(FnType::Function, tail_expr, ret_range, action)
128127
};
129128
let range = ctx.selection_trimmed();
130129
if return_type_range.contains_range(range) {
@@ -136,7 +135,7 @@ fn extract_tail(ctx: &AssistContext) -> Option<(FnType, ast::Expr, InsertOrRepla
136135
} else {
137136
return None;
138137
}
139-
Some((fn_type, tail_expr, action, needs_whitespace))
138+
Some((fn_type, tail_expr, action))
140139
}
141140

142141
#[cfg(test)]

0 commit comments

Comments
 (0)