Skip to content

Migrate sort_items Assist to Use SyntaxFactory #18538

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 5, 2024
Merged
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
44 changes: 21 additions & 23 deletions crates/ide-assists/src/handlers/sort_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -114,7 +114,7 @@ trait AddRewrite {
label: &str,
old: Vec<T>,
new: Vec<T>,
target: TextRange,
target: &SyntaxNode,
) -> Option<()>;
}

Expand All @@ -124,15 +124,23 @@ impl AddRewrite for Assists {
label: &str,
old: Vec<T>,
new: Vec<T>,
target: TextRange,
target: &SyntaxNode,
) -> Option<()> {
self.add(AssistId("sort_items", AssistKind::RefactorRewrite), label, target, |builder| {
let mutable: Vec<T> = 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()));
})
self.add(
AssistId("sort_items", AssistKind::RefactorRewrite),
label,
target.text_range(),
|builder| {
let mut editor = builder.make_editor(target);

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)
},
)
}
}

Expand Down Expand Up @@ -167,7 +175,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())
}

fn add_sort_fields_assist(
Expand All @@ -182,12 +190,7 @@ fn add_sort_fields_assist(
return None;
}

acc.add_rewrite(
"Sort fields alphabetically",
fields,
sorted,
record_field_list.syntax().text_range(),
)
acc.add_rewrite("Sort fields alphabetically", fields, sorted, record_field_list.syntax())
}

fn add_sort_variants_assist(acc: &mut Assists, variant_list: ast::VariantList) -> Option<()> {
Expand All @@ -199,12 +202,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())
}

fn sort_by_name<T: HasName + Clone>(initial: &[T]) -> Vec<T> {
Expand Down
Loading