rope.rs

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