Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit f8b6b4c

Browse files
committed
Migrate wrap_return_type_in_result to mutable ast
1 parent 25088fc commit f8b6b4c

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

crates/ide-assists/src/handlers/wrap_return_type_in_result.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use ide_db::{
66
};
77
use syntax::{
88
ast::{self, make, Expr},
9-
match_ast, AstNode,
9+
match_ast, ted, AstNode,
1010
};
1111

1212
use crate::{AssistContext, AssistId, AssistKind, Assists};
@@ -52,8 +52,8 @@ pub(crate) fn wrap_return_type_in_result(acc: &mut Assists, ctx: &AssistContext<
5252
AssistId("wrap_return_type_in_result", AssistKind::RefactorRewrite),
5353
"Wrap return type in Result",
5454
type_ref.syntax().text_range(),
55-
|builder| {
56-
let body = ast::Expr::BlockExpr(body);
55+
|edit| {
56+
let body = edit.make_mut(ast::Expr::BlockExpr(body));
5757

5858
let mut exprs_to_wrap = Vec::new();
5959
let tail_cb = &mut |e: &_| tail_cb_impl(&mut exprs_to_wrap, e);
@@ -70,17 +70,24 @@ pub(crate) fn wrap_return_type_in_result(acc: &mut Assists, ctx: &AssistContext<
7070
let ok_wrapped = make::expr_call(
7171
make::expr_path(make::ext::ident_path("Ok")),
7272
make::arg_list(iter::once(ret_expr_arg.clone())),
73-
);
74-
builder.replace_ast(ret_expr_arg, ok_wrapped);
73+
)
74+
.clone_for_update();
75+
ted::replace(ret_expr_arg.syntax(), ok_wrapped.syntax());
7576
}
7677

77-
match ctx.config.snippet_cap {
78-
Some(cap) => {
79-
let snippet = format!("Result<{type_ref}, ${{0:_}}>");
80-
builder.replace_snippet(cap, type_ref.syntax().text_range(), snippet)
81-
}
82-
None => builder
83-
.replace(type_ref.syntax().text_range(), format!("Result<{type_ref}, _>")),
78+
let new_result_ty =
79+
make::ext::ty_result(type_ref.clone(), make::ty_placeholder()).clone_for_update();
80+
let old_result_ty = edit.make_mut(type_ref.clone());
81+
82+
ted::replace(old_result_ty.syntax(), new_result_ty.syntax());
83+
84+
if let Some(cap) = ctx.config.snippet_cap {
85+
let generic_args = new_result_ty
86+
.syntax()
87+
.descendants()
88+
.find_map(ast::GenericArgList::cast)
89+
.unwrap();
90+
edit.add_placeholder_snippet(cap, generic_args.generic_args().last().unwrap());
8491
}
8592
},
8693
)

0 commit comments

Comments
 (0)