From 48b851f637be467e71cf1273b641d059009f3144 Mon Sep 17 00:00:00 2001 From: bit-aloo Date: Fri, 29 May 2026 12:12:10 +0530 Subject: [PATCH] remove clone subtree from clone_subtree --- crates/ide-ssr/src/fragments.rs | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/crates/ide-ssr/src/fragments.rs b/crates/ide-ssr/src/fragments.rs index 8d6b7c637d73..bb5234ed8f3c 100644 --- a/crates/ide-ssr/src/fragments.rs +++ b/crates/ide-ssr/src/fragments.rs @@ -6,7 +6,7 @@ //! needs to determine it somehow. We do this in a stupid way -- by pasting SSR //! rule into different contexts and checking what works. -use syntax::{AstNode, SyntaxNode, ast}; +use syntax::{AstNode, SyntaxNode, ast, syntax_editor::SyntaxEditor}; pub(crate) fn ty(s: &str) -> Result { fragment::("type T = {};", s) @@ -31,18 +31,20 @@ pub(crate) fn stmt(s: &str) -> Result { if !parse.errors().is_empty() { return Err(()); } - let mut node = - parse.tree().syntax().descendants().skip(2).find_map(ast::Stmt::cast).ok_or(())?; - if !s.ends_with(';') && node.to_string().ends_with(';') { - node = node.clone_for_update(); - if let Some(it) = node.syntax().last_token() { - it.detach() - } + let node = parse.tree().syntax().descendants().skip(2).find_map(ast::Stmt::cast).ok_or(())?; + let (editor, node) = SyntaxEditor::new(node.syntax().clone()); + let node = ast::Stmt::cast(node).ok_or(())?; + if !s.ends_with(';') + && node.to_string().ends_with(';') + && let Some(token) = node.syntax().last_token() + { + editor.delete(token); } + let node = editor.finish().new_root().clone(); if node.to_string() != s { return Err(()); } - Ok(node.syntax().clone_subtree()) + Ok(node) } fn fragment(template: &str, s: &str) -> Result { @@ -53,8 +55,9 @@ fn fragment(template: &str, s: &str) -> Result { return Err(()); } let node = parse.tree().syntax().descendants().find_map(T::cast).ok_or(())?; - if node.syntax().text() != s { + let (_, node) = SyntaxEditor::new(node.syntax().clone()); + if node.text() != s { return Err(()); } - Ok(node.syntax().clone_subtree()) + Ok(node) }