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    offset: usize,
 561    reversed: bool,
 562}
 563
 564impl<'a> Chunks<'a> {
 565    pub fn new(rope: &'a Rope, range: Range<usize>, reversed: bool) -> Self {
 566        let mut chunks = rope.chunks.cursor();
 567        let offset = if reversed {
 568            chunks.seek(&range.end, Bias::Left, &());
 569            range.end
 570        } else {
 571            chunks.seek(&range.start, Bias::Right, &());
 572            range.start
 573        };
 574        Self {
 575            chunks,
 576            range,
 577            offset,
 578            reversed,
 579        }
 580    }
 581
 582    fn offset_is_valid(&self) -> bool {
 583        if self.reversed {
 584            if self.offset <= self.range.start || self.offset > self.range.end {
 585                return false;
 586            }
 587        } else if self.offset < self.range.start || self.offset >= self.range.end {
 588            return false;
 589        }
 590
 591        true
 592    }
 593
 594    pub fn offset(&self) -> usize {
 595        self.offset
 596    }
 597
 598    pub fn seek(&mut self, mut offset: usize) {
 599        offset = offset.clamp(self.range.start, self.range.end);
 600
 601        let bias = if self.reversed {
 602            Bias::Left
 603        } else {
 604            Bias::Right
 605        };
 606
 607        if offset >= self.chunks.end(&()) {
 608            self.chunks.seek_forward(&offset, bias, &());
 609        } else {
 610            self.chunks.seek(&offset, bias, &());
 611        }
 612
 613        self.offset = offset;
 614    }
 615
 616    pub fn set_range(&mut self, range: Range<usize>) {
 617        self.range = range.clone();
 618        self.seek(range.start);
 619    }
 620
 621    /// Moves this cursor to the start of the next line in the rope.
 622    ///
 623    /// This method advances the cursor to the beginning of the next line.
 624    /// If the cursor is already at the end of the rope, this method does nothing.
 625    /// Reversed chunks iterators are not currently supported and will panic.
 626    ///
 627    /// Returns `true` if the cursor was successfully moved to the next line start,
 628    /// or `false` if the cursor was already at the end of the rope.
 629    pub fn next_line(&mut self) -> bool {
 630        assert!(!self.reversed);
 631
 632        let mut found = false;
 633        if let Some(chunk) = self.peek() {
 634            if let Some(newline_ix) = chunk.find('\n') {
 635                self.offset += newline_ix + 1;
 636                found = self.offset <= self.range.end;
 637            } else {
 638                self.chunks
 639                    .search_forward(|summary| summary.text.lines.row > 0, &());
 640                self.offset = *self.chunks.start();
 641
 642                if let Some(newline_ix) = self.peek().and_then(|chunk| chunk.find('\n')) {
 643                    self.offset += newline_ix + 1;
 644                    found = self.offset <= self.range.end;
 645                } else {
 646                    self.offset = self.chunks.end(&());
 647                }
 648            }
 649
 650            if self.offset == self.chunks.end(&()) {
 651                self.next();
 652            }
 653        }
 654
 655        if self.offset > self.range.end {
 656            self.offset = cmp::min(self.offset, self.range.end);
 657            self.chunks.seek(&self.offset, Bias::Right, &());
 658        }
 659
 660        found
 661    }
 662
 663    /// Move this cursor to the preceding position in the rope that starts a new line.
 664    /// Reversed chunks iterators are not currently supported and will panic.
 665    ///
 666    /// If this cursor is not on the start of a line, it will be moved to the start of
 667    /// its current line. Otherwise it will be moved to the start of the previous line.
 668    /// It updates the cursor's position and returns true if a previous line was found,
 669    /// or false if the cursor was already at the start of the rope.
 670    pub fn prev_line(&mut self) -> bool {
 671        assert!(!self.reversed);
 672
 673        let initial_offset = self.offset;
 674
 675        if self.offset == *self.chunks.start() {
 676            self.chunks.prev(&());
 677        }
 678
 679        if let Some(chunk) = self.chunks.item() {
 680            let mut end_ix = self.offset - *self.chunks.start();
 681            if chunk.0.as_bytes()[end_ix - 1] == b'\n' {
 682                end_ix -= 1;
 683            }
 684
 685            if let Some(newline_ix) = chunk.0[..end_ix].rfind('\n') {
 686                self.offset = *self.chunks.start() + newline_ix + 1;
 687                if self.offset_is_valid() {
 688                    return true;
 689                }
 690            }
 691        }
 692
 693        self.chunks
 694            .search_backward(|summary| summary.text.lines.row > 0, &());
 695        self.offset = *self.chunks.start();
 696        if let Some(chunk) = self.chunks.item() {
 697            if let Some(newline_ix) = chunk.0.rfind('\n') {
 698                self.offset += newline_ix + 1;
 699                if self.offset_is_valid() {
 700                    if self.offset == self.chunks.end(&()) {
 701                        self.chunks.next(&());
 702                    }
 703
 704                    return true;
 705                }
 706            }
 707        }
 708
 709        if !self.offset_is_valid() || self.chunks.item().is_none() {
 710            self.offset = self.range.start;
 711            self.chunks.seek(&self.offset, Bias::Right, &());
 712        }
 713
 714        self.offset < initial_offset && self.offset == 0
 715    }
 716
 717    pub fn peek(&self) -> Option<&'a str> {
 718        if !self.offset_is_valid() {
 719            return None;
 720        }
 721
 722        let chunk = self.chunks.item()?;
 723        let chunk_start = *self.chunks.start();
 724        let slice_range = if self.reversed {
 725            let slice_start = cmp::max(chunk_start, self.range.start) - chunk_start;
 726            let slice_end = self.offset - chunk_start;
 727            slice_start..slice_end
 728        } else {
 729            let slice_start = self.offset - chunk_start;
 730            let slice_end = cmp::min(self.chunks.end(&()), self.range.end) - chunk_start;
 731            slice_start..slice_end
 732        };
 733
 734        Some(&chunk.0[slice_range])
 735    }
 736
 737    pub fn lines(self) -> Lines<'a> {
 738        let reversed = self.reversed;
 739        Lines {
 740            chunks: self,
 741            current_line: String::new(),
 742            done: false,
 743            reversed,
 744        }
 745    }
 746}
 747
 748impl<'a> Iterator for Chunks<'a> {
 749    type Item = &'a str;
 750
 751    fn next(&mut self) -> Option<Self::Item> {
 752        let chunk = self.peek()?;
 753        if self.reversed {
 754            self.offset -= chunk.len();
 755            if self.offset <= *self.chunks.start() {
 756                self.chunks.prev(&());
 757            }
 758        } else {
 759            self.offset += chunk.len();
 760            if self.offset >= self.chunks.end(&()) {
 761                self.chunks.next(&());
 762            }
 763        }
 764
 765        Some(chunk)
 766    }
 767}
 768
 769pub struct Bytes<'a> {
 770    chunks: sum_tree::Cursor<'a, Chunk, usize>,
 771    range: Range<usize>,
 772    reversed: bool,
 773}
 774
 775impl<'a> Bytes<'a> {
 776    pub fn new(rope: &'a Rope, range: Range<usize>, reversed: bool) -> Self {
 777        let mut chunks = rope.chunks.cursor();
 778        if reversed {
 779            chunks.seek(&range.end, Bias::Left, &());
 780        } else {
 781            chunks.seek(&range.start, Bias::Right, &());
 782        }
 783        Self {
 784            chunks,
 785            range,
 786            reversed,
 787        }
 788    }
 789
 790    pub fn peek(&self) -> Option<&'a [u8]> {
 791        let chunk = self.chunks.item()?;
 792        if self.reversed && self.range.start >= self.chunks.end(&()) {
 793            return None;
 794        }
 795        let chunk_start = *self.chunks.start();
 796        if self.range.end <= chunk_start {
 797            return None;
 798        }
 799        let start = self.range.start.saturating_sub(chunk_start);
 800        let end = self.range.end - chunk_start;
 801        Some(&chunk.0.as_bytes()[start..chunk.0.len().min(end)])
 802    }
 803}
 804
 805impl<'a> Iterator for Bytes<'a> {
 806    type Item = &'a [u8];
 807
 808    fn next(&mut self) -> Option<Self::Item> {
 809        let result = self.peek();
 810        if result.is_some() {
 811            if self.reversed {
 812                self.chunks.prev(&());
 813            } else {
 814                self.chunks.next(&());
 815            }
 816        }
 817        result
 818    }
 819}
 820
 821impl<'a> io::Read for Bytes<'a> {
 822    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
 823        if let Some(chunk) = self.peek() {
 824            let len = cmp::min(buf.len(), chunk.len());
 825            if self.reversed {
 826                buf[..len].copy_from_slice(&chunk[chunk.len() - len..]);
 827                buf[..len].reverse();
 828                self.range.end -= len;
 829            } else {
 830                buf[..len].copy_from_slice(&chunk[..len]);
 831                self.range.start += len;
 832            }
 833
 834            if len == chunk.len() {
 835                if self.reversed {
 836                    self.chunks.prev(&());
 837                } else {
 838                    self.chunks.next(&());
 839                }
 840            }
 841            Ok(len)
 842        } else {
 843            Ok(0)
 844        }
 845    }
 846}
 847
 848pub struct Lines<'a> {
 849    chunks: Chunks<'a>,
 850    current_line: String,
 851    done: bool,
 852    reversed: bool,
 853}
 854
 855impl<'a> Lines<'a> {
 856    pub fn next(&mut self) -> Option<&str> {
 857        if self.done {
 858            return None;
 859        }
 860
 861        self.current_line.clear();
 862
 863        while let Some(chunk) = self.chunks.peek() {
 864            let lines = chunk.split('\n');
 865            if self.reversed {
 866                let mut lines = lines.rev().peekable();
 867                while let Some(line) = lines.next() {
 868                    self.current_line.insert_str(0, line);
 869                    if lines.peek().is_some() {
 870                        self.chunks
 871                            .seek(self.chunks.offset() - line.len() - "\n".len());
 872                        return Some(&self.current_line);
 873                    }
 874                }
 875            } else {
 876                let mut lines = lines.peekable();
 877                while let Some(line) = lines.next() {
 878                    self.current_line.push_str(line);
 879                    if lines.peek().is_some() {
 880                        self.chunks
 881                            .seek(self.chunks.offset() + line.len() + "\n".len());
 882                        return Some(&self.current_line);
 883                    }
 884                }
 885            }
 886
 887            self.chunks.next();
 888        }
 889
 890        self.done = true;
 891        Some(&self.current_line)
 892    }
 893
 894    pub fn seek(&mut self, offset: usize) {
 895        self.chunks.seek(offset);
 896        self.current_line.clear();
 897        self.done = false;
 898    }
 899
 900    pub fn offset(&self) -> usize {
 901        self.chunks.offset()
 902    }
 903}
 904
 905#[derive(Clone, Debug, Default)]
 906struct Chunk(ArrayString<{ 2 * CHUNK_BASE }>);
 907
 908impl Chunk {
 909    fn offset_to_offset_utf16(&self, target: usize) -> OffsetUtf16 {
 910        let mut offset = 0;
 911        let mut offset_utf16 = OffsetUtf16(0);
 912        for ch in self.0.chars() {
 913            if offset >= target {
 914                break;
 915            }
 916
 917            offset += ch.len_utf8();
 918            offset_utf16.0 += ch.len_utf16();
 919        }
 920        offset_utf16
 921    }
 922
 923    fn offset_utf16_to_offset(&self, target: OffsetUtf16) -> usize {
 924        let mut offset_utf16 = OffsetUtf16(0);
 925        let mut offset = 0;
 926        for ch in self.0.chars() {
 927            if offset_utf16 >= target {
 928                break;
 929            }
 930
 931            offset += ch.len_utf8();
 932            offset_utf16.0 += ch.len_utf16();
 933        }
 934        offset
 935    }
 936
 937    fn offset_to_point(&self, target: usize) -> Point {
 938        let mut offset = 0;
 939        let mut point = Point::new(0, 0);
 940        for ch in self.0.chars() {
 941            if offset >= target {
 942                break;
 943            }
 944
 945            if ch == '\n' {
 946                point.row += 1;
 947                point.column = 0;
 948            } else {
 949                point.column += ch.len_utf8() as u32;
 950            }
 951            offset += ch.len_utf8();
 952        }
 953        point
 954    }
 955
 956    fn offset_to_point_utf16(&self, target: usize) -> PointUtf16 {
 957        let mut offset = 0;
 958        let mut point = PointUtf16::new(0, 0);
 959        for ch in self.0.chars() {
 960            if offset >= target {
 961                break;
 962            }
 963
 964            if ch == '\n' {
 965                point.row += 1;
 966                point.column = 0;
 967            } else {
 968                point.column += ch.len_utf16() as u32;
 969            }
 970            offset += ch.len_utf8();
 971        }
 972        point
 973    }
 974
 975    fn point_to_offset(&self, target: Point) -> usize {
 976        let mut offset = 0;
 977        let mut point = Point::new(0, 0);
 978
 979        for ch in self.0.chars() {
 980            if point >= target {
 981                if point > target {
 982                    debug_panic!("point {target:?} is inside of character {ch:?}");
 983                }
 984                break;
 985            }
 986
 987            if ch == '\n' {
 988                point.row += 1;
 989                point.column = 0;
 990
 991                if point.row > target.row {
 992                    debug_panic!(
 993                        "point {target:?} is beyond the end of a line with length {}",
 994                        point.column
 995                    );
 996                    break;
 997                }
 998            } else {
 999                point.column += ch.len_utf8() as u32;
1000            }
1001
1002            offset += ch.len_utf8();
1003        }
1004
1005        offset
1006    }
1007
1008    fn point_to_point_utf16(&self, target: Point) -> PointUtf16 {
1009        let mut point = Point::zero();
1010        let mut point_utf16 = PointUtf16::new(0, 0);
1011        for ch in self.0.chars() {
1012            if point >= target {
1013                break;
1014            }
1015
1016            if ch == '\n' {
1017                point_utf16.row += 1;
1018                point_utf16.column = 0;
1019                point.row += 1;
1020                point.column = 0;
1021            } else {
1022                point_utf16.column += ch.len_utf16() as u32;
1023                point.column += ch.len_utf8() as u32;
1024            }
1025        }
1026        point_utf16
1027    }
1028
1029    fn point_utf16_to_offset(&self, target: PointUtf16, clip: bool) -> usize {
1030        let mut offset = 0;
1031        let mut point = PointUtf16::new(0, 0);
1032
1033        for ch in self.0.chars() {
1034            if point == target {
1035                break;
1036            }
1037
1038            if ch == '\n' {
1039                point.row += 1;
1040                point.column = 0;
1041
1042                if point.row > target.row {
1043                    if !clip {
1044                        debug_panic!(
1045                            "point {target:?} is beyond the end of a line with length {}",
1046                            point.column
1047                        );
1048                    }
1049                    // Return the offset of the newline
1050                    return offset;
1051                }
1052            } else {
1053                point.column += ch.len_utf16() as u32;
1054            }
1055
1056            if point > target {
1057                if !clip {
1058                    debug_panic!("point {target:?} is inside of codepoint {ch:?}");
1059                }
1060                // Return the offset of the codepoint which we have landed within, bias left
1061                return offset;
1062            }
1063
1064            offset += ch.len_utf8();
1065        }
1066
1067        offset
1068    }
1069
1070    fn unclipped_point_utf16_to_point(&self, target: Unclipped<PointUtf16>) -> Point {
1071        let mut point = Point::zero();
1072        let mut point_utf16 = PointUtf16::zero();
1073
1074        for ch in self.0.chars() {
1075            if point_utf16 == target.0 {
1076                break;
1077            }
1078
1079            if point_utf16 > target.0 {
1080                // If the point is past the end of a line or inside of a code point,
1081                // return the last valid point before the target.
1082                return point;
1083            }
1084
1085            if ch == '\n' {
1086                point_utf16 += PointUtf16::new(1, 0);
1087                point += Point::new(1, 0);
1088            } else {
1089                point_utf16 += PointUtf16::new(0, ch.len_utf16() as u32);
1090                point += Point::new(0, ch.len_utf8() as u32);
1091            }
1092        }
1093
1094        point
1095    }
1096
1097    fn clip_point(&self, target: Point, bias: Bias) -> Point {
1098        for (row, line) in self.0.split('\n').enumerate() {
1099            if row == target.row as usize {
1100                let bytes = line.as_bytes();
1101                let mut column = target.column.min(bytes.len() as u32) as usize;
1102                if column == 0
1103                    || column == bytes.len()
1104                    || (bytes[column - 1] < 128 && bytes[column] < 128)
1105                {
1106                    return Point::new(row as u32, column as u32);
1107                }
1108
1109                let mut grapheme_cursor = GraphemeCursor::new(column, bytes.len(), true);
1110                loop {
1111                    if line.is_char_boundary(column)
1112                        && grapheme_cursor.is_boundary(line, 0).unwrap_or(false)
1113                    {
1114                        break;
1115                    }
1116
1117                    match bias {
1118                        Bias::Left => column -= 1,
1119                        Bias::Right => column += 1,
1120                    }
1121                    grapheme_cursor.set_cursor(column);
1122                }
1123                return Point::new(row as u32, column as u32);
1124            }
1125        }
1126        unreachable!()
1127    }
1128
1129    fn clip_point_utf16(&self, target: Unclipped<PointUtf16>, bias: Bias) -> PointUtf16 {
1130        for (row, line) in self.0.split('\n').enumerate() {
1131            if row == target.0.row as usize {
1132                let mut code_units = line.encode_utf16();
1133                let mut column = code_units.by_ref().take(target.0.column as usize).count();
1134                if char::decode_utf16(code_units).next().transpose().is_err() {
1135                    match bias {
1136                        Bias::Left => column -= 1,
1137                        Bias::Right => column += 1,
1138                    }
1139                }
1140                return PointUtf16::new(row as u32, column as u32);
1141            }
1142        }
1143        unreachable!()
1144    }
1145
1146    fn clip_offset_utf16(&self, target: OffsetUtf16, bias: Bias) -> OffsetUtf16 {
1147        let mut code_units = self.0.encode_utf16();
1148        let mut offset = code_units.by_ref().take(target.0).count();
1149        if char::decode_utf16(code_units).next().transpose().is_err() {
1150            match bias {
1151                Bias::Left => offset -= 1,
1152                Bias::Right => offset += 1,
1153            }
1154        }
1155        OffsetUtf16(offset)
1156    }
1157}
1158
1159impl sum_tree::Item for Chunk {
1160    type Summary = ChunkSummary;
1161
1162    fn summary(&self) -> Self::Summary {
1163        ChunkSummary::from(self.0.as_str())
1164    }
1165}
1166
1167#[derive(Clone, Debug, Default, Eq, PartialEq)]
1168pub struct ChunkSummary {
1169    text: TextSummary,
1170}
1171
1172impl<'a> From<&'a str> for ChunkSummary {
1173    fn from(text: &'a str) -> Self {
1174        Self {
1175            text: TextSummary::from(text),
1176        }
1177    }
1178}
1179
1180impl sum_tree::Summary for ChunkSummary {
1181    type Context = ();
1182
1183    fn add_summary(&mut self, summary: &Self, _: &()) {
1184        self.text += &summary.text;
1185    }
1186}
1187
1188/// Summary of a string of text.
1189#[derive(Clone, Debug, Default, Eq, PartialEq)]
1190pub struct TextSummary {
1191    /// Length in UTF-8
1192    pub len: usize,
1193    /// Length in UTF-16 code units
1194    pub len_utf16: OffsetUtf16,
1195    /// A point representing the number of lines and the length of the last line
1196    pub lines: Point,
1197    /// How many `char`s are in the first line
1198    pub first_line_chars: u32,
1199    /// How many `char`s are in the last line
1200    pub last_line_chars: u32,
1201    /// How many UTF-16 code units are in the last line
1202    pub last_line_len_utf16: u32,
1203    /// The row idx of the longest row
1204    pub longest_row: u32,
1205    /// How many `char`s are in the longest row
1206    pub longest_row_chars: u32,
1207}
1208
1209impl TextSummary {
1210    pub fn lines_utf16(&self) -> PointUtf16 {
1211        PointUtf16 {
1212            row: self.lines.row,
1213            column: self.last_line_len_utf16,
1214        }
1215    }
1216}
1217
1218impl<'a> From<&'a str> for TextSummary {
1219    fn from(text: &'a str) -> Self {
1220        let mut len_utf16 = OffsetUtf16(0);
1221        let mut lines = Point::new(0, 0);
1222        let mut first_line_chars = 0;
1223        let mut last_line_chars = 0;
1224        let mut last_line_len_utf16 = 0;
1225        let mut longest_row = 0;
1226        let mut longest_row_chars = 0;
1227        for c in text.chars() {
1228            len_utf16.0 += c.len_utf16();
1229
1230            if c == '\n' {
1231                lines += Point::new(1, 0);
1232                last_line_len_utf16 = 0;
1233                last_line_chars = 0;
1234            } else {
1235                lines.column += c.len_utf8() as u32;
1236                last_line_len_utf16 += c.len_utf16() as u32;
1237                last_line_chars += 1;
1238            }
1239
1240            if lines.row == 0 {
1241                first_line_chars = last_line_chars;
1242            }
1243
1244            if last_line_chars > longest_row_chars {
1245                longest_row = lines.row;
1246                longest_row_chars = last_line_chars;
1247            }
1248        }
1249
1250        TextSummary {
1251            len: text.len(),
1252            len_utf16,
1253            lines,
1254            first_line_chars,
1255            last_line_chars,
1256            last_line_len_utf16,
1257            longest_row,
1258            longest_row_chars,
1259        }
1260    }
1261}
1262
1263impl sum_tree::Summary for TextSummary {
1264    type Context = ();
1265
1266    fn add_summary(&mut self, summary: &Self, _: &Self::Context) {
1267        *self += summary;
1268    }
1269}
1270
1271impl std::ops::Add<Self> for TextSummary {
1272    type Output = Self;
1273
1274    fn add(mut self, rhs: Self) -> Self::Output {
1275        AddAssign::add_assign(&mut self, &rhs);
1276        self
1277    }
1278}
1279
1280impl<'a> std::ops::AddAssign<&'a Self> for TextSummary {
1281    fn add_assign(&mut self, other: &'a Self) {
1282        let joined_chars = self.last_line_chars + other.first_line_chars;
1283        if joined_chars > self.longest_row_chars {
1284            self.longest_row = self.lines.row;
1285            self.longest_row_chars = joined_chars;
1286        }
1287        if other.longest_row_chars > self.longest_row_chars {
1288            self.longest_row = self.lines.row + other.longest_row;
1289            self.longest_row_chars = other.longest_row_chars;
1290        }
1291
1292        if self.lines.row == 0 {
1293            self.first_line_chars += other.first_line_chars;
1294        }
1295
1296        if other.lines.row == 0 {
1297            self.last_line_chars += other.first_line_chars;
1298            self.last_line_len_utf16 += other.last_line_len_utf16;
1299        } else {
1300            self.last_line_chars = other.last_line_chars;
1301            self.last_line_len_utf16 = other.last_line_len_utf16;
1302        }
1303
1304        self.len += other.len;
1305        self.len_utf16 += other.len_utf16;
1306        self.lines += other.lines;
1307    }
1308}
1309
1310impl std::ops::AddAssign<Self> for TextSummary {
1311    fn add_assign(&mut self, other: Self) {
1312        *self += &other;
1313    }
1314}
1315
1316pub trait TextDimension: 'static + for<'a> Dimension<'a, ChunkSummary> {
1317    fn from_text_summary(summary: &TextSummary) -> Self;
1318    fn add_assign(&mut self, other: &Self);
1319}
1320
1321impl<D1: TextDimension, D2: TextDimension> TextDimension for (D1, D2) {
1322    fn from_text_summary(summary: &TextSummary) -> Self {
1323        (
1324            D1::from_text_summary(summary),
1325            D2::from_text_summary(summary),
1326        )
1327    }
1328
1329    fn add_assign(&mut self, other: &Self) {
1330        self.0.add_assign(&other.0);
1331        self.1.add_assign(&other.1);
1332    }
1333}
1334
1335impl<'a> sum_tree::Dimension<'a, ChunkSummary> for TextSummary {
1336    fn add_summary(&mut self, summary: &'a ChunkSummary, _: &()) {
1337        *self += &summary.text;
1338    }
1339}
1340
1341impl TextDimension for TextSummary {
1342    fn from_text_summary(summary: &TextSummary) -> Self {
1343        summary.clone()
1344    }
1345
1346    fn add_assign(&mut self, other: &Self) {
1347        *self += other;
1348    }
1349}
1350
1351impl<'a> sum_tree::Dimension<'a, ChunkSummary> for usize {
1352    fn add_summary(&mut self, summary: &'a ChunkSummary, _: &()) {
1353        *self += summary.text.len;
1354    }
1355}
1356
1357impl TextDimension for usize {
1358    fn from_text_summary(summary: &TextSummary) -> Self {
1359        summary.len
1360    }
1361
1362    fn add_assign(&mut self, other: &Self) {
1363        *self += other;
1364    }
1365}
1366
1367impl<'a> sum_tree::Dimension<'a, ChunkSummary> for OffsetUtf16 {
1368    fn add_summary(&mut self, summary: &'a ChunkSummary, _: &()) {
1369        *self += summary.text.len_utf16;
1370    }
1371}
1372
1373impl TextDimension for OffsetUtf16 {
1374    fn from_text_summary(summary: &TextSummary) -> Self {
1375        summary.len_utf16
1376    }
1377
1378    fn add_assign(&mut self, other: &Self) {
1379        *self += other;
1380    }
1381}
1382
1383impl<'a> sum_tree::Dimension<'a, ChunkSummary> for Point {
1384    fn add_summary(&mut self, summary: &'a ChunkSummary, _: &()) {
1385        *self += summary.text.lines;
1386    }
1387}
1388
1389impl TextDimension for Point {
1390    fn from_text_summary(summary: &TextSummary) -> Self {
1391        summary.lines
1392    }
1393
1394    fn add_assign(&mut self, other: &Self) {
1395        *self += other;
1396    }
1397}
1398
1399impl<'a> sum_tree::Dimension<'a, ChunkSummary> for PointUtf16 {
1400    fn add_summary(&mut self, summary: &'a ChunkSummary, _: &()) {
1401        *self += summary.text.lines_utf16();
1402    }
1403}
1404
1405impl TextDimension for PointUtf16 {
1406    fn from_text_summary(summary: &TextSummary) -> Self {
1407        summary.lines_utf16()
1408    }
1409
1410    fn add_assign(&mut self, other: &Self) {
1411        *self += other;
1412    }
1413}
1414
1415#[cfg(test)]
1416mod tests {
1417    use super::*;
1418    use rand::prelude::*;
1419    use std::{cmp::Ordering, env, io::Read};
1420    use util::RandomCharIter;
1421    use Bias::{Left, Right};
1422
1423    #[ctor::ctor]
1424    fn init_logger() {
1425        if std::env::var("RUST_LOG").is_ok() {
1426            env_logger::init();
1427        }
1428    }
1429
1430    #[test]
1431    fn test_all_4_byte_chars() {
1432        let mut rope = Rope::new();
1433        let text = "🏀".repeat(256);
1434        rope.push(&text);
1435        assert_eq!(rope.text(), text);
1436    }
1437
1438    #[test]
1439    fn test_clip() {
1440        let rope = Rope::from("🧘");
1441
1442        assert_eq!(rope.clip_offset(1, Bias::Left), 0);
1443        assert_eq!(rope.clip_offset(1, Bias::Right), 4);
1444        assert_eq!(rope.clip_offset(5, Bias::Right), 4);
1445
1446        assert_eq!(
1447            rope.clip_point(Point::new(0, 1), Bias::Left),
1448            Point::new(0, 0)
1449        );
1450        assert_eq!(
1451            rope.clip_point(Point::new(0, 1), Bias::Right),
1452            Point::new(0, 4)
1453        );
1454        assert_eq!(
1455            rope.clip_point(Point::new(0, 5), Bias::Right),
1456            Point::new(0, 4)
1457        );
1458
1459        assert_eq!(
1460            rope.clip_point_utf16(Unclipped(PointUtf16::new(0, 1)), Bias::Left),
1461            PointUtf16::new(0, 0)
1462        );
1463        assert_eq!(
1464            rope.clip_point_utf16(Unclipped(PointUtf16::new(0, 1)), Bias::Right),
1465            PointUtf16::new(0, 2)
1466        );
1467        assert_eq!(
1468            rope.clip_point_utf16(Unclipped(PointUtf16::new(0, 3)), Bias::Right),
1469            PointUtf16::new(0, 2)
1470        );
1471
1472        assert_eq!(
1473            rope.clip_offset_utf16(OffsetUtf16(1), Bias::Left),
1474            OffsetUtf16(0)
1475        );
1476        assert_eq!(
1477            rope.clip_offset_utf16(OffsetUtf16(1), Bias::Right),
1478            OffsetUtf16(2)
1479        );
1480        assert_eq!(
1481            rope.clip_offset_utf16(OffsetUtf16(3), Bias::Right),
1482            OffsetUtf16(2)
1483        );
1484    }
1485
1486    #[test]
1487    fn test_prev_next_line() {
1488        let rope = Rope::from("abc\ndef\nghi\njkl");
1489
1490        let mut chunks = rope.chunks();
1491        assert_eq!(chunks.peek().unwrap().chars().next().unwrap(), 'a');
1492
1493        assert!(chunks.next_line());
1494        assert_eq!(chunks.peek().unwrap().chars().next().unwrap(), 'd');
1495
1496        assert!(chunks.next_line());
1497        assert_eq!(chunks.peek().unwrap().chars().next().unwrap(), 'g');
1498
1499        assert!(chunks.next_line());
1500        assert_eq!(chunks.peek().unwrap().chars().next().unwrap(), 'j');
1501
1502        assert!(!chunks.next_line());
1503        assert_eq!(chunks.peek(), None);
1504
1505        assert!(chunks.prev_line());
1506        assert_eq!(chunks.peek().unwrap().chars().next().unwrap(), 'j');
1507
1508        assert!(chunks.prev_line());
1509        assert_eq!(chunks.peek().unwrap().chars().next().unwrap(), 'g');
1510
1511        assert!(chunks.prev_line());
1512        assert_eq!(chunks.peek().unwrap().chars().next().unwrap(), 'd');
1513
1514        assert!(chunks.prev_line());
1515        assert_eq!(chunks.peek().unwrap().chars().next().unwrap(), 'a');
1516
1517        assert!(!chunks.prev_line());
1518        assert_eq!(chunks.peek().unwrap().chars().next().unwrap(), 'a');
1519
1520        // Only return true when the cursor has moved to the start of a line
1521        let mut chunks = rope.chunks_in_range(5..7);
1522        chunks.seek(6);
1523        assert!(!chunks.prev_line());
1524        assert_eq!(chunks.peek().unwrap().chars().next().unwrap(), 'e');
1525
1526        assert!(!chunks.next_line());
1527        assert_eq!(chunks.peek(), None);
1528    }
1529
1530    #[test]
1531    fn test_lines() {
1532        let rope = Rope::from("abc\ndefg\nhi");
1533        let mut lines = rope.chunks().lines();
1534        assert_eq!(lines.next(), Some("abc"));
1535        assert_eq!(lines.next(), Some("defg"));
1536        assert_eq!(lines.next(), Some("hi"));
1537        assert_eq!(lines.next(), None);
1538
1539        let rope = Rope::from("abc\ndefg\nhi\n");
1540        let mut lines = rope.chunks().lines();
1541        assert_eq!(lines.next(), Some("abc"));
1542        assert_eq!(lines.next(), Some("defg"));
1543        assert_eq!(lines.next(), Some("hi"));
1544        assert_eq!(lines.next(), Some(""));
1545        assert_eq!(lines.next(), None);
1546
1547        let rope = Rope::from("abc\ndefg\nhi");
1548        let mut lines = rope.reversed_chunks_in_range(0..rope.len()).lines();
1549        assert_eq!(lines.next(), Some("hi"));
1550        assert_eq!(lines.next(), Some("defg"));
1551        assert_eq!(lines.next(), Some("abc"));
1552        assert_eq!(lines.next(), None);
1553
1554        let rope = Rope::from("abc\ndefg\nhi\n");
1555        let mut lines = rope.reversed_chunks_in_range(0..rope.len()).lines();
1556        assert_eq!(lines.next(), Some(""));
1557        assert_eq!(lines.next(), Some("hi"));
1558        assert_eq!(lines.next(), Some("defg"));
1559        assert_eq!(lines.next(), Some("abc"));
1560        assert_eq!(lines.next(), None);
1561    }
1562
1563    #[gpui::test(iterations = 100)]
1564    fn test_random_rope(mut rng: StdRng) {
1565        let operations = env::var("OPERATIONS")
1566            .map(|i| i.parse().expect("invalid `OPERATIONS` variable"))
1567            .unwrap_or(10);
1568
1569        let mut expected = String::new();
1570        let mut actual = Rope::new();
1571        for _ in 0..operations {
1572            let end_ix = clip_offset(&expected, rng.gen_range(0..=expected.len()), Right);
1573            let start_ix = clip_offset(&expected, rng.gen_range(0..=end_ix), Left);
1574            let len = rng.gen_range(0..=64);
1575            let new_text: String = RandomCharIter::new(&mut rng).take(len).collect();
1576
1577            let mut new_actual = Rope::new();
1578            let mut cursor = actual.cursor(0);
1579            new_actual.append(cursor.slice(start_ix));
1580            new_actual.push(&new_text);
1581            cursor.seek_forward(end_ix);
1582            new_actual.append(cursor.suffix());
1583            actual = new_actual;
1584
1585            expected.replace_range(start_ix..end_ix, &new_text);
1586
1587            assert_eq!(actual.text(), expected);
1588            log::info!("text: {:?}", expected);
1589
1590            for _ in 0..5 {
1591                let end_ix = clip_offset(&expected, rng.gen_range(0..=expected.len()), Right);
1592                let start_ix = clip_offset(&expected, rng.gen_range(0..=end_ix), Left);
1593
1594                let actual_text = actual.chunks_in_range(start_ix..end_ix).collect::<String>();
1595                assert_eq!(actual_text, &expected[start_ix..end_ix]);
1596
1597                let mut actual_text = String::new();
1598                actual
1599                    .bytes_in_range(start_ix..end_ix)
1600                    .read_to_string(&mut actual_text)
1601                    .unwrap();
1602                assert_eq!(actual_text, &expected[start_ix..end_ix]);
1603
1604                assert_eq!(
1605                    actual
1606                        .reversed_chunks_in_range(start_ix..end_ix)
1607                        .collect::<Vec<&str>>()
1608                        .into_iter()
1609                        .rev()
1610                        .collect::<String>(),
1611                    &expected[start_ix..end_ix]
1612                );
1613
1614                let mut expected_line_starts: Vec<_> = expected[start_ix..end_ix]
1615                    .match_indices('\n')
1616                    .map(|(index, _)| start_ix + index + 1)
1617                    .collect();
1618
1619                let mut chunks = actual.chunks_in_range(start_ix..end_ix);
1620
1621                let mut actual_line_starts = Vec::new();
1622                while chunks.next_line() {
1623                    actual_line_starts.push(chunks.offset());
1624                }
1625                assert_eq!(
1626                    actual_line_starts,
1627                    expected_line_starts,
1628                    "actual line starts != expected line starts when using next_line() for {:?} ({:?})",
1629                    &expected[start_ix..end_ix],
1630                    start_ix..end_ix
1631                );
1632
1633                if start_ix < end_ix
1634                    && (start_ix == 0 || expected.as_bytes()[start_ix - 1] == b'\n')
1635                {
1636                    expected_line_starts.insert(0, start_ix);
1637                }
1638                // Remove the last index if it starts at the end of the range.
1639                if expected_line_starts.last() == Some(&end_ix) {
1640                    expected_line_starts.pop();
1641                }
1642
1643                let mut actual_line_starts = Vec::new();
1644                while chunks.prev_line() {
1645                    actual_line_starts.push(chunks.offset());
1646                }
1647                actual_line_starts.reverse();
1648                assert_eq!(
1649                    actual_line_starts,
1650                    expected_line_starts,
1651                    "actual line starts != expected line starts when using prev_line() for {:?} ({:?})",
1652                    &expected[start_ix..end_ix],
1653                    start_ix..end_ix
1654                );
1655
1656                // Check that next_line/prev_line work correctly from random positions
1657                let mut offset = rng.gen_range(start_ix..=end_ix);
1658                while !expected.is_char_boundary(offset) {
1659                    offset -= 1;
1660                }
1661                chunks.seek(offset);
1662
1663                for _ in 0..5 {
1664                    if rng.gen() {
1665                        let expected_next_line_start = expected[offset..end_ix]
1666                            .find('\n')
1667                            .map(|newline_ix| offset + newline_ix + 1);
1668
1669                        let moved = chunks.next_line();
1670                        assert_eq!(
1671                            moved,
1672                            expected_next_line_start.is_some(),
1673                            "unexpected result from next_line after seeking to {} in range {:?} ({:?})",
1674                            offset,
1675                            start_ix..end_ix,
1676                            &expected[start_ix..end_ix]
1677                        );
1678                        if let Some(expected_next_line_start) = expected_next_line_start {
1679                            assert_eq!(
1680                                chunks.offset(),
1681                                expected_next_line_start,
1682                                "invalid position after seeking to {} in range {:?} ({:?})",
1683                                offset,
1684                                start_ix..end_ix,
1685                                &expected[start_ix..end_ix]
1686                            );
1687                        } else {
1688                            assert_eq!(
1689                                chunks.offset(),
1690                                end_ix,
1691                                "invalid position after seeking to {} in range {:?} ({:?})",
1692                                offset,
1693                                start_ix..end_ix,
1694                                &expected[start_ix..end_ix]
1695                            );
1696                        }
1697                    } else {
1698                        let search_end = if offset > 0 && expected.as_bytes()[offset - 1] == b'\n' {
1699                            offset - 1
1700                        } else {
1701                            offset
1702                        };
1703
1704                        let expected_prev_line_start = expected[..search_end]
1705                            .rfind('\n')
1706                            .and_then(|newline_ix| {
1707                                let line_start_ix = newline_ix + 1;
1708                                if line_start_ix >= start_ix {
1709                                    Some(line_start_ix)
1710                                } else {
1711                                    None
1712                                }
1713                            })
1714                            .or({
1715                                if offset > 0 && start_ix == 0 {
1716                                    Some(0)
1717                                } else {
1718                                    None
1719                                }
1720                            });
1721
1722                        let moved = chunks.prev_line();
1723                        assert_eq!(
1724                            moved,
1725                            expected_prev_line_start.is_some(),
1726                            "unexpected result from prev_line after seeking to {} in range {:?} ({:?})",
1727                            offset,
1728                            start_ix..end_ix,
1729                            &expected[start_ix..end_ix]
1730                        );
1731                        if let Some(expected_prev_line_start) = expected_prev_line_start {
1732                            assert_eq!(
1733                                chunks.offset(),
1734                                expected_prev_line_start,
1735                                "invalid position after seeking to {} in range {:?} ({:?})",
1736                                offset,
1737                                start_ix..end_ix,
1738                                &expected[start_ix..end_ix]
1739                            );
1740                        } else {
1741                            assert_eq!(
1742                                chunks.offset(),
1743                                start_ix,
1744                                "invalid position after seeking to {} in range {:?} ({:?})",
1745                                offset,
1746                                start_ix..end_ix,
1747                                &expected[start_ix..end_ix]
1748                            );
1749                        }
1750                    }
1751
1752                    assert!((start_ix..=end_ix).contains(&chunks.offset()));
1753                    if rng.gen() {
1754                        offset = rng.gen_range(start_ix..=end_ix);
1755                        while !expected.is_char_boundary(offset) {
1756                            offset -= 1;
1757                        }
1758                        chunks.seek(offset);
1759                    } else {
1760                        chunks.next();
1761                        offset = chunks.offset();
1762                        assert!((start_ix..=end_ix).contains(&chunks.offset()));
1763                    }
1764                }
1765            }
1766
1767            let mut offset_utf16 = OffsetUtf16(0);
1768            let mut point = Point::new(0, 0);
1769            let mut point_utf16 = PointUtf16::new(0, 0);
1770            for (ix, ch) in expected.char_indices().chain(Some((expected.len(), '\0'))) {
1771                assert_eq!(actual.offset_to_point(ix), point, "offset_to_point({})", ix);
1772                assert_eq!(
1773                    actual.offset_to_point_utf16(ix),
1774                    point_utf16,
1775                    "offset_to_point_utf16({})",
1776                    ix
1777                );
1778                assert_eq!(
1779                    actual.point_to_offset(point),
1780                    ix,
1781                    "point_to_offset({:?})",
1782                    point
1783                );
1784                assert_eq!(
1785                    actual.point_utf16_to_offset(point_utf16),
1786                    ix,
1787                    "point_utf16_to_offset({:?})",
1788                    point_utf16
1789                );
1790                assert_eq!(
1791                    actual.offset_to_offset_utf16(ix),
1792                    offset_utf16,
1793                    "offset_to_offset_utf16({:?})",
1794                    ix
1795                );
1796                assert_eq!(
1797                    actual.offset_utf16_to_offset(offset_utf16),
1798                    ix,
1799                    "offset_utf16_to_offset({:?})",
1800                    offset_utf16
1801                );
1802                if ch == '\n' {
1803                    point += Point::new(1, 0);
1804                    point_utf16 += PointUtf16::new(1, 0);
1805                } else {
1806                    point.column += ch.len_utf8() as u32;
1807                    point_utf16.column += ch.len_utf16() as u32;
1808                }
1809                offset_utf16.0 += ch.len_utf16();
1810            }
1811
1812            let mut offset_utf16 = OffsetUtf16(0);
1813            let mut point_utf16 = Unclipped(PointUtf16::zero());
1814            for unit in expected.encode_utf16() {
1815                let left_offset = actual.clip_offset_utf16(offset_utf16, Bias::Left);
1816                let right_offset = actual.clip_offset_utf16(offset_utf16, Bias::Right);
1817                assert!(right_offset >= left_offset);
1818                // Ensure translating UTF-16 offsets to UTF-8 offsets doesn't panic.
1819                actual.offset_utf16_to_offset(left_offset);
1820                actual.offset_utf16_to_offset(right_offset);
1821
1822                let left_point = actual.clip_point_utf16(point_utf16, Bias::Left);
1823                let right_point = actual.clip_point_utf16(point_utf16, Bias::Right);
1824                assert!(right_point >= left_point);
1825                // Ensure translating valid UTF-16 points to offsets doesn't panic.
1826                actual.point_utf16_to_offset(left_point);
1827                actual.point_utf16_to_offset(right_point);
1828
1829                offset_utf16.0 += 1;
1830                if unit == b'\n' as u16 {
1831                    point_utf16.0 += PointUtf16::new(1, 0);
1832                } else {
1833                    point_utf16.0 += PointUtf16::new(0, 1);
1834                }
1835            }
1836
1837            for _ in 0..5 {
1838                let end_ix = clip_offset(&expected, rng.gen_range(0..=expected.len()), Right);
1839                let start_ix = clip_offset(&expected, rng.gen_range(0..=end_ix), Left);
1840                assert_eq!(
1841                    actual.cursor(start_ix).summary::<TextSummary>(end_ix),
1842                    TextSummary::from(&expected[start_ix..end_ix])
1843                );
1844            }
1845
1846            let mut expected_longest_rows = Vec::new();
1847            let mut longest_line_len = -1_isize;
1848            for (row, line) in expected.split('\n').enumerate() {
1849                let row = row as u32;
1850                assert_eq!(
1851                    actual.line_len(row),
1852                    line.len() as u32,
1853                    "invalid line len for row {}",
1854                    row
1855                );
1856
1857                let line_char_count = line.chars().count() as isize;
1858                match line_char_count.cmp(&longest_line_len) {
1859                    Ordering::Less => {}
1860                    Ordering::Equal => expected_longest_rows.push(row),
1861                    Ordering::Greater => {
1862                        longest_line_len = line_char_count;
1863                        expected_longest_rows.clear();
1864                        expected_longest_rows.push(row);
1865                    }
1866                }
1867            }
1868
1869            let longest_row = actual.summary().longest_row;
1870            assert!(
1871                expected_longest_rows.contains(&longest_row),
1872                "incorrect longest row {}. expected {:?} with length {}",
1873                longest_row,
1874                expected_longest_rows,
1875                longest_line_len,
1876            );
1877        }
1878    }
1879
1880    fn clip_offset(text: &str, mut offset: usize, bias: Bias) -> usize {
1881        while !text.is_char_boundary(offset) {
1882            match bias {
1883                Bias::Left => offset -= 1,
1884                Bias::Right => offset += 1,
1885            }
1886        }
1887        offset
1888    }
1889
1890    impl Rope {
1891        fn text(&self) -> String {
1892            let mut text = String::new();
1893            for chunk in self.chunks.cursor::<()>() {
1894                text.push_str(&chunk.0);
1895            }
1896            text
1897        }
1898    }
1899}