inlay_map.rs

  1#![allow(unused)]
  2// TODO kb
  3
  4use std::{
  5    cmp::{self, Reverse},
  6    ops::{Add, AddAssign, Range, Sub},
  7    sync::atomic::{self, AtomicUsize},
  8};
  9
 10use crate::{Anchor, ExcerptId, InlayHintLocation, MultiBufferSnapshot, ToOffset, ToPoint};
 11
 12use super::{
 13    suggestion_map::{
 14        SuggestionBufferRows, SuggestionChunks, SuggestionEdit, SuggestionOffset, SuggestionPoint,
 15        SuggestionSnapshot,
 16    },
 17    TextHighlights,
 18};
 19use collections::{BTreeMap, HashMap, HashSet};
 20use gpui::fonts::HighlightStyle;
 21use language::{Chunk, Edit, Point, Rope, TextSummary};
 22use parking_lot::Mutex;
 23use project::InlayHint;
 24use rand::Rng;
 25use sum_tree::{Bias, Cursor, SumTree};
 26use util::post_inc;
 27
 28#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
 29pub struct InlayId(usize);
 30
 31pub struct InlayMap {
 32    snapshot: Mutex<InlaySnapshot>,
 33    next_inlay_id: usize,
 34    pub(super) inlays: HashMap<InlayId, (InlayHintLocation, Inlay)>,
 35}
 36
 37#[derive(Clone)]
 38pub struct InlaySnapshot {
 39    // TODO kb merge these two together?
 40    pub suggestion_snapshot: SuggestionSnapshot,
 41    transforms: SumTree<Transform>,
 42    pub version: usize,
 43}
 44
 45#[derive(Clone, Debug)]
 46enum Transform {
 47    Isomorphic(TextSummary),
 48    Inlay(Inlay),
 49}
 50
 51impl Transform {
 52    fn is_inlay(&self) -> bool {
 53        matches!(self, Self::Inlay(_))
 54    }
 55}
 56
 57impl sum_tree::Item for Transform {
 58    type Summary = TransformSummary;
 59
 60    fn summary(&self) -> Self::Summary {
 61        match self {
 62            Transform::Isomorphic(summary) => TransformSummary {
 63                input: summary.clone(),
 64                output: summary.clone(),
 65            },
 66            Transform::Inlay(inlay) => TransformSummary {
 67                input: TextSummary::default(),
 68                output: inlay.properties.text.summary(),
 69            },
 70        }
 71    }
 72}
 73
 74#[derive(Clone, Debug, Default)]
 75struct TransformSummary {
 76    input: TextSummary,
 77    output: TextSummary,
 78}
 79
 80impl sum_tree::Summary for TransformSummary {
 81    type Context = ();
 82
 83    fn add_summary(&mut self, other: &Self, _: &()) {
 84        self.input += &other.input;
 85        self.output += &other.output;
 86    }
 87}
 88
 89pub type InlayEdit = Edit<InlayOffset>;
 90
 91#[derive(Copy, Clone, Debug, Default, Eq, Ord, PartialOrd, PartialEq)]
 92pub struct InlayOffset(pub usize);
 93
 94impl Add for InlayOffset {
 95    type Output = Self;
 96
 97    fn add(self, rhs: Self) -> Self::Output {
 98        Self(self.0 + rhs.0)
 99    }
100}
101
102impl Sub for InlayOffset {
103    type Output = Self;
104
105    fn sub(self, rhs: Self) -> Self::Output {
106        Self(self.0 - rhs.0)
107    }
108}
109
110impl AddAssign for InlayOffset {
111    fn add_assign(&mut self, rhs: Self) {
112        self.0 += rhs.0;
113    }
114}
115
116impl<'a> sum_tree::Dimension<'a, TransformSummary> for InlayOffset {
117    fn add_summary(&mut self, summary: &'a TransformSummary, _: &()) {
118        self.0 += &summary.output.len;
119    }
120}
121
122impl<'a> sum_tree::Dimension<'a, TransformSummary> for SuggestionOffset {
123    fn add_summary(&mut self, summary: &'a TransformSummary, _: &()) {
124        self.0 += &summary.input.len;
125    }
126}
127
128#[derive(Copy, Clone, Debug, Default, Eq, Ord, PartialOrd, PartialEq)]
129pub struct InlayPoint(pub Point);
130
131impl<'a> sum_tree::Dimension<'a, TransformSummary> for InlayPoint {
132    fn add_summary(&mut self, summary: &'a TransformSummary, _: &()) {
133        self.0 += &summary.output.lines;
134    }
135}
136
137impl<'a> sum_tree::Dimension<'a, TransformSummary> for SuggestionPoint {
138    fn add_summary(&mut self, summary: &'a TransformSummary, _: &()) {
139        self.0 += &summary.input.lines;
140    }
141}
142
143#[derive(Clone)]
144pub struct InlayBufferRows<'a> {
145    suggestion_rows: SuggestionBufferRows<'a>,
146}
147
148pub struct InlayChunks<'a> {
149    transforms: Cursor<'a, Transform, (InlayOffset, SuggestionOffset)>,
150    suggestion_chunks: SuggestionChunks<'a>,
151    suggestion_chunk: Option<Chunk<'a>>,
152    inlay_chunks: Option<text::Chunks<'a>>,
153    output_offset: InlayOffset,
154    max_output_offset: InlayOffset,
155}
156
157#[derive(Debug, Clone)]
158pub struct Inlay {
159    pub(super) id: InlayId,
160    pub(super) properties: InlayProperties,
161}
162
163#[derive(Debug, Clone)]
164pub struct InlayProperties {
165    pub(super) position: Anchor,
166    pub(super) text: Rope,
167}
168
169impl<'a> Iterator for InlayChunks<'a> {
170    type Item = Chunk<'a>;
171
172    fn next(&mut self) -> Option<Self::Item> {
173        if self.output_offset == self.max_output_offset {
174            return None;
175        }
176
177        let chunk = match self.transforms.item()? {
178            Transform::Isomorphic(transform) => {
179                let chunk = self
180                    .suggestion_chunk
181                    .get_or_insert_with(|| self.suggestion_chunks.next().unwrap());
182                if chunk.text.is_empty() {
183                    *chunk = self.suggestion_chunks.next().unwrap();
184                }
185
186                let (prefix, suffix) = chunk
187                    .text
188                    .split_at(cmp::min(transform.len, chunk.text.len()));
189                chunk.text = suffix;
190                self.output_offset.0 += prefix.len();
191                Chunk {
192                    text: prefix,
193                    ..chunk.clone()
194                }
195            }
196            Transform::Inlay(inlay) => {
197                let inlay_chunks = self.inlay_chunks.get_or_insert_with(|| {
198                    let start = self.output_offset - self.transforms.start().0;
199                    let end = cmp::min(self.max_output_offset, self.transforms.end(&()).0)
200                        - self.transforms.start().0;
201                    inlay.properties.text.chunks_in_range(start.0..end.0)
202                });
203
204                let chunk = inlay_chunks.next().unwrap();
205                self.output_offset.0 += chunk.len();
206                Chunk {
207                    text: chunk,
208                    ..Default::default()
209                }
210            }
211        };
212
213        if self.output_offset == self.transforms.end(&()).0 {
214            self.transforms.next(&());
215        }
216
217        Some(chunk)
218    }
219}
220
221impl<'a> Iterator for InlayBufferRows<'a> {
222    type Item = Option<u32>;
223
224    fn next(&mut self) -> Option<Self::Item> {
225        self.suggestion_rows.next()
226    }
227}
228
229impl InlayPoint {
230    pub fn new(row: u32, column: u32) -> Self {
231        Self(Point::new(row, column))
232    }
233
234    pub fn row(self) -> u32 {
235        self.0.row
236    }
237
238    pub fn column(self) -> u32 {
239        self.0.column
240    }
241}
242
243impl InlayMap {
244    pub fn new(suggestion_snapshot: SuggestionSnapshot) -> (Self, InlaySnapshot) {
245        let snapshot = InlaySnapshot {
246            suggestion_snapshot: suggestion_snapshot.clone(),
247            version: 0,
248            transforms: SumTree::from_item(
249                Transform::Isomorphic(suggestion_snapshot.text_summary()),
250                &(),
251            ),
252        };
253
254        (
255            Self {
256                snapshot: Mutex::new(snapshot.clone()),
257                next_inlay_id: 0,
258                inlays: HashMap::default(),
259            },
260            snapshot,
261        )
262    }
263
264    pub fn sync(
265        &self,
266        suggestion_snapshot: SuggestionSnapshot,
267        suggestion_edits: Vec<SuggestionEdit>,
268    ) -> (InlaySnapshot, Vec<InlayEdit>) {
269        let mut snapshot = self.snapshot.lock();
270
271        if snapshot.suggestion_snapshot.version != suggestion_snapshot.version {
272            snapshot.version += 1;
273        }
274
275        let mut new_transforms = SumTree::new();
276        let mut cursor = snapshot.transforms.cursor::<SuggestionOffset>();
277        let mut suggestion_edits = suggestion_edits.iter().peekable();
278
279        while let Some(suggestion_edit) = suggestion_edits.next() {
280            if suggestion_edit.old.start >= *cursor.start() {
281                new_transforms.push_tree(
282                    cursor.slice(&suggestion_edit.old.start, Bias::Right, &()),
283                    &(),
284                );
285            }
286
287            if suggestion_edit.old.end > cursor.end(&()) {
288                cursor.seek_forward(&suggestion_edit.old.end, Bias::Right, &());
289            }
290
291            let transform_start = SuggestionOffset(new_transforms.summary().input.len);
292            let mut transform_end = suggestion_edit.new.end;
293            if suggestion_edits
294                .peek()
295                .map_or(true, |edit| edit.old.start > cursor.end(&()))
296            {
297                transform_end += cursor.end(&()) - suggestion_edit.old.end;
298                cursor.next(&());
299            }
300            new_transforms.push(
301                Transform::Isomorphic(suggestion_snapshot.text_summary_for_range(
302                    suggestion_snapshot.to_point(transform_start)
303                        ..suggestion_snapshot.to_point(transform_end),
304                )),
305                &(),
306            );
307        }
308
309        new_transforms.push_tree(cursor.suffix(&()), &());
310        drop(cursor);
311
312        snapshot.suggestion_snapshot = suggestion_snapshot;
313        snapshot.transforms = new_transforms;
314
315        (snapshot.clone(), Default::default())
316    }
317
318    pub fn splice(
319        &mut self,
320        to_remove: HashSet<InlayId>,
321        to_insert: Vec<(InlayHintLocation, InlayProperties)>,
322    ) -> (InlaySnapshot, Vec<InlayEdit>, Vec<InlayId>) {
323        let mut snapshot = self.snapshot.lock();
324
325        let mut inlays = BTreeMap::new();
326        let mut new_ids = Vec::new();
327        for (location, properties) in to_insert {
328            let inlay = Inlay {
329                id: InlayId(post_inc(&mut self.next_inlay_id)),
330                properties,
331            };
332            self.inlays.insert(inlay.id, (location, inlay.clone()));
333            new_ids.push(inlay.id);
334
335            let buffer_point = inlay
336                .properties
337                .position
338                .to_point(snapshot.buffer_snapshot());
339            let fold_point = snapshot
340                .suggestion_snapshot
341                .fold_snapshot
342                .to_fold_point(buffer_point, Bias::Left);
343            let suggestion_point = snapshot.suggestion_snapshot.to_suggestion_point(fold_point);
344            let inlay_point = snapshot.to_inlay_point(suggestion_point);
345
346            inlays.insert((inlay_point, Reverse(inlay.id)), Some(inlay));
347        }
348
349        for inlay_id in to_remove {
350            if let Some((_, inlay)) = self.inlays.remove(&inlay_id) {
351                let buffer_point = inlay
352                    .properties
353                    .position
354                    .to_point(snapshot.buffer_snapshot());
355                let fold_point = snapshot
356                    .suggestion_snapshot
357                    .fold_snapshot
358                    .to_fold_point(buffer_point, Bias::Left);
359                let suggestion_point = snapshot.suggestion_snapshot.to_suggestion_point(fold_point);
360                let inlay_point = snapshot.to_inlay_point(suggestion_point);
361                inlays.insert((inlay_point, Reverse(inlay.id)), None);
362            }
363        }
364
365        let mut new_transforms = SumTree::new();
366        let mut cursor = snapshot
367            .transforms
368            .cursor::<(InlayPoint, SuggestionPoint)>();
369        for ((inlay_point, inlay_id), inlay) in inlays {
370            new_transforms.push_tree(cursor.slice(&inlay_point, Bias::Right, &()), &());
371            while let Some(transform) = cursor.item() {
372                match transform {
373                    Transform::Isomorphic(_) => break,
374                    Transform::Inlay(inlay) => {
375                        if inlay.id > inlay_id.0 {
376                            new_transforms.push(transform.clone(), &());
377                            cursor.next(&());
378                        } else {
379                            if inlay.id == inlay_id.0 {
380                                cursor.next(&());
381                            }
382                            break;
383                        }
384                    }
385                }
386            }
387
388            if let Some(inlay) = inlay {
389                if let Some(Transform::Isomorphic(transform)) = cursor.item() {
390                    let prefix = inlay_point.0 - cursor.start().0 .0;
391                    if !prefix.is_zero() {
392                        let prefix_suggestion_start = cursor.start().1;
393                        let prefix_suggestion_end = SuggestionPoint(cursor.start().1 .0 + prefix);
394                        new_transforms.push(
395                            Transform::Isomorphic(
396                                snapshot.suggestion_snapshot.text_summary_for_range(
397                                    prefix_suggestion_start..prefix_suggestion_end,
398                                ),
399                            ),
400                            &(),
401                        );
402                    }
403
404                    new_transforms.push(Transform::Inlay(inlay), &());
405
406                    let suffix_suggestion_start = SuggestionPoint(cursor.start().1 .0 + prefix);
407                    let suffix_suggestion_end = cursor.end(&()).1;
408                    new_transforms.push(
409                        Transform::Isomorphic(snapshot.suggestion_snapshot.text_summary_for_range(
410                            suffix_suggestion_start..suffix_suggestion_end,
411                        )),
412                        &(),
413                    );
414
415                    cursor.next(&());
416                } else {
417                    new_transforms.push(Transform::Inlay(inlay), &());
418                }
419            }
420        }
421
422        new_transforms.push_tree(cursor.suffix(&()), &());
423        drop(cursor);
424        snapshot.transforms = new_transforms;
425        snapshot.version += 1;
426
427        (snapshot.clone(), Vec::new(), new_ids)
428    }
429}
430
431impl InlaySnapshot {
432    pub fn buffer_snapshot(&self) -> &MultiBufferSnapshot {
433        self.suggestion_snapshot.buffer_snapshot()
434    }
435
436    pub fn to_point(&self, offset: InlayOffset) -> InlayPoint {
437        let mut cursor = self
438            .transforms
439            .cursor::<(InlayOffset, (InlayPoint, SuggestionOffset))>();
440        cursor.seek(&offset, Bias::Right, &());
441        let overshoot = offset.0 - cursor.start().0 .0;
442        match cursor.item() {
443            Some(Transform::Isomorphic(transform)) => {
444                let suggestion_offset_start = cursor.start().1 .1;
445                let suggestion_offset_end = SuggestionOffset(suggestion_offset_start.0 + overshoot);
446                let suggestion_start = self.suggestion_snapshot.to_point(suggestion_offset_start);
447                let suggestion_end = self.suggestion_snapshot.to_point(suggestion_offset_end);
448                InlayPoint(cursor.start().1 .0 .0 + (suggestion_end.0 - suggestion_start.0))
449            }
450            Some(Transform::Inlay(inlay)) => {
451                let overshoot = inlay.properties.text.offset_to_point(overshoot);
452                InlayPoint(cursor.start().1 .0 .0 + overshoot)
453            }
454            None => self.max_point(),
455        }
456    }
457
458    pub fn len(&self) -> InlayOffset {
459        InlayOffset(self.transforms.summary().output.len)
460    }
461
462    pub fn max_point(&self) -> InlayPoint {
463        InlayPoint(self.transforms.summary().output.lines)
464    }
465
466    pub fn to_offset(&self, point: InlayPoint) -> InlayOffset {
467        let mut cursor = self
468            .transforms
469            .cursor::<(InlayPoint, (InlayOffset, SuggestionPoint))>();
470        cursor.seek(&point, Bias::Right, &());
471        let overshoot = point.0 - cursor.start().0 .0;
472        match cursor.item() {
473            Some(Transform::Isomorphic(transform)) => {
474                let suggestion_point_start = cursor.start().1 .1;
475                let suggestion_point_end = SuggestionPoint(suggestion_point_start.0 + overshoot);
476                let suggestion_start = self.suggestion_snapshot.to_offset(suggestion_point_start);
477                let suggestion_end = self.suggestion_snapshot.to_offset(suggestion_point_end);
478                InlayOffset(cursor.start().1 .0 .0 + (suggestion_end.0 - suggestion_start.0))
479            }
480            Some(Transform::Inlay(inlay)) => {
481                let overshoot = inlay.properties.text.point_to_offset(overshoot);
482                InlayOffset(cursor.start().1 .0 .0 + overshoot)
483            }
484            None => self.len(),
485        }
486    }
487
488    pub fn chars_at(&self, start: InlayPoint) -> impl '_ + Iterator<Item = char> {
489        self.chunks(self.to_offset(start)..self.len(), false, None, None)
490            .flat_map(|chunk| chunk.text.chars())
491    }
492
493    pub fn to_suggestion_point(&self, point: InlayPoint) -> SuggestionPoint {
494        let mut cursor = self.transforms.cursor::<(InlayPoint, SuggestionPoint)>();
495        cursor.seek(&point, Bias::Right, &());
496        let overshoot = point.0 - cursor.start().0 .0;
497        match cursor.item() {
498            Some(Transform::Isomorphic(transform)) => {
499                SuggestionPoint(cursor.start().1 .0 + overshoot)
500            }
501            Some(Transform::Inlay(inlay)) => cursor.start().1,
502            None => self.suggestion_snapshot.max_point(),
503        }
504    }
505
506    pub fn to_suggestion_offset(&self, offset: InlayOffset) -> SuggestionOffset {
507        let mut cursor = self.transforms.cursor::<(InlayOffset, SuggestionOffset)>();
508        cursor.seek(&offset, Bias::Right, &());
509        match cursor.item() {
510            Some(Transform::Isomorphic(transform)) => {
511                let overshoot = offset - cursor.start().0;
512                cursor.start().1 + SuggestionOffset(overshoot.0)
513            }
514            Some(Transform::Inlay(inlay)) => cursor.start().1,
515            None => self.suggestion_snapshot.len(),
516        }
517    }
518
519    pub fn to_inlay_point(&self, point: SuggestionPoint) -> InlayPoint {
520        let mut cursor = self.transforms.cursor::<(SuggestionPoint, InlayPoint)>();
521        // TODO kb is the bias right? should we have an external one instead?
522        cursor.seek(&point, Bias::Right, &());
523        let overshoot = point.0 - cursor.start().0 .0;
524        match cursor.item() {
525            Some(Transform::Isomorphic(transform)) => InlayPoint(cursor.start().1 .0 + overshoot),
526            Some(Transform::Inlay(inlay)) => cursor.start().1,
527            None => self.max_point(),
528        }
529    }
530
531    pub fn clip_point(&self, point: InlayPoint, bias: Bias) -> InlayPoint {
532        let mut cursor = self.transforms.cursor::<InlayPoint>();
533        cursor.seek(&point, bias, &());
534        match cursor.item() {
535            Some(Transform::Isomorphic(_)) => return point,
536            Some(Transform::Inlay(_)) => {}
537            None => cursor.prev(&()),
538        }
539
540        while cursor
541            .item()
542            .map_or(false, |transform| transform.is_inlay())
543        {
544            match bias {
545                Bias::Left => cursor.prev(&()),
546                Bias::Right => cursor.next(&()),
547            }
548        }
549
550        match bias {
551            Bias::Left => cursor.end(&()),
552            Bias::Right => cursor.start(),
553        }
554    }
555
556    pub fn text_summary_for_range(&self, range: Range<InlayPoint>) -> TextSummary {
557        let mut summary = TextSummary::default();
558
559        let mut cursor = self.transforms.cursor::<(InlayPoint, SuggestionPoint)>();
560        cursor.seek(&range.start, Bias::Right, &());
561
562        let overshoot = range.start.0 - cursor.start().0 .0;
563        match cursor.item() {
564            Some(Transform::Isomorphic(transform)) => {
565                let suggestion_start = cursor.start().1 .0;
566                let suffix_start = SuggestionPoint(suggestion_start + overshoot);
567                let suffix_end = SuggestionPoint(
568                    suggestion_start
569                        + (cmp::min(cursor.end(&()).0, range.end).0 - cursor.start().0 .0),
570                );
571                summary = self
572                    .suggestion_snapshot
573                    .text_summary_for_range(suffix_start..suffix_end);
574                cursor.next(&());
575            }
576            Some(Transform::Inlay(inlay)) => {
577                let text = &inlay.properties.text;
578                let suffix_start = text.point_to_offset(overshoot);
579                let suffix_end = text.point_to_offset(
580                    cmp::min(cursor.end(&()).0, range.end).0 - cursor.start().0 .0,
581                );
582                summary = text.cursor(suffix_start).summary(suffix_end);
583                cursor.next(&());
584            }
585            None => {}
586        }
587
588        if range.end > cursor.start().0 {
589            summary += cursor
590                .summary::<_, TransformSummary>(&range.end, Bias::Right, &())
591                .output;
592
593            let overshoot = range.end.0 - cursor.start().0 .0;
594            match cursor.item() {
595                Some(Transform::Isomorphic(transform)) => {
596                    let prefix_start = cursor.start().1;
597                    let prefix_end = SuggestionPoint(prefix_start.0 + overshoot);
598                    summary += self
599                        .suggestion_snapshot
600                        .text_summary_for_range(prefix_start..prefix_end);
601                }
602                Some(Transform::Inlay(inlay)) => {
603                    let text = &inlay.properties.text;
604                    let prefix_end = text.point_to_offset(overshoot);
605                    summary += text.cursor(0).summary::<TextSummary>(prefix_end);
606                }
607                None => {}
608            }
609        }
610
611        summary
612    }
613
614    pub fn buffer_rows<'a>(&'a self, row: u32) -> InlayBufferRows<'a> {
615        InlayBufferRows {
616            suggestion_rows: self.suggestion_snapshot.buffer_rows(row),
617        }
618    }
619
620    pub fn line_len(&self, row: u32) -> u32 {
621        let line_start = self.to_offset(InlayPoint::new(row, 0)).0;
622        let line_end = if row >= self.max_point().row() {
623            self.len().0
624        } else {
625            self.to_offset(InlayPoint::new(row + 1, 0)).0 - 1
626        };
627        (line_end - line_start) as u32
628    }
629
630    pub fn chunks<'a>(
631        &'a self,
632        range: Range<InlayOffset>,
633        language_aware: bool,
634        text_highlights: Option<&'a TextHighlights>,
635        suggestion_highlight: Option<HighlightStyle>,
636    ) -> InlayChunks<'a> {
637        let mut cursor = self.transforms.cursor::<(InlayOffset, SuggestionOffset)>();
638        cursor.seek(&range.start, Bias::Right, &());
639
640        let suggestion_range =
641            self.to_suggestion_offset(range.start)..self.to_suggestion_offset(range.end);
642        let suggestion_chunks = self.suggestion_snapshot.chunks(
643            suggestion_range,
644            language_aware,
645            text_highlights,
646            suggestion_highlight,
647        );
648
649        InlayChunks {
650            transforms: cursor,
651            suggestion_chunks,
652            inlay_chunks: None,
653            suggestion_chunk: None,
654            output_offset: range.start,
655            max_output_offset: range.end,
656        }
657    }
658
659    #[cfg(test)]
660    pub fn text(&self) -> String {
661        self.chunks(Default::default()..self.len(), false, None, None)
662            .map(|chunk| chunk.text)
663            .collect()
664    }
665}
666
667#[cfg(test)]
668mod tests {
669    use super::*;
670    use crate::{
671        display_map::{fold_map::FoldMap, suggestion_map::SuggestionMap},
672        MultiBuffer,
673    };
674    use gpui::AppContext;
675
676    #[gpui::test]
677    fn test_basic_inlays(cx: &mut AppContext) {
678        let buffer = MultiBuffer::build_simple("abcdefghi", cx);
679        let buffer_edits = buffer.update(cx, |buffer, _| buffer.subscribe());
680        let (mut fold_map, fold_snapshot) = FoldMap::new(buffer.read(cx).snapshot(cx));
681        let (suggestion_map, suggestion_snapshot) = SuggestionMap::new(fold_snapshot.clone());
682        let (mut inlay_map, inlay_snapshot) = InlayMap::new(suggestion_snapshot.clone());
683        assert_eq!(inlay_snapshot.text(), "abcdefghi");
684
685        let (inlay_snapshot, _, inlay_ids) = inlay_map.splice(
686            HashSet::default(),
687            vec![(
688                InlayHintLocation {
689                    buffer_id: 0,
690                    excerpt_id: ExcerptId::default(),
691                },
692                InlayProperties {
693                    position: buffer.read(cx).read(cx).anchor_before(3),
694                    text: "|123|".into(),
695                },
696            )],
697        );
698        assert_eq!(inlay_snapshot.text(), "abc|123|defghi");
699
700        buffer.update(cx, |buffer, cx| buffer.edit([(0..0, "XYZ")], None, cx));
701        let (fold_snapshot, fold_edits) = fold_map.read(
702            buffer.read(cx).snapshot(cx),
703            buffer_edits.consume().into_inner(),
704        );
705        let (suggestion_snapshot, suggestion_edits) =
706            suggestion_map.sync(fold_snapshot.clone(), fold_edits);
707        let (inlay_snapshot, _) = inlay_map.sync(suggestion_snapshot.clone(), suggestion_edits);
708        assert_eq!(inlay_snapshot.text(), "XYZabc|123|defghi");
709
710        //////// case: folding and unfolding the text should hine and then return the hint back
711        let (mut fold_map_writer, _, _) = fold_map.write(
712            buffer.read(cx).snapshot(cx),
713            buffer_edits.consume().into_inner(),
714        );
715        let (fold_snapshot, fold_edits) = fold_map_writer.fold([4..8]);
716        let (suggestion_snapshot, suggestion_edits) =
717            suggestion_map.sync(fold_snapshot.clone(), fold_edits);
718        let (inlay_snapshot, _) = inlay_map.sync(suggestion_snapshot.clone(), suggestion_edits);
719        assert_eq!(inlay_snapshot.text(), "XYZa⋯fghi");
720
721        let (fold_snapshot, fold_edits) = fold_map_writer.unfold([4..8], false);
722        let (suggestion_snapshot, suggestion_edits) =
723            suggestion_map.sync(fold_snapshot.clone(), fold_edits);
724        let (inlay_snapshot, _) = inlay_map.sync(suggestion_snapshot.clone(), suggestion_edits);
725        assert_eq!(inlay_snapshot.text(), "XYZabc|123|defghi");
726
727        ////////// case: replacing the anchor that got the hint: it should disappear
728        buffer.update(cx, |buffer, cx| buffer.edit([(2..3, "C")], None, cx));
729        let (fold_snapshot, fold_edits) = fold_map.read(
730            buffer.read(cx).snapshot(cx),
731            buffer_edits.consume().into_inner(),
732        );
733        let (suggestion_snapshot, suggestion_edits) =
734            suggestion_map.sync(fold_snapshot.clone(), fold_edits);
735        let (inlay_snapshot, _) = inlay_map.sync(suggestion_snapshot.clone(), suggestion_edits);
736        assert_eq!(inlay_snapshot.text(), "XYZabCdefghi");
737    }
738}