Add text manipulation facilities to Rope for test purposes

This commit is contained in:
Antonio Scandurra 2021-11-12 17:02:51 +01:00
parent fe786f3366
commit 4e32fabfdc
3 changed files with 19 additions and 6 deletions

View file

@ -3,7 +3,7 @@ use crate::PointUtf16;
use super::Point;
use arrayvec::ArrayString;
use smallvec::SmallVec;
use std::{cmp, ops::Range, str};
use std::{cmp, fmt, ops::Range, str};
use sum_tree::{Bias, Dimension, SumTree};
#[cfg(test)]
@ -38,6 +38,16 @@ impl Rope {
self.check_invariants();
}
pub fn replace(&mut self, range: Range<usize>, text: &str) {
let mut new_rope = Rope::new();
let mut cursor = self.cursor(0);
new_rope.append(cursor.slice(range.start));
cursor.seek_forward(range.end);
new_rope.push(text);
new_rope.append(cursor.suffix());
*self = new_rope;
}
pub fn push(&mut self, text: &str) {
let mut new_chunks = SmallVec::<[_; 16]>::new();
let mut new_chunk = ArrayString::new();
@ -236,9 +246,12 @@ impl<'a> From<&'a str> for Rope {
}
}
impl Into<String> for Rope {
fn into(self) -> String {
self.chunks().collect()
impl fmt::Display for Rope {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for chunk in self.chunks() {
write!(f, "{}", chunk)?;
}
Ok(())
}
}

View file

@ -1352,7 +1352,7 @@ mod tests {
}
let buffer = map.buffer.read(cx).snapshot();
let mut expected_text: String = buffer.text().into();
let mut expected_text: String = buffer.text().to_string();
let mut expected_buffer_rows = Vec::new();
let mut next_row = buffer.max_point().row;
for fold_range in map.merged_fold_ranges(cx.as_ref()).into_iter().rev() {

View file

@ -439,7 +439,7 @@ impl Buffer {
uri,
Default::default(),
snapshot.version as i32,
snapshot.buffer_snapshot.text().into(),
snapshot.buffer_snapshot.text().to_string(),
),
},
)