Skip to content
Merged
Show file tree
Hide file tree
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
21 changes: 9 additions & 12 deletions crates/ide_assists/src/handlers/replace_let_with_if_let.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,19 @@ pub(crate) fn replace_let_with_if_let(acc: &mut Assists, ctx: &AssistContext) ->
"Replace with if-let",
target,
|edit| {
let with_placeholder: ast::Pat = match happy_variant {
None => make::wildcard_pat().into(),
Some(var_name) => make::tuple_struct_pat(
make::ext::ident_path(var_name),
once(make::wildcard_pat().into()),
)
.into(),
let pat = match happy_variant {
None => original_pat,
Some(var_name) => {
make::tuple_struct_pat(make::ext::ident_path(var_name), once(original_pat))
.into()
}
};

let block =
make::block_expr(None, None).indent(IndentLevel::from_node(let_stmt.syntax()));
let if_ = make::expr_if(make::condition(init, Some(with_placeholder)), block, None);
make::ext::empty_block_expr().indent(IndentLevel::from_node(let_stmt.syntax()));
let if_ = make::expr_if(make::condition(init, Some(pat)), block, None);
let stmt = make::expr_stmt(if_);

let placeholder = stmt.syntax().descendants().find_map(ast::WildcardPat::cast).unwrap();
let stmt = stmt.replace_descendant(placeholder.into(), original_pat);

edit.replace_ast(ast::Stmt::from(let_stmt), ast::Stmt::from(stmt));
},
)
Expand Down
3 changes: 3 additions & 0 deletions crates/syntax/src/ast/make.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ pub mod ext {
pub fn expr_todo() -> ast::Expr {
expr_from_text("todo!()")
}
pub fn empty_block_expr() -> ast::BlockExpr {
block_expr(None, None)
}

pub fn ty_bool() -> ast::Type {
ty_path(ident_path("bool"))
Expand Down