mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-29 02:52:11 +00:00
add SyntaxEditor::delete_all to migrate utils.rs add_trait_assoc_items_to_impl
This commit is contained in:
parent
8611b71459
commit
e600060680
3 changed files with 68 additions and 27 deletions
|
|
@ -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));
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue