Skip to main content

servo_base/
rope.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::iter::once;
6use std::ops::Range;
7
8use malloc_size_of_derive::MallocSizeOf;
9use rayon::iter::Either;
10use unicode_segmentation::UnicodeSegmentation;
11
12use crate::text::{Utf8CodeUnits, Utf16CodeUnits};
13
14fn contents_vec(contents: impl Into<String>) -> Vec<String> {
15    let mut contents: Vec<_> = contents
16        .into()
17        .split('\n')
18        .map(|line| format!("{line}\n"))
19        .collect();
20    // The last line should not have a newline.
21    if let Some(last_line) = contents.last_mut() {
22        last_line.truncate(last_line.len() - 1);
23    }
24    contents
25}
26
27/// Describes a unit of movement for [`Rope::move_by`].
28pub enum RopeMovement {
29    Character,
30    Grapheme,
31    Word,
32    Line,
33    LineStartOrEnd,
34    RopeStartOrEnd,
35}
36
37/// An implementation of a [rope data structure], composed of lines of
38/// owned strings. This is used to implement text controls in Servo.
39///
40/// [rope data structure]: https://en.wikipedia.org/wiki/Rope_(data_structure)
41#[derive(MallocSizeOf)]
42pub struct Rope {
43    /// The lines of the rope. Each line is an owned string that ends with a newline
44    /// (`\n`), apart from the last line which has no trailing newline.
45    lines: Vec<String>,
46}
47
48impl Rope {
49    pub fn new(contents: impl Into<String>) -> Self {
50        Self {
51            lines: contents_vec(contents),
52        }
53    }
54
55    pub fn contents(&self) -> String {
56        self.lines.join("")
57    }
58
59    pub fn last_index(&self) -> RopeIndex {
60        let line_index = self.lines.len() - 1;
61        RopeIndex::new(line_index, self.line(line_index).len())
62    }
63
64    /// Replace the given range of [`RopeIndex`]s with the given string. Returns the
65    /// [`RopeIndex`] of the end of the insertion.
66    pub fn replace_range(
67        &mut self,
68        mut range: Range<RopeIndex>,
69        string: impl Into<String>,
70    ) -> RopeIndex {
71        range.start = self.normalize_index(range.start);
72        range.end = self.normalize_index(range.end);
73        assert!(range.start <= range.end);
74
75        let start_index = range.start;
76        self.delete_range(range);
77
78        let mut new_contents = contents_vec(string);
79        let Some(first_line_of_new_contents) = new_contents.first() else {
80            return start_index;
81        };
82
83        if new_contents.len() == 1 {
84            self.line_for_index_mut(start_index)
85                .insert_str(start_index.code_point, first_line_of_new_contents);
86            return RopeIndex::new(
87                start_index.line,
88                start_index.code_point + first_line_of_new_contents.len(),
89            );
90        }
91
92        let start_line = self.line_for_index_mut(start_index);
93        let last_line = new_contents.last().expect("Should have at least one line");
94        let last_index = RopeIndex::new(
95            start_index.line + new_contents.len().saturating_sub(1),
96            last_line.len(),
97        );
98
99        let remaining_string = start_line.split_off(start_index.code_point);
100        start_line.push_str(first_line_of_new_contents);
101        new_contents
102            .last_mut()
103            .expect("Should have at least one line")
104            .push_str(&remaining_string);
105
106        let splice_index = start_index.line + 1;
107        self.lines
108            .splice(splice_index..splice_index, new_contents.into_iter().skip(1));
109        last_index
110    }
111
112    fn delete_range(&mut self, mut range: Range<RopeIndex>) {
113        range.start = self.normalize_index(range.start);
114        range.end = self.normalize_index(range.end);
115        assert!(range.start <= range.end);
116
117        if range.start.line == range.end.line {
118            self.line_for_index_mut(range.start)
119                .replace_range(range.start.code_point..range.end.code_point, "");
120            return;
121        }
122
123        // Remove the start line and any before the last line.
124        let removed_lines = self.lines.splice(range.start.line..range.end.line, []);
125        let first_line = removed_lines
126            .into_iter()
127            .nth(0)
128            .expect("Should have removed at least one line");
129
130        let first_line_prefix = &first_line[0..range.start.code_point];
131        let new_end_line = range.start.line;
132        self.lines[new_end_line].replace_range(0..range.end.code_point, first_line_prefix);
133    }
134
135    /// Create a [`RopeSlice`] for this [`Rope`] from `start` to `end`. If either of
136    /// these is `None`, then the slice will extend to the extent of the rope.
137    pub fn slice<'a>(&'a self, start: Option<RopeIndex>, end: Option<RopeIndex>) -> RopeSlice<'a> {
138        RopeSlice {
139            rope: self,
140            start: start.unwrap_or_default(),
141            end: end.unwrap_or_else(|| self.last_index()),
142        }
143    }
144
145    pub fn chars<'a>(&'a self) -> RopeChars<'a> {
146        self.slice(None, None).chars()
147    }
148
149    /// Return `true` if the [`Rope`] is empty or false otherwise. This will also
150    /// return `true` if the contents of the [`Rope`] are a single empty line.
151    pub fn is_empty(&self) -> bool {
152        self.lines.first().is_none_or(String::is_empty)
153    }
154
155    /// The total number of code units required to encode the content in utf16.
156    pub fn len_utf16(&self) -> Utf16CodeUnits {
157        Utf16CodeUnits(self.chars().map(char::len_utf16).sum())
158    }
159
160    fn line(&self, index: usize) -> &str {
161        &self.lines[index]
162    }
163
164    fn line_for_index(&self, index: RopeIndex) -> &String {
165        &self.lines[index.line]
166    }
167
168    fn line_for_index_mut(&mut self, index: RopeIndex) -> &mut String {
169        &mut self.lines[index.line]
170    }
171
172    fn last_index_in_line(&self, line: usize) -> RopeIndex {
173        if line >= self.lines.len() - 1 {
174            return self.last_index();
175        }
176        RopeIndex {
177            line,
178            code_point: self.line(line).len() - 1,
179        }
180    }
181
182    /// Return a [`RopeIndex`] which points to the start of the subsequent line.
183    /// If the given [`RopeIndex`] is already on the final line, this will return
184    /// the final index of the entire [`Rope`].
185    fn start_of_following_line(&self, index: RopeIndex) -> RopeIndex {
186        if index.line >= self.lines.len() - 1 {
187            return self.last_index();
188        }
189        RopeIndex::new(index.line + 1, 0)
190    }
191
192    /// Return a [`RopeIndex`] which points to the end of preceding line. If already
193    /// at the end of the first line, this will return the start index of the entire
194    /// [`Rope`].
195    fn end_of_preceding_line(&self, index: RopeIndex) -> RopeIndex {
196        if index.line == 0 {
197            return Default::default();
198        }
199        let line_index = index.line.saturating_sub(1);
200        RopeIndex::new(line_index, self.line(line_index).len())
201    }
202
203    pub fn move_by(&self, origin: RopeIndex, unit: RopeMovement, amount: isize) -> RopeIndex {
204        if amount == 0 {
205            return origin;
206        }
207
208        match unit {
209            RopeMovement::Character | RopeMovement::Grapheme | RopeMovement::Word => {
210                self.move_by_iterator(origin, unit, amount)
211            },
212            RopeMovement::Line => self.move_by_lines(origin, amount),
213            RopeMovement::LineStartOrEnd => {
214                if amount >= 0 {
215                    self.last_index_in_line(origin.line)
216                } else {
217                    RopeIndex::new(origin.line, 0)
218                }
219            },
220            RopeMovement::RopeStartOrEnd => {
221                if amount >= 0 {
222                    self.last_index()
223                } else {
224                    Default::default()
225                }
226            },
227        }
228    }
229
230    fn move_by_lines(&self, origin: RopeIndex, lines_to_move: isize) -> RopeIndex {
231        let new_line_index = (origin.line as isize) + lines_to_move;
232        if new_line_index < 0 {
233            return Default::default();
234        }
235        if new_line_index > (self.lines.len() - 1) as isize {
236            return self.last_index();
237        }
238
239        let new_line_index = new_line_index.unsigned_abs();
240        let char_count = self.line(origin.line)[0..origin.code_point].chars().count();
241        let new_code_point_index = self
242            .line(new_line_index)
243            .char_indices()
244            .take(char_count)
245            .last()
246            .map(|(byte_index, character)| byte_index + character.len_utf8())
247            .unwrap_or_default();
248        RopeIndex::new(new_line_index, new_code_point_index)
249            .min(self.last_index_in_line(new_line_index))
250    }
251
252    fn move_by_iterator(&self, origin: RopeIndex, unit: RopeMovement, amount: isize) -> RopeIndex {
253        assert_ne!(amount, 0);
254        let (boundary_value, slice) = if amount > 0 {
255            (self.last_index(), self.slice(Some(origin), None))
256        } else {
257            (RopeIndex::default(), self.slice(None, Some(origin)))
258        };
259
260        let iterator = match unit {
261            RopeMovement::Character => slice.char_indices(),
262            RopeMovement::Grapheme => slice.grapheme_indices(),
263            RopeMovement::Word => slice.word_indices(),
264            _ => unreachable!("Should not be called for other movement types"),
265        };
266        let iterator = if amount > 0 {
267            Either::Left(iterator)
268        } else {
269            Either::Right(iterator.rev())
270        };
271
272        let mut iterations = amount.unsigned_abs();
273        for mut index in iterator {
274            iterations = iterations.saturating_sub(1);
275            if iterations == 0 {
276                // Instead of returning offsets for the absolute end of a line, return the
277                // start offset for the next line.
278                if index.code_point >= self.line_for_index(index).len() {
279                    index = self.start_of_following_line(index);
280                }
281                return index;
282            }
283        }
284
285        boundary_value
286    }
287
288    /// Given a [`RopeIndex`], clamp it and ensure that it is on a character boundary,
289    /// meaning that its indices are all bound by the actual size of the line and the
290    /// number of lines in this [`Rope`].
291    pub fn normalize_index(&self, rope_index: RopeIndex) -> RopeIndex {
292        let last_line = self.lines.len().saturating_sub(1);
293        let line_index = rope_index.line.min(last_line);
294
295        // This may appear a bit odd as we are adding an index to the end of the line,
296        // but `RopeIndex` isn't just an offset to a UTF-8 code point, but also can
297        // serve as the end of an exclusive range so there is one more index at the end
298        // that is still valid.
299        //
300        // Lines other than the last line have a trailing newline. We do not want to allow
301        // an index past the trailing newline.
302        let line = self.line(line_index);
303        let line_length_utf8 = if line_index == last_line {
304            line.len()
305        } else {
306            line.len() - 1
307        };
308
309        let mut code_point = rope_index.code_point.min(line_length_utf8);
310        while code_point < line.len() && !line.is_char_boundary(code_point) {
311            code_point += 1;
312        }
313
314        RopeIndex::new(line_index, code_point)
315    }
316
317    /// Convert a [`RopeIndex`] into a byte offset from the start of the content.
318    pub fn index_to_utf8_offset(&self, rope_index: RopeIndex) -> Utf8CodeUnits {
319        let rope_index = self.normalize_index(rope_index);
320        Utf8CodeUnits(
321            self.lines
322                .iter()
323                .take(rope_index.line)
324                .map(String::len)
325                .sum::<usize>() +
326                rope_index.code_point,
327        )
328    }
329
330    pub fn index_to_utf16_offset(&self, rope_index: RopeIndex) -> Utf16CodeUnits {
331        let rope_index = self.normalize_index(rope_index);
332        let final_line = self.line(rope_index.line);
333
334        // The offset might be past the end of the line due to being an exclusive offset.
335        let final_line_offset = Utf16CodeUnits(
336            final_line[0..rope_index.code_point]
337                .chars()
338                .map(char::len_utf16)
339                .sum(),
340        );
341
342        self.lines
343            .iter()
344            .take(rope_index.line)
345            .map(|line| Utf16CodeUnits(line.chars().map(char::len_utf16).sum()))
346            .sum::<Utf16CodeUnits>() +
347            final_line_offset
348    }
349
350    /// Convert a [`RopeIndex`] into a character offset from the start of the content.
351    pub fn index_to_character_offset(&self, rope_index: RopeIndex) -> usize {
352        let rope_index = self.normalize_index(rope_index);
353
354        // The offset might be past the end of the line due to being an exclusive offset.
355        let final_line = self.line(rope_index.line);
356        let final_line_offset = final_line[0..rope_index.code_point].chars().count();
357        self.lines
358            .iter()
359            .take(rope_index.line)
360            .map(|line| line.chars().count())
361            .sum::<usize>() +
362            final_line_offset
363    }
364
365    /// Convert a byte offset from the start of the content into a [`RopeIndex`].
366    pub fn utf8_offset_to_rope_index(&self, utf8_offset: Utf8CodeUnits) -> RopeIndex {
367        let mut current_utf8_offset = utf8_offset.0;
368        for (line_index, line) in self.lines.iter().enumerate() {
369            if current_utf8_offset == 0 || current_utf8_offset < line.len() {
370                return RopeIndex::new(line_index, current_utf8_offset);
371            }
372            current_utf8_offset -= line.len();
373        }
374        self.last_index()
375    }
376
377    pub fn utf16_offset_to_utf8_offset(&self, utf16_offset: Utf16CodeUnits) -> Utf8CodeUnits {
378        let mut current_utf16_offset = Utf16CodeUnits::zero();
379        let mut current_utf8_offset = Utf8CodeUnits::zero();
380
381        for character in self.chars() {
382            let utf16_length = character.len_utf16();
383            if current_utf16_offset + Utf16CodeUnits(utf16_length) > utf16_offset {
384                return current_utf8_offset;
385            }
386            current_utf8_offset += Utf8CodeUnits(character.len_utf8());
387            current_utf16_offset += Utf16CodeUnits(utf16_length);
388        }
389        current_utf8_offset
390    }
391
392    /// Find the boundaries of the word most relevant to the given [`RopeIndex`]. Word
393    /// returned in order or precedence:
394    ///
395    /// - If the index intersects the word or is the index directly preceding a word,
396    ///   the boundaries of that word are returned.
397    /// - The word preceding the cursor.
398    /// - If there is no word preceding the cursor, the start of the line to the end
399    ///   of the next word.
400    pub fn relevant_word_boundaries<'a>(&'a self, index: RopeIndex) -> RopeSlice<'a> {
401        let line = self.line_for_index(index);
402        let mut result_start = 0;
403        let mut result_end = None;
404        for (word_start, word) in line.unicode_word_indices() {
405            if word_start > index.code_point {
406                result_end = result_end.or_else(|| Some(word_start + word.len()));
407                break;
408            }
409            result_start = word_start;
410            result_end = Some(word_start + word.len());
411        }
412
413        let result_end = result_end.unwrap_or(result_start);
414        self.slice(
415            Some(RopeIndex::new(index.line, result_start)),
416            Some(RopeIndex::new(index.line, result_end)),
417        )
418    }
419
420    /// Return the boundaries of the line that contains the given [`RopeIndex`].
421    pub fn line_boundaries<'a>(&'a self, index: RopeIndex) -> RopeSlice<'a> {
422        self.slice(
423            Some(RopeIndex::new(index.line, 0)),
424            Some(self.last_index_in_line(index.line)),
425        )
426    }
427
428    fn character_at(&self, index: RopeIndex) -> Option<char> {
429        let line = self.line_for_index(index);
430        line[index.code_point..].chars().next()
431    }
432
433    fn character_before(&self, index: RopeIndex) -> Option<char> {
434        let line = self.line_for_index(index);
435        line[..index.code_point].chars().next_back()
436    }
437}
438
439/// An index into a [`Rope`] data structure. Used to efficiently identify a particular
440/// position in a [`Rope`]. As [`Rope`] always uses Rust strings interally, code point
441/// indices represented in a [`RopeIndex`] are assumed to be UTF-8 code points (one byte
442/// each).
443///
444/// Note that it is possible for a [`RopeIndex`] to point past the end of the last line,
445/// as it can be used in exclusive ranges. In lines other than the last line, it should
446/// always refer to offsets before the trailing newline.
447#[derive(Clone, Copy, Debug, Default, Eq, MallocSizeOf, PartialEq, PartialOrd, Ord)]
448pub struct RopeIndex {
449    /// The index of the line that this [`RopeIndex`] refers to.
450    pub line: usize,
451    /// The index of the code point on the [`RopeIndex`]'s line in UTF-8 code
452    /// points.
453    ///
454    /// Note: This is not a `Utf8CodeUnits` in order to avoid continually having
455    /// to unpack the inner value.
456    pub code_point: usize,
457}
458
459impl RopeIndex {
460    pub fn new(line: usize, code_point: usize) -> Self {
461        Self { line, code_point }
462    }
463}
464
465/// A slice of a [`Rope`]. This can be used to to iterate over a subset of characters of a
466/// [`Rope`] or to return the content of the [`RopeSlice`] as a `String`.
467pub struct RopeSlice<'a> {
468    /// The underlying [`Rope`] of this [`RopeSlice`]
469    rope: &'a Rope,
470    /// The inclusive `RopeIndex` of the start of this [`RopeSlice`].
471    pub start: RopeIndex,
472    /// The exclusive end `RopeIndex` of this [`RopeSlice`].
473    pub end: RopeIndex,
474}
475
476impl From<RopeSlice<'_>> for String {
477    fn from(slice: RopeSlice<'_>) -> Self {
478        if slice.start.line == slice.end.line {
479            slice.rope.line_for_index(slice.start)[slice.start.code_point..slice.end.code_point]
480                .into()
481        } else {
482            once(&slice.rope.line_for_index(slice.start)[slice.start.code_point..])
483                .chain(
484                    (slice.start.line + 1..slice.end.line)
485                        .map(|line_index| slice.rope.line(line_index)),
486                )
487                .chain(once(
488                    &slice.rope.line_for_index(slice.end)[..slice.end.code_point],
489                ))
490                .collect()
491        }
492    }
493}
494
495impl<'a> RopeSlice<'a> {
496    pub fn chars(self) -> RopeChars<'a> {
497        RopeChars {
498            movement_iterator: RopeMovementIterator {
499                slice: self,
500                end_of_forward_motion: |_, string| {
501                    let (offset, character) = string.char_indices().next()?;
502                    Some(offset + character.len_utf8())
503                },
504                start_of_backward_motion: |_, string: &str| {
505                    Some(string.char_indices().next_back()?.0)
506                },
507            },
508        }
509    }
510
511    fn char_indices(self) -> RopeMovementIterator<'a> {
512        RopeMovementIterator {
513            slice: self,
514            end_of_forward_motion: |_, string| {
515                let (offset, character) = string.char_indices().next()?;
516                Some(offset + character.len_utf8())
517            },
518            start_of_backward_motion: |_, string: &str| Some(string.char_indices().next_back()?.0),
519        }
520    }
521
522    fn grapheme_indices(self) -> RopeMovementIterator<'a> {
523        RopeMovementIterator {
524            slice: self,
525            end_of_forward_motion: |_, string| {
526                let (offset, grapheme) = string.grapheme_indices(true).next()?;
527                Some(offset + grapheme.len())
528            },
529            start_of_backward_motion: |_, string| {
530                Some(string.grapheme_indices(true).next_back()?.0)
531            },
532        }
533    }
534
535    fn word_indices(self) -> RopeMovementIterator<'a> {
536        RopeMovementIterator {
537            slice: self,
538            end_of_forward_motion: |_, string| {
539                let (offset, word) = string.unicode_word_indices().next()?;
540                Some(offset + word.len())
541            },
542            start_of_backward_motion: |_, string| {
543                Some(string.unicode_word_indices().next_back()?.0)
544            },
545        }
546    }
547}
548
549/// A generic movement iterator for a [`Rope`]. This can move in both directions. Note
550/// than when moving forward and backward, the indices returned for each unit are
551/// different. When moving forward, the end of the unit of movement is returned and when
552/// moving backward the start of the unit of movement is returned. This matches the
553/// expected behavior when interactively moving through editable text.
554struct RopeMovementIterator<'a> {
555    slice: RopeSlice<'a>,
556    end_of_forward_motion: fn(&RopeSlice, &'a str) -> Option<usize>,
557    start_of_backward_motion: fn(&RopeSlice, &'a str) -> Option<usize>,
558}
559
560impl Iterator for RopeMovementIterator<'_> {
561    type Item = RopeIndex;
562
563    fn next(&mut self) -> Option<Self::Item> {
564        // If the two indices have crossed over, iteration is done.
565        if self.slice.start >= self.slice.end {
566            return None;
567        }
568
569        assert!(self.slice.start.line < self.slice.rope.lines.len());
570        let line = self.slice.rope.line_for_index(self.slice.start);
571
572        if self.slice.start.code_point < line.len() + 1 &&
573            let Some(end_offset) =
574                (self.end_of_forward_motion)(&self.slice, &line[self.slice.start.code_point..])
575        {
576            self.slice.start.code_point += end_offset;
577            return Some(self.slice.start);
578        }
579
580        // Advance the line as we are at the end of the line.
581        self.slice.start = self.slice.rope.start_of_following_line(self.slice.start);
582        self.next()
583    }
584}
585
586impl DoubleEndedIterator for RopeMovementIterator<'_> {
587    fn next_back(&mut self) -> Option<Self::Item> {
588        // If the two indices have crossed over, iteration is done.
589        if self.slice.end <= self.slice.start {
590            return None;
591        }
592
593        let line = self.slice.rope.line_for_index(self.slice.end);
594        if self.slice.end.code_point > 0 &&
595            let Some(new_start_index) =
596                (self.start_of_backward_motion)(&self.slice, &line[..self.slice.end.code_point])
597        {
598            self.slice.end.code_point = new_start_index;
599            return Some(self.slice.end);
600        }
601
602        // Decrease the line index as we are at the start of the line.
603        self.slice.end = self.slice.rope.end_of_preceding_line(self.slice.end);
604        self.next_back()
605    }
606}
607
608/// A `Chars`-like iterator for [`Rope`].
609pub struct RopeChars<'a> {
610    movement_iterator: RopeMovementIterator<'a>,
611}
612
613impl Iterator for RopeChars<'_> {
614    type Item = char;
615    fn next(&mut self) -> Option<Self::Item> {
616        self.movement_iterator
617            .next()
618            .and_then(|index| self.movement_iterator.slice.rope.character_before(index))
619    }
620}
621
622impl DoubleEndedIterator for RopeChars<'_> {
623    fn next_back(&mut self) -> Option<Self::Item> {
624        self.movement_iterator
625            .next_back()
626            .and_then(|index| self.movement_iterator.slice.rope.character_at(index))
627    }
628}
629
630#[test]
631fn test_rope_index_conversion_to_utf8_offset() {
632    let rope = Rope::new("A\nBB\nCCC\nDDDD");
633    assert_eq!(
634        rope.index_to_utf8_offset(RopeIndex::new(0, 0)),
635        Utf8CodeUnits(0),
636    );
637    assert_eq!(
638        rope.index_to_utf8_offset(RopeIndex::new(0, 1)),
639        Utf8CodeUnits(1),
640    );
641    assert_eq!(
642        rope.index_to_utf8_offset(RopeIndex::new(0, 10)),
643        Utf8CodeUnits(1),
644        "RopeIndex with offset past the end of the line should return final offset in line",
645    );
646    assert_eq!(
647        rope.index_to_utf8_offset(RopeIndex::new(1, 0)),
648        Utf8CodeUnits(2),
649    );
650    assert_eq!(
651        rope.index_to_utf8_offset(RopeIndex::new(1, 2)),
652        Utf8CodeUnits(4),
653    );
654
655    assert_eq!(
656        rope.index_to_utf8_offset(RopeIndex::new(3, 0)),
657        Utf8CodeUnits(9),
658    );
659    assert_eq!(
660        rope.index_to_utf8_offset(RopeIndex::new(3, 3)),
661        Utf8CodeUnits(12),
662    );
663    assert_eq!(
664        rope.index_to_utf8_offset(RopeIndex::new(3, 4)),
665        Utf8CodeUnits(13),
666        "There should be no newline at the end of the TextInput",
667    );
668    assert_eq!(
669        rope.index_to_utf8_offset(RopeIndex::new(3, 40)),
670        Utf8CodeUnits(13),
671        "There should be no newline at the end of the TextInput",
672    );
673}
674
675#[test]
676fn test_rope_index_conversion_to_utf16_offset() {
677    let rope = Rope::new("A\nBB\nCCC\n家家");
678    assert_eq!(
679        rope.index_to_utf16_offset(RopeIndex::new(0, 0)),
680        Utf16CodeUnits(0),
681    );
682    assert_eq!(
683        rope.index_to_utf16_offset(RopeIndex::new(0, 1)),
684        Utf16CodeUnits(1),
685    );
686    assert_eq!(
687        rope.index_to_utf16_offset(RopeIndex::new(0, 10)),
688        Utf16CodeUnits(1),
689        "RopeIndex with offset past the end of the line should return final offset in line",
690    );
691    assert_eq!(
692        rope.index_to_utf16_offset(RopeIndex::new(3, 0)),
693        Utf16CodeUnits(9),
694    );
695
696    assert_eq!(
697        rope.index_to_utf16_offset(RopeIndex::new(3, 3)),
698        Utf16CodeUnits(10),
699        "3 code unit UTF-8 encodede character"
700    );
701    assert_eq!(
702        rope.index_to_utf16_offset(RopeIndex::new(3, 6)),
703        Utf16CodeUnits(11),
704    );
705    assert_eq!(
706        rope.index_to_utf16_offset(RopeIndex::new(3, 20)),
707        Utf16CodeUnits(11),
708    );
709}
710
711#[test]
712fn test_utf16_offset_to_utf8_offset() {
713    let rope = Rope::new("A\nBB\nCCC\n家家");
714    assert_eq!(
715        rope.utf16_offset_to_utf8_offset(Utf16CodeUnits(0)),
716        Utf8CodeUnits(0),
717    );
718    assert_eq!(
719        rope.utf16_offset_to_utf8_offset(Utf16CodeUnits(1)),
720        Utf8CodeUnits(1),
721    );
722    assert_eq!(
723        rope.utf16_offset_to_utf8_offset(Utf16CodeUnits(2)),
724        Utf8CodeUnits(2),
725        "Offset past the end of the line",
726    );
727    assert_eq!(
728        rope.utf16_offset_to_utf8_offset(Utf16CodeUnits(9)),
729        Utf8CodeUnits(9),
730    );
731
732    assert_eq!(
733        rope.utf16_offset_to_utf8_offset(Utf16CodeUnits(10)),
734        Utf8CodeUnits(12),
735        "3 code unit UTF-8 encodede character"
736    );
737    assert_eq!(
738        rope.utf16_offset_to_utf8_offset(Utf16CodeUnits(11)),
739        Utf8CodeUnits(15),
740    );
741    assert_eq!(
742        rope.utf16_offset_to_utf8_offset(Utf16CodeUnits(300)),
743        Utf8CodeUnits(15),
744    );
745}
746
747#[test]
748fn test_rope_delete_slice() {
749    let mut rope = Rope::new("ABC\nDEF\n");
750    rope.delete_range(RopeIndex::new(0, 1)..RopeIndex::new(0, 2));
751    assert_eq!(rope.contents(), "AC\nDEF\n");
752
753    // Trying to delete beyond the last index of the line should note remove any trailing
754    // newlines from the rope.
755    let mut rope = Rope::new("ABC\nDEF\n");
756    rope.delete_range(RopeIndex::new(0, 3)..RopeIndex::new(0, 4));
757    assert_eq!(rope.lines, ["ABC\n", "DEF\n", ""]);
758
759    let mut rope = Rope::new("ABC\nDEF\n");
760    rope.delete_range(RopeIndex::new(0, 0)..RopeIndex::new(0, 4));
761    assert_eq!(rope.lines, ["\n", "DEF\n", ""]);
762
763    let mut rope = Rope::new("A\nBB\nCCC");
764    rope.delete_range(RopeIndex::new(0, 2)..RopeIndex::new(1, 0));
765    assert_eq!(rope.lines, ["ABB\n", "CCC"]);
766}
767
768#[test]
769fn test_rope_replace_slice() {
770    let mut rope = Rope::new("AAA\nBBB\nCCC");
771    rope.replace_range(RopeIndex::new(0, 1)..RopeIndex::new(0, 2), "x");
772    assert_eq!(rope.contents(), "AxA\nBBB\nCCC",);
773
774    let mut rope = Rope::new("A\nBB\nCCC");
775    rope.replace_range(RopeIndex::new(0, 2)..RopeIndex::new(1, 0), "D");
776    assert_eq!(rope.lines, ["ADBB\n", "CCC"]);
777
778    let mut rope = Rope::new("AAA\nBBB\nCCC\nDDD");
779    rope.replace_range(RopeIndex::new(0, 2)..RopeIndex::new(2, 1), "x");
780    assert_eq!(rope.lines, ["AAxCC\n", "DDD"]);
781}
782
783#[test]
784fn test_rope_relevant_word() {
785    let rope = Rope::new("AAA    BBB   CCC");
786    let boundaries = rope.relevant_word_boundaries(RopeIndex::new(0, 0));
787    assert_eq!(boundaries.start, RopeIndex::new(0, 0));
788    assert_eq!(boundaries.end, RopeIndex::new(0, 3));
789
790    // Choose previous word if starting on whitespace.
791    let boundaries = rope.relevant_word_boundaries(RopeIndex::new(0, 4));
792    assert_eq!(boundaries.start, RopeIndex::new(0, 0));
793    assert_eq!(boundaries.end, RopeIndex::new(0, 3));
794
795    // Choose next word if starting at word start.
796    let boundaries = rope.relevant_word_boundaries(RopeIndex::new(0, 7));
797    assert_eq!(boundaries.start, RopeIndex::new(0, 7));
798    assert_eq!(boundaries.end, RopeIndex::new(0, 10));
799
800    // Choose word if starting at in middle.
801    let boundaries = rope.relevant_word_boundaries(RopeIndex::new(0, 8));
802    assert_eq!(boundaries.start, RopeIndex::new(0, 7));
803    assert_eq!(boundaries.end, RopeIndex::new(0, 10));
804
805    // Choose start of line to end of first word if in whitespace at start of line.
806    let rope = Rope::new("         AAA    BBB   CCC");
807    let boundaries = rope.relevant_word_boundaries(RopeIndex::new(0, 3));
808    assert_eq!(boundaries.start, RopeIndex::new(0, 0));
809    assert_eq!(boundaries.end, RopeIndex::new(0, 12));
810
811    // Works properly if line is empty.
812    let rope = Rope::new("");
813    let boundaries = rope.relevant_word_boundaries(RopeIndex::new(0, 0));
814    assert_eq!(boundaries.start, RopeIndex::new(0, 0));
815    assert_eq!(boundaries.end, RopeIndex::new(0, 0));
816}
817
818#[test]
819fn test_rope_index_intersects_character() {
820    let rope = Rope::new("񉡚");
821    let rope_index = RopeIndex::new(0, 1);
822    assert_eq!(rope.normalize_index(rope_index), RopeIndex::new(0, 4));
823    assert_eq!(rope.index_to_utf16_offset(rope_index), Utf16CodeUnits(2));
824    assert_eq!(rope.index_to_utf8_offset(rope_index), Utf8CodeUnits(4));
825
826    let rope = Rope::new("abc\ndef");
827    assert_eq!(
828        rope.normalize_index(RopeIndex::new(0, 100)),
829        RopeIndex::new(0, 3),
830        "Normalizing index past end of line should just clamp to line length."
831    );
832    assert_eq!(
833        rope.normalize_index(RopeIndex::new(1, 100)),
834        RopeIndex::new(1, 3),
835        "Normalizing index past end of line should just clamp to line length."
836    );
837}