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