add SyntaxEditor::delete_all to migrate utils.rs add_trait_assoc_items_to_impl

This commit is contained in:
Hmikihiro 2025-07-30 00:03:53 +09:00
parent 8611b71459
commit e600060680
3 changed files with 68 additions and 27 deletions

View file

@ -83,6 +83,16 @@ impl SyntaxEditor {
self.changes.push(Change::Replace(element.syntax_element(), None));
}
pub fn delete_all(&mut self, range: RangeInclusive<SyntaxElement>) {
if range.start() == range.end() {
self.delete(range.start());
return;
}
debug_assert!(is_ancestor_or_self_of_element(range.start(), &self.root));
self.changes.push(Change::ReplaceAll(range, Vec::new()))
}
pub fn replace(&mut self, old: impl Element, new: impl Element) {
let old = old.syntax_element();
debug_assert!(is_ancestor_or_self_of_element(&old, &self.root));

View file

@ -153,6 +153,23 @@ impl ast::VariantList {
}
}
impl ast::Fn {
pub fn replace_or_insert_body(&self, editor: &mut SyntaxEditor, body: ast::BlockExpr) {
if let Some(old_body) = self.body() {
editor.replace(old_body.syntax(), body.syntax());
} else {
let single_space = make::tokens::single_space();
let elements = vec![single_space.into(), body.syntax().clone().into()];
if let Some(semicolon) = self.semicolon_token() {
editor.replace_with_many(semicolon, elements);
} else {
editor.insert_all(Position::last_child_of(self.syntax()), elements);
}
}
}
}
fn normalize_ws_between_braces(editor: &mut SyntaxEditor, node: &SyntaxNode) -> Option<()> {
let make = SyntaxFactory::without_mappings();
let l = node
@ -184,6 +201,15 @@ pub trait Removable: AstNode {
fn remove(&self, editor: &mut SyntaxEditor);
}
impl Removable for ast::TypeBoundList {
fn remove(&self, editor: &mut SyntaxEditor) {
match self.syntax().siblings_with_tokens(Direction::Prev).find(|it| it.kind() == T![:]) {
Some(colon) => editor.delete_all(colon..=self.syntax().clone().into()),
None => editor.delete(self.syntax()),
}
}
}
impl Removable for ast::Use {
fn remove(&self, editor: &mut SyntaxEditor) {
let make = SyntaxFactory::without_mappings();