rope.rs

   1mod offset_utf16;
   2mod point;
   3mod point_utf16;
   4mod unclipped;
   5
   6use arrayvec::ArrayString;
   7use bromberg_sl2::HashMatrix;
   8use smallvec::SmallVec;
   9use std::{
  10    cmp, fmt, io, mem,
  11    ops::{AddAssign, Range},
  12    str,
  13};
  14use sum_tree::{Bias, Dimension, SumTree};
  15use unicode_segmentation::GraphemeCursor;
  16use util::debug_panic;
  17
  18pub use offset_utf16::OffsetUtf16;
  19pub use point::Point;
  20pub use point_utf16::PointUtf16;
  21pub use unclipped::Unclipped;
  22
  23#[cfg(test)]
  24const CHUNK_BASE: usize = 6;
  25
  26#[cfg(not(test))]
  27const CHUNK_BASE: usize = 64;
  28
  29/// Type alias to [`HashMatrix`], an implementation of a homomorphic hash function. Two [`Rope`] instances
  30/// containing the same text will produce the same fingerprint. This hash function is special in that
  31/// it allows us to hash individual chunks and aggregate them up the [`Rope`]'s tree, with the resulting
  32/// hash being equivalent to hashing all the text contained in the [`Rope`] at once.
  33pub type RopeFingerprint = HashMatrix;
  34
  35#[derive(Clone, Default)]
  36pub struct Rope {
  37    chunks: SumTree<Chunk>,
  38}
  39
  40impl Rope {
  41    pub fn new() -> Self {
  42        Self::default()
  43    }
  44
  45    pub fn text_fingerprint(text: &str) -> RopeFingerprint {
  46        bromberg_sl2::hash_strict(text.as_bytes())
  47    }
  48
  49    pub fn append(&mut self, rope: Rope) {
  50        let mut chunks = rope.chunks.cursor::<()>();
  51        chunks.next(&());
  52        if let Some(chunk) = chunks.item() {
  53            if self.chunks.last().map_or(false, |c| c.0.len() < CHUNK_BASE)
  54                || chunk.0.len() < CHUNK_BASE
  55            {
  56                self.push(&chunk.0);
  57                chunks.next(&());
  58            }
  59        }
  60
  61        self.chunks.append(chunks.suffix(&()), &());
  62        self.check_invariants();
  63    }
  64
  65    pub fn replace(&mut self, range: Range<usize>, text: &str) {
  66        let mut new_rope = Rope::new();
  67        let mut cursor = self.cursor(0);
  68        new_rope.append(cursor.slice(range.start));
  69        cursor.seek_forward(range.end);
  70        new_rope.push(text);
  71        new_rope.append(cursor.suffix());
  72        *self = new_rope;
  73    }
  74
  75    pub fn slice(&self, range: Range<usize>) -> Rope {
  76        let mut cursor = self.cursor(0);
  77        cursor.seek_forward(range.start);
  78        cursor.slice(range.end)
  79    }
  80
  81    pub fn slice_rows(&self, range: Range<u32>) -> Rope {
  82        // This would be more efficient with a forward advance after the first, but it's fine.
  83        let start = self.point_to_offset(Point::new(range.start, 0));
  84        let end = self.point_to_offset(Point::new(range.end, 0));
  85        self.slice(start..end)
  86    }
  87
  88    pub fn push(&mut self, mut text: &str) {
  89        self.chunks.update_last(
  90            |last_chunk| {
  91                let split_ix = if last_chunk.0.len() + text.len() <= 2 * CHUNK_BASE {
  92                    text.len()
  93                } else {
  94                    let mut split_ix =
  95                        cmp::min(CHUNK_BASE.saturating_sub(last_chunk.0.len()), text.len());
  96                    while !text.is_char_boundary(split_ix) {
  97                        split_ix += 1;
  98                    }
  99                    split_ix
 100                };
 101
 102                let (suffix, remainder) = text.split_at(split_ix);
 103                last_chunk.0.push_str(suffix);
 104                text = remainder;
 105            },
 106            &(),
 107        );
 108
 109        if text.len() > 2048 {
 110            return self.push_large(text);
 111        }
 112        let mut new_chunks = SmallVec::<[_; 16]>::new();
 113
 114        while !text.is_empty() {
 115            let mut split_ix = cmp::min(2 * CHUNK_BASE, text.len());
 116            while !text.is_char_boundary(split_ix) {
 117                split_ix -= 1;
 118            }
 119            let (chunk, remainder) = text.split_at(split_ix);
 120            new_chunks.push(Chunk(ArrayString::from(chunk).unwrap()));
 121            text = remainder;
 122        }
 123
 124        #[cfg(test)]
 125        const PARALLEL_THRESHOLD: usize = 4;
 126        #[cfg(not(test))]
 127        const PARALLEL_THRESHOLD: usize = 4 * (2 * sum_tree::TREE_BASE);
 128
 129        if new_chunks.len() >= PARALLEL_THRESHOLD {
 130            self.chunks.par_extend(new_chunks.into_vec(), &());
 131        } else {
 132            self.chunks.extend(new_chunks, &());
 133        }
 134
 135        self.check_invariants();
 136    }
 137
 138    /// A copy of `push` specialized for working with large quantities of text.
 139    fn push_large(&mut self, mut text: &str) {
 140        // To avoid frequent reallocs when loading large swaths of file contents,
 141        // we estimate worst-case `new_chunks` capacity;
 142        // Chunk is a fixed-capacity buffer. If a character falls on
 143        // chunk boundary, we push it off to the following chunk (thus leaving a small bit of capacity unfilled in current chunk).
 144        // Worst-case chunk count when loading a file is then a case where every chunk ends up with that unused capacity.
 145        // Since we're working with UTF-8, each character is at most 4 bytes wide. It follows then that the worst case is where
 146        // a chunk ends with 3 bytes of a 4-byte character. These 3 bytes end up being stored in the following chunk, thus wasting
 147        // 3 bytes of storage in current chunk.
 148        // For example, a 1024-byte string can occupy between 32 (full ASCII, 1024/32) and 36 (full 4-byte UTF-8, 1024 / 29 rounded up) chunks.
 149        const MIN_CHUNK_SIZE: usize = 2 * CHUNK_BASE - 3;
 150
 151        // We also round up the capacity up by one, for a good measure; we *really* don't want to realloc here, as we assume that the # of characters
 152        // we're working with there is large.
 153        let capacity = (text.len() + MIN_CHUNK_SIZE - 1) / MIN_CHUNK_SIZE;
 154        let mut new_chunks = Vec::with_capacity(capacity);
 155
 156        while !text.is_empty() {
 157            let mut split_ix = cmp::min(2 * CHUNK_BASE, text.len());
 158            while !text.is_char_boundary(split_ix) {
 159                split_ix -= 1;
 160            }
 161            let (chunk, remainder) = text.split_at(split_ix);
 162            new_chunks.push(Chunk(ArrayString::from(chunk).unwrap()));
 163            text = remainder;
 164        }
 165
 166        #[cfg(test)]
 167        const PARALLEL_THRESHOLD: usize = 4;
 168        #[cfg(not(test))]
 169        const PARALLEL_THRESHOLD: usize = 4 * (2 * sum_tree::TREE_BASE);
 170
 171        if new_chunks.len() >= PARALLEL_THRESHOLD {
 172            self.chunks.par_extend(new_chunks, &());
 173        } else {
 174            self.chunks.extend(new_chunks, &());
 175        }
 176
 177        self.check_invariants();
 178    }
 179    pub fn push_front(&mut self, text: &str) {
 180        let suffix = mem::replace(self, Rope::from(text));
 181        self.append(suffix);
 182    }
 183
 184    fn check_invariants(&self) {
 185        #[cfg(test)]
 186        {
 187            // Ensure all chunks except maybe the last one are not underflowing.
 188            // Allow some wiggle room for multibyte characters at chunk boundaries.
 189            let mut chunks = self.chunks.cursor::<()>().peekable();
 190            while let Some(chunk) = chunks.next() {
 191                if chunks.peek().is_some() {
 192                    assert!(chunk.0.len() + 3 >= CHUNK_BASE);
 193                }
 194            }
 195        }
 196    }
 197
 198    pub fn summary(&self) -> TextSummary {
 199        self.chunks.summary().text.clone()
 200    }
 201
 202    pub fn len(&self) -> usize {
 203        self.chunks.extent(&())
 204    }
 205
 206    pub fn is_empty(&self) -> bool {
 207        self.len() == 0
 208    }
 209
 210    pub fn max_point(&self) -> Point {
 211        self.chunks.extent(&())
 212    }
 213
 214    pub fn max_point_utf16(&self) -> PointUtf16 {
 215        self.chunks.extent(&())
 216    }
 217
 218    pub fn cursor(&self, offset: usize) -> Cursor {
 219        Cursor::new(self, offset)
 220    }
 221
 222    pub fn chars(&self) -> impl Iterator<Item = char> + '_ {
 223        self.chars_at(0)
 224    }
 225
 226    pub fn chars_at(&self, start: usize) -> impl Iterator<Item = char> + '_ {
 227        self.chunks_in_range(start..self.len()).flat_map(str::chars)
 228    }
 229
 230    pub fn reversed_chars_at(&self, start: usize) -> impl Iterator<Item = char> + '_ {
 231        self.reversed_chunks_in_range(0..start)
 232            .flat_map(|chunk| chunk.chars().rev())
 233    }
 234
 235    pub fn bytes_in_range(&self, range: Range<usize>) -> Bytes {
 236        Bytes::new(self, range, false)
 237    }
 238
 239    pub fn reversed_bytes_in_range(&self, range: Range<usize>) -> Bytes {
 240        Bytes::new(self, range, true)
 241    }
 242
 243    pub fn chunks(&self) -> Chunks {
 244        self.chunks_in_range(0..self.len())
 245    }
 246
 247    pub fn chunks_in_range(&self, range: Range<usize>) -> Chunks {
 248        Chunks::new(self, range, false)
 249    }
 250
 251    pub fn reversed_chunks_in_range(&self, range: Range<usize>) -> Chunks {
 252        Chunks::new(self, range, true)
 253    }
 254
 255    pub fn offset_to_offset_utf16(&self, offset: usize) -> OffsetUtf16 {
 256        if offset >= self.summary().len {
 257            return self.summary().len_utf16;
 258        }
 259        let mut cursor = self.chunks.cursor::<(usize, OffsetUtf16)>();
 260        cursor.seek(&offset, Bias::Left, &());
 261        let overshoot = offset - cursor.start().0;
 262        cursor.start().1
 263            + cursor.item().map_or(Default::default(), |chunk| {
 264                chunk.offset_to_offset_utf16(overshoot)
 265            })
 266    }
 267
 268    pub fn offset_utf16_to_offset(&self, offset: OffsetUtf16) -> usize {
 269        if offset >= self.summary().len_utf16 {
 270            return self.summary().len;
 271        }
 272        let mut cursor = self.chunks.cursor::<(OffsetUtf16, usize)>();
 273        cursor.seek(&offset, Bias::Left, &());
 274        let overshoot = offset - cursor.start().0;
 275        cursor.start().1
 276            + cursor.item().map_or(Default::default(), |chunk| {
 277                chunk.offset_utf16_to_offset(overshoot)
 278            })
 279    }
 280
 281    pub fn offset_to_point(&self, offset: usize) -> Point {
 282        if offset >= self.summary().len {
 283            return self.summary().lines;
 284        }
 285        let mut cursor = self.chunks.cursor::<(usize, Point)>();
 286        cursor.seek(&offset, Bias::Left, &());
 287        let overshoot = offset - cursor.start().0;
 288        cursor.start().1
 289            + cursor
 290                .item()
 291                .map_or(Point::zero(), |chunk| chunk.offset_to_point(overshoot))
 292    }
 293
 294    pub fn offset_to_point_utf16(&self, offset: usize) -> PointUtf16 {
 295        if offset >= self.summary().len {
 296            return self.summary().lines_utf16();
 297        }
 298        let mut cursor = self.chunks.cursor::<(usize, PointUtf16)>();
 299        cursor.seek(&offset, Bias::Left, &());
 300        let overshoot = offset - cursor.start().0;
 301        cursor.start().1
 302            + cursor.item().map_or(PointUtf16::zero(), |chunk| {
 303                chunk.offset_to_point_utf16(overshoot)
 304            })
 305    }
 306
 307    pub fn point_to_point_utf16(&self, point: Point) -> PointUtf16 {
 308        if point >= self.summary().lines {
 309            return self.summary().lines_utf16();
 310        }
 311        let mut cursor = self.chunks.cursor::<(Point, PointUtf16)>();
 312        cursor.seek(&point, Bias::Left, &());
 313        let overshoot = point - cursor.start().0;
 314        cursor.start().1
 315            + cursor.item().map_or(PointUtf16::zero(), |chunk| {
 316                chunk.point_to_point_utf16(overshoot)
 317            })
 318    }
 319
 320    pub fn point_to_offset(&self, point: Point) -> usize {
 321        if point >= self.summary().lines {
 322            return self.summary().len;
 323        }
 324        let mut cursor = self.chunks.cursor::<(Point, usize)>();
 325        cursor.seek(&point, Bias::Left, &());
 326        let overshoot = point - cursor.start().0;
 327        cursor.start().1
 328            + cursor
 329                .item()
 330                .map_or(0, |chunk| chunk.point_to_offset(overshoot))
 331    }
 332
 333    pub fn point_utf16_to_offset(&self, point: PointUtf16) -> usize {
 334        self.point_utf16_to_offset_impl(point, false)
 335    }
 336
 337    pub fn unclipped_point_utf16_to_offset(&self, point: Unclipped<PointUtf16>) -> usize {
 338        self.point_utf16_to_offset_impl(point.0, true)
 339    }
 340
 341    fn point_utf16_to_offset_impl(&self, point: PointUtf16, clip: bool) -> usize {
 342        if point >= self.summary().lines_utf16() {
 343            return self.summary().len;
 344        }
 345        let mut cursor = self.chunks.cursor::<(PointUtf16, usize)>();
 346        cursor.seek(&point, Bias::Left, &());
 347        let overshoot = point - cursor.start().0;
 348        cursor.start().1
 349            + cursor
 350                .item()
 351                .map_or(0, |chunk| chunk.point_utf16_to_offset(overshoot, clip))
 352    }
 353
 354    pub fn unclipped_point_utf16_to_point(&self, point: Unclipped<PointUtf16>) -> Point {
 355        if point.0 >= self.summary().lines_utf16() {
 356            return self.summary().lines;
 357        }
 358        let mut cursor = self.chunks.cursor::<(PointUtf16, Point)>();
 359        cursor.seek(&point.0, Bias::Left, &());
 360        let overshoot = Unclipped(point.0 - cursor.start().0);
 361        cursor.start().1
 362            + cursor.item().map_or(Point::zero(), |chunk| {
 363                chunk.unclipped_point_utf16_to_point(overshoot)
 364            })
 365    }
 366
 367    pub fn clip_offset(&self, mut offset: usize, bias: Bias) -> usize {
 368        let mut cursor = self.chunks.cursor::<usize>();
 369        cursor.seek(&offset, Bias::Left, &());
 370        if let Some(chunk) = cursor.item() {
 371            let mut ix = offset - cursor.start();
 372            while !chunk.0.is_char_boundary(ix) {
 373                match bias {
 374                    Bias::Left => {
 375                        ix -= 1;
 376                        offset -= 1;
 377                    }
 378                    Bias::Right => {
 379                        ix += 1;
 380                        offset += 1;
 381                    }
 382                }
 383            }
 384            offset
 385        } else {
 386            self.summary().len
 387        }
 388    }
 389
 390    pub fn clip_offset_utf16(&self, offset: OffsetUtf16, bias: Bias) -> OffsetUtf16 {
 391        let mut cursor = self.chunks.cursor::<OffsetUtf16>();
 392        cursor.seek(&offset, Bias::Right, &());
 393        if let Some(chunk) = cursor.item() {
 394            let overshoot = offset - cursor.start();
 395            *cursor.start() + chunk.clip_offset_utf16(overshoot, bias)
 396        } else {
 397            self.summary().len_utf16
 398        }
 399    }
 400
 401    pub fn clip_point(&self, point: Point, bias: Bias) -> Point {
 402        let mut cursor = self.chunks.cursor::<Point>();
 403        cursor.seek(&point, Bias::Right, &());
 404        if let Some(chunk) = cursor.item() {
 405            let overshoot = point - cursor.start();
 406            *cursor.start() + chunk.clip_point(overshoot, bias)
 407        } else {
 408            self.summary().lines
 409        }
 410    }
 411
 412    pub fn clip_point_utf16(&self, point: Unclipped<PointUtf16>, bias: Bias) -> PointUtf16 {
 413        let mut cursor = self.chunks.cursor::<PointUtf16>();
 414        cursor.seek(&point.0, Bias::Right, &());
 415        if let Some(chunk) = cursor.item() {
 416            let overshoot = Unclipped(point.0 - cursor.start());
 417            *cursor.start() + chunk.clip_point_utf16(overshoot, bias)
 418        } else {
 419            self.summary().lines_utf16()
 420        }
 421    }
 422
 423    pub fn line_len(&self, row: u32) -> u32 {
 424        self.clip_point(Point::new(row, u32::MAX), Bias::Left)
 425            .column
 426    }
 427
 428    pub fn fingerprint(&self) -> RopeFingerprint {
 429        self.chunks.summary().fingerprint
 430    }
 431}
 432
 433impl<'a> From<&'a str> for Rope {
 434    fn from(text: &'a str) -> Self {
 435        let mut rope = Self::new();
 436        rope.push(text);
 437        rope
 438    }
 439}
 440
 441impl<'a> FromIterator<&'a str> for Rope {
 442    fn from_iter<T: IntoIterator<Item = &'a str>>(iter: T) -> Self {
 443        let mut rope = Rope::new();
 444        for chunk in iter {
 445            rope.push(chunk);
 446        }
 447        rope
 448    }
 449}
 450
 451impl From<String> for Rope {
 452    fn from(text: String) -> Self {
 453        Rope::from(text.as_str())
 454    }
 455}
 456
 457impl fmt::Display for Rope {
 458    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 459        for chunk in self.chunks() {
 460            write!(f, "{}", chunk)?;
 461        }
 462        Ok(())
 463    }
 464}
 465
 466impl fmt::Debug for Rope {
 467    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 468        use std::fmt::Write as _;
 469
 470        write!(f, "\"")?;
 471        let mut format_string = String::new();
 472        for chunk in self.chunks() {
 473            write!(&mut format_string, "{:?}", chunk)?;
 474            write!(f, "{}", &format_string[1..format_string.len() - 1])?;
 475            format_string.clear();
 476        }
 477        write!(f, "\"")?;
 478        Ok(())
 479    }
 480}
 481
 482pub struct Cursor<'a> {
 483    rope: &'a Rope,
 484    chunks: sum_tree::Cursor<'a, Chunk, usize>,
 485    offset: usize,
 486}
 487
 488impl<'a> Cursor<'a> {
 489    pub fn new(rope: &'a Rope, offset: usize) -> Self {
 490        let mut chunks = rope.chunks.cursor();
 491        chunks.seek(&offset, Bias::Right, &());
 492        Self {
 493            rope,
 494            chunks,
 495            offset,
 496        }
 497    }
 498
 499    pub fn seek_forward(&mut self, end_offset: usize) {
 500        debug_assert!(end_offset >= self.offset);
 501
 502        self.chunks.seek_forward(&end_offset, Bias::Right, &());
 503        self.offset = end_offset;
 504    }
 505
 506    pub fn slice(&mut self, end_offset: usize) -> Rope {
 507        debug_assert!(
 508            end_offset >= self.offset,
 509            "cannot slice backwards from {} to {}",
 510            self.offset,
 511            end_offset
 512        );
 513
 514        let mut slice = Rope::new();
 515        if let Some(start_chunk) = self.chunks.item() {
 516            let start_ix = self.offset - self.chunks.start();
 517            let end_ix = cmp::min(end_offset, self.chunks.end(&())) - self.chunks.start();
 518            slice.push(&start_chunk.0[start_ix..end_ix]);
 519        }
 520
 521        if end_offset > self.chunks.end(&()) {
 522            self.chunks.next(&());
 523            slice.append(Rope {
 524                chunks: self.chunks.slice(&end_offset, Bias::Right, &()),
 525            });
 526            if let Some(end_chunk) = self.chunks.item() {
 527                let end_ix = end_offset - self.chunks.start();
 528                slice.push(&end_chunk.0[..end_ix]);
 529            }
 530        }
 531
 532        self.offset = end_offset;
 533        slice
 534    }
 535
 536    pub fn summary<D: TextDimension>(&mut self, end_offset: usize) -> D {
 537        debug_assert!(end_offset >= self.offset);
 538
 539        let mut summary = D::default();
 540        if let Some(start_chunk) = self.chunks.item() {
 541            let start_ix = self.offset - self.chunks.start();
 542            let end_ix = cmp::min(end_offset, self.chunks.end(&())) - self.chunks.start();
 543            summary.add_assign(&D::from_text_summary(&TextSummary::from(
 544                &start_chunk.0[start_ix..end_ix],
 545            )));
 546        }
 547
 548        if end_offset > self.chunks.end(&()) {
 549            self.chunks.next(&());
 550            summary.add_assign(&self.chunks.summary(&end_offset, Bias::Right, &()));
 551            if let Some(end_chunk) = self.chunks.item() {
 552                let end_ix = end_offset - self.chunks.start();
 553                summary.add_assign(&D::from_text_summary(&TextSummary::from(
 554                    &end_chunk.0[..end_ix],
 555                )));
 556            }
 557        }
 558
 559        self.offset = end_offset;
 560        summary
 561    }
 562
 563    pub fn suffix(mut self) -> Rope {
 564        self.slice(self.rope.chunks.extent(&()))
 565    }
 566
 567    pub fn offset(&self) -> usize {
 568        self.offset
 569    }
 570}
 571
 572pub struct Chunks<'a> {
 573    chunks: sum_tree::Cursor<'a, Chunk, usize>,
 574    range: Range<usize>,
 575    reversed: bool,
 576}
 577
 578impl<'a> Chunks<'a> {
 579    pub fn new(rope: &'a Rope, range: Range<usize>, reversed: bool) -> Self {
 580        let mut chunks = rope.chunks.cursor();
 581        if reversed {
 582            chunks.seek(&range.end, Bias::Left, &());
 583        } else {
 584            chunks.seek(&range.start, Bias::Right, &());
 585        }
 586        Self {
 587            chunks,
 588            range,
 589            reversed,
 590        }
 591    }
 592
 593    pub fn offset(&self) -> usize {
 594        if self.reversed {
 595            self.range.end.min(self.chunks.end(&()))
 596        } else {
 597            self.range.start.max(*self.chunks.start())
 598        }
 599    }
 600
 601    pub fn seek(&mut self, offset: usize) {
 602        let bias = if self.reversed {
 603            Bias::Left
 604        } else {
 605            Bias::Right
 606        };
 607
 608        if offset >= self.chunks.end(&()) {
 609            self.chunks.seek_forward(&offset, bias, &());
 610        } else {
 611            self.chunks.seek(&offset, bias, &());
 612        }
 613
 614        if self.reversed {
 615            self.range.end = offset;
 616        } else {
 617            self.range.start = offset;
 618        }
 619    }
 620
 621    pub fn peek(&self) -> Option<&'a str> {
 622        let chunk = self.chunks.item()?;
 623        if self.reversed && self.range.start >= self.chunks.end(&()) {
 624            return None;
 625        }
 626        let chunk_start = *self.chunks.start();
 627        if self.range.end <= chunk_start {
 628            return None;
 629        }
 630
 631        let start = self.range.start.saturating_sub(chunk_start);
 632        let end = self.range.end - chunk_start;
 633        Some(&chunk.0[start..chunk.0.len().min(end)])
 634    }
 635}
 636
 637impl<'a> Iterator for Chunks<'a> {
 638    type Item = &'a str;
 639
 640    fn next(&mut self) -> Option<Self::Item> {
 641        let result = self.peek();
 642        if result.is_some() {
 643            if self.reversed {
 644                self.chunks.prev(&());
 645            } else {
 646                self.chunks.next(&());
 647            }
 648        }
 649        result
 650    }
 651}
 652
 653pub struct Bytes<'a> {
 654    chunks: sum_tree::Cursor<'a, Chunk, usize>,
 655    range: Range<usize>,
 656    reversed: bool,
 657}
 658
 659impl<'a> Bytes<'a> {
 660    pub fn new(rope: &'a Rope, range: Range<usize>, reversed: bool) -> Self {
 661        let mut chunks = rope.chunks.cursor();
 662        if reversed {
 663            chunks.seek(&range.end, Bias::Left, &());
 664        } else {
 665            chunks.seek(&range.start, Bias::Right, &());
 666        }
 667        Self {
 668            chunks,
 669            range,
 670            reversed,
 671        }
 672    }
 673
 674    pub fn peek(&self) -> Option<&'a [u8]> {
 675        let chunk = self.chunks.item()?;
 676        if self.reversed && self.range.start >= self.chunks.end(&()) {
 677            return None;
 678        }
 679        let chunk_start = *self.chunks.start();
 680        if self.range.end <= chunk_start {
 681            return None;
 682        }
 683        let start = self.range.start.saturating_sub(chunk_start);
 684        let end = self.range.end - chunk_start;
 685        Some(&chunk.0.as_bytes()[start..chunk.0.len().min(end)])
 686    }
 687}
 688
 689impl<'a> Iterator for Bytes<'a> {
 690    type Item = &'a [u8];
 691
 692    fn next(&mut self) -> Option<Self::Item> {
 693        let result = self.peek();
 694        if result.is_some() {
 695            if self.reversed {
 696                self.chunks.prev(&());
 697            } else {
 698                self.chunks.next(&());
 699            }
 700        }
 701        result
 702    }
 703}
 704
 705impl<'a> io::Read for Bytes<'a> {
 706    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
 707        if let Some(chunk) = self.peek() {
 708            let len = cmp::min(buf.len(), chunk.len());
 709            if self.reversed {
 710                buf[..len].copy_from_slice(&chunk[chunk.len() - len..]);
 711                buf[..len].reverse();
 712                self.range.end -= len;
 713            } else {
 714                buf[..len].copy_from_slice(&chunk[..len]);
 715                self.range.start += len;
 716            }
 717
 718            if len == chunk.len() {
 719                if self.reversed {
 720                    self.chunks.prev(&());
 721                } else {
 722                    self.chunks.next(&());
 723                }
 724            }
 725            Ok(len)
 726        } else {
 727            Ok(0)
 728        }
 729    }
 730}
 731
 732#[derive(Clone, Debug, Default)]
 733struct Chunk(ArrayString<{ 2 * CHUNK_BASE }>);
 734
 735impl Chunk {
 736    fn offset_to_offset_utf16(&self, target: usize) -> OffsetUtf16 {
 737        let mut offset = 0;
 738        let mut offset_utf16 = OffsetUtf16(0);
 739        for ch in self.0.chars() {
 740            if offset >= target {
 741                break;
 742            }
 743
 744            offset += ch.len_utf8();
 745            offset_utf16.0 += ch.len_utf16();
 746        }
 747        offset_utf16
 748    }
 749
 750    fn offset_utf16_to_offset(&self, target: OffsetUtf16) -> usize {
 751        let mut offset_utf16 = OffsetUtf16(0);
 752        let mut offset = 0;
 753        for ch in self.0.chars() {
 754            if offset_utf16 >= target {
 755                break;
 756            }
 757
 758            offset += ch.len_utf8();
 759            offset_utf16.0 += ch.len_utf16();
 760        }
 761        offset
 762    }
 763
 764    fn offset_to_point(&self, target: usize) -> Point {
 765        let mut offset = 0;
 766        let mut point = Point::new(0, 0);
 767        for ch in self.0.chars() {
 768            if offset >= target {
 769                break;
 770            }
 771
 772            if ch == '\n' {
 773                point.row += 1;
 774                point.column = 0;
 775            } else {
 776                point.column += ch.len_utf8() as u32;
 777            }
 778            offset += ch.len_utf8();
 779        }
 780        point
 781    }
 782
 783    fn offset_to_point_utf16(&self, target: usize) -> PointUtf16 {
 784        let mut offset = 0;
 785        let mut point = PointUtf16::new(0, 0);
 786        for ch in self.0.chars() {
 787            if offset >= target {
 788                break;
 789            }
 790
 791            if ch == '\n' {
 792                point.row += 1;
 793                point.column = 0;
 794            } else {
 795                point.column += ch.len_utf16() as u32;
 796            }
 797            offset += ch.len_utf8();
 798        }
 799        point
 800    }
 801
 802    fn point_to_offset(&self, target: Point) -> usize {
 803        let mut offset = 0;
 804        let mut point = Point::new(0, 0);
 805
 806        for ch in self.0.chars() {
 807            if point >= target {
 808                if point > target {
 809                    debug_panic!("point {target:?} is inside of character {ch:?}");
 810                }
 811                break;
 812            }
 813
 814            if ch == '\n' {
 815                point.row += 1;
 816                point.column = 0;
 817
 818                if point.row > target.row {
 819                    debug_panic!(
 820                        "point {target:?} is beyond the end of a line with length {}",
 821                        point.column
 822                    );
 823                    break;
 824                }
 825            } else {
 826                point.column += ch.len_utf8() as u32;
 827            }
 828
 829            offset += ch.len_utf8();
 830        }
 831
 832        offset
 833    }
 834
 835    fn point_to_point_utf16(&self, target: Point) -> PointUtf16 {
 836        let mut point = Point::zero();
 837        let mut point_utf16 = PointUtf16::new(0, 0);
 838        for ch in self.0.chars() {
 839            if point >= target {
 840                break;
 841            }
 842
 843            if ch == '\n' {
 844                point_utf16.row += 1;
 845                point_utf16.column = 0;
 846                point.row += 1;
 847                point.column = 0;
 848            } else {
 849                point_utf16.column += ch.len_utf16() as u32;
 850                point.column += ch.len_utf8() as u32;
 851            }
 852        }
 853        point_utf16
 854    }
 855
 856    fn point_utf16_to_offset(&self, target: PointUtf16, clip: bool) -> usize {
 857        let mut offset = 0;
 858        let mut point = PointUtf16::new(0, 0);
 859
 860        for ch in self.0.chars() {
 861            if point == target {
 862                break;
 863            }
 864
 865            if ch == '\n' {
 866                point.row += 1;
 867                point.column = 0;
 868
 869                if point.row > target.row {
 870                    if !clip {
 871                        debug_panic!(
 872                            "point {target:?} is beyond the end of a line with length {}",
 873                            point.column
 874                        );
 875                    }
 876                    // Return the offset of the newline
 877                    return offset;
 878                }
 879            } else {
 880                point.column += ch.len_utf16() as u32;
 881            }
 882
 883            if point > target {
 884                if !clip {
 885                    debug_panic!("point {target:?} is inside of codepoint {ch:?}");
 886                }
 887                // Return the offset of the codepoint which we have landed within, bias left
 888                return offset;
 889            }
 890
 891            offset += ch.len_utf8();
 892        }
 893
 894        offset
 895    }
 896
 897    fn unclipped_point_utf16_to_point(&self, target: Unclipped<PointUtf16>) -> Point {
 898        let mut point = Point::zero();
 899        let mut point_utf16 = PointUtf16::zero();
 900
 901        for ch in self.0.chars() {
 902            if point_utf16 == target.0 {
 903                break;
 904            }
 905
 906            if point_utf16 > target.0 {
 907                // If the point is past the end of a line or inside of a code point,
 908                // return the last valid point before the target.
 909                return point;
 910            }
 911
 912            if ch == '\n' {
 913                point_utf16 += PointUtf16::new(1, 0);
 914                point += Point::new(1, 0);
 915            } else {
 916                point_utf16 += PointUtf16::new(0, ch.len_utf16() as u32);
 917                point += Point::new(0, ch.len_utf8() as u32);
 918            }
 919        }
 920
 921        point
 922    }
 923
 924    fn clip_point(&self, target: Point, bias: Bias) -> Point {
 925        for (row, line) in self.0.split('\n').enumerate() {
 926            if row == target.row as usize {
 927                let bytes = line.as_bytes();
 928                let mut column = target.column.min(bytes.len() as u32) as usize;
 929                if column == 0
 930                    || column == bytes.len()
 931                    || (bytes[column - 1] < 128 && bytes[column] < 128)
 932                {
 933                    return Point::new(row as u32, column as u32);
 934                }
 935
 936                let mut grapheme_cursor = GraphemeCursor::new(column, bytes.len(), true);
 937                loop {
 938                    if line.is_char_boundary(column) {
 939                        if grapheme_cursor.is_boundary(line, 0).unwrap_or(false) {
 940                            break;
 941                        }
 942                    }
 943
 944                    match bias {
 945                        Bias::Left => column -= 1,
 946                        Bias::Right => column += 1,
 947                    }
 948                    grapheme_cursor.set_cursor(column);
 949                }
 950                return Point::new(row as u32, column as u32);
 951            }
 952        }
 953        unreachable!()
 954    }
 955
 956    fn clip_point_utf16(&self, target: Unclipped<PointUtf16>, bias: Bias) -> PointUtf16 {
 957        for (row, line) in self.0.split('\n').enumerate() {
 958            if row == target.0.row as usize {
 959                let mut code_units = line.encode_utf16();
 960                let mut column = code_units.by_ref().take(target.0.column as usize).count();
 961                if char::decode_utf16(code_units).next().transpose().is_err() {
 962                    match bias {
 963                        Bias::Left => column -= 1,
 964                        Bias::Right => column += 1,
 965                    }
 966                }
 967                return PointUtf16::new(row as u32, column as u32);
 968            }
 969        }
 970        unreachable!()
 971    }
 972
 973    fn clip_offset_utf16(&self, target: OffsetUtf16, bias: Bias) -> OffsetUtf16 {
 974        let mut code_units = self.0.encode_utf16();
 975        let mut offset = code_units.by_ref().take(target.0).count();
 976        if char::decode_utf16(code_units).next().transpose().is_err() {
 977            match bias {
 978                Bias::Left => offset -= 1,
 979                Bias::Right => offset += 1,
 980            }
 981        }
 982        OffsetUtf16(offset)
 983    }
 984}
 985
 986impl sum_tree::Item for Chunk {
 987    type Summary = ChunkSummary;
 988
 989    fn summary(&self) -> Self::Summary {
 990        ChunkSummary::from(self.0.as_str())
 991    }
 992}
 993
 994#[derive(Clone, Debug, Default, Eq, PartialEq)]
 995pub struct ChunkSummary {
 996    text: TextSummary,
 997    fingerprint: RopeFingerprint,
 998}
 999
1000impl<'a> From<&'a str> for ChunkSummary {
1001    fn from(text: &'a str) -> Self {
1002        Self {
1003            text: TextSummary::from(text),
1004            fingerprint: Rope::text_fingerprint(text),
1005        }
1006    }
1007}
1008
1009impl sum_tree::Summary for ChunkSummary {
1010    type Context = ();
1011
1012    fn add_summary(&mut self, summary: &Self, _: &()) {
1013        self.text += &summary.text;
1014        self.fingerprint = self.fingerprint * summary.fingerprint;
1015    }
1016}
1017
1018/// Summary of a string of text.
1019#[derive(Clone, Debug, Default, Eq, PartialEq)]
1020pub struct TextSummary {
1021    /// Length in UTF-8
1022    pub len: usize,
1023    /// Length in UTF-16 code units
1024    pub len_utf16: OffsetUtf16,
1025    /// A point representing the number of lines and the length of the last line
1026    pub lines: Point,
1027    /// How many `char`s are in the first line
1028    pub first_line_chars: u32,
1029    /// How many `char`s are in the last line
1030    pub last_line_chars: u32,
1031    /// How many UTF-16 code units are in the last line
1032    pub last_line_len_utf16: u32,
1033    /// The row idx of the longest row
1034    pub longest_row: u32,
1035    /// How many `char`s are in the longest row
1036    pub longest_row_chars: u32,
1037}
1038
1039impl TextSummary {
1040    pub fn lines_utf16(&self) -> PointUtf16 {
1041        PointUtf16 {
1042            row: self.lines.row,
1043            column: self.last_line_len_utf16,
1044        }
1045    }
1046}
1047
1048impl<'a> From<&'a str> for TextSummary {
1049    fn from(text: &'a str) -> Self {
1050        let mut len_utf16 = OffsetUtf16(0);
1051        let mut lines = Point::new(0, 0);
1052        let mut first_line_chars = 0;
1053        let mut last_line_chars = 0;
1054        let mut last_line_len_utf16 = 0;
1055        let mut longest_row = 0;
1056        let mut longest_row_chars = 0;
1057        for c in text.chars() {
1058            len_utf16.0 += c.len_utf16();
1059
1060            if c == '\n' {
1061                lines += Point::new(1, 0);
1062                last_line_len_utf16 = 0;
1063                last_line_chars = 0;
1064            } else {
1065                lines.column += c.len_utf8() as u32;
1066                last_line_len_utf16 += c.len_utf16() as u32;
1067                last_line_chars += 1;
1068            }
1069
1070            if lines.row == 0 {
1071                first_line_chars = last_line_chars;
1072            }
1073
1074            if last_line_chars > longest_row_chars {
1075                longest_row = lines.row;
1076                longest_row_chars = last_line_chars;
1077            }
1078        }
1079
1080        TextSummary {
1081            len: text.len(),
1082            len_utf16,
1083            lines,
1084            first_line_chars,
1085            last_line_chars,
1086            last_line_len_utf16,
1087            longest_row,
1088            longest_row_chars,
1089        }
1090    }
1091}
1092
1093impl sum_tree::Summary for TextSummary {
1094    type Context = ();
1095
1096    fn add_summary(&mut self, summary: &Self, _: &Self::Context) {
1097        *self += summary;
1098    }
1099}
1100
1101impl std::ops::Add<Self> for TextSummary {
1102    type Output = Self;
1103
1104    fn add(mut self, rhs: Self) -> Self::Output {
1105        AddAssign::add_assign(&mut self, &rhs);
1106        self
1107    }
1108}
1109
1110impl<'a> std::ops::AddAssign<&'a Self> for TextSummary {
1111    fn add_assign(&mut self, other: &'a Self) {
1112        let joined_chars = self.last_line_chars + other.first_line_chars;
1113        if joined_chars > self.longest_row_chars {
1114            self.longest_row = self.lines.row;
1115            self.longest_row_chars = joined_chars;
1116        }
1117        if other.longest_row_chars > self.longest_row_chars {
1118            self.longest_row = self.lines.row + other.longest_row;
1119            self.longest_row_chars = other.longest_row_chars;
1120        }
1121
1122        if self.lines.row == 0 {
1123            self.first_line_chars += other.first_line_chars;
1124        }
1125
1126        if other.lines.row == 0 {
1127            self.last_line_chars += other.first_line_chars;
1128            self.last_line_len_utf16 += other.last_line_len_utf16;
1129        } else {
1130            self.last_line_chars = other.last_line_chars;
1131            self.last_line_len_utf16 = other.last_line_len_utf16;
1132        }
1133
1134        self.len += other.len;
1135        self.len_utf16 += other.len_utf16;
1136        self.lines += other.lines;
1137    }
1138}
1139
1140impl std::ops::AddAssign<Self> for TextSummary {
1141    fn add_assign(&mut self, other: Self) {
1142        *self += &other;
1143    }
1144}
1145
1146pub trait TextDimension: 'static + for<'a> Dimension<'a, ChunkSummary> {
1147    fn from_text_summary(summary: &TextSummary) -> Self;
1148    fn add_assign(&mut self, other: &Self);
1149}
1150
1151impl<D1: TextDimension, D2: TextDimension> TextDimension for (D1, D2) {
1152    fn from_text_summary(summary: &TextSummary) -> Self {
1153        (
1154            D1::from_text_summary(summary),
1155            D2::from_text_summary(summary),
1156        )
1157    }
1158
1159    fn add_assign(&mut self, other: &Self) {
1160        self.0.add_assign(&other.0);
1161        self.1.add_assign(&other.1);
1162    }
1163}
1164
1165impl<'a> sum_tree::Dimension<'a, ChunkSummary> for TextSummary {
1166    fn add_summary(&mut self, summary: &'a ChunkSummary, _: &()) {
1167        *self += &summary.text;
1168    }
1169}
1170
1171impl TextDimension for TextSummary {
1172    fn from_text_summary(summary: &TextSummary) -> Self {
1173        summary.clone()
1174    }
1175
1176    fn add_assign(&mut self, other: &Self) {
1177        *self += other;
1178    }
1179}
1180
1181impl<'a> sum_tree::Dimension<'a, ChunkSummary> for usize {
1182    fn add_summary(&mut self, summary: &'a ChunkSummary, _: &()) {
1183        *self += summary.text.len;
1184    }
1185}
1186
1187impl TextDimension for usize {
1188    fn from_text_summary(summary: &TextSummary) -> Self {
1189        summary.len
1190    }
1191
1192    fn add_assign(&mut self, other: &Self) {
1193        *self += other;
1194    }
1195}
1196
1197impl<'a> sum_tree::Dimension<'a, ChunkSummary> for OffsetUtf16 {
1198    fn add_summary(&mut self, summary: &'a ChunkSummary, _: &()) {
1199        *self += summary.text.len_utf16;
1200    }
1201}
1202
1203impl TextDimension for OffsetUtf16 {
1204    fn from_text_summary(summary: &TextSummary) -> Self {
1205        summary.len_utf16
1206    }
1207
1208    fn add_assign(&mut self, other: &Self) {
1209        *self += other;
1210    }
1211}
1212
1213impl<'a> sum_tree::Dimension<'a, ChunkSummary> for Point {
1214    fn add_summary(&mut self, summary: &'a ChunkSummary, _: &()) {
1215        *self += summary.text.lines;
1216    }
1217}
1218
1219impl TextDimension for Point {
1220    fn from_text_summary(summary: &TextSummary) -> Self {
1221        summary.lines
1222    }
1223
1224    fn add_assign(&mut self, other: &Self) {
1225        *self += other;
1226    }
1227}
1228
1229impl<'a> sum_tree::Dimension<'a, ChunkSummary> for PointUtf16 {
1230    fn add_summary(&mut self, summary: &'a ChunkSummary, _: &()) {
1231        *self += summary.text.lines_utf16();
1232    }
1233}
1234
1235impl TextDimension for PointUtf16 {
1236    fn from_text_summary(summary: &TextSummary) -> Self {
1237        summary.lines_utf16()
1238    }
1239
1240    fn add_assign(&mut self, other: &Self) {
1241        *self += other;
1242    }
1243}
1244
1245#[cfg(test)]
1246mod tests {
1247    use super::*;
1248    use rand::prelude::*;
1249    use std::{cmp::Ordering, env, io::Read};
1250    use util::RandomCharIter;
1251    use Bias::{Left, Right};
1252
1253    #[test]
1254    fn test_all_4_byte_chars() {
1255        let mut rope = Rope::new();
1256        let text = "🏀".repeat(256);
1257        rope.push(&text);
1258        assert_eq!(rope.text(), text);
1259    }
1260
1261    #[test]
1262    fn test_clip() {
1263        let rope = Rope::from("🧘");
1264
1265        assert_eq!(rope.clip_offset(1, Bias::Left), 0);
1266        assert_eq!(rope.clip_offset(1, Bias::Right), 4);
1267        assert_eq!(rope.clip_offset(5, Bias::Right), 4);
1268
1269        assert_eq!(
1270            rope.clip_point(Point::new(0, 1), Bias::Left),
1271            Point::new(0, 0)
1272        );
1273        assert_eq!(
1274            rope.clip_point(Point::new(0, 1), Bias::Right),
1275            Point::new(0, 4)
1276        );
1277        assert_eq!(
1278            rope.clip_point(Point::new(0, 5), Bias::Right),
1279            Point::new(0, 4)
1280        );
1281
1282        assert_eq!(
1283            rope.clip_point_utf16(Unclipped(PointUtf16::new(0, 1)), Bias::Left),
1284            PointUtf16::new(0, 0)
1285        );
1286        assert_eq!(
1287            rope.clip_point_utf16(Unclipped(PointUtf16::new(0, 1)), Bias::Right),
1288            PointUtf16::new(0, 2)
1289        );
1290        assert_eq!(
1291            rope.clip_point_utf16(Unclipped(PointUtf16::new(0, 3)), Bias::Right),
1292            PointUtf16::new(0, 2)
1293        );
1294
1295        assert_eq!(
1296            rope.clip_offset_utf16(OffsetUtf16(1), Bias::Left),
1297            OffsetUtf16(0)
1298        );
1299        assert_eq!(
1300            rope.clip_offset_utf16(OffsetUtf16(1), Bias::Right),
1301            OffsetUtf16(2)
1302        );
1303        assert_eq!(
1304            rope.clip_offset_utf16(OffsetUtf16(3), Bias::Right),
1305            OffsetUtf16(2)
1306        );
1307    }
1308
1309    #[gpui::test(iterations = 100)]
1310    fn test_random_rope(mut rng: StdRng) {
1311        let operations = env::var("OPERATIONS")
1312            .map(|i| i.parse().expect("invalid `OPERATIONS` variable"))
1313            .unwrap_or(10);
1314
1315        let mut expected = String::new();
1316        let mut actual = Rope::new();
1317        for _ in 0..operations {
1318            let end_ix = clip_offset(&expected, rng.gen_range(0..=expected.len()), Right);
1319            let start_ix = clip_offset(&expected, rng.gen_range(0..=end_ix), Left);
1320            let len = rng.gen_range(0..=64);
1321            let new_text: String = RandomCharIter::new(&mut rng).take(len).collect();
1322
1323            let mut new_actual = Rope::new();
1324            let mut cursor = actual.cursor(0);
1325            new_actual.append(cursor.slice(start_ix));
1326            new_actual.push(&new_text);
1327            cursor.seek_forward(end_ix);
1328            new_actual.append(cursor.suffix());
1329            actual = new_actual;
1330
1331            expected.replace_range(start_ix..end_ix, &new_text);
1332
1333            assert_eq!(actual.text(), expected);
1334            log::info!("text: {:?}", expected);
1335
1336            for _ in 0..5 {
1337                let end_ix = clip_offset(&expected, rng.gen_range(0..=expected.len()), Right);
1338                let start_ix = clip_offset(&expected, rng.gen_range(0..=end_ix), Left);
1339
1340                let actual_text = actual.chunks_in_range(start_ix..end_ix).collect::<String>();
1341                assert_eq!(actual_text, &expected[start_ix..end_ix]);
1342
1343                let mut actual_text = String::new();
1344                actual
1345                    .bytes_in_range(start_ix..end_ix)
1346                    .read_to_string(&mut actual_text)
1347                    .unwrap();
1348                assert_eq!(actual_text, &expected[start_ix..end_ix]);
1349
1350                assert_eq!(
1351                    actual
1352                        .reversed_chunks_in_range(start_ix..end_ix)
1353                        .collect::<Vec<&str>>()
1354                        .into_iter()
1355                        .rev()
1356                        .collect::<String>(),
1357                    &expected[start_ix..end_ix]
1358                );
1359            }
1360
1361            let mut offset_utf16 = OffsetUtf16(0);
1362            let mut point = Point::new(0, 0);
1363            let mut point_utf16 = PointUtf16::new(0, 0);
1364            for (ix, ch) in expected.char_indices().chain(Some((expected.len(), '\0'))) {
1365                assert_eq!(actual.offset_to_point(ix), point, "offset_to_point({})", ix);
1366                assert_eq!(
1367                    actual.offset_to_point_utf16(ix),
1368                    point_utf16,
1369                    "offset_to_point_utf16({})",
1370                    ix
1371                );
1372                assert_eq!(
1373                    actual.point_to_offset(point),
1374                    ix,
1375                    "point_to_offset({:?})",
1376                    point
1377                );
1378                assert_eq!(
1379                    actual.point_utf16_to_offset(point_utf16),
1380                    ix,
1381                    "point_utf16_to_offset({:?})",
1382                    point_utf16
1383                );
1384                assert_eq!(
1385                    actual.offset_to_offset_utf16(ix),
1386                    offset_utf16,
1387                    "offset_to_offset_utf16({:?})",
1388                    ix
1389                );
1390                assert_eq!(
1391                    actual.offset_utf16_to_offset(offset_utf16),
1392                    ix,
1393                    "offset_utf16_to_offset({:?})",
1394                    offset_utf16
1395                );
1396                if ch == '\n' {
1397                    point += Point::new(1, 0);
1398                    point_utf16 += PointUtf16::new(1, 0);
1399                } else {
1400                    point.column += ch.len_utf8() as u32;
1401                    point_utf16.column += ch.len_utf16() as u32;
1402                }
1403                offset_utf16.0 += ch.len_utf16();
1404            }
1405
1406            let mut offset_utf16 = OffsetUtf16(0);
1407            let mut point_utf16 = Unclipped(PointUtf16::zero());
1408            for unit in expected.encode_utf16() {
1409                let left_offset = actual.clip_offset_utf16(offset_utf16, Bias::Left);
1410                let right_offset = actual.clip_offset_utf16(offset_utf16, Bias::Right);
1411                assert!(right_offset >= left_offset);
1412                // Ensure translating UTF-16 offsets to UTF-8 offsets doesn't panic.
1413                actual.offset_utf16_to_offset(left_offset);
1414                actual.offset_utf16_to_offset(right_offset);
1415
1416                let left_point = actual.clip_point_utf16(point_utf16, Bias::Left);
1417                let right_point = actual.clip_point_utf16(point_utf16, Bias::Right);
1418                assert!(right_point >= left_point);
1419                // Ensure translating valid UTF-16 points to offsets doesn't panic.
1420                actual.point_utf16_to_offset(left_point);
1421                actual.point_utf16_to_offset(right_point);
1422
1423                offset_utf16.0 += 1;
1424                if unit == b'\n' as u16 {
1425                    point_utf16.0 += PointUtf16::new(1, 0);
1426                } else {
1427                    point_utf16.0 += PointUtf16::new(0, 1);
1428                }
1429            }
1430
1431            for _ in 0..5 {
1432                let end_ix = clip_offset(&expected, rng.gen_range(0..=expected.len()), Right);
1433                let start_ix = clip_offset(&expected, rng.gen_range(0..=end_ix), Left);
1434                assert_eq!(
1435                    actual.cursor(start_ix).summary::<TextSummary>(end_ix),
1436                    TextSummary::from(&expected[start_ix..end_ix])
1437                );
1438            }
1439
1440            let mut expected_longest_rows = Vec::new();
1441            let mut longest_line_len = -1_isize;
1442            for (row, line) in expected.split('\n').enumerate() {
1443                let row = row as u32;
1444                assert_eq!(
1445                    actual.line_len(row),
1446                    line.len() as u32,
1447                    "invalid line len for row {}",
1448                    row
1449                );
1450
1451                let line_char_count = line.chars().count() as isize;
1452                match line_char_count.cmp(&longest_line_len) {
1453                    Ordering::Less => {}
1454                    Ordering::Equal => expected_longest_rows.push(row),
1455                    Ordering::Greater => {
1456                        longest_line_len = line_char_count;
1457                        expected_longest_rows.clear();
1458                        expected_longest_rows.push(row);
1459                    }
1460                }
1461            }
1462
1463            let longest_row = actual.summary().longest_row;
1464            assert!(
1465                expected_longest_rows.contains(&longest_row),
1466                "incorrect longest row {}. expected {:?} with length {}",
1467                longest_row,
1468                expected_longest_rows,
1469                longest_line_len,
1470            );
1471        }
1472    }
1473
1474    fn clip_offset(text: &str, mut offset: usize, bias: Bias) -> usize {
1475        while !text.is_char_boundary(offset) {
1476            match bias {
1477                Bias::Left => offset -= 1,
1478                Bias::Right => offset += 1,
1479            }
1480        }
1481        offset
1482    }
1483
1484    impl Rope {
1485        fn text(&self) -> String {
1486            let mut text = String::new();
1487            for chunk in self.chunks.cursor::<()>() {
1488                text.push_str(&chunk.0);
1489            }
1490            text
1491        }
1492    }
1493}