Skip to main content

wgpui_base/input/base/
rope_ext.rs

1use std::ops::Range;
2
3use ropey::{Rope, RopeSlice};
4use sum_tree::Bias;
5
6/// Parser-independent byte/row/column position used for incremental edits.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8pub struct Point {
9    pub row: usize,
10    pub column: usize,
11}
12
13impl Point {
14    pub fn new(row: usize, column: usize) -> Self {
15        Self { row, column }
16    }
17}
18
19/// Parser-independent description of an incremental text replacement.
20#[derive(Debug, Clone, Copy)]
21pub struct InputEdit {
22    pub start_byte: usize,
23    pub old_end_byte: usize,
24    pub new_end_byte: usize,
25    pub start_position: Point,
26    pub old_end_position: Point,
27    pub new_end_position: Point,
28}
29
30use lsp_types::Position;
31
32/// An iterator over the lines of a `Rope`.
33pub struct RopeLines<'a> {
34    rope: &'a Rope,
35    row: usize,
36    end_row: usize,
37}
38
39impl<'a> RopeLines<'a> {
40    /// Create a new `RopeLines` iterator.
41    pub fn new(rope: &'a Rope) -> Self {
42        let end_row = rope.lines_len();
43        Self {
44            row: 0,
45            end_row,
46            rope,
47        }
48    }
49}
50impl<'a> Iterator for RopeLines<'a> {
51    type Item = RopeSlice<'a>;
52
53    #[inline]
54    fn next(&mut self) -> Option<Self::Item> {
55        if self.row >= self.end_row {
56            return None;
57        }
58
59        let line = self.rope.slice_line(self.row);
60        self.row += 1;
61        Some(line)
62    }
63
64    #[inline]
65    fn nth(&mut self, n: usize) -> Option<Self::Item> {
66        self.row = self.row.saturating_add(n);
67        self.next()
68    }
69
70    #[inline]
71    fn size_hint(&self) -> (usize, Option<usize>) {
72        let len = self.end_row - self.row;
73        (len, Some(len))
74    }
75}
76
77impl std::iter::ExactSizeIterator for RopeLines<'_> {}
78impl std::iter::FusedIterator for RopeLines<'_> {}
79
80/// Byte-oriented editing and LF-only coordinates over stable Ropey storage.
81///
82/// These offsets differ from Ropey's inherent character-indexed edit methods.
83/// Positions count Unicode scalar values, points count UTF-8 bytes, and the
84/// explicit UTF-16 conversions count code units. Only `\n` starts a new row,
85/// including when another dependency enables Ropey's Unicode line features.
86/// Those additional features require scanning for LF boundaries instead of
87/// using Ropey's line index; byte and UTF-16 conversions retain indexed lookups.
88pub trait RopeExt {
89    /// Whether a byte offset lies on a UTF-8 boundary, including the end of the text.
90    fn is_char_boundary(&self, offset: usize) -> bool;
91    /// Start offset of the line at the given row (0-based) index.
92    ///
93    /// # Example
94    ///
95    /// ```
96    /// use wgpui_base::input::{Rope, RopeExt};
97    ///
98    /// let rope = Rope::from("Hello\nWorld\r\nThis is a test 中文\nRope");
99    /// assert_eq!(rope.line_start_offset(0), 0);
100    /// assert_eq!(rope.line_start_offset(1), 6);
101    /// ```
102    fn line_start_offset(&self, row: usize) -> usize;
103
104    /// Line the end offset (including `\n`) of the line at the given row (0-based) index.
105    ///
106    /// Return the end of the rope if the row is out of bounds.
107    ///
108    /// ```
109    /// use wgpui_base::input::{Rope, RopeExt};
110    /// let rope = Rope::from("Hello\nWorld\r\nThis is a test 中文\nRope");
111    /// assert_eq!(rope.line_end_offset(0), 5); // "Hello\n"
112    /// assert_eq!(rope.line_end_offset(1), 12); // "World\r\n"
113    /// ```
114    fn line_end_offset(&self, row: usize) -> usize;
115
116    /// Return a line slice at the given row (0-based) index. including `\r` if present, but not `\n`.
117    ///
118    /// ```
119    /// use wgpui_base::input::{Rope, RopeExt};
120    /// let rope = Rope::from("Hello\nWorld\r\nThis is a test 中文\nRope");
121    /// assert_eq!(rope.slice_line(0).to_string(), "Hello");
122    /// assert_eq!(rope.slice_line(1).to_string(), "World\r");
123    /// assert_eq!(rope.slice_line(2).to_string(), "This is a test 中文");
124    /// assert_eq!(rope.slice_line(6).to_string(), ""); // out of bounds
125    /// ```
126    fn slice_line(&self, row: usize) -> RopeSlice<'_>;
127
128    /// Return a slice of rows in the given range (0-based, end exclusive).
129    ///
130    /// If the range is out of bounds, it will be clamped to the valid range.
131    ///
132    /// ```
133    /// use wgpui_base::input::{Rope, RopeExt};
134    /// let rope = Rope::from("Hello\nWorld\r\nThis is a test 中文\nRope");
135    /// assert_eq!(rope.slice_lines(0..2).to_string(), "Hello\nWorld\r");
136    /// assert_eq!(rope.slice_lines(1..3).to_string(), "World\r\nThis is a test 中文");
137    /// assert_eq!(rope.slice_lines(2..5).to_string(), "This is a test 中文\nRope");
138    /// assert_eq!(rope.slice_lines(3..10).to_string(), "Rope");
139    /// assert_eq!(rope.slice_lines(5..10).to_string(), ""); // out of bounds
140    /// ```
141    fn slice_lines(&self, rows_range: Range<usize>) -> RopeSlice<'_>;
142
143    /// Return an iterator over all lines in the rope.
144    ///
145    /// Each line slice includes `\r` if present, but not `\n`.
146    ///
147    /// ```
148    /// use wgpui_base::input::{Rope, RopeExt};
149    /// let rope = Rope::from("Hello\nWorld\r\nThis is a test 中文\nRope");
150    /// let lines: Vec<_> = rope.iter_lines().map(|r| r.to_string()).collect();
151    /// assert_eq!(lines, vec!["Hello", "World\r", "This is a test 中文", "Rope"]);
152    /// ```
153    fn iter_lines(&self) -> RopeLines<'_>;
154
155    /// Return the number of lines in the rope.
156    ///
157    /// ```
158    /// use wgpui_base::input::{Rope, RopeExt};
159    /// let rope = Rope::from("Hello\nWorld\r\nThis is a test 中文\nRope");
160    /// assert_eq!(rope.lines_len(), 4);
161    /// ```
162    fn lines_len(&self) -> usize;
163
164    /// Return the byte length of the row (0-based), including `\r` if present, but not `\n`.
165    ///
166    /// If the row is out of bounds, return 0.
167    ///
168    /// ```
169    /// use wgpui_base::input::{Rope, RopeExt};
170    /// let rope = Rope::from("Hello\nWorld\r\nThis is a test 中文\nRope");
171    /// assert_eq!(rope.line_len(0), 5); // "Hello"
172    /// assert_eq!(rope.line_len(1), 6); // "World\r"
173    /// assert_eq!(rope.line_len(2), 21); // "This is a test 中文"
174    /// assert_eq!(rope.line_len(4), 0); // out of bounds
175    /// ```
176    fn line_len(&self, row: usize) -> usize;
177
178    /// Replace the text in the given byte range with new text.
179    ///
180    /// Out-of-bounds endpoints are clamped, and endpoints inside a character
181    /// expand to include that character.
182    ///
183    /// ```
184    /// use wgpui_base::input::{Rope, RopeExt};
185    /// let mut rope = Rope::from("Hello\nWorld\r\nThis is a test 中文\nRope");
186    /// rope.replace(6..11, "Universe");
187    /// assert_eq!(rope.to_string(), "Hello\nUniverse\r\nThis is a test 中文\nRope");
188    /// ```
189    fn replace(&mut self, range: Range<usize>, new_text: &str);
190
191    /// Get char at the given offset (byte).
192    ///
193    /// - If the offset is in the middle of a multi-byte character, return None.
194    /// - If the offset is out of bounds, return None.
195    fn char_at(&self, offset: usize) -> Option<char>;
196
197    /// Get the byte offset from the given line, column [`Position`] (0-based).
198    ///
199    /// The column is in characters.
200    fn position_to_offset(&self, line_col: &Position) -> usize;
201
202    /// Get the line, column [`Position`] (0-based) from the given byte offset.
203    ///
204    /// The column is in characters.
205    fn offset_to_position(&self, offset: usize) -> Position;
206
207    /// Get point (row, column) from the given byte offset.
208    ///
209    /// The column is in bytes.
210    fn offset_to_point(&self, offset: usize) -> Point;
211
212    /// Get byte offset from the given point (row, column).
213    ///
214    /// The column is 0-based in bytes.
215    fn point_to_offset(&self, point: Point) -> usize;
216
217    /// Get the word byte range at the given byte offset (0-based).
218    fn word_range(&self, offset: usize) -> Option<Range<usize>>;
219
220    /// Get word at the given byte offset (0-based).
221    fn word_at(&self, offset: usize) -> String;
222
223    /// Convert offset in UTF-16 to byte offset (0-based).
224    ///
225    /// Runs in O(log N) time.
226    fn offset_utf16_to_offset(&self, offset_utf16: usize) -> usize;
227
228    /// Convert byte offset (0-based) to offset in UTF-16.
229    ///
230    /// Runs in O(log N) time.
231    fn offset_to_offset_utf16(&self, offset: usize) -> usize;
232
233    /// Get a clipped offset (avoid in a char boundary).
234    ///
235    /// - If Bias::Left and inside the char boundary, return the ix - 1;
236    /// - If Bias::Right and in inside char boundary, return the ix + 1;
237    /// - Otherwise return the ix.
238    ///
239    /// ```
240    /// use wgpui_base::input::{Rope, RopeExt};
241    /// use sum_tree::Bias;
242    ///
243    /// let rope = Rope::from("Hello 中文🎉 test\nRope");
244    /// assert_eq!(rope.clip_offset(5, Bias::Left), 5);
245    /// // Inside multi-byte character '中' (3 bytes)
246    /// assert_eq!(rope.clip_offset(7, Bias::Left), 6);
247    /// assert_eq!(rope.clip_offset(7, Bias::Right), 9);
248    /// ```
249    fn clip_offset(&self, offset: usize, bias: Bias) -> usize;
250
251    /// Convert offset in characters to byte offset (0-based).
252    ///
253    /// Run in O(n) time.
254    ///
255    /// # Example
256    ///
257    /// ```
258    /// use wgpui_base::input::{Rope, RopeExt};
259    /// let rope = Rope::from("a 中文🎉 test\nRope");
260    /// assert_eq!(rope.char_index_to_offset(0), 0);
261    /// assert_eq!(rope.char_index_to_offset(1), 1);
262    /// assert_eq!(rope.char_index_to_offset(3), "a 中".len());
263    /// assert_eq!(rope.char_index_to_offset(5), "a 中文🎉".len());
264    /// ```
265    fn char_index_to_offset(&self, char_index: usize) -> usize;
266
267    /// Convert byte offset (0-based) to offset in characters.
268    ///
269    /// Run in O(n) time.
270    ///
271    /// # Example
272    ///
273    /// ```
274    /// use wgpui_base::input::{Rope, RopeExt};
275    /// let rope = Rope::from("a 中文🎉 test\nRope");
276    /// assert_eq!(rope.offset_to_char_index(0), 0);
277    /// assert_eq!(rope.offset_to_char_index(1), 1);
278    /// assert_eq!(rope.offset_to_char_index(3), 3);
279    /// assert_eq!(rope.offset_to_char_index(4), 3);
280    /// ```
281    fn offset_to_char_index(&self, offset: usize) -> usize;
282}
283
284impl RopeExt for Rope {
285    fn is_char_boundary(&self, offset: usize) -> bool {
286        offset == self.len_bytes()
287            || self
288                .get_byte(offset)
289                .is_some_and(|byte| byte & 0xc0 != 0x80)
290    }
291
292    fn slice_line(&self, row: usize) -> RopeSlice<'_> {
293        let start = self.line_start_offset(row);
294        let mut end = self.line_start_offset(row.saturating_add(1));
295        if end > start && self.get_byte(end - 1) == Some(b'\n') {
296            end -= 1;
297        }
298        self.byte_slice(start..end)
299    }
300
301    fn slice_lines(&self, rows_range: Range<usize>) -> RopeSlice<'_> {
302        let start = self.line_start_offset(rows_range.start);
303        let end = self.line_end_offset(rows_range.end.saturating_sub(1));
304        self.byte_slice(start..end)
305    }
306
307    fn iter_lines(&self) -> RopeLines<'_> {
308        RopeLines::new(&self)
309    }
310
311    fn line_len(&self, row: usize) -> usize {
312        self.slice_line(row).len_bytes()
313    }
314
315    fn line_start_offset(&self, row: usize) -> usize {
316        if native_lines_are_lf_only() {
317            return self.line_to_byte(row.min(self.len_lines()));
318        }
319
320        if row == 0 {
321            return 0;
322        }
323        let mut remaining = row;
324        let mut chunk_start = 0;
325        for chunk in self.chunks() {
326            for (index, byte) in chunk.bytes().enumerate() {
327                if byte == b'\n' {
328                    remaining -= 1;
329                    if remaining == 0 {
330                        return chunk_start + index + 1;
331                    }
332                }
333            }
334            chunk_start += chunk.len();
335        }
336        self.len_bytes()
337    }
338
339    fn offset_to_point(&self, offset: usize) -> Point {
340        let offset = self.clip_offset(offset, Bias::Left);
341        if native_lines_are_lf_only() {
342            let row = self.byte_to_line(offset);
343            return Point::new(row, offset - self.line_to_byte(row));
344        }
345
346        let mut row = 0;
347        let mut line_start = 0;
348        let mut chunk_start = 0;
349        for chunk in self.byte_slice(..offset).chunks() {
350            for (index, byte) in chunk.bytes().enumerate() {
351                if byte == b'\n' {
352                    row += 1;
353                    line_start = chunk_start + index + 1;
354                }
355            }
356            chunk_start += chunk.len();
357        }
358        let column = offset.saturating_sub(line_start);
359        Point::new(row, column)
360    }
361
362    fn point_to_offset(&self, point: Point) -> usize {
363        if point.row >= self.lines_len() {
364            return self.len_bytes();
365        }
366        let line_start = self.line_start_offset(point.row);
367        line_start.saturating_add(point.column)
368    }
369
370    fn position_to_offset(&self, pos: &Position) -> usize {
371        let line = self.slice_line(pos.line as usize);
372        // Clamp out-of-range columns, then use Ropey's index to avoid rescanning long lines.
373        self.line_start_offset(pos.line as usize)
374            + line.char_to_byte((pos.character as usize).min(line.len_chars()))
375    }
376
377    fn offset_to_position(&self, offset: usize) -> Position {
378        let point = self.offset_to_point(offset);
379        let line = self.slice_line(point.row);
380        let character = line.byte_to_char(point.column);
381        Position::new(point.row as u32, character as u32)
382    }
383
384    fn line_end_offset(&self, row: usize) -> usize {
385        if row > self.lines_len() {
386            return self.len_bytes();
387        }
388
389        self.line_start_offset(row) + self.line_len(row)
390    }
391
392    fn lines_len(&self) -> usize {
393        if native_lines_are_lf_only() {
394            self.len_lines()
395        } else {
396            self.bytes().filter(|byte| *byte == b'\n').count() + 1
397        }
398    }
399
400    fn char_at(&self, offset: usize) -> Option<char> {
401        if !self.is_char_boundary(offset) {
402            return None;
403        }
404        self.get_char(self.byte_to_char(offset))
405    }
406
407    fn word_range(&self, offset: usize) -> Option<Range<usize>> {
408        if offset >= self.len_bytes() {
409            return None;
410        }
411
412        let mut left = String::new();
413        let offset = self.clip_offset(offset, Bias::Left);
414        for c in self.chars_at(self.byte_to_char(offset)).reversed() {
415            if c.is_alphanumeric() || c == '_' {
416                left.insert(0, c);
417            } else {
418                break;
419            }
420        }
421        let start = offset.saturating_sub(left.len());
422
423        let right = self
424            .chars_at(self.byte_to_char(offset))
425            .take_while(|c| c.is_alphanumeric() || *c == '_')
426            .collect::<String>();
427
428        let end = offset + right.len();
429
430        if start == end { None } else { Some(start..end) }
431    }
432
433    fn word_at(&self, offset: usize) -> String {
434        if let Some(range) = self.word_range(offset) {
435            self.byte_slice(range).to_string()
436        } else {
437            String::new()
438        }
439    }
440
441    #[inline]
442    fn offset_utf16_to_offset(&self, offset_utf16: usize) -> usize {
443        let offset_utf16 = offset_utf16.min(self.len_utf16_cu());
444        self.char_to_byte(self.utf16_cu_to_char(offset_utf16))
445    }
446
447    #[inline]
448    fn offset_to_offset_utf16(&self, offset: usize) -> usize {
449        self.char_to_utf16_cu(self.byte_to_char(offset.min(self.len_bytes())))
450    }
451
452    fn replace(&mut self, range: Range<usize>, new_text: &str) {
453        let start = self.byte_to_char(self.clip_offset(range.start, Bias::Left));
454        let end = self.byte_to_char(self.clip_offset(range.end, Bias::Right));
455        self.remove(start..end);
456        self.insert(start, new_text);
457    }
458
459    fn clip_offset(&self, offset: usize, bias: Bias) -> usize {
460        let offset = offset.min(self.len_bytes());
461        let character = self.byte_to_char(offset);
462        let boundary = self.char_to_byte(character);
463        if bias == Bias::Right && boundary < offset {
464            self.char_to_byte(character + 1)
465        } else {
466            boundary
467        }
468    }
469
470    fn char_index_to_offset(&self, char_offset: usize) -> usize {
471        self.char_to_byte(char_offset.min(self.len_chars()))
472    }
473
474    fn offset_to_char_index(&self, offset: usize) -> usize {
475        let offset = self.clip_offset(offset, Bias::Right);
476        self.byte_to_char(offset)
477    }
478}
479
480fn native_lines_are_lf_only() -> bool {
481    // Downstream feature unification can enable Ropey's extra line metrics.
482    // Use its indexed operations only when they match the editor's LF contract.
483    static LF_ONLY: std::sync::LazyLock<bool> = std::sync::LazyLock::new(|| {
484        Rope::from("\r\u{b}\u{c}\u{85}\u{2028}\u{2029}").len_lines() == 1
485    });
486    *LF_ONLY
487}
488
489#[cfg(test)]
490mod tests {
491    use super::Point;
492    use ropey::Rope;
493    use sum_tree::Bias;
494
495    use crate::input::{Position, RopeExt};
496
497    #[test]
498    fn test_slice_line() {
499        let rope = Rope::from("Hello\nWorld\r\nThis is a test 中文\nRope");
500        assert_eq!(rope.slice_line(0).to_string(), "Hello");
501        assert_eq!(rope.slice_line(1).to_string(), "World\r");
502        assert_eq!(rope.slice_line(2).to_string(), "This is a test 中文");
503        assert_eq!(rope.slice_line(3).to_string(), "Rope");
504
505        // over bounds
506        assert_eq!(rope.slice_line(6).to_string(), "");
507
508        // only have \r end
509        let rope = Rope::from("Hello\r");
510        assert_eq!(rope.slice_line(0).to_string(), "Hello\r");
511        assert_eq!(rope.slice_line(1).to_string(), "");
512    }
513
514    #[test]
515    fn test_lines_len() {
516        let rope = Rope::from("Hello\nWorld\r\nThis is a test 中文\nRope");
517        assert_eq!(rope.lines_len(), 4);
518        let rope = Rope::from("");
519        assert_eq!(rope.lines_len(), 1);
520        let rope = Rope::from("Single line");
521        assert_eq!(rope.lines_len(), 1);
522
523        // only have \r end
524        let rope = Rope::from("Hello\r");
525        assert_eq!(rope.lines_len(), 1);
526    }
527
528    #[test]
529    fn test_lines() {
530        let rope = Rope::from("Hello\nWorld\r\nThis is a test 中文\nRope\r");
531        let lines: Vec<_> = rope.iter_lines().map(|r| r.to_string()).collect();
532        assert_eq!(
533            lines,
534            vec!["Hello", "World\r", "This is a test 中文", "Rope\r"]
535        );
536    }
537
538    #[test]
539    fn test_eq() {
540        let rope = Rope::from("Hello\nWorld\r\nThis is a test 中文\nRope");
541        assert!(rope.eq(&Rope::from("Hello\nWorld\r\nThis is a test 中文\nRope")));
542        assert!(!rope.eq(&Rope::from("Hello\nWorld")));
543
544        let rope1 = rope.clone();
545        assert!(rope.eq(&rope1));
546    }
547
548    #[test]
549    fn test_iter_lines() {
550        let rope = Rope::from("Hello\nWorld\r\nThis is a test 中文\nRope");
551        let lines: Vec<_> = rope
552            .iter_lines()
553            .skip(1)
554            .take(2)
555            .map(|r| r.to_string())
556            .collect();
557        assert_eq!(lines, vec!["World\r", "This is a test 中文"]);
558    }
559
560    #[test]
561    fn test_line_start_end_offset() {
562        let rope = Rope::from("Hello\nWorld\r\nThis is a test 中文\nRope");
563        assert_eq!(rope.line_start_offset(0), 0);
564        assert_eq!(rope.line_end_offset(0), 5);
565
566        assert_eq!(rope.line_start_offset(1), 6);
567        assert_eq!(rope.line_end_offset(1), 12);
568
569        assert_eq!(rope.line_start_offset(2), 13);
570        assert_eq!(rope.line_end_offset(2), 34);
571
572        assert_eq!(rope.line_start_offset(3), 35);
573        assert_eq!(rope.line_end_offset(3), 39);
574
575        assert_eq!(rope.line_start_offset(4), 39);
576        assert_eq!(rope.line_end_offset(4), 39);
577    }
578
579    #[test]
580    fn test_line_column() {
581        let rope = Rope::from("a 中文🎉 test\nRope");
582        assert_eq!(rope.position_to_offset(&Position::new(0, 3)), "a 中".len());
583        assert_eq!(
584            rope.position_to_offset(&Position::new(0, 5)),
585            "a 中文🎉".len()
586        );
587        assert_eq!(
588            rope.position_to_offset(&Position::new(1, 1)),
589            "a 中文🎉 test\nR".len()
590        );
591        assert_eq!(
592            rope.position_to_offset(&Position::new(0, u32::MAX)),
593            "a 中文🎉 test".len()
594        );
595
596        assert_eq!(
597            rope.offset_to_position("a 中文🎉 test\nR".len()),
598            Position::new(1, 1)
599        );
600        assert_eq!(
601            rope.offset_to_position("a 中文🎉".len()),
602            Position::new(0, 5)
603        );
604    }
605
606    #[test]
607    fn test_offset_to_point() {
608        let rope = Rope::from("a 中文🎉 test\nRope");
609        assert_eq!(rope.offset_to_point(0), Point::new(0, 0));
610        assert_eq!(rope.offset_to_point(1), Point::new(0, 1));
611        assert_eq!(rope.offset_to_point("a 中".len()), Point::new(0, 5));
612        assert_eq!(rope.offset_to_point("a 中文🎉".len()), Point::new(0, 12));
613        assert_eq!(
614            rope.offset_to_point("a 中文🎉 test\nR".len()),
615            Point::new(1, 1)
616        );
617    }
618
619    #[test]
620    fn test_point_to_offset() {
621        let rope = Rope::from("a 中文🎉 test\nRope");
622        assert_eq!(rope.point_to_offset(Point::new(0, 0)), 0);
623        assert_eq!(rope.point_to_offset(Point::new(0, 1)), 1);
624        assert_eq!(rope.point_to_offset(Point::new(0, 5)), "a 中".len());
625        assert_eq!(rope.point_to_offset(Point::new(0, 12)), "a 中文🎉".len());
626        assert_eq!(
627            rope.point_to_offset(Point::new(1, 1)),
628            "a 中文🎉 test\nR".len()
629        );
630    }
631
632    #[test]
633    fn test_char_at() {
634        let rope = Rope::from("Hello\nWorld\r\nThis is a test 中文🎉\nRope");
635        assert_eq!(rope.char_at(0), Some('H'));
636        assert_eq!(rope.char_at(5), Some('\n'));
637        assert_eq!(rope.char_at(13), Some('T'));
638        assert_eq!(rope.char_at(28), Some('中'));
639        assert_eq!(rope.char_at(34), Some('🎉'));
640        assert_eq!(rope.char_at(38), Some('\n'));
641        assert_eq!(rope.char_at(50), None);
642    }
643
644    #[test]
645    fn test_word_at() {
646        let rope = Rope::from("Hello\nWorld\r\nThis is a test 中文 世界\nRope");
647        assert_eq!(rope.word_at(0), "Hello");
648        assert_eq!(rope.word_range(0), Some(0..5));
649        assert_eq!(rope.word_at(8), "World");
650        assert_eq!(rope.word_range(8), Some(6..11));
651        assert_eq!(rope.word_at(12), "");
652        assert_eq!(rope.word_range(12), None);
653        assert_eq!(rope.word_at(13), "This");
654        assert_eq!(rope.word_range(13), Some(13..17));
655        assert_eq!(rope.word_at(31), "中文");
656        assert_eq!(rope.word_range(31), Some(28..34));
657        assert_eq!(rope.word_at(38), "世界");
658        assert_eq!(rope.word_range(38), Some(35..41));
659        assert_eq!(rope.word_at(44), "Rope");
660        assert_eq!(rope.word_range(44), Some(42..46));
661        assert_eq!(rope.word_at(45), "Rope");
662    }
663
664    #[test]
665    fn test_offset_utf16_conversion() {
666        let rope = Rope::from("hello 中文🎉 test\nRope");
667        assert_eq!(rope.offset_to_offset_utf16("hello".len()), 5);
668        assert_eq!(rope.offset_to_offset_utf16("hello 中".len()), 7);
669        assert_eq!(rope.offset_to_offset_utf16("hello 中文".len()), 8);
670        assert_eq!(rope.offset_to_offset_utf16("hello 中文🎉".len()), 10);
671        assert_eq!(rope.offset_to_offset_utf16(100), 20);
672
673        assert_eq!(rope.offset_utf16_to_offset(5), "hello".len());
674        assert_eq!(rope.offset_utf16_to_offset(7), "hello 中".len());
675        assert_eq!(rope.offset_utf16_to_offset(8), "hello 中文".len());
676        assert_eq!(rope.offset_utf16_to_offset(10), "hello 中文🎉".len());
677        assert_eq!(rope.offset_utf16_to_offset(100), rope.len_bytes());
678    }
679
680    #[test]
681    fn test_replace() {
682        let mut rope = Rope::from("Hello\nWorld\r\nThis is a test 中文\nRope");
683        rope.replace(6..11, "Universe");
684        assert_eq!(
685            rope.to_string(),
686            "Hello\nUniverse\r\nThis is a test 中文\nRope"
687        );
688
689        rope.replace(0..5, "Hi");
690        assert_eq!(
691            rope.to_string(),
692            "Hi\nUniverse\r\nThis is a test 中文\nRope"
693        );
694
695        rope.replace(rope.len_bytes() - 4..rope.len_bytes(), "String");
696        assert_eq!(
697            rope.to_string(),
698            "Hi\nUniverse\r\nThis is a test 中文\nString"
699        );
700
701        // Test for not on a char boundary
702        let mut rope = Rope::from("中文");
703        rope.replace(0..1, "New");
704        // autocorrect-disable
705        assert_eq!(rope.to_string(), "New文");
706        let mut rope = Rope::from("中文");
707        rope.replace(0..2, "New");
708        assert_eq!(rope.to_string(), "New文");
709        let mut rope = Rope::from("中文");
710        rope.replace(0..3, "New");
711        assert_eq!(rope.to_string(), "New文");
712        // autocorrect-enable
713        let mut rope = Rope::from("中文");
714        rope.replace(1..4, "New");
715        assert_eq!(rope.to_string(), "New");
716    }
717
718    #[test]
719    fn test_clip_offset() {
720        let rope = Rope::from("Hello 中文🎉 test\nRope");
721        // Inside multi-byte character '中' (3 bytes)
722        assert_eq!(rope.clip_offset(5, Bias::Left), 5);
723        assert_eq!(rope.clip_offset(7, Bias::Left), 6);
724        assert_eq!(rope.clip_offset(7, Bias::Right), 9);
725        assert_eq!(rope.clip_offset(9, Bias::Left), 9);
726
727        // Inside multi-byte character '🎉' (4 bytes)
728        assert_eq!(rope.clip_offset(13, Bias::Left), 12);
729        assert_eq!(rope.clip_offset(13, Bias::Right), 16);
730        assert_eq!(rope.clip_offset(16, Bias::Left), 16);
731
732        // At character boundary
733        assert_eq!(rope.clip_offset(5, Bias::Left), 5);
734        assert_eq!(rope.clip_offset(5, Bias::Right), 5);
735
736        // Out of bounds
737        assert_eq!(rope.clip_offset(26, Bias::Left), 26);
738        assert_eq!(rope.clip_offset(100, Bias::Left), 26);
739    }
740
741    #[test]
742    fn test_char_index_to_offset() {
743        let rope = Rope::from("a 中文🎉 test\nRope");
744        assert_eq!(rope.char_index_to_offset(0), 0);
745        assert_eq!(rope.char_index_to_offset(1), 1);
746        assert_eq!(rope.char_index_to_offset(3), "a 中".len());
747        assert_eq!(rope.char_index_to_offset(5), "a 中文🎉".len());
748        assert_eq!(rope.char_index_to_offset(6), "a 中文🎉 ".len());
749
750        assert_eq!(rope.offset_to_char_index(0), 0);
751        assert_eq!(rope.offset_to_char_index(1), 1);
752        assert_eq!(rope.offset_to_char_index(3), 3);
753        assert_eq!(rope.offset_to_char_index(4), 3);
754        assert_eq!(rope.offset_to_char_index(5), 3);
755        assert_eq!(rope.offset_to_char_index(6), 4);
756        assert_eq!(rope.offset_to_char_index(10), 5);
757    }
758
759    #[test]
760    fn test_lf_coordinates_ignore_other_unicode_line_breaks() {
761        let source = "a\rb\u{b}c\u{c}d\u{85}e\u{2028}f\u{2029}g\n中\r\n🎉\n";
762        let rope = Rope::from(source);
763        let expected_lines: Vec<_> = source.split('\n').collect();
764        assert_eq!(rope.lines_len(), expected_lines.len());
765        assert_eq!(
766            rope.iter_lines()
767                .map(|line| line.to_string())
768                .collect::<Vec<_>>(),
769            expected_lines
770        );
771
772        let mut line_start = 0;
773        for (row, line) in expected_lines.iter().enumerate() {
774            assert_eq!(rope.line_start_offset(row), line_start);
775            assert_eq!(rope.line_end_offset(row), line_start + line.len());
776            assert_eq!(rope.line_len(row), line.len());
777            line_start += line.len() + 1;
778        }
779
780        for offset in 0..=source.len() {
781            let mut clipped = offset;
782            while !source.is_char_boundary(clipped) {
783                clipped -= 1;
784            }
785            let prefix = &source[..clipped];
786            let row = prefix.bytes().filter(|byte| *byte == b'\n').count();
787            let line_start = prefix.rfind('\n').map_or(0, |index| index + 1);
788            let point = Point::new(row, clipped - line_start);
789            assert_eq!(
790                rope.is_char_boundary(offset),
791                source.is_char_boundary(offset)
792            );
793            assert_eq!(rope.offset_to_point(offset), point);
794            assert_eq!(rope.point_to_offset(point), clipped);
795        }
796        assert_eq!(rope.slice_line(usize::MAX).to_string(), "");
797    }
798
799    #[test]
800    fn test_byte_edits_and_utf16_roundtrips_across_chunks() {
801        let source = "a中🎉e\u{301}\r\n".repeat(400);
802        let mut rope = Rope::from(source.as_str());
803        assert!(rope.chunks().count() > 1);
804        for (character, (offset, _)) in source.char_indices().enumerate() {
805            let utf16 = source[..offset].encode_utf16().count();
806            assert_eq!(rope.char_index_to_offset(character), offset);
807            assert_eq!(rope.offset_to_char_index(offset), character);
808            assert_eq!(rope.offset_to_offset_utf16(offset), utf16);
809            assert_eq!(rope.offset_utf16_to_offset(utf16), offset);
810        }
811        assert_eq!(rope.offset_utf16_to_offset(3), "a中".len());
812        rope.replace(2..6, "é");
813        assert_eq!(
814            rope.to_string(),
815            format!("aée\u{301}\r\n{}", "a中🎉e\u{301}\r\n".repeat(399))
816        );
817        assert_eq!(
818            rope.offset_to_point("aée\u{301}\r\n".len()),
819            Point::new(1, 0)
820        );
821    }
822}