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