Introduce rope::Cursor::summary to avoid slicing only to get a summary
This also deletes `Rope::slice`, as it's just a convenience method that can be easily re-implemented by using the cursor.
This commit is contained in:
@@ -600,8 +600,7 @@ impl Buffer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn text_summary_for_range(&self, range: Range<usize>) -> TextSummary {
|
pub fn text_summary_for_range(&self, range: Range<usize>) -> TextSummary {
|
||||||
// TODO: Use a dedicated ::summarize method in Rope.
|
self.visible_text.cursor(range.start).summary(range.end)
|
||||||
self.visible_text.slice(range).summary()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn len(&self) -> usize {
|
pub fn len(&self) -> usize {
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ use crate::util::byte_range_for_char_range;
|
|||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
use arrayvec::ArrayString;
|
use arrayvec::ArrayString;
|
||||||
use smallvec::SmallVec;
|
use smallvec::SmallVec;
|
||||||
use std::{cmp, iter::Skip, ops::Range, str};
|
use std::{cmp, iter::Skip, str};
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
const CHUNK_BASE: usize = 6;
|
const CHUNK_BASE: usize = 6;
|
||||||
@@ -89,10 +89,6 @@ impl Rope {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn slice(&self, range: Range<usize>) -> Rope {
|
|
||||||
self.cursor(range.start).slice(range.end)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn summary(&self) -> TextSummary {
|
pub fn summary(&self) -> TextSummary {
|
||||||
self.chunks.summary()
|
self.chunks.summary()
|
||||||
}
|
}
|
||||||
@@ -207,6 +203,30 @@ impl<'a> Cursor<'a> {
|
|||||||
slice
|
slice
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn summary(&mut self, end_offset: usize) -> TextSummary {
|
||||||
|
debug_assert!(end_offset >= self.offset);
|
||||||
|
|
||||||
|
let mut summary = TextSummary::default();
|
||||||
|
if let Some(start_chunk) = self.chunks.item() {
|
||||||
|
let start_ix = self.offset - self.chunks.start();
|
||||||
|
let end_ix = cmp::min(end_offset, self.chunks.end()) - self.chunks.start();
|
||||||
|
let byte_range = byte_range_for_char_range(start_chunk.0, start_ix..end_ix);
|
||||||
|
summary = TextSummary::from(&start_chunk.0[byte_range]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if end_offset > self.chunks.end() {
|
||||||
|
self.chunks.next();
|
||||||
|
summary += &self.chunks.summary(&end_offset, SeekBias::Right, &());
|
||||||
|
if let Some(end_chunk) = self.chunks.item() {
|
||||||
|
let end_ix = end_offset - self.chunks.start();
|
||||||
|
let byte_range = byte_range_for_char_range(end_chunk.0, 0..end_ix);
|
||||||
|
summary += TextSummary::from(&end_chunk.0[byte_range]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
summary
|
||||||
|
}
|
||||||
|
|
||||||
pub fn suffix(mut self) -> Rope {
|
pub fn suffix(mut self) -> Rope {
|
||||||
self.slice(self.rope.chunks.extent())
|
self.slice(self.rope.chunks.extent())
|
||||||
}
|
}
|
||||||
@@ -263,12 +283,27 @@ impl sum_tree::Item for Chunk {
|
|||||||
type Summary = TextSummary;
|
type Summary = TextSummary;
|
||||||
|
|
||||||
fn summary(&self) -> Self::Summary {
|
fn summary(&self) -> Self::Summary {
|
||||||
|
TextSummary::from(self.0.as_str())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Default, Eq, PartialEq)]
|
||||||
|
pub struct TextSummary {
|
||||||
|
pub chars: usize,
|
||||||
|
pub bytes: usize,
|
||||||
|
pub lines: Point,
|
||||||
|
pub first_line_len: u32,
|
||||||
|
pub rightmost_point: Point,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> From<&'a str> for TextSummary {
|
||||||
|
fn from(text: &'a str) -> Self {
|
||||||
let mut chars = 0;
|
let mut chars = 0;
|
||||||
let mut bytes = 0;
|
let mut bytes = 0;
|
||||||
let mut lines = Point::new(0, 0);
|
let mut lines = Point::new(0, 0);
|
||||||
let mut first_line_len = 0;
|
let mut first_line_len = 0;
|
||||||
let mut rightmost_point = Point::new(0, 0);
|
let mut rightmost_point = Point::new(0, 0);
|
||||||
for c in self.0.chars() {
|
for c in text.chars() {
|
||||||
chars += 1;
|
chars += 1;
|
||||||
bytes += c.len_utf8();
|
bytes += c.len_utf8();
|
||||||
if c == '\n' {
|
if c == '\n' {
|
||||||
@@ -295,15 +330,6 @@ impl sum_tree::Item for Chunk {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default, Eq, PartialEq)]
|
|
||||||
pub struct TextSummary {
|
|
||||||
pub chars: usize,
|
|
||||||
pub bytes: usize,
|
|
||||||
pub lines: Point,
|
|
||||||
pub first_line_len: u32,
|
|
||||||
pub rightmost_point: Point,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl sum_tree::Summary for TextSummary {
|
impl sum_tree::Summary for TextSummary {
|
||||||
type Context = ();
|
type Context = ();
|
||||||
|
|
||||||
@@ -364,7 +390,7 @@ pub struct Chars<'a> {
|
|||||||
impl<'a> Chars<'a> {
|
impl<'a> Chars<'a> {
|
||||||
pub fn new(rope: &'a Rope, start: usize) -> Self {
|
pub fn new(rope: &'a Rope, start: usize) -> Self {
|
||||||
let mut cursor = rope.chunks.cursor::<usize, usize>();
|
let mut cursor = rope.chunks.cursor::<usize, usize>();
|
||||||
cursor.slice(&start, SeekBias::Left, &());
|
cursor.seek(&start, SeekBias::Left, &());
|
||||||
let chars = if let Some(chunk) = cursor.item() {
|
let chars = if let Some(chunk) = cursor.item() {
|
||||||
let ix = start - cursor.start();
|
let ix = start - cursor.start();
|
||||||
cursor.next();
|
cursor.next();
|
||||||
@@ -472,7 +498,7 @@ mod tests {
|
|||||||
log::info!("text: {:?}", expected);
|
log::info!("text: {:?}", expected);
|
||||||
|
|
||||||
for _ in 0..5 {
|
for _ in 0..5 {
|
||||||
let ix = rng.gen_range(0..=expected.len());
|
let ix = rng.gen_range(0..=expected.chars().count());
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
actual.chars_at(ix).collect::<String>(),
|
actual.chars_at(ix).collect::<String>(),
|
||||||
expected.chars().skip(ix).collect::<String>()
|
expected.chars().skip(ix).collect::<String>()
|
||||||
@@ -492,6 +518,16 @@ mod tests {
|
|||||||
}
|
}
|
||||||
offset += 1;
|
offset += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _ in 0..5 {
|
||||||
|
let end_ix = rng.gen_range(0..=expected.chars().count());
|
||||||
|
let start_ix = rng.gen_range(0..=end_ix);
|
||||||
|
let byte_range = byte_range_for_char_range(&expected, start_ix..end_ix);
|
||||||
|
assert_eq!(
|
||||||
|
actual.cursor(start_ix).summary(end_ix),
|
||||||
|
TextSummary::from(&expected[byte_range.start..byte_range.end])
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user