Skip to content

Commit b2fa162

Browse files
committed
internal: Migrate replace_let_with_if_let assist to SyntaxEditor
1 parent fa8acfa commit b2fa162

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

src/tools/rust-analyzer/crates/ide-assists/src/handlers/replace_let_with_if_let.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
use std::iter::once;
2-
31
use ide_db::ty_filter::TryEnum;
42
use syntax::{
5-
ast::{
6-
self,
7-
edit::{AstNodeEdit, IndentLevel},
8-
make,
9-
},
3+
ast::{self, edit::IndentLevel, edit_in_place::Indent, syntax_factory::SyntaxFactory},
104
AstNode, T,
115
};
126

@@ -47,25 +41,28 @@ pub(crate) fn replace_let_with_if_let(acc: &mut Assists, ctx: &AssistContext<'_>
4741
AssistId("replace_let_with_if_let", AssistKind::RefactorRewrite),
4842
"Replace let with if let",
4943
target,
50-
|edit| {
44+
|builder| {
45+
let mut editor = builder.make_editor(let_stmt.syntax());
46+
let make = SyntaxFactory::new();
5147
let ty = ctx.sema.type_of_expr(&init);
5248
let happy_variant = ty
5349
.and_then(|ty| TryEnum::from_ty(&ctx.sema, &ty.adjusted()))
5450
.map(|it| it.happy_case());
5551
let pat = match happy_variant {
5652
None => original_pat,
5753
Some(var_name) => {
58-
make::tuple_struct_pat(make::ext::ident_path(var_name), once(original_pat))
59-
.into()
54+
make.tuple_struct_pat(make.ident_path(var_name), [original_pat]).into()
6055
}
6156
};
6257

63-
let block =
64-
make::ext::empty_block_expr().indent(IndentLevel::from_node(let_stmt.syntax()));
65-
let if_ = make::expr_if(make::expr_let(pat, init).into(), block, None);
66-
let stmt = make::expr_stmt(if_.into());
58+
let block = make.block_expr([], None);
59+
block.indent(IndentLevel::from_node(let_stmt.syntax()));
60+
let if_expr = make.expr_if(make.expr_let(pat, init).into(), block, None);
61+
let if_stmt = make.expr_stmt(if_expr.into());
6762

68-
edit.replace_ast(ast::Stmt::from(let_stmt), ast::Stmt::from(stmt));
63+
editor.replace(let_stmt.syntax(), if_stmt.syntax());
64+
editor.add_mappings(make.finish_with_mappings());
65+
builder.add_file_edits(ctx.file_id(), editor);
6966
},
7067
)
7168
}

0 commit comments

Comments
 (0)