From 7149c4dab91476e295695ad4293d4879f4b3f9dd Mon Sep 17 00:00:00 2001 From: Tarek Date: Wed, 27 Nov 2024 12:08:21 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20migrate=C2=A0`sort=5Fitems`=C2=A0assist?= =?UTF-8?q?=20to=20use=C2=A0`SyntaxFactory`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tarek --- crates/ide-assists/src/handlers/sort_items.rs | 40 ++++++++++--------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/crates/ide-assists/src/handlers/sort_items.rs b/crates/ide-assists/src/handlers/sort_items.rs index 64e30b1834..6a37a72996 100644 --- a/crates/ide-assists/src/handlers/sort_items.rs +++ b/crates/ide-assists/src/handlers/sort_items.rs @@ -4,7 +4,7 @@ use itertools::Itertools; use syntax::{ ast::{self, HasName}, - ted, AstNode, TextRange, + AstNode, SyntaxNode, }; use crate::{utils::get_methods, AssistContext, AssistId, AssistKind, Assists}; @@ -114,7 +114,7 @@ trait AddRewrite { label: &str, old: Vec, new: Vec, - target: TextRange, + target: SyntaxNode, ) -> Option<()>; } @@ -124,15 +124,24 @@ impl AddRewrite for Assists { label: &str, old: Vec, new: Vec, - target: TextRange, + target: SyntaxNode, ) -> Option<()> { - self.add(AssistId("sort_items", AssistKind::RefactorRewrite), label, target, |builder| { - let mutable: Vec = old.into_iter().map(|it| builder.make_mut(it)).collect(); - mutable - .into_iter() - .zip(new) - .for_each(|(old, new)| ted::replace(old.syntax(), new.clone_for_update().syntax())); - }) + let node = old.first().unwrap().syntax().parent().unwrap(); + self.add( + AssistId("sort_items", AssistKind::RefactorRewrite), + label, + target.text_range(), + |builder| { + let mut editor = builder.make_editor(&node); + + old.into_iter().zip(new).for_each(|(old, new)| { + // FIXME: remove `clone_for_update` when `SyntaxEditor` handles it for us + editor.replace(old.syntax(), new.clone_for_update().syntax()) + }); + + builder.add_file_edits(builder.file_id, editor) + }, + ) } } @@ -167,7 +176,7 @@ fn add_sort_methods_assist( return None; } - acc.add_rewrite("Sort methods alphabetically", methods, sorted, item_list.syntax().text_range()) + acc.add_rewrite("Sort methods alphabetically", methods, sorted, item_list.syntax().clone()) } fn add_sort_fields_assist( @@ -186,7 +195,7 @@ fn add_sort_fields_assist( "Sort fields alphabetically", fields, sorted, - record_field_list.syntax().text_range(), + record_field_list.syntax().clone(), ) } @@ -199,12 +208,7 @@ fn add_sort_variants_assist(acc: &mut Assists, variant_list: ast::VariantList) - return None; } - acc.add_rewrite( - "Sort variants alphabetically", - variants, - sorted, - variant_list.syntax().text_range(), - ) + acc.add_rewrite("Sort variants alphabetically", variants, sorted, variant_list.syntax().clone()) } fn sort_by_name(initial: &[T]) -> Vec {