From 4e32fabfdce75c634eab03158fc3d969b7a61f05 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Fri, 12 Nov 2021 17:02:51 +0100 Subject: [PATCH] Add text manipulation facilities to `Rope` for test purposes --- crates/buffer/src/rope.rs | 21 +++++++++++++++++---- crates/editor/src/display_map/fold_map.rs | 2 +- crates/language/src/lib.rs | 2 +- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/crates/buffer/src/rope.rs b/crates/buffer/src/rope.rs index 5498c403bf..d5bba2ef2e 100644 --- a/crates/buffer/src/rope.rs +++ b/crates/buffer/src/rope.rs @@ -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, 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 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(()) } } diff --git a/crates/editor/src/display_map/fold_map.rs b/crates/editor/src/display_map/fold_map.rs index 8a5b4c5584..a76fd2e508 100644 --- a/crates/editor/src/display_map/fold_map.rs +++ b/crates/editor/src/display_map/fold_map.rs @@ -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() { diff --git a/crates/language/src/lib.rs b/crates/language/src/lib.rs index f7e5016fc9..00c63e1df6 100644 --- a/crates/language/src/lib.rs +++ b/crates/language/src/lib.rs @@ -439,7 +439,7 @@ impl Buffer { uri, Default::default(), snapshot.version as i32, - snapshot.buffer_snapshot.text().into(), + snapshot.buffer_snapshot.text().to_string(), ), }, )