1use crate::PointUtf16;
2
3use super::Point;
4use arrayvec::ArrayString;
5use smallvec::SmallVec;
6use std::{cmp, fmt, io, mem, ops::Range, str};
7use sum_tree::{Bias, Dimension, SumTree};
8
9#[cfg(test)]
10const CHUNK_BASE: usize = 6;
11
12#[cfg(not(test))]
13const CHUNK_BASE: usize = 16;
14
15#[derive(Clone, Default, Debug)]
16pub struct Rope {
17 chunks: SumTree<Chunk>,
18}
19
20impl Rope {
21 pub fn new() -> Self {
22 Self::default()
23 }
24
25 pub fn append(&mut self, rope: Rope) {
26 let mut chunks = rope.chunks.cursor::<()>();
27 chunks.next(&());
28 if let Some(chunk) = chunks.item() {
29 if self.chunks.last().map_or(false, |c| c.0.len() < CHUNK_BASE)
30 || chunk.0.len() < CHUNK_BASE
31 {
32 self.push(&chunk.0);
33 chunks.next(&());
34 }
35 }
36
37 self.chunks.push_tree(chunks.suffix(&()), &());
38 self.check_invariants();
39 }
40
41 pub fn replace(&mut self, range: Range<usize>, text: &str) {
42 let mut new_rope = Rope::new();
43 let mut cursor = self.cursor(0);
44 new_rope.append(cursor.slice(range.start));
45 cursor.seek_forward(range.end);
46 new_rope.push(text);
47 new_rope.append(cursor.suffix());
48 *self = new_rope;
49 }
50
51 pub fn push(&mut self, text: &str) {
52 let mut new_chunks = SmallVec::<[_; 16]>::new();
53 let mut new_chunk = ArrayString::new();
54 for ch in text.chars() {
55 if new_chunk.len() + ch.len_utf8() > 2 * CHUNK_BASE {
56 new_chunks.push(Chunk(new_chunk));
57 new_chunk = ArrayString::new();
58 }
59 new_chunk.push(ch);
60 }
61 if !new_chunk.is_empty() {
62 new_chunks.push(Chunk(new_chunk));
63 }
64
65 let mut new_chunks = new_chunks.into_iter();
66 let mut first_new_chunk = new_chunks.next();
67 self.chunks.update_last(
68 |last_chunk| {
69 if let Some(first_new_chunk_ref) = first_new_chunk.as_mut() {
70 if last_chunk.0.len() + first_new_chunk_ref.0.len() <= 2 * CHUNK_BASE {
71 last_chunk.0.push_str(&first_new_chunk.take().unwrap().0);
72 } else {
73 let mut text = ArrayString::<{ 4 * CHUNK_BASE }>::new();
74 text.push_str(&last_chunk.0);
75 text.push_str(&first_new_chunk_ref.0);
76 let (left, right) = text.split_at(find_split_ix(&text));
77 last_chunk.0.clear();
78 last_chunk.0.push_str(left);
79 first_new_chunk_ref.0.clear();
80 first_new_chunk_ref.0.push_str(right);
81 }
82 }
83 },
84 &(),
85 );
86
87 self.chunks
88 .extend(first_new_chunk.into_iter().chain(new_chunks), &());
89 self.check_invariants();
90 }
91
92 pub fn push_front(&mut self, text: &str) {
93 let suffix = mem::replace(self, Rope::from(text));
94 self.append(suffix);
95 }
96
97 fn check_invariants(&self) {
98 #[cfg(test)]
99 {
100 // Ensure all chunks except maybe the last one are not underflowing.
101 // Allow some wiggle room for multibyte characters at chunk boundaries.
102 let mut chunks = self.chunks.cursor::<()>().peekable();
103 while let Some(chunk) = chunks.next() {
104 if chunks.peek().is_some() {
105 assert!(chunk.0.len() + 3 >= CHUNK_BASE);
106 }
107 }
108 }
109 }
110
111 pub fn summary(&self) -> TextSummary {
112 self.chunks.summary()
113 }
114
115 pub fn len(&self) -> usize {
116 self.chunks.extent(&())
117 }
118
119 pub fn max_point(&self) -> Point {
120 self.chunks.extent(&())
121 }
122
123 pub fn cursor(&self, offset: usize) -> Cursor {
124 Cursor::new(self, offset)
125 }
126
127 pub fn chars(&self) -> impl Iterator<Item = char> + '_ {
128 self.chars_at(0)
129 }
130
131 pub fn chars_at(&self, start: usize) -> impl Iterator<Item = char> + '_ {
132 self.chunks_in_range(start..self.len()).flat_map(str::chars)
133 }
134
135 pub fn reversed_chars_at(&self, start: usize) -> impl Iterator<Item = char> + '_ {
136 self.reversed_chunks_in_range(0..start)
137 .flat_map(|chunk| chunk.chars().rev())
138 }
139
140 pub fn bytes_in_range(&self, range: Range<usize>) -> Bytes {
141 Bytes::new(self, range)
142 }
143
144 pub fn chunks<'a>(&'a self) -> Chunks<'a> {
145 self.chunks_in_range(0..self.len())
146 }
147
148 pub fn chunks_in_range(&self, range: Range<usize>) -> Chunks {
149 Chunks::new(self, range, false)
150 }
151
152 pub fn reversed_chunks_in_range(&self, range: Range<usize>) -> Chunks {
153 Chunks::new(self, range, true)
154 }
155
156 pub fn offset_to_point(&self, offset: usize) -> Point {
157 if offset >= self.summary().bytes {
158 return self.summary().lines;
159 }
160 let mut cursor = self.chunks.cursor::<(usize, Point)>();
161 cursor.seek(&offset, Bias::Left, &());
162 let overshoot = offset - cursor.start().0;
163 cursor.start().1
164 + cursor
165 .item()
166 .map_or(Point::zero(), |chunk| chunk.offset_to_point(overshoot))
167 }
168
169 pub fn offset_to_point_utf16(&self, offset: usize) -> PointUtf16 {
170 if offset >= self.summary().bytes {
171 return self.summary().lines_utf16;
172 }
173 let mut cursor = self.chunks.cursor::<(usize, PointUtf16)>();
174 cursor.seek(&offset, Bias::Left, &());
175 let overshoot = offset - cursor.start().0;
176 cursor.start().1
177 + cursor.item().map_or(PointUtf16::zero(), |chunk| {
178 chunk.offset_to_point_utf16(overshoot)
179 })
180 }
181
182 pub fn point_to_offset(&self, point: Point) -> usize {
183 if point >= self.summary().lines {
184 return self.summary().bytes;
185 }
186 let mut cursor = self.chunks.cursor::<(Point, usize)>();
187 cursor.seek(&point, Bias::Left, &());
188 let overshoot = point - cursor.start().0;
189 cursor.start().1
190 + cursor
191 .item()
192 .map_or(0, |chunk| chunk.point_to_offset(overshoot))
193 }
194
195 pub fn point_utf16_to_offset(&self, point: PointUtf16) -> usize {
196 if point >= self.summary().lines_utf16 {
197 return self.summary().bytes;
198 }
199 let mut cursor = self.chunks.cursor::<(PointUtf16, usize)>();
200 cursor.seek(&point, Bias::Left, &());
201 let overshoot = point - cursor.start().0;
202 cursor.start().1
203 + cursor
204 .item()
205 .map_or(0, |chunk| chunk.point_utf16_to_offset(overshoot))
206 }
207
208 pub fn point_utf16_to_point(&self, point: PointUtf16) -> Point {
209 if point >= self.summary().lines_utf16 {
210 return self.summary().lines;
211 }
212 let mut cursor = self.chunks.cursor::<(PointUtf16, Point)>();
213 cursor.seek(&point, Bias::Left, &());
214 let overshoot = point - cursor.start().0;
215 cursor.start().1
216 + cursor
217 .item()
218 .map_or(Point::zero(), |chunk| chunk.point_utf16_to_point(overshoot))
219 }
220
221 pub fn clip_offset(&self, mut offset: usize, bias: Bias) -> usize {
222 let mut cursor = self.chunks.cursor::<usize>();
223 cursor.seek(&offset, Bias::Left, &());
224 if let Some(chunk) = cursor.item() {
225 let mut ix = offset - cursor.start();
226 while !chunk.0.is_char_boundary(ix) {
227 match bias {
228 Bias::Left => {
229 ix -= 1;
230 offset -= 1;
231 }
232 Bias::Right => {
233 ix += 1;
234 offset += 1;
235 }
236 }
237 }
238 offset
239 } else {
240 self.summary().bytes
241 }
242 }
243
244 pub fn clip_point(&self, point: Point, bias: Bias) -> Point {
245 let mut cursor = self.chunks.cursor::<Point>();
246 cursor.seek(&point, Bias::Right, &());
247 if let Some(chunk) = cursor.item() {
248 let overshoot = point - cursor.start();
249 *cursor.start() + chunk.clip_point(overshoot, bias)
250 } else {
251 self.summary().lines
252 }
253 }
254
255 pub fn clip_point_utf16(&self, point: PointUtf16, bias: Bias) -> PointUtf16 {
256 let mut cursor = self.chunks.cursor::<PointUtf16>();
257 cursor.seek(&point, Bias::Right, &());
258 if let Some(chunk) = cursor.item() {
259 let overshoot = point - cursor.start();
260 *cursor.start() + chunk.clip_point_utf16(overshoot, bias)
261 } else {
262 self.summary().lines_utf16
263 }
264 }
265
266 pub fn line_len(&self, row: u32) -> u32 {
267 self.clip_point(Point::new(row, u32::MAX), Bias::Left)
268 .column
269 }
270}
271
272impl<'a> From<&'a str> for Rope {
273 fn from(text: &'a str) -> Self {
274 let mut rope = Self::new();
275 rope.push(text);
276 rope
277 }
278}
279
280impl fmt::Display for Rope {
281 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
282 for chunk in self.chunks() {
283 write!(f, "{}", chunk)?;
284 }
285 Ok(())
286 }
287}
288
289pub struct Cursor<'a> {
290 rope: &'a Rope,
291 chunks: sum_tree::Cursor<'a, Chunk, usize>,
292 offset: usize,
293}
294
295impl<'a> Cursor<'a> {
296 pub fn new(rope: &'a Rope, offset: usize) -> Self {
297 let mut chunks = rope.chunks.cursor();
298 chunks.seek(&offset, Bias::Right, &());
299 Self {
300 rope,
301 chunks,
302 offset,
303 }
304 }
305
306 pub fn seek_forward(&mut self, end_offset: usize) {
307 debug_assert!(end_offset >= self.offset);
308
309 self.chunks.seek_forward(&end_offset, Bias::Right, &());
310 self.offset = end_offset;
311 }
312
313 pub fn slice(&mut self, end_offset: usize) -> Rope {
314 debug_assert!(
315 end_offset >= self.offset,
316 "cannot slice backwards from {} to {}",
317 self.offset,
318 end_offset
319 );
320
321 let mut slice = Rope::new();
322 if let Some(start_chunk) = self.chunks.item() {
323 let start_ix = self.offset - self.chunks.start();
324 let end_ix = cmp::min(end_offset, self.chunks.end(&())) - self.chunks.start();
325 slice.push(&start_chunk.0[start_ix..end_ix]);
326 }
327
328 if end_offset > self.chunks.end(&()) {
329 self.chunks.next(&());
330 slice.append(Rope {
331 chunks: self.chunks.slice(&end_offset, Bias::Right, &()),
332 });
333 if let Some(end_chunk) = self.chunks.item() {
334 let end_ix = end_offset - self.chunks.start();
335 slice.push(&end_chunk.0[..end_ix]);
336 }
337 }
338
339 self.offset = end_offset;
340 slice
341 }
342
343 pub fn summary<D: TextDimension>(&mut self, end_offset: usize) -> D {
344 debug_assert!(end_offset >= self.offset);
345
346 let mut summary = D::default();
347 if let Some(start_chunk) = self.chunks.item() {
348 let start_ix = self.offset - self.chunks.start();
349 let end_ix = cmp::min(end_offset, self.chunks.end(&())) - self.chunks.start();
350 summary.add_assign(&D::from_text_summary(&TextSummary::from(
351 &start_chunk.0[start_ix..end_ix],
352 )));
353 }
354
355 if end_offset > self.chunks.end(&()) {
356 self.chunks.next(&());
357 summary.add_assign(&self.chunks.summary(&end_offset, Bias::Right, &()));
358 if let Some(end_chunk) = self.chunks.item() {
359 let end_ix = end_offset - self.chunks.start();
360 summary.add_assign(&D::from_text_summary(&TextSummary::from(
361 &end_chunk.0[..end_ix],
362 )));
363 }
364 }
365
366 self.offset = end_offset;
367 summary
368 }
369
370 pub fn suffix(mut self) -> Rope {
371 self.slice(self.rope.chunks.extent(&()))
372 }
373
374 pub fn offset(&self) -> usize {
375 self.offset
376 }
377}
378
379pub struct Chunks<'a> {
380 chunks: sum_tree::Cursor<'a, Chunk, usize>,
381 range: Range<usize>,
382 reversed: bool,
383}
384
385impl<'a> Chunks<'a> {
386 pub fn new(rope: &'a Rope, range: Range<usize>, reversed: bool) -> Self {
387 let mut chunks = rope.chunks.cursor();
388 if reversed {
389 chunks.seek(&range.end, Bias::Left, &());
390 } else {
391 chunks.seek(&range.start, Bias::Right, &());
392 }
393 Self {
394 chunks,
395 range,
396 reversed,
397 }
398 }
399
400 pub fn offset(&self) -> usize {
401 if self.reversed {
402 self.range.end.min(self.chunks.end(&()))
403 } else {
404 self.range.start.max(*self.chunks.start())
405 }
406 }
407
408 pub fn seek(&mut self, offset: usize) {
409 let bias = if self.reversed {
410 Bias::Left
411 } else {
412 Bias::Right
413 };
414
415 if offset >= self.chunks.end(&()) {
416 self.chunks.seek_forward(&offset, bias, &());
417 } else {
418 self.chunks.seek(&offset, bias, &());
419 }
420
421 if self.reversed {
422 self.range.end = offset;
423 } else {
424 self.range.start = offset;
425 }
426 }
427
428 pub fn peek(&self) -> Option<&'a str> {
429 let chunk = self.chunks.item()?;
430 if self.reversed && self.range.start >= self.chunks.end(&()) {
431 return None;
432 }
433 let chunk_start = *self.chunks.start();
434 if self.range.end <= chunk_start {
435 return None;
436 }
437
438 let start = self.range.start.saturating_sub(chunk_start);
439 let end = self.range.end - chunk_start;
440 Some(&chunk.0[start..chunk.0.len().min(end)])
441 }
442}
443
444impl<'a> Iterator for Chunks<'a> {
445 type Item = &'a str;
446
447 fn next(&mut self) -> Option<Self::Item> {
448 let result = self.peek();
449 if result.is_some() {
450 if self.reversed {
451 self.chunks.prev(&());
452 } else {
453 self.chunks.next(&());
454 }
455 }
456 result
457 }
458}
459
460pub struct Bytes<'a> {
461 chunks: sum_tree::Cursor<'a, Chunk, usize>,
462 range: Range<usize>,
463}
464
465impl<'a> Bytes<'a> {
466 pub fn new(rope: &'a Rope, range: Range<usize>) -> Self {
467 let mut chunks = rope.chunks.cursor();
468 chunks.seek(&range.start, Bias::Right, &());
469 Self { chunks, range }
470 }
471
472 pub fn peek(&self) -> Option<&'a [u8]> {
473 let chunk = self.chunks.item()?;
474 let chunk_start = *self.chunks.start();
475 if self.range.end <= chunk_start {
476 return None;
477 }
478
479 let start = self.range.start.saturating_sub(chunk_start);
480 let end = self.range.end - chunk_start;
481 Some(&chunk.0.as_bytes()[start..chunk.0.len().min(end)])
482 }
483}
484
485impl<'a> Iterator for Bytes<'a> {
486 type Item = &'a [u8];
487
488 fn next(&mut self) -> Option<Self::Item> {
489 let result = self.peek();
490 if result.is_some() {
491 self.chunks.next(&());
492 }
493 result
494 }
495}
496
497impl<'a> io::Read for Bytes<'a> {
498 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
499 if let Some(chunk) = self.peek() {
500 let len = cmp::min(buf.len(), chunk.len());
501 buf[..len].copy_from_slice(&chunk[..len]);
502 self.range.start += len;
503 if len == chunk.len() {
504 self.chunks.next(&());
505 }
506 Ok(len)
507 } else {
508 Ok(0)
509 }
510 }
511}
512
513#[derive(Clone, Debug, Default)]
514struct Chunk(ArrayString<{ 2 * CHUNK_BASE }>);
515
516impl Chunk {
517 fn offset_to_point(&self, target: usize) -> Point {
518 let mut offset = 0;
519 let mut point = Point::new(0, 0);
520 for ch in self.0.chars() {
521 if offset >= target {
522 break;
523 }
524
525 if ch == '\n' {
526 point.row += 1;
527 point.column = 0;
528 } else {
529 point.column += ch.len_utf8() as u32;
530 }
531 offset += ch.len_utf8();
532 }
533 point
534 }
535
536 fn offset_to_point_utf16(&self, target: usize) -> PointUtf16 {
537 let mut offset = 0;
538 let mut point = PointUtf16::new(0, 0);
539 for ch in self.0.chars() {
540 if offset >= target {
541 break;
542 }
543
544 if ch == '\n' {
545 point.row += 1;
546 point.column = 0;
547 } else {
548 point.column += ch.len_utf16() as u32;
549 }
550 offset += ch.len_utf8();
551 }
552 point
553 }
554
555 fn point_to_offset(&self, target: Point) -> usize {
556 let mut offset = 0;
557 let mut point = Point::new(0, 0);
558 for ch in self.0.chars() {
559 if point >= target {
560 if point > target {
561 panic!("point {:?} is inside of character {:?}", target, ch);
562 }
563 break;
564 }
565
566 if ch == '\n' {
567 point.row += 1;
568 if point.row > target.row {
569 panic!(
570 "point {:?} is beyond the end of a line with length {}",
571 target, point.column
572 );
573 }
574 point.column = 0;
575 } else {
576 point.column += ch.len_utf8() as u32;
577 }
578 offset += ch.len_utf8();
579 }
580 offset
581 }
582
583 fn point_utf16_to_offset(&self, target: PointUtf16) -> usize {
584 let mut offset = 0;
585 let mut point = PointUtf16::new(0, 0);
586 for ch in self.0.chars() {
587 if point >= target {
588 if point > target {
589 panic!("point {:?} is inside of character {:?}", target, ch);
590 }
591 break;
592 }
593
594 if ch == '\n' {
595 point.row += 1;
596 point.column = 0;
597 } else {
598 point.column += ch.len_utf16() as u32;
599 }
600 offset += ch.len_utf8();
601 }
602 offset
603 }
604
605 fn point_utf16_to_point(&self, target: PointUtf16) -> Point {
606 let mut point = Point::zero();
607 let mut point_utf16 = PointUtf16::zero();
608 for ch in self.0.chars() {
609 if point_utf16 >= target {
610 if point_utf16 > target {
611 panic!("point {:?} is inside of character {:?}", target, ch);
612 }
613 break;
614 }
615
616 if ch == '\n' {
617 point_utf16 += PointUtf16::new(1, 0);
618 point += Point::new(1, 0);
619 } else {
620 point_utf16 += PointUtf16::new(0, ch.len_utf16() as u32);
621 point += Point::new(0, ch.len_utf8() as u32);
622 }
623 }
624 point
625 }
626
627 fn clip_point(&self, target: Point, bias: Bias) -> Point {
628 for (row, line) in self.0.split('\n').enumerate() {
629 if row == target.row as usize {
630 let mut column = target.column.min(line.len() as u32);
631 while !line.is_char_boundary(column as usize) {
632 match bias {
633 Bias::Left => column -= 1,
634 Bias::Right => column += 1,
635 }
636 }
637 return Point::new(row as u32, column);
638 }
639 }
640 unreachable!()
641 }
642
643 fn clip_point_utf16(&self, target: PointUtf16, bias: Bias) -> PointUtf16 {
644 for (row, line) in self.0.split('\n').enumerate() {
645 if row == target.row as usize {
646 let mut code_units = line.encode_utf16();
647 let mut column = code_units.by_ref().take(target.column as usize).count();
648 if char::decode_utf16(code_units).next().transpose().is_err() {
649 match bias {
650 Bias::Left => column -= 1,
651 Bias::Right => column += 1,
652 }
653 }
654 return PointUtf16::new(row as u32, column as u32);
655 }
656 }
657 unreachable!()
658 }
659}
660
661impl sum_tree::Item for Chunk {
662 type Summary = TextSummary;
663
664 fn summary(&self) -> Self::Summary {
665 TextSummary::from(self.0.as_str())
666 }
667}
668
669#[derive(Clone, Debug, Default, Eq, PartialEq)]
670pub struct TextSummary {
671 pub bytes: usize,
672 pub lines: Point,
673 pub lines_utf16: PointUtf16,
674 pub first_line_chars: u32,
675 pub last_line_chars: u32,
676 pub longest_row: u32,
677 pub longest_row_chars: u32,
678}
679
680impl<'a> From<&'a str> for TextSummary {
681 fn from(text: &'a str) -> Self {
682 let mut lines = Point::new(0, 0);
683 let mut lines_utf16 = PointUtf16::new(0, 0);
684 let mut first_line_chars = 0;
685 let mut last_line_chars = 0;
686 let mut longest_row = 0;
687 let mut longest_row_chars = 0;
688 for c in text.chars() {
689 if c == '\n' {
690 lines += Point::new(1, 0);
691 lines_utf16 += PointUtf16::new(1, 0);
692 last_line_chars = 0;
693 } else {
694 lines.column += c.len_utf8() as u32;
695 lines_utf16.column += c.len_utf16() as u32;
696 last_line_chars += 1;
697 }
698
699 if lines.row == 0 {
700 first_line_chars = last_line_chars;
701 }
702
703 if last_line_chars > longest_row_chars {
704 longest_row = lines.row;
705 longest_row_chars = last_line_chars;
706 }
707 }
708
709 TextSummary {
710 bytes: text.len(),
711 lines,
712 lines_utf16,
713 first_line_chars,
714 last_line_chars,
715 longest_row,
716 longest_row_chars,
717 }
718 }
719}
720
721impl sum_tree::Summary for TextSummary {
722 type Context = ();
723
724 fn add_summary(&mut self, summary: &Self, _: &Self::Context) {
725 *self += summary;
726 }
727}
728
729impl<'a> std::ops::Add<Self> for TextSummary {
730 type Output = Self;
731
732 fn add(mut self, rhs: Self) -> Self::Output {
733 self.add_assign(&rhs);
734 self
735 }
736}
737
738impl<'a> std::ops::AddAssign<&'a Self> for TextSummary {
739 fn add_assign(&mut self, other: &'a Self) {
740 let joined_chars = self.last_line_chars + other.first_line_chars;
741 if joined_chars > self.longest_row_chars {
742 self.longest_row = self.lines.row;
743 self.longest_row_chars = joined_chars;
744 }
745 if other.longest_row_chars > self.longest_row_chars {
746 self.longest_row = self.lines.row + other.longest_row;
747 self.longest_row_chars = other.longest_row_chars;
748 }
749
750 if self.lines.row == 0 {
751 self.first_line_chars += other.first_line_chars;
752 }
753
754 if other.lines.row == 0 {
755 self.last_line_chars += other.first_line_chars;
756 } else {
757 self.last_line_chars = other.last_line_chars;
758 }
759
760 self.bytes += other.bytes;
761 self.lines += other.lines;
762 self.lines_utf16 += other.lines_utf16;
763 }
764}
765
766impl std::ops::AddAssign<Self> for TextSummary {
767 fn add_assign(&mut self, other: Self) {
768 *self += &other;
769 }
770}
771
772pub trait TextDimension: 'static + for<'a> Dimension<'a, TextSummary> {
773 fn from_text_summary(summary: &TextSummary) -> Self;
774 fn add_assign(&mut self, other: &Self);
775}
776
777impl<'a, D1: TextDimension, D2: TextDimension> TextDimension for (D1, D2) {
778 fn from_text_summary(summary: &TextSummary) -> Self {
779 (
780 D1::from_text_summary(summary),
781 D2::from_text_summary(summary),
782 )
783 }
784
785 fn add_assign(&mut self, other: &Self) {
786 self.0.add_assign(&other.0);
787 self.1.add_assign(&other.1);
788 }
789}
790
791impl TextDimension for TextSummary {
792 fn from_text_summary(summary: &TextSummary) -> Self {
793 summary.clone()
794 }
795
796 fn add_assign(&mut self, other: &Self) {
797 *self += other;
798 }
799}
800
801impl<'a> sum_tree::Dimension<'a, TextSummary> for usize {
802 fn add_summary(&mut self, summary: &'a TextSummary, _: &()) {
803 *self += summary.bytes;
804 }
805}
806
807impl TextDimension for usize {
808 fn from_text_summary(summary: &TextSummary) -> Self {
809 summary.bytes
810 }
811
812 fn add_assign(&mut self, other: &Self) {
813 *self += other;
814 }
815}
816
817impl<'a> sum_tree::Dimension<'a, TextSummary> for Point {
818 fn add_summary(&mut self, summary: &'a TextSummary, _: &()) {
819 *self += summary.lines;
820 }
821}
822
823impl TextDimension for Point {
824 fn from_text_summary(summary: &TextSummary) -> Self {
825 summary.lines
826 }
827
828 fn add_assign(&mut self, other: &Self) {
829 *self += other;
830 }
831}
832
833impl<'a> sum_tree::Dimension<'a, TextSummary> for PointUtf16 {
834 fn add_summary(&mut self, summary: &'a TextSummary, _: &()) {
835 *self += summary.lines_utf16;
836 }
837}
838
839impl TextDimension for PointUtf16 {
840 fn from_text_summary(summary: &TextSummary) -> Self {
841 summary.lines_utf16
842 }
843
844 fn add_assign(&mut self, other: &Self) {
845 *self += other;
846 }
847}
848
849fn find_split_ix(text: &str) -> usize {
850 let mut ix = text.len() / 2;
851 while !text.is_char_boundary(ix) {
852 if ix < 2 * CHUNK_BASE {
853 ix += 1;
854 } else {
855 ix = (text.len() / 2) - 1;
856 break;
857 }
858 }
859 while !text.is_char_boundary(ix) {
860 ix -= 1;
861 }
862
863 debug_assert!(ix <= 2 * CHUNK_BASE);
864 debug_assert!(text.len() - ix <= 2 * CHUNK_BASE);
865 ix
866}
867
868#[cfg(test)]
869mod tests {
870 use super::*;
871 use crate::random_char_iter::RandomCharIter;
872 use rand::prelude::*;
873 use std::{cmp::Ordering, env, io::Read};
874 use Bias::{Left, Right};
875
876 #[test]
877 fn test_all_4_byte_chars() {
878 let mut rope = Rope::new();
879 let text = "🏀".repeat(256);
880 rope.push(&text);
881 assert_eq!(rope.text(), text);
882 }
883
884 #[test]
885 fn test_clip() {
886 let rope = Rope::from("🧘");
887
888 assert_eq!(rope.clip_offset(1, Bias::Left), 0);
889 assert_eq!(rope.clip_offset(1, Bias::Right), 4);
890 assert_eq!(rope.clip_offset(5, Bias::Right), 4);
891
892 assert_eq!(
893 rope.clip_point(Point::new(0, 1), Bias::Left),
894 Point::new(0, 0)
895 );
896 assert_eq!(
897 rope.clip_point(Point::new(0, 1), Bias::Right),
898 Point::new(0, 4)
899 );
900 assert_eq!(
901 rope.clip_point(Point::new(0, 5), Bias::Right),
902 Point::new(0, 4)
903 );
904
905 assert_eq!(
906 rope.clip_point_utf16(PointUtf16::new(0, 1), Bias::Left),
907 PointUtf16::new(0, 0)
908 );
909 assert_eq!(
910 rope.clip_point_utf16(PointUtf16::new(0, 1), Bias::Right),
911 PointUtf16::new(0, 2)
912 );
913 assert_eq!(
914 rope.clip_point_utf16(PointUtf16::new(0, 3), Bias::Right),
915 PointUtf16::new(0, 2)
916 );
917 }
918
919 #[gpui::test(iterations = 100)]
920 fn test_random_rope(mut rng: StdRng) {
921 let operations = env::var("OPERATIONS")
922 .map(|i| i.parse().expect("invalid `OPERATIONS` variable"))
923 .unwrap_or(10);
924
925 let mut expected = String::new();
926 let mut actual = Rope::new();
927 for _ in 0..operations {
928 let end_ix = clip_offset(&expected, rng.gen_range(0..=expected.len()), Right);
929 let start_ix = clip_offset(&expected, rng.gen_range(0..=end_ix), Left);
930 let len = rng.gen_range(0..=64);
931 let new_text: String = RandomCharIter::new(&mut rng).take(len).collect();
932
933 let mut new_actual = Rope::new();
934 let mut cursor = actual.cursor(0);
935 new_actual.append(cursor.slice(start_ix));
936 new_actual.push(&new_text);
937 cursor.seek_forward(end_ix);
938 new_actual.append(cursor.suffix());
939 actual = new_actual;
940
941 expected.replace_range(start_ix..end_ix, &new_text);
942
943 assert_eq!(actual.text(), expected);
944 log::info!("text: {:?}", expected);
945
946 for _ in 0..5 {
947 let end_ix = clip_offset(&expected, rng.gen_range(0..=expected.len()), Right);
948 let start_ix = clip_offset(&expected, rng.gen_range(0..=end_ix), Left);
949
950 let actual_text = actual.chunks_in_range(start_ix..end_ix).collect::<String>();
951 assert_eq!(actual_text, &expected[start_ix..end_ix]);
952
953 let mut actual_text = String::new();
954 actual
955 .bytes_in_range(start_ix..end_ix)
956 .read_to_string(&mut actual_text)
957 .unwrap();
958 assert_eq!(actual_text, &expected[start_ix..end_ix]);
959
960 assert_eq!(
961 actual
962 .reversed_chunks_in_range(start_ix..end_ix)
963 .collect::<Vec<&str>>()
964 .into_iter()
965 .rev()
966 .collect::<String>(),
967 &expected[start_ix..end_ix]
968 );
969 }
970
971 let mut point = Point::new(0, 0);
972 let mut point_utf16 = PointUtf16::new(0, 0);
973 for (ix, ch) in expected.char_indices().chain(Some((expected.len(), '\0'))) {
974 assert_eq!(actual.offset_to_point(ix), point, "offset_to_point({})", ix);
975 assert_eq!(
976 actual.offset_to_point_utf16(ix),
977 point_utf16,
978 "offset_to_point_utf16({})",
979 ix
980 );
981 assert_eq!(
982 actual.point_to_offset(point),
983 ix,
984 "point_to_offset({:?})",
985 point
986 );
987 assert_eq!(
988 actual.point_utf16_to_offset(point_utf16),
989 ix,
990 "point_utf16_to_offset({:?})",
991 point_utf16
992 );
993 if ch == '\n' {
994 point += Point::new(1, 0);
995 point_utf16 += PointUtf16::new(1, 0);
996 } else {
997 point.column += ch.len_utf8() as u32;
998 point_utf16.column += ch.len_utf16() as u32;
999 }
1000 }
1001
1002 let mut point_utf16 = PointUtf16::zero();
1003 for unit in expected.encode_utf16() {
1004 let left_point = actual.clip_point_utf16(point_utf16, Bias::Left);
1005 let right_point = actual.clip_point_utf16(point_utf16, Bias::Right);
1006 assert!(right_point >= left_point);
1007 // Ensure translating UTF-16 points to offsets doesn't panic.
1008 actual.point_utf16_to_offset(left_point);
1009 actual.point_utf16_to_offset(right_point);
1010
1011 if unit == b'\n' as u16 {
1012 point_utf16 += PointUtf16::new(1, 0);
1013 } else {
1014 point_utf16 += PointUtf16::new(0, 1);
1015 }
1016 }
1017
1018 for _ in 0..5 {
1019 let end_ix = clip_offset(&expected, rng.gen_range(0..=expected.len()), Right);
1020 let start_ix = clip_offset(&expected, rng.gen_range(0..=end_ix), Left);
1021 assert_eq!(
1022 actual.cursor(start_ix).summary::<TextSummary>(end_ix),
1023 TextSummary::from(&expected[start_ix..end_ix])
1024 );
1025 }
1026
1027 let mut expected_longest_rows = Vec::new();
1028 let mut longest_line_len = -1_isize;
1029 for (row, line) in expected.split('\n').enumerate() {
1030 let row = row as u32;
1031 assert_eq!(
1032 actual.line_len(row),
1033 line.len() as u32,
1034 "invalid line len for row {}",
1035 row
1036 );
1037
1038 let line_char_count = line.chars().count() as isize;
1039 match line_char_count.cmp(&longest_line_len) {
1040 Ordering::Less => {}
1041 Ordering::Equal => expected_longest_rows.push(row),
1042 Ordering::Greater => {
1043 longest_line_len = line_char_count;
1044 expected_longest_rows.clear();
1045 expected_longest_rows.push(row);
1046 }
1047 }
1048 }
1049
1050 let longest_row = actual.summary().longest_row;
1051 assert!(
1052 expected_longest_rows.contains(&longest_row),
1053 "incorrect longest row {}. expected {:?} with length {}",
1054 longest_row,
1055 expected_longest_rows,
1056 longest_line_len,
1057 );
1058 }
1059 }
1060
1061 fn clip_offset(text: &str, mut offset: usize, bias: Bias) -> usize {
1062 while !text.is_char_boundary(offset) {
1063 match bias {
1064 Bias::Left => offset -= 1,
1065 Bias::Right => offset += 1,
1066 }
1067 }
1068 offset
1069 }
1070
1071 impl Rope {
1072 fn text(&self) -> String {
1073 let mut text = String::new();
1074 for chunk in self.chunks.cursor::<()>() {
1075 text.push_str(&chunk.0);
1076 }
1077 text
1078 }
1079 }
1080}