1use super::{
2 fold_map::{FoldBufferRows, FoldChunks, FoldEdit, FoldOffset, FoldPoint, FoldSnapshot},
3 TextHighlights,
4};
5use crate::ToPoint;
6use gpui::fonts::HighlightStyle;
7use language::{Bias, Chunk, Edit, Patch, Point, Rope, TextSummary};
8use parking_lot::Mutex;
9use std::{
10 cmp,
11 ops::{Add, AddAssign, Range, Sub},
12};
13use util::post_inc;
14
15pub type SuggestionEdit = Edit<SuggestionOffset>;
16
17#[derive(Copy, Clone, Debug, Default, Eq, Ord, PartialOrd, PartialEq)]
18pub struct SuggestionOffset(pub usize);
19
20impl Add for SuggestionOffset {
21 type Output = Self;
22
23 fn add(self, rhs: Self) -> Self::Output {
24 Self(self.0 + rhs.0)
25 }
26}
27
28impl Sub for SuggestionOffset {
29 type Output = Self;
30
31 fn sub(self, rhs: Self) -> Self::Output {
32 Self(self.0 - rhs.0)
33 }
34}
35
36impl AddAssign for SuggestionOffset {
37 fn add_assign(&mut self, rhs: Self) {
38 self.0 += rhs.0;
39 }
40}
41
42#[derive(Copy, Clone, Debug, Default, Eq, Ord, PartialOrd, PartialEq)]
43pub struct SuggestionPoint(pub Point);
44
45impl SuggestionPoint {
46 pub fn row(self) -> u32 {
47 self.0.row
48 }
49
50 pub fn column(self) -> u32 {
51 self.0.column
52 }
53}
54
55#[derive(Clone, Debug)]
56pub struct Suggestion<T> {
57 position: T,
58 text: Rope,
59 highlight_style: HighlightStyle,
60}
61
62pub struct SuggestionMap(Mutex<SuggestionSnapshot>);
63
64impl SuggestionMap {
65 pub fn new(fold_snapshot: FoldSnapshot) -> (Self, SuggestionSnapshot) {
66 let snapshot = SuggestionSnapshot {
67 fold_snapshot,
68 suggestion: None,
69 };
70 (Self(Mutex::new(snapshot.clone())), snapshot)
71 }
72
73 pub fn replace<T>(
74 &self,
75 new_suggestion: Option<Suggestion<T>>,
76 fold_snapshot: FoldSnapshot,
77 fold_edits: Vec<FoldEdit>,
78 ) -> (SuggestionSnapshot, Vec<SuggestionEdit>)
79 where
80 T: ToPoint,
81 {
82 let new_suggestion = new_suggestion.map(|new_suggestion| {
83 let buffer_point = new_suggestion
84 .position
85 .to_point(fold_snapshot.buffer_snapshot());
86 let fold_point = fold_snapshot.to_fold_point(buffer_point, Bias::Left);
87 let fold_offset = fold_point.to_offset(&fold_snapshot);
88 Suggestion {
89 position: fold_offset,
90 text: new_suggestion.text,
91 highlight_style: new_suggestion.highlight_style,
92 }
93 });
94
95 let (_, edits) = self.sync(fold_snapshot, fold_edits);
96 let mut snapshot = self.0.lock();
97
98 let mut patch = Patch::new(edits);
99 if let Some(suggestion) = snapshot.suggestion.take() {
100 patch = patch.compose([SuggestionEdit {
101 old: SuggestionOffset(suggestion.position.0)
102 ..SuggestionOffset(suggestion.position.0 + suggestion.text.len()),
103 new: SuggestionOffset(suggestion.position.0)
104 ..SuggestionOffset(suggestion.position.0),
105 }]);
106 }
107
108 if let Some(suggestion) = new_suggestion.as_ref() {
109 patch = patch.compose([SuggestionEdit {
110 old: SuggestionOffset(suggestion.position.0)
111 ..SuggestionOffset(suggestion.position.0),
112 new: SuggestionOffset(suggestion.position.0)
113 ..SuggestionOffset(suggestion.position.0 + suggestion.text.len()),
114 }]);
115 }
116
117 snapshot.suggestion = new_suggestion;
118 (snapshot.clone(), patch.into_inner())
119 }
120
121 pub fn sync(
122 &self,
123 fold_snapshot: FoldSnapshot,
124 fold_edits: Vec<FoldEdit>,
125 ) -> (SuggestionSnapshot, Vec<SuggestionEdit>) {
126 let mut snapshot = self.0.lock();
127 let mut suggestion_edits = Vec::new();
128
129 let mut suggestion_old_len = 0;
130 let mut suggestion_new_len = 0;
131 for fold_edit in fold_edits {
132 let start = fold_edit.new.start;
133 let end = FoldOffset(start.0 + fold_edit.old_len().0);
134 if let Some(suggestion) = snapshot.suggestion.as_mut() {
135 if end <= suggestion.position {
136 suggestion.position.0 += fold_edit.new_len().0;
137 suggestion.position.0 -= fold_edit.old_len().0;
138 } else if start > suggestion.position {
139 suggestion_old_len = suggestion.text.len();
140 suggestion_new_len = suggestion_old_len;
141 } else {
142 suggestion_old_len = suggestion.text.len();
143 snapshot.suggestion.take();
144 suggestion_edits.push(SuggestionEdit {
145 old: SuggestionOffset(fold_edit.old.start.0)
146 ..SuggestionOffset(fold_edit.old.end.0 + suggestion_old_len),
147 new: SuggestionOffset(fold_edit.new.start.0)
148 ..SuggestionOffset(fold_edit.new.end.0),
149 });
150 continue;
151 }
152 }
153
154 suggestion_edits.push(SuggestionEdit {
155 old: SuggestionOffset(fold_edit.old.start.0 + suggestion_old_len)
156 ..SuggestionOffset(fold_edit.old.end.0 + suggestion_old_len),
157 new: SuggestionOffset(fold_edit.new.start.0 + suggestion_new_len)
158 ..SuggestionOffset(fold_edit.new.end.0 + suggestion_new_len),
159 });
160 }
161 snapshot.fold_snapshot = fold_snapshot;
162
163 (snapshot.clone(), suggestion_edits)
164 }
165}
166
167#[derive(Clone)]
168pub struct SuggestionSnapshot {
169 fold_snapshot: FoldSnapshot,
170 suggestion: Option<Suggestion<FoldOffset>>,
171}
172
173impl SuggestionSnapshot {
174 pub fn max_point(&self) -> SuggestionPoint {
175 if let Some(suggestion) = self.suggestion.as_ref() {
176 let suggestion_point = suggestion.position.to_point(&self.fold_snapshot);
177 let mut max_point = suggestion_point.0;
178 max_point += suggestion.text.max_point();
179 max_point += self.fold_snapshot.max_point().0 - suggestion_point.0;
180 SuggestionPoint(max_point)
181 } else {
182 SuggestionPoint(self.fold_snapshot.max_point().0)
183 }
184 }
185
186 pub fn len(&self) -> SuggestionOffset {
187 if let Some(suggestion) = self.suggestion.as_ref() {
188 let mut len = suggestion.position.0;
189 len += suggestion.text.len();
190 len += self.fold_snapshot.len().0 - suggestion.position.0;
191 SuggestionOffset(len)
192 } else {
193 SuggestionOffset(self.fold_snapshot.len().0)
194 }
195 }
196
197 pub fn clip_point(&self, point: SuggestionPoint, bias: Bias) -> SuggestionPoint {
198 if let Some(suggestion) = self.suggestion.as_ref() {
199 let suggestion_start = suggestion.position.to_point(&self.fold_snapshot).0;
200 let suggestion_end = suggestion_start + suggestion.text.max_point();
201 if point.0 <= suggestion_start {
202 SuggestionPoint(self.fold_snapshot.clip_point(FoldPoint(point.0), bias).0)
203 } else if point.0 > suggestion_end {
204 let fold_point = self.fold_snapshot.clip_point(
205 FoldPoint(suggestion_start + (point.0 - suggestion_end)),
206 bias,
207 );
208 let suggestion_point = suggestion_end + (fold_point.0 - suggestion_start);
209 if bias == Bias::Left && suggestion_point == suggestion_end {
210 SuggestionPoint(suggestion_start)
211 } else {
212 SuggestionPoint(suggestion_point)
213 }
214 } else if bias == Bias::Left || suggestion_start == self.fold_snapshot.max_point().0 {
215 SuggestionPoint(suggestion_start)
216 } else {
217 let fold_point = if self.fold_snapshot.line_len(suggestion_start.row)
218 > suggestion_start.column
219 {
220 FoldPoint(suggestion_start + Point::new(0, 1))
221 } else {
222 FoldPoint(suggestion_start + Point::new(1, 0))
223 };
224 let clipped_fold_point = self.fold_snapshot.clip_point(fold_point, bias);
225 SuggestionPoint(suggestion_end + (clipped_fold_point.0 - suggestion_start))
226 }
227 } else {
228 SuggestionPoint(self.fold_snapshot.clip_point(FoldPoint(point.0), bias).0)
229 }
230 }
231
232 pub fn to_offset(&self, point: SuggestionPoint) -> SuggestionOffset {
233 if let Some(suggestion) = self.suggestion.as_ref() {
234 let suggestion_start = suggestion.position.to_point(&self.fold_snapshot).0;
235 let suggestion_end = suggestion_start + suggestion.text.max_point();
236
237 if point.0 <= suggestion_start {
238 SuggestionOffset(FoldPoint(point.0).to_offset(&self.fold_snapshot).0)
239 } else if point.0 > suggestion_end {
240 let fold_offset = FoldPoint(suggestion_start + (point.0 - suggestion_end))
241 .to_offset(&self.fold_snapshot);
242 SuggestionOffset(fold_offset.0 + suggestion.text.len())
243 } else {
244 let offset_in_suggestion =
245 suggestion.text.point_to_offset(point.0 - suggestion_start);
246 SuggestionOffset(suggestion.position.0 + offset_in_suggestion)
247 }
248 } else {
249 SuggestionOffset(FoldPoint(point.0).to_offset(&self.fold_snapshot).0)
250 }
251 }
252
253 pub fn to_point(&self, offset: SuggestionOffset) -> SuggestionPoint {
254 if let Some(suggestion) = self.suggestion.as_ref() {
255 let suggestion_point_start = suggestion.position.to_point(&self.fold_snapshot).0;
256 if offset.0 <= suggestion.position.0 {
257 SuggestionPoint(FoldOffset(offset.0).to_point(&self.fold_snapshot).0)
258 } else if offset.0 > (suggestion.position.0 + suggestion.text.len()) {
259 let fold_point = FoldOffset(offset.0 - suggestion.text.len())
260 .to_point(&self.fold_snapshot)
261 .0;
262
263 SuggestionPoint(
264 suggestion_point_start
265 + suggestion.text.max_point()
266 + (fold_point - suggestion_point_start),
267 )
268 } else {
269 let point_in_suggestion = suggestion
270 .text
271 .offset_to_point(offset.0 - suggestion.position.0);
272 SuggestionPoint(suggestion_point_start + point_in_suggestion)
273 }
274 } else {
275 SuggestionPoint(FoldOffset(offset.0).to_point(&self.fold_snapshot).0)
276 }
277 }
278
279 pub fn to_fold_point(&self, point: SuggestionPoint) -> FoldPoint {
280 if let Some(suggestion) = self.suggestion.as_ref() {
281 let suggestion_start = suggestion.position.to_point(&self.fold_snapshot).0;
282 let suggestion_end = suggestion_start + suggestion.text.max_point();
283
284 if point.0 <= suggestion_start {
285 FoldPoint(point.0)
286 } else if point.0 > suggestion_end {
287 FoldPoint(suggestion_start + (point.0 - suggestion_end))
288 } else {
289 FoldPoint(suggestion_start)
290 }
291 } else {
292 FoldPoint(point.0)
293 }
294 }
295
296 pub fn to_suggestion_point(&self, point: FoldPoint) -> SuggestionPoint {
297 if let Some(suggestion) = self.suggestion.as_ref() {
298 let suggestion_start = suggestion.position.to_point(&self.fold_snapshot).0;
299
300 if point.0 <= suggestion_start {
301 SuggestionPoint(point.0)
302 } else {
303 let suggestion_end = suggestion_start + suggestion.text.max_point();
304 SuggestionPoint(suggestion_end + (point.0 - suggestion_start))
305 }
306 } else {
307 SuggestionPoint(point.0)
308 }
309 }
310
311 pub fn line_len(&self, row: u32) -> u32 {
312 if let Some(suggestion) = self.suggestion.as_ref() {
313 let suggestion_lines = suggestion.text.max_point();
314 let suggestion_start = suggestion.position.to_point(&self.fold_snapshot).0;
315 let suggestion_end = suggestion_start + suggestion_lines;
316
317 if row < suggestion_start.row {
318 self.fold_snapshot.line_len(row)
319 } else if row > suggestion_end.row {
320 self.fold_snapshot.line_len(row - suggestion_lines.row)
321 } else {
322 let mut len = suggestion.text.line_len(row - suggestion_start.row);
323 if row == suggestion_start.row {
324 len += suggestion_start.column;
325 }
326 if row == suggestion_end.row {
327 len +=
328 self.fold_snapshot.line_len(suggestion_start.row) - suggestion_start.column;
329 }
330 len
331 }
332 } else {
333 self.fold_snapshot.line_len(row)
334 }
335 }
336
337 pub fn text_summary_for_range(&self, range: Range<SuggestionPoint>) -> TextSummary {
338 if let Some(suggestion) = self.suggestion.as_ref() {
339 let suggestion_start = suggestion.position.to_point(&self.fold_snapshot).0;
340 let suggestion_end = suggestion_start + suggestion.text.max_point();
341 let mut summary = TextSummary::default();
342
343 let prefix_range =
344 cmp::min(range.start.0, suggestion_start)..cmp::min(range.end.0, suggestion_start);
345 if prefix_range.start < prefix_range.end {
346 summary += self.fold_snapshot.text_summary_for_range(
347 FoldPoint(prefix_range.start)..FoldPoint(prefix_range.end),
348 );
349 }
350
351 let suggestion_range =
352 cmp::max(range.start.0, suggestion_start)..cmp::min(range.end.0, suggestion_end);
353 if suggestion_range.start < suggestion_range.end {
354 let point_range = suggestion_range.start - suggestion_start
355 ..suggestion_range.end - suggestion_start;
356 let offset_range = suggestion.text.point_to_offset(point_range.start)
357 ..suggestion.text.point_to_offset(point_range.end);
358 summary += suggestion
359 .text
360 .cursor(offset_range.start)
361 .summary::<TextSummary>(offset_range.end);
362 }
363
364 let suffix_range = cmp::max(range.start.0, suggestion_end)..range.end.0;
365 if suffix_range.start < suffix_range.end {
366 let start = suggestion_start + (suffix_range.start - suggestion_end);
367 let end = suggestion_start + (suffix_range.end - suggestion_end);
368 summary += self
369 .fold_snapshot
370 .text_summary_for_range(FoldPoint(start)..FoldPoint(end));
371 }
372
373 summary
374 } else {
375 self.fold_snapshot
376 .text_summary_for_range(FoldPoint(range.start.0)..FoldPoint(range.end.0))
377 }
378 }
379
380 pub fn chars_at(&self, start: SuggestionPoint) -> impl '_ + Iterator<Item = char> {
381 let start = self.to_offset(start);
382 self.chunks(start..self.len(), false, None)
383 .flat_map(|chunk| chunk.text.chars())
384 }
385
386 pub fn chunks<'a>(
387 &'a self,
388 range: Range<SuggestionOffset>,
389 language_aware: bool,
390 text_highlights: Option<&'a TextHighlights>,
391 ) -> Chunks<'a> {
392 if let Some(suggestion) = self.suggestion.as_ref() {
393 let suggestion_range =
394 suggestion.position.0..suggestion.position.0 + suggestion.text.len();
395
396 let prefix_chunks = if range.start.0 < suggestion_range.start {
397 Some(self.fold_snapshot.chunks(
398 FoldOffset(range.start.0)
399 ..cmp::min(FoldOffset(suggestion_range.start), FoldOffset(range.end.0)),
400 language_aware,
401 text_highlights,
402 ))
403 } else {
404 None
405 };
406
407 let clipped_suggestion_range = cmp::max(range.start.0, suggestion_range.start)
408 ..cmp::min(range.end.0, suggestion_range.end);
409 let suggestion_chunks = if clipped_suggestion_range.start < clipped_suggestion_range.end
410 {
411 let start = clipped_suggestion_range.start - suggestion_range.start;
412 let end = clipped_suggestion_range.end - suggestion_range.start;
413 Some(suggestion.text.chunks_in_range(start..end))
414 } else {
415 None
416 };
417
418 let suffix_chunks = if range.end.0 > suggestion_range.end {
419 let start = cmp::max(suggestion_range.end, range.start.0) - suggestion_range.len();
420 let end = range.end.0 - suggestion_range.len();
421 Some(self.fold_snapshot.chunks(
422 FoldOffset(start)..FoldOffset(end),
423 language_aware,
424 text_highlights,
425 ))
426 } else {
427 None
428 };
429
430 Chunks {
431 prefix_chunks,
432 suggestion_chunks,
433 suffix_chunks,
434 highlight_style: suggestion.highlight_style,
435 }
436 } else {
437 Chunks {
438 prefix_chunks: Some(self.fold_snapshot.chunks(
439 FoldOffset(range.start.0)..FoldOffset(range.end.0),
440 language_aware,
441 text_highlights,
442 )),
443 suggestion_chunks: None,
444 suffix_chunks: None,
445 highlight_style: Default::default(),
446 }
447 }
448 }
449
450 pub fn buffer_rows<'a>(&'a self, row: u32) -> SuggestionBufferRows<'a> {
451 let suggestion_range = if let Some(suggestion) = self.suggestion.as_ref() {
452 let start = suggestion.position.to_point(&self.fold_snapshot).0;
453 let end = start + suggestion.text.max_point();
454 start.row..end.row
455 } else {
456 u32::MAX..u32::MAX
457 };
458
459 let fold_buffer_rows = if row <= suggestion_range.start {
460 self.fold_snapshot.buffer_rows(row)
461 } else if row > suggestion_range.end {
462 self.fold_snapshot
463 .buffer_rows(row - (suggestion_range.end - suggestion_range.start))
464 } else {
465 let mut rows = self.fold_snapshot.buffer_rows(suggestion_range.start);
466 rows.next();
467 rows
468 };
469
470 SuggestionBufferRows {
471 current_row: row,
472 suggestion_row_start: suggestion_range.start,
473 suggestion_row_end: suggestion_range.end,
474 fold_buffer_rows,
475 }
476 }
477
478 #[cfg(test)]
479 pub fn text(&self) -> String {
480 self.chunks(Default::default()..self.len(), false, None)
481 .map(|chunk| chunk.text)
482 .collect()
483 }
484}
485
486pub struct Chunks<'a> {
487 prefix_chunks: Option<FoldChunks<'a>>,
488 suggestion_chunks: Option<text::Chunks<'a>>,
489 suffix_chunks: Option<FoldChunks<'a>>,
490 highlight_style: HighlightStyle,
491}
492
493impl<'a> Iterator for Chunks<'a> {
494 type Item = Chunk<'a>;
495
496 fn next(&mut self) -> Option<Self::Item> {
497 if let Some(chunks) = self.prefix_chunks.as_mut() {
498 if let Some(chunk) = chunks.next() {
499 return Some(chunk);
500 } else {
501 self.prefix_chunks = None;
502 }
503 }
504
505 if let Some(chunks) = self.suggestion_chunks.as_mut() {
506 if let Some(chunk) = chunks.next() {
507 return Some(Chunk {
508 text: chunk,
509 syntax_highlight_id: None,
510 highlight_style: Some(self.highlight_style),
511 diagnostic_severity: None,
512 is_unnecessary: false,
513 });
514 } else {
515 self.suggestion_chunks = None;
516 }
517 }
518
519 if let Some(chunks) = self.suffix_chunks.as_mut() {
520 if let Some(chunk) = chunks.next() {
521 return Some(chunk);
522 } else {
523 self.suffix_chunks = None;
524 }
525 }
526
527 None
528 }
529}
530
531pub struct SuggestionBufferRows<'a> {
532 current_row: u32,
533 suggestion_row_start: u32,
534 suggestion_row_end: u32,
535 fold_buffer_rows: FoldBufferRows<'a>,
536}
537
538impl<'a> Iterator for SuggestionBufferRows<'a> {
539 type Item = Option<u32>;
540
541 fn next(&mut self) -> Option<Self::Item> {
542 let row = post_inc(&mut self.current_row);
543 if row <= self.suggestion_row_start || row > self.suggestion_row_end {
544 self.fold_buffer_rows.next()
545 } else {
546 Some(None)
547 }
548 }
549}
550
551#[cfg(test)]
552mod tests {
553 use super::*;
554 use crate::{display_map::fold_map::FoldMap, MultiBuffer};
555 use gpui::MutableAppContext;
556 use rand::{prelude::StdRng, Rng};
557 use settings::Settings;
558 use std::{
559 env,
560 ops::{Bound, RangeBounds},
561 };
562
563 #[gpui::test]
564 fn test_basic(cx: &mut MutableAppContext) {
565 let buffer = MultiBuffer::build_simple("abcdefghi", cx);
566 let buffer_edits = buffer.update(cx, |buffer, _| buffer.subscribe());
567 let (mut fold_map, fold_snapshot) = FoldMap::new(buffer.read(cx).snapshot(cx));
568 let (suggestion_map, suggestion_snapshot) = SuggestionMap::new(fold_snapshot.clone());
569 assert_eq!(suggestion_snapshot.text(), "abcdefghi");
570
571 let (suggestion_snapshot, _) = suggestion_map.replace(
572 Some(Suggestion {
573 position: 3,
574 text: "123\n456".into(),
575 highlight_style: Default::default(),
576 }),
577 fold_snapshot,
578 Default::default(),
579 );
580 assert_eq!(suggestion_snapshot.text(), "abc123\n456defghi");
581
582 buffer.update(cx, |buffer, cx| {
583 buffer.edit(
584 [(0..0, "ABC"), (3..3, "DEF"), (4..4, "GHI"), (9..9, "JKL")],
585 None,
586 cx,
587 )
588 });
589 let (fold_snapshot, fold_edits) = fold_map.read(
590 buffer.read(cx).snapshot(cx),
591 buffer_edits.consume().into_inner(),
592 );
593 let (suggestion_snapshot, _) = suggestion_map.sync(fold_snapshot.clone(), fold_edits);
594 assert_eq!(suggestion_snapshot.text(), "ABCabcDEF123\n456dGHIefghiJKL");
595
596 let (mut fold_map_writer, _, _) =
597 fold_map.write(buffer.read(cx).snapshot(cx), Default::default());
598 let (fold_snapshot, fold_edits) = fold_map_writer.fold([0..3]);
599 let (suggestion_snapshot, _) = suggestion_map.sync(fold_snapshot, fold_edits);
600 assert_eq!(suggestion_snapshot.text(), "⋯abcDEF123\n456dGHIefghiJKL");
601
602 let (mut fold_map_writer, _, _) =
603 fold_map.write(buffer.read(cx).snapshot(cx), Default::default());
604 let (fold_snapshot, fold_edits) = fold_map_writer.fold([6..10]);
605 let (suggestion_snapshot, _) = suggestion_map.sync(fold_snapshot, fold_edits);
606 assert_eq!(suggestion_snapshot.text(), "⋯abc⋯GHIefghiJKL");
607 }
608
609 #[gpui::test(iterations = 100)]
610 fn test_random_suggestions(cx: &mut MutableAppContext, mut rng: StdRng) {
611 cx.set_global(Settings::test(cx));
612 let operations = env::var("OPERATIONS")
613 .map(|i| i.parse().expect("invalid `OPERATIONS` variable"))
614 .unwrap_or(10);
615
616 let len = rng.gen_range(0..30);
617 let buffer = if rng.gen() {
618 let text = util::RandomCharIter::new(&mut rng)
619 .take(len)
620 .collect::<String>();
621 MultiBuffer::build_simple(&text, cx)
622 } else {
623 MultiBuffer::build_random(&mut rng, cx)
624 };
625 let mut buffer_snapshot = buffer.read(cx).snapshot(cx);
626 log::info!("buffer text: {:?}", buffer_snapshot.text());
627
628 let (mut fold_map, mut fold_snapshot) = FoldMap::new(buffer_snapshot.clone());
629 let (suggestion_map, mut suggestion_snapshot) = SuggestionMap::new(fold_snapshot.clone());
630
631 for _ in 0..operations {
632 let mut suggestion_edits = Patch::default();
633
634 let mut prev_suggestion_text = suggestion_snapshot.text();
635 let mut buffer_edits = Vec::new();
636 match rng.gen_range(0..=100) {
637 0..=29 => {
638 let new_suggestion = if rng.gen_bool(0.3) {
639 None
640 } else {
641 let index = rng.gen_range(0..=buffer_snapshot.len());
642 let len = rng.gen_range(0..30);
643 Some(Suggestion {
644 position: index,
645 text: util::RandomCharIter::new(&mut rng)
646 .take(len)
647 .collect::<String>()
648 .as_str()
649 .into(),
650 highlight_style: Default::default(),
651 })
652 };
653
654 log::info!("replacing suggestion with {:?}", new_suggestion);
655 let (_, edits) =
656 suggestion_map.replace(new_suggestion, fold_snapshot, Default::default());
657 suggestion_edits = suggestion_edits.compose(edits);
658 }
659 30..=59 => {
660 for (new_fold_snapshot, fold_edits) in fold_map.randomly_mutate(&mut rng) {
661 fold_snapshot = new_fold_snapshot;
662 let (_, edits) = suggestion_map.sync(fold_snapshot.clone(), fold_edits);
663 suggestion_edits = suggestion_edits.compose(edits);
664 }
665 }
666 _ => buffer.update(cx, |buffer, cx| {
667 let subscription = buffer.subscribe();
668 let edit_count = rng.gen_range(1..=5);
669 buffer.randomly_mutate(&mut rng, edit_count, cx);
670 buffer_snapshot = buffer.snapshot(cx);
671 let edits = subscription.consume().into_inner();
672 log::info!("editing {:?}", edits);
673 buffer_edits.extend(edits);
674 }),
675 };
676
677 let (new_fold_snapshot, fold_edits) =
678 fold_map.read(buffer_snapshot.clone(), buffer_edits);
679 fold_snapshot = new_fold_snapshot;
680 let (new_suggestion_snapshot, edits) =
681 suggestion_map.sync(fold_snapshot.clone(), fold_edits);
682 suggestion_snapshot = new_suggestion_snapshot;
683 suggestion_edits = suggestion_edits.compose(edits);
684
685 log::info!("buffer text: {:?}", buffer_snapshot.text());
686 log::info!("folds text: {:?}", fold_snapshot.text());
687 log::info!("suggestions text: {:?}", suggestion_snapshot.text());
688
689 let mut expected_text = Rope::from(fold_snapshot.text().as_str());
690 let mut expected_buffer_rows = fold_snapshot.buffer_rows(0).collect::<Vec<_>>();
691 if let Some(suggestion) = suggestion_snapshot.suggestion.as_ref() {
692 expected_text.replace(
693 suggestion.position.0..suggestion.position.0,
694 &suggestion.text.to_string(),
695 );
696 let suggestion_start = suggestion.position.to_point(&fold_snapshot).0;
697 let suggestion_end = suggestion_start + suggestion.text.max_point();
698 expected_buffer_rows.splice(
699 (suggestion_start.row + 1) as usize..(suggestion_start.row + 1) as usize,
700 (0..suggestion_end.row - suggestion_start.row).map(|_| None),
701 );
702 }
703 assert_eq!(suggestion_snapshot.text(), expected_text.to_string());
704 for row_start in 0..expected_buffer_rows.len() {
705 assert_eq!(
706 suggestion_snapshot
707 .buffer_rows(row_start as u32)
708 .collect::<Vec<_>>(),
709 &expected_buffer_rows[row_start..],
710 "incorrect buffer rows starting at {}",
711 row_start
712 );
713 }
714
715 for _ in 0..5 {
716 let mut end = rng.gen_range(0..=suggestion_snapshot.len().0);
717 end = expected_text.clip_offset(end, Bias::Right);
718 let mut start = rng.gen_range(0..=end);
719 start = expected_text.clip_offset(start, Bias::Right);
720
721 let actual_text = suggestion_snapshot
722 .chunks(SuggestionOffset(start)..SuggestionOffset(end), false, None)
723 .map(|chunk| chunk.text)
724 .collect::<String>();
725 assert_eq!(
726 actual_text,
727 expected_text.slice(start..end).to_string(),
728 "incorrect text in range {:?}",
729 start..end
730 );
731
732 let start_point = SuggestionPoint(expected_text.offset_to_point(start));
733 let end_point = SuggestionPoint(expected_text.offset_to_point(end));
734 assert_eq!(
735 suggestion_snapshot.text_summary_for_range(start_point..end_point),
736 expected_text.slice(start..end).summary()
737 );
738 }
739
740 for edit in suggestion_edits.into_inner() {
741 prev_suggestion_text.replace_range(
742 edit.new.start.0..edit.new.start.0 + edit.old_len().0,
743 &suggestion_snapshot.text()[edit.new.start.0..edit.new.end.0],
744 );
745 }
746 assert_eq!(prev_suggestion_text, suggestion_snapshot.text());
747
748 assert_eq!(expected_text.max_point(), suggestion_snapshot.max_point().0);
749 assert_eq!(expected_text.len(), suggestion_snapshot.len().0);
750 for row in 0..=suggestion_snapshot.max_point().row() {
751 assert_eq!(
752 suggestion_snapshot.line_len(row),
753 expected_text.line_len(row),
754 "incorrect line len for row {}",
755 row
756 );
757 }
758
759 let mut suggestion_point = SuggestionPoint::default();
760 let mut suggestion_offset = SuggestionOffset::default();
761 for ch in expected_text.chars() {
762 assert_eq!(
763 suggestion_snapshot.to_offset(suggestion_point),
764 suggestion_offset,
765 "invalid to_offset({:?})",
766 suggestion_point
767 );
768 assert_eq!(
769 suggestion_snapshot.to_point(suggestion_offset),
770 suggestion_point,
771 "invalid to_point({:?})",
772 suggestion_offset
773 );
774 assert_eq!(
775 suggestion_snapshot
776 .to_suggestion_point(suggestion_snapshot.to_fold_point(suggestion_point)),
777 suggestion_snapshot.clip_point(suggestion_point, Bias::Left),
778 );
779
780 let mut bytes = [0; 4];
781 for byte in ch.encode_utf8(&mut bytes).as_bytes() {
782 suggestion_offset.0 += 1;
783 if *byte == b'\n' {
784 suggestion_point.0 += Point::new(1, 0);
785 } else {
786 suggestion_point.0 += Point::new(0, 1);
787 }
788
789 let clipped_left_point =
790 suggestion_snapshot.clip_point(suggestion_point, Bias::Left);
791 let clipped_right_point =
792 suggestion_snapshot.clip_point(suggestion_point, Bias::Right);
793 assert!(
794 clipped_left_point <= clipped_right_point,
795 "clipped left point {:?} is greater than clipped right point {:?}",
796 clipped_left_point,
797 clipped_right_point
798 );
799 assert_eq!(
800 clipped_left_point.0,
801 expected_text.clip_point(clipped_left_point.0, Bias::Left)
802 );
803 assert_eq!(
804 clipped_right_point.0,
805 expected_text.clip_point(clipped_right_point.0, Bias::Right)
806 );
807 assert!(clipped_left_point <= suggestion_snapshot.max_point());
808 assert!(clipped_right_point <= suggestion_snapshot.max_point());
809
810 if let Some(suggestion) = suggestion_snapshot.suggestion.as_ref() {
811 let suggestion_start = suggestion.position.to_point(&fold_snapshot).0;
812 let suggestion_end = suggestion_start + suggestion.text.max_point();
813 let invalid_range = (
814 Bound::Excluded(suggestion_start),
815 Bound::Included(suggestion_end),
816 );
817 assert!(
818 !invalid_range.contains(&clipped_left_point.0),
819 "clipped left point {:?} is inside invalid suggestion range {:?}",
820 clipped_left_point,
821 invalid_range
822 );
823 assert!(
824 !invalid_range.contains(&clipped_right_point.0),
825 "clipped right point {:?} is inside invalid suggestion range {:?}",
826 clipped_right_point,
827 invalid_range
828 );
829 }
830 }
831 }
832 }
833 }
834}