Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/syntax/src/syntax_editor.rs')
-rw-r--r--crates/syntax/src/syntax_editor.rs29
1 files changed, 24 insertions, 5 deletions
diff --git a/crates/syntax/src/syntax_editor.rs b/crates/syntax/src/syntax_editor.rs
index 3fa584850f..18f5015e9e 100644
--- a/crates/syntax/src/syntax_editor.rs
+++ b/crates/syntax/src/syntax_editor.rs
@@ -5,7 +5,7 @@
//! [`SyntaxEditor`]: https://github.com/dotnet/roslyn/blob/43b0b05cc4f492fd5de00f6f6717409091df8daa/src/Workspaces/Core/Portable/Editing/SyntaxEditor.cs
use std::{
- fmt,
+ fmt, iter,
num::NonZeroU32,
ops::RangeInclusive,
sync::atomic::{AtomicU32, Ordering},
@@ -41,6 +41,15 @@ impl SyntaxEditor {
self.annotations.push((element.syntax_element(), annotation))
}
+ pub fn add_annotation_all(
+ &mut self,
+ elements: Vec<impl Element>,
+ annotation: SyntaxAnnotation,
+ ) {
+ self.annotations
+ .extend(elements.into_iter().map(|e| e.syntax_element()).zip(iter::repeat(annotation)));
+ }
+
pub fn merge(&mut self, mut other: SyntaxEditor) {
debug_assert!(
self.root == other.root || other.root.ancestors().any(|node| node == self.root),
@@ -74,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));
@@ -617,10 +636,10 @@ mod tests {
if let Some(ret_ty) = parent_fn.ret_type() {
editor.delete(ret_ty.syntax().clone());
- if let Some(SyntaxElement::Token(token)) = ret_ty.syntax().next_sibling_or_token() {
- if token.kind().is_trivia() {
- editor.delete(token);
- }
+ if let Some(SyntaxElement::Token(token)) = ret_ty.syntax().next_sibling_or_token()
+ && token.kind().is_trivia()
+ {
+ editor.delete(token);
}
}