From f6a247dc573bb2a614234d248328a0cac81f1e57 Mon Sep 17 00:00:00 2001 From: Hayashi Mikihiro <34ttrweoewiwe28@gmail.com> Date: Tue, 1 Jul 2025 17:19:16 +0900 Subject: [PATCH 1/2] Migrate `replace_is_method_with_if_let_method` Assist to use `SyntaxFactory` Signed-off-by: Hayashi Mikihiro <34ttrweoewiwe28@gmail.com> --- .../replace_is_method_with_if_let_method.rs | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/crates/ide-assists/src/handlers/replace_is_method_with_if_let_method.rs b/crates/ide-assists/src/handlers/replace_is_method_with_if_let_method.rs index e933bcc40dbb..5ef8ba46b9e5 100644 --- a/crates/ide-assists/src/handlers/replace_is_method_with_if_let_method.rs +++ b/crates/ide-assists/src/handlers/replace_is_method_with_if_let_method.rs @@ -1,8 +1,5 @@ use ide_db::syntax_helpers::suggest_name; -use syntax::{ - ast::{self, AstNode, make}, - ted, -}; +use syntax::ast::{self, AstNode, syntax_factory::SyntaxFactory}; use crate::{AssistContext, AssistId, Assists}; @@ -60,21 +57,24 @@ pub(crate) fn replace_is_method_with_if_let_method( message, call_expr.syntax().text_range(), |edit| { - let call_expr = edit.make_mut(call_expr); - - let var_pat = make::ident_pat(false, false, make::name(&var_name)); - let pat = make::tuple_struct_pat(make::ext::ident_path(text), [var_pat.into()]); - let let_expr = make::expr_let(pat.into(), receiver).clone_for_update(); - - if let Some(cap) = ctx.config.snippet_cap { - if let Some(ast::Pat::TupleStructPat(pat)) = let_expr.pat() { - if let Some(first_var) = pat.fields().next() { - edit.add_placeholder_snippet(cap, first_var); - } - } + let make = SyntaxFactory::with_mappings(); + let mut editor = edit.make_editor(call_expr.syntax()); + + let var_pat = make.ident_pat(false, false, make.name(&var_name)); + let pat = make.tuple_struct_pat(make.ident_path(text), [var_pat.into()]); + let let_expr = make.expr_let(pat.into(), receiver); + + if let Some(cap) = ctx.config.snippet_cap + && let Some(ast::Pat::TupleStructPat(pat)) = let_expr.pat() + && let Some(first_var) = pat.fields().next() + { + let placeholder = edit.make_placeholder_snippet(cap); + editor.add_annotation(first_var.syntax(), placeholder); } - ted::replace(call_expr.syntax(), let_expr.syntax()); + editor.replace(call_expr.syntax(), let_expr.syntax()); + editor.add_mappings(make.finish_with_mappings()); + edit.add_file_edits(ctx.vfs_file_id(), editor); }, ) } From fcc81a38b3ed55447735f71e8d374fcb36c22f24 Mon Sep 17 00:00:00 2001 From: Hayashi Mikihiro <34ttrweoewiwe28@gmail.com> Date: Tue, 1 Jul 2025 18:38:50 +0900 Subject: [PATCH 2/2] remove `if-let` chains Signed-off-by: Hayashi Mikihiro <34ttrweoewiwe28@gmail.com> --- .../replace_is_method_with_if_let_method.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/crates/ide-assists/src/handlers/replace_is_method_with_if_let_method.rs b/crates/ide-assists/src/handlers/replace_is_method_with_if_let_method.rs index 5ef8ba46b9e5..62914ee7f383 100644 --- a/crates/ide-assists/src/handlers/replace_is_method_with_if_let_method.rs +++ b/crates/ide-assists/src/handlers/replace_is_method_with_if_let_method.rs @@ -64,12 +64,13 @@ pub(crate) fn replace_is_method_with_if_let_method( let pat = make.tuple_struct_pat(make.ident_path(text), [var_pat.into()]); let let_expr = make.expr_let(pat.into(), receiver); - if let Some(cap) = ctx.config.snippet_cap - && let Some(ast::Pat::TupleStructPat(pat)) = let_expr.pat() - && let Some(first_var) = pat.fields().next() - { - let placeholder = edit.make_placeholder_snippet(cap); - editor.add_annotation(first_var.syntax(), placeholder); + if let Some(cap) = ctx.config.snippet_cap { + if let Some(ast::Pat::TupleStructPat(pat)) = let_expr.pat() { + if let Some(first_var) = pat.fields().next() { + let placeholder = edit.make_placeholder_snippet(cap); + editor.add_annotation(first_var.syntax(), placeholder); + } + } } editor.replace(call_expr.syntax(), let_expr.syntax());