1//! The inlay map. See the [`display_map`][super] docs for an overview of how the inlay map fits
2//! into the rest of the [`DisplayMap`][super::DisplayMap]. Much of the documentation for this
3//! module generalizes to other layers.
4//!
5//! The core of this module is the [`InlayMap`] struct, which maintains a vec of [`Inlay`]s, and
6//! [`InlaySnapshot`], which holds a sum tree of [`Transform`]s.
7
8use crate::{
9 ChunkRenderer, HighlightStyles,
10 inlays::{Inlay, InlayContent},
11};
12use collections::BTreeSet;
13use language::{Chunk, Edit, LanguageAwareStyling, Point, TextSummary};
14use multi_buffer::{
15 MBTextSummary, MultiBufferOffset, MultiBufferRow, MultiBufferRows, MultiBufferSnapshot,
16 RowInfo, ToOffset,
17};
18use project::InlayId;
19use std::{
20 cmp, iter,
21 ops::{Add, AddAssign, Range, Sub, SubAssign},
22 sync::Arc,
23};
24use sum_tree::{Bias, Cursor, Dimensions, SumTree};
25use text::{ChunkBitmaps, Patch};
26use ui::{ActiveTheme, IntoElement as _, ParentElement as _, Styled as _, div};
27
28use super::{Highlights, custom_highlights::CustomHighlightsChunks, fold_map::ChunkRendererId};
29
30/// Decides where the [`Inlay`]s should be displayed.
31///
32/// See the [`display_map` module documentation](crate::display_map) for more information.
33pub struct InlayMap {
34 snapshot: InlaySnapshot,
35 inlays: Vec<Inlay>,
36}
37
38#[derive(Clone)]
39pub struct InlaySnapshot {
40 pub buffer: MultiBufferSnapshot,
41 transforms: SumTree<Transform>,
42 pub version: usize,
43}
44
45impl std::ops::Deref for InlaySnapshot {
46 type Target = MultiBufferSnapshot;
47
48 fn deref(&self) -> &Self::Target {
49 &self.buffer
50 }
51}
52
53#[derive(Clone, Debug)]
54enum Transform {
55 Isomorphic(MBTextSummary),
56 Inlay(Inlay),
57}
58
59impl sum_tree::Item for Transform {
60 type Summary = TransformSummary;
61
62 #[ztracing::instrument(skip_all)]
63 fn summary(&self, _: ()) -> Self::Summary {
64 match self {
65 Transform::Isomorphic(summary) => TransformSummary {
66 input: *summary,
67 output: *summary,
68 },
69 Transform::Inlay(inlay) => TransformSummary {
70 input: MBTextSummary::default(),
71 output: MBTextSummary::from(inlay.text().summary()),
72 },
73 }
74 }
75}
76
77#[derive(Clone, Debug, Default)]
78struct TransformSummary {
79 /// Summary of the text before inlays have been applied.
80 input: MBTextSummary,
81 /// Summary of the text after inlays have been applied.
82 output: MBTextSummary,
83}
84
85impl sum_tree::ContextLessSummary for TransformSummary {
86 fn zero() -> Self {
87 Default::default()
88 }
89
90 fn add_summary(&mut self, other: &Self) {
91 self.input += other.input;
92 self.output += other.output;
93 }
94}
95
96pub type InlayEdit = Edit<InlayOffset>;
97
98#[derive(Copy, Clone, Debug, Default, Eq, Ord, PartialOrd, PartialEq)]
99pub struct InlayOffset(pub MultiBufferOffset);
100
101impl Add for InlayOffset {
102 type Output = Self;
103
104 fn add(self, rhs: Self) -> Self::Output {
105 Self(self.0 + rhs.0)
106 }
107}
108
109impl Sub for InlayOffset {
110 type Output = <MultiBufferOffset as Sub>::Output;
111
112 fn sub(self, rhs: Self) -> Self::Output {
113 self.0 - rhs.0
114 }
115}
116
117impl<T> SubAssign<T> for InlayOffset
118where
119 MultiBufferOffset: SubAssign<T>,
120{
121 fn sub_assign(&mut self, rhs: T) {
122 self.0 -= rhs;
123 }
124}
125
126impl<T> Add<T> for InlayOffset
127where
128 MultiBufferOffset: Add<T, Output = MultiBufferOffset>,
129{
130 type Output = Self;
131
132 fn add(self, rhs: T) -> Self::Output {
133 Self(self.0 + rhs)
134 }
135}
136
137impl AddAssign for InlayOffset {
138 fn add_assign(&mut self, rhs: Self) {
139 self.0 += rhs.0;
140 }
141}
142
143impl<T> AddAssign<T> for InlayOffset
144where
145 MultiBufferOffset: AddAssign<T>,
146{
147 fn add_assign(&mut self, rhs: T) {
148 self.0 += rhs;
149 }
150}
151
152impl<'a> sum_tree::Dimension<'a, TransformSummary> for InlayOffset {
153 fn zero(_cx: ()) -> Self {
154 Default::default()
155 }
156
157 fn add_summary(&mut self, summary: &'a TransformSummary, _: ()) {
158 self.0 += summary.output.len;
159 }
160}
161
162#[derive(Copy, Clone, Debug, Default, Eq, Ord, PartialOrd, PartialEq)]
163pub struct InlayPoint(pub Point);
164
165impl Add for InlayPoint {
166 type Output = Self;
167
168 fn add(self, rhs: Self) -> Self::Output {
169 Self(self.0 + rhs.0)
170 }
171}
172
173impl Sub for InlayPoint {
174 type Output = Self;
175
176 fn sub(self, rhs: Self) -> Self::Output {
177 Self(self.0 - rhs.0)
178 }
179}
180
181impl<'a> sum_tree::Dimension<'a, TransformSummary> for InlayPoint {
182 fn zero(_cx: ()) -> Self {
183 Default::default()
184 }
185
186 fn add_summary(&mut self, summary: &'a TransformSummary, _: ()) {
187 self.0 += &summary.output.lines;
188 }
189}
190
191impl<'a> sum_tree::Dimension<'a, TransformSummary> for MultiBufferOffset {
192 fn zero(_cx: ()) -> Self {
193 Default::default()
194 }
195
196 fn add_summary(&mut self, summary: &'a TransformSummary, _: ()) {
197 *self += summary.input.len;
198 }
199}
200
201impl<'a> sum_tree::Dimension<'a, TransformSummary> for Point {
202 fn zero(_cx: ()) -> Self {
203 Default::default()
204 }
205
206 fn add_summary(&mut self, summary: &'a TransformSummary, _: ()) {
207 *self += &summary.input.lines;
208 }
209}
210
211#[derive(Clone)]
212pub struct InlayBufferRows<'a> {
213 transforms: Cursor<'a, 'static, Transform, Dimensions<InlayPoint, Point>>,
214 buffer_rows: MultiBufferRows<'a>,
215 inlay_row: u32,
216 max_buffer_row: MultiBufferRow,
217}
218
219pub struct InlayChunks<'a> {
220 transforms: Cursor<'a, 'static, Transform, Dimensions<InlayOffset, MultiBufferOffset>>,
221 buffer_chunks: CustomHighlightsChunks<'a>,
222 buffer_chunk: Option<Chunk<'a>>,
223 inlay_chunks: Option<text::ChunkWithBitmaps<'a>>,
224 /// text, char bitmap, tabs bitmap
225 inlay_chunk: Option<ChunkBitmaps<'a>>,
226 output_offset: InlayOffset,
227 max_output_offset: InlayOffset,
228 highlight_styles: HighlightStyles,
229 highlights: Highlights<'a>,
230 snapshot: &'a InlaySnapshot,
231}
232
233#[derive(Clone)]
234pub struct InlayChunk<'a> {
235 pub chunk: Chunk<'a>,
236 /// Whether the inlay should be customly rendered.
237 pub renderer: Option<ChunkRenderer>,
238}
239
240impl InlayChunks<'_> {
241 #[ztracing::instrument(skip_all)]
242 pub fn seek(&mut self, new_range: Range<InlayOffset>) {
243 self.transforms.seek(&new_range.start, Bias::Right);
244
245 let buffer_range = self.snapshot.to_buffer_offset(new_range.start)
246 ..self.snapshot.to_buffer_offset(new_range.end);
247 self.buffer_chunks.seek(buffer_range);
248 self.inlay_chunks = None;
249 self.buffer_chunk = None;
250 self.output_offset = new_range.start;
251 self.max_output_offset = new_range.end;
252 }
253
254 pub fn offset(&self) -> InlayOffset {
255 self.output_offset
256 }
257}
258
259impl<'a> Iterator for InlayChunks<'a> {
260 type Item = InlayChunk<'a>;
261
262 #[ztracing::instrument(skip_all)]
263 fn next(&mut self) -> Option<Self::Item> {
264 if self.output_offset == self.max_output_offset {
265 return None;
266 }
267
268 let chunk = match self.transforms.item()? {
269 Transform::Isomorphic(_) => {
270 let chunk = self
271 .buffer_chunk
272 .get_or_insert_with(|| self.buffer_chunks.next().unwrap());
273 if chunk.text.is_empty() {
274 *chunk = self.buffer_chunks.next().unwrap();
275 }
276
277 let desired_bytes = self.transforms.end().0.0 - self.output_offset.0;
278
279 // If we're already at the transform boundary, skip to the next transform
280 if desired_bytes == 0 {
281 self.inlay_chunks = None;
282 self.transforms.next();
283 return self.next();
284 }
285
286 // Determine split index handling edge cases
287 let split_index = if desired_bytes >= chunk.text.len() {
288 chunk.text.len()
289 } else {
290 chunk.text.ceil_char_boundary(desired_bytes)
291 };
292
293 let (prefix, suffix) = chunk.text.split_at(split_index);
294 self.output_offset.0 += prefix.len();
295
296 let mask = 1u128.unbounded_shl(split_index as u32).wrapping_sub(1);
297 let chars = chunk.chars & mask;
298 let tabs = chunk.tabs & mask;
299 let newlines = chunk.newlines & mask;
300
301 chunk.chars = chunk.chars.unbounded_shr(split_index as u32);
302 chunk.tabs = chunk.tabs.unbounded_shr(split_index as u32);
303 chunk.newlines = chunk.newlines.unbounded_shr(split_index as u32);
304 chunk.text = suffix;
305
306 InlayChunk {
307 chunk: Chunk {
308 text: prefix,
309 chars,
310 tabs,
311 newlines,
312 ..chunk.clone()
313 },
314 renderer: None,
315 }
316 }
317 Transform::Inlay(inlay) => {
318 let mut inlay_style_and_highlight = None;
319 if let Some(inlay_highlights) = self.highlights.inlay_highlights {
320 for (_, inlay_id_to_data) in inlay_highlights.iter() {
321 let style_and_highlight = inlay_id_to_data.get(&inlay.id);
322 if style_and_highlight.is_some() {
323 inlay_style_and_highlight = style_and_highlight;
324 break;
325 }
326 }
327 }
328
329 let mut renderer = None;
330 let mut highlight_style = match inlay.id {
331 InlayId::EditPrediction(_) => self.highlight_styles.edit_prediction.map(|s| {
332 if inlay.text().chars().all(|c| c.is_whitespace()) {
333 s.whitespace
334 } else {
335 s.insertion
336 }
337 }),
338 InlayId::Hint(_) => self.highlight_styles.inlay_hint,
339 InlayId::DebuggerValue(_) => self.highlight_styles.inlay_hint,
340 InlayId::ReplResult(_) => {
341 let text = inlay.text().to_string();
342 renderer = Some(ChunkRenderer {
343 id: ChunkRendererId::Inlay(inlay.id),
344 render: Arc::new(move |cx| {
345 let colors = cx.theme().colors();
346 div()
347 .flex()
348 .flex_row()
349 .items_center()
350 .child(div().w_4())
351 .child(
352 div()
353 .px_1()
354 .rounded_sm()
355 .bg(colors.surface_background)
356 .text_color(colors.text_muted)
357 .text_xs()
358 .child(text.trim().to_string()),
359 )
360 .into_any_element()
361 }),
362 constrain_width: false,
363 measured_width: None,
364 });
365 self.highlight_styles.inlay_hint
366 }
367 InlayId::Color(_) => {
368 if let InlayContent::Color(color) = inlay.content {
369 renderer = Some(ChunkRenderer {
370 id: ChunkRendererId::Inlay(inlay.id),
371 render: Arc::new(move |cx| {
372 div()
373 .relative()
374 .size_3p5()
375 .child(
376 div()
377 .absolute()
378 .right_1()
379 .size_3()
380 .border_1()
381 .border_color(
382 if cx.theme().appearance().is_light() {
383 gpui::black().opacity(0.5)
384 } else {
385 gpui::white().opacity(0.5)
386 },
387 )
388 .bg(color),
389 )
390 .into_any_element()
391 }),
392 constrain_width: false,
393 measured_width: None,
394 });
395 }
396 self.highlight_styles.inlay_hint
397 }
398 };
399 let next_inlay_highlight_endpoint;
400 let offset_in_inlay = self.output_offset - self.transforms.start().0;
401 if let Some((style, highlight)) = inlay_style_and_highlight {
402 let range = &highlight.range;
403 if offset_in_inlay < range.start {
404 next_inlay_highlight_endpoint = range.start - offset_in_inlay;
405 } else if offset_in_inlay >= range.end {
406 next_inlay_highlight_endpoint = usize::MAX;
407 } else {
408 next_inlay_highlight_endpoint = range.end - offset_in_inlay;
409 highlight_style = highlight_style
410 .map(|highlight| highlight.highlight(*style))
411 .or_else(|| Some(*style));
412 }
413 } else {
414 next_inlay_highlight_endpoint = usize::MAX;
415 }
416
417 let inlay_chunks = self.inlay_chunks.get_or_insert_with(|| {
418 let start = offset_in_inlay;
419 let end = cmp::min(self.max_output_offset, self.transforms.end().0)
420 - self.transforms.start().0;
421 let chunks = inlay.text().chunks_in_range(start..end);
422 text::ChunkWithBitmaps(chunks)
423 });
424 let ChunkBitmaps {
425 text: inlay_chunk,
426 chars,
427 tabs,
428 newlines,
429 } = self
430 .inlay_chunk
431 .get_or_insert_with(|| inlay_chunks.next().unwrap());
432
433 // Determine split index handling edge cases
434 let split_index = if next_inlay_highlight_endpoint >= inlay_chunk.len() {
435 inlay_chunk.len()
436 } else if next_inlay_highlight_endpoint == 0 {
437 // Need to take at least one character to make progress
438 inlay_chunk
439 .chars()
440 .next()
441 .map(|c| c.len_utf8())
442 .unwrap_or(1)
443 } else {
444 inlay_chunk.ceil_char_boundary(next_inlay_highlight_endpoint)
445 };
446
447 let (chunk, remainder) = inlay_chunk.split_at(split_index);
448 *inlay_chunk = remainder;
449
450 let mask = 1u128.unbounded_shl(split_index as u32).wrapping_sub(1);
451 let new_chars = *chars & mask;
452 let new_tabs = *tabs & mask;
453 let new_newlines = *newlines & mask;
454
455 *chars = chars.unbounded_shr(split_index as u32);
456 *tabs = tabs.unbounded_shr(split_index as u32);
457 *newlines = newlines.unbounded_shr(split_index as u32);
458
459 if inlay_chunk.is_empty() {
460 self.inlay_chunk = None;
461 }
462
463 self.output_offset.0 += chunk.len();
464
465 InlayChunk {
466 chunk: Chunk {
467 text: chunk,
468 chars: new_chars,
469 tabs: new_tabs,
470 newlines: new_newlines,
471 highlight_style,
472 is_inlay: true,
473 ..Chunk::default()
474 },
475 renderer,
476 }
477 }
478 };
479
480 if self.output_offset >= self.transforms.end().0 {
481 self.inlay_chunks = None;
482 self.transforms.next();
483 }
484
485 Some(chunk)
486 }
487}
488
489impl InlayBufferRows<'_> {
490 #[ztracing::instrument(skip_all)]
491 pub fn seek(&mut self, row: u32) {
492 let inlay_point = InlayPoint::new(row, 0);
493 self.transforms.seek(&inlay_point, Bias::Left);
494
495 let mut buffer_point = self.transforms.start().1;
496 let buffer_row = MultiBufferRow(if row == 0 {
497 0
498 } else {
499 match self.transforms.item() {
500 Some(Transform::Isomorphic(_)) => {
501 buffer_point += inlay_point.0 - self.transforms.start().0.0;
502 buffer_point.row
503 }
504 _ => cmp::min(buffer_point.row + 1, self.max_buffer_row.0),
505 }
506 });
507 self.inlay_row = inlay_point.row();
508 self.buffer_rows.seek(buffer_row);
509 }
510}
511
512impl Iterator for InlayBufferRows<'_> {
513 type Item = RowInfo;
514
515 #[ztracing::instrument(skip_all)]
516 fn next(&mut self) -> Option<Self::Item> {
517 let buffer_row = if self.inlay_row == 0 {
518 self.buffer_rows.next().unwrap()
519 } else {
520 match self.transforms.item()? {
521 Transform::Inlay(_) => Default::default(),
522 Transform::Isomorphic(_) => self.buffer_rows.next().unwrap(),
523 }
524 };
525
526 self.inlay_row += 1;
527 self.transforms
528 .seek_forward(&InlayPoint::new(self.inlay_row, 0), Bias::Left);
529
530 Some(buffer_row)
531 }
532}
533
534impl InlayPoint {
535 pub fn new(row: u32, column: u32) -> Self {
536 Self(Point::new(row, column))
537 }
538
539 pub fn row(self) -> u32 {
540 self.0.row
541 }
542}
543
544impl InlayMap {
545 #[ztracing::instrument(skip_all)]
546 pub fn new(buffer: MultiBufferSnapshot) -> (Self, InlaySnapshot) {
547 let version = 0;
548 let snapshot = InlaySnapshot {
549 transforms: SumTree::from_iter(
550 iter::once(Transform::Isomorphic(buffer.text_summary())),
551 (),
552 ),
553 buffer,
554 version,
555 };
556
557 (
558 Self {
559 snapshot: snapshot.clone(),
560 inlays: Vec::new(),
561 },
562 snapshot,
563 )
564 }
565
566 #[ztracing::instrument(skip_all)]
567 pub fn sync(
568 &mut self,
569 buffer_snapshot: MultiBufferSnapshot,
570 mut buffer_edits: Vec<text::Edit<MultiBufferOffset>>,
571 ) -> (InlaySnapshot, Vec<InlayEdit>) {
572 let snapshot = &mut self.snapshot;
573
574 if buffer_edits.is_empty()
575 && snapshot.buffer.trailing_excerpt_update_count()
576 != buffer_snapshot.trailing_excerpt_update_count()
577 {
578 buffer_edits.push(Edit {
579 old: snapshot.buffer.len()..snapshot.buffer.len(),
580 new: buffer_snapshot.len()..buffer_snapshot.len(),
581 });
582 }
583
584 if buffer_edits.is_empty() {
585 if snapshot.buffer.edit_count() != buffer_snapshot.edit_count()
586 || snapshot.buffer.non_text_state_update_count()
587 != buffer_snapshot.non_text_state_update_count()
588 || snapshot.buffer.trailing_excerpt_update_count()
589 != buffer_snapshot.trailing_excerpt_update_count()
590 {
591 snapshot.version += 1;
592 }
593
594 snapshot.buffer = buffer_snapshot;
595 (snapshot.clone(), Vec::new())
596 } else {
597 let mut inlay_edits = Patch::default();
598 let mut new_transforms = SumTree::default();
599 let mut cursor = snapshot
600 .transforms
601 .cursor::<Dimensions<MultiBufferOffset, InlayOffset>>(());
602 let mut buffer_edits_iter = buffer_edits.iter().peekable();
603 while let Some(buffer_edit) = buffer_edits_iter.next() {
604 new_transforms.append(cursor.slice(&buffer_edit.old.start, Bias::Left), ());
605 if let Some(Transform::Isomorphic(transform)) = cursor.item()
606 && cursor.end().0 == buffer_edit.old.start
607 {
608 push_isomorphic(&mut new_transforms, *transform);
609 cursor.next();
610 }
611
612 // Remove all the inlays and transforms contained by the edit.
613 let old_start = cursor.start().1 + (buffer_edit.old.start - cursor.start().0);
614 cursor.seek(&buffer_edit.old.end, Bias::Right);
615 let old_end = cursor.start().1 + (buffer_edit.old.end - cursor.start().0);
616
617 // Push the unchanged prefix.
618 let prefix_start = new_transforms.summary().input.len;
619 let prefix_end = buffer_edit.new.start;
620 push_isomorphic(
621 &mut new_transforms,
622 buffer_snapshot.text_summary_for_range(prefix_start..prefix_end),
623 );
624 let new_start = InlayOffset(new_transforms.summary().output.len);
625
626 let start_ix = match self.inlays.binary_search_by(|probe| {
627 probe
628 .position
629 .to_offset(&buffer_snapshot)
630 .cmp(&buffer_edit.new.start)
631 .then(std::cmp::Ordering::Greater)
632 }) {
633 Ok(ix) | Err(ix) => ix,
634 };
635
636 for inlay in &self.inlays[start_ix..] {
637 if !inlay.position.is_valid(&buffer_snapshot) {
638 continue;
639 }
640 let buffer_offset = inlay.position.to_offset(&buffer_snapshot);
641 if buffer_offset > buffer_edit.new.end {
642 break;
643 }
644
645 let prefix_start = new_transforms.summary().input.len;
646 let prefix_end = buffer_offset;
647 push_isomorphic(
648 &mut new_transforms,
649 buffer_snapshot.text_summary_for_range(prefix_start..prefix_end),
650 );
651
652 new_transforms.push(Transform::Inlay(inlay.clone()), ());
653 }
654
655 // Apply the rest of the edit.
656 let transform_start = new_transforms.summary().input.len;
657 push_isomorphic(
658 &mut new_transforms,
659 buffer_snapshot.text_summary_for_range(transform_start..buffer_edit.new.end),
660 );
661 let new_end = InlayOffset(new_transforms.summary().output.len);
662 inlay_edits.push(Edit {
663 old: old_start..old_end,
664 new: new_start..new_end,
665 });
666
667 // If the next edit doesn't intersect the current isomorphic transform, then
668 // we can push its remainder.
669 if buffer_edits_iter
670 .peek()
671 .is_none_or(|edit| edit.old.start >= cursor.end().0)
672 {
673 let transform_start = new_transforms.summary().input.len;
674 let transform_end =
675 buffer_edit.new.end + (cursor.end().0 - buffer_edit.old.end);
676 push_isomorphic(
677 &mut new_transforms,
678 buffer_snapshot.text_summary_for_range(transform_start..transform_end),
679 );
680 cursor.next();
681 }
682 }
683
684 new_transforms.append(cursor.suffix(), ());
685 if new_transforms.is_empty() {
686 new_transforms.push(Transform::Isomorphic(Default::default()), ());
687 }
688
689 drop(cursor);
690 snapshot.transforms = new_transforms;
691 snapshot.version += 1;
692 snapshot.buffer = buffer_snapshot;
693 snapshot.check_invariants();
694
695 (snapshot.clone(), inlay_edits.into_inner())
696 }
697 }
698
699 #[ztracing::instrument(skip_all)]
700 pub fn splice(
701 &mut self,
702 to_remove: &[InlayId],
703 to_insert: Vec<Inlay>,
704 ) -> (InlaySnapshot, Vec<InlayEdit>) {
705 let snapshot = &mut self.snapshot;
706 let mut edits = BTreeSet::new();
707
708 self.inlays.retain(|inlay| {
709 let retain = !to_remove.contains(&inlay.id);
710 if !retain {
711 let offset = inlay.position.to_offset(&snapshot.buffer);
712 edits.insert(offset);
713 }
714 retain
715 });
716
717 for inlay_to_insert in to_insert {
718 // Avoid inserting empty inlays.
719 if inlay_to_insert.text().is_empty() {
720 continue;
721 }
722
723 let offset = inlay_to_insert.position.to_offset(&snapshot.buffer);
724 match self.inlays.binary_search_by(|probe| {
725 probe
726 .position
727 .cmp(&inlay_to_insert.position, &snapshot.buffer)
728 .then(std::cmp::Ordering::Less)
729 }) {
730 Ok(ix) | Err(ix) => {
731 self.inlays.insert(ix, inlay_to_insert);
732 }
733 }
734
735 edits.insert(offset);
736 }
737
738 let buffer_edits = edits
739 .into_iter()
740 .map(|offset| Edit {
741 old: offset..offset,
742 new: offset..offset,
743 })
744 .collect();
745 let buffer_snapshot = snapshot.buffer.clone();
746 let (snapshot, edits) = self.sync(buffer_snapshot, buffer_edits);
747 (snapshot, edits)
748 }
749
750 #[ztracing::instrument(skip_all)]
751 pub fn current_inlays(&self) -> impl Iterator<Item = &Inlay> + Default {
752 self.inlays.iter()
753 }
754
755 #[cfg(test)]
756 #[ztracing::instrument(skip_all)]
757 pub(crate) fn randomly_mutate(
758 &mut self,
759 next_inlay_id: &mut usize,
760 rng: &mut rand::rngs::StdRng,
761 ) -> (InlaySnapshot, Vec<InlayEdit>) {
762 use rand::prelude::*;
763 use util::post_inc;
764
765 let mut to_remove = Vec::new();
766 let mut to_insert = Vec::new();
767 let snapshot = &mut self.snapshot;
768 for i in 0..rng.random_range(1..=5) {
769 if self.inlays.is_empty() || rng.random() {
770 let position = snapshot
771 .buffer
772 .random_byte_range(MultiBufferOffset(0), rng)
773 .start;
774 let bias = if rng.random() {
775 Bias::Left
776 } else {
777 Bias::Right
778 };
779 let len = if rng.random_bool(0.01) {
780 0
781 } else {
782 rng.random_range(1..=5)
783 };
784 let text = util::RandomCharIter::new(&mut *rng)
785 .filter(|ch| *ch != '\r')
786 .take(len)
787 .collect::<String>();
788
789 let next_inlay = if i % 2 == 0 {
790 Inlay::mock_hint(
791 post_inc(next_inlay_id),
792 snapshot.buffer.anchor_at(position, bias),
793 &text,
794 )
795 } else {
796 Inlay::edit_prediction(
797 post_inc(next_inlay_id),
798 snapshot.buffer.anchor_at(position, bias),
799 &text,
800 )
801 };
802 let inlay_id = next_inlay.id;
803 log::info!(
804 "creating inlay {inlay_id:?} at buffer offset {position} with bias {bias:?} and text {text:?}"
805 );
806 to_insert.push(next_inlay);
807 } else {
808 to_remove.push(
809 self.inlays
810 .iter()
811 .choose(rng)
812 .map(|inlay| inlay.id)
813 .unwrap(),
814 );
815 }
816 }
817 log::info!("removing inlays: {:?}", to_remove);
818
819 let (snapshot, edits) = self.splice(&to_remove, to_insert);
820 (snapshot, edits)
821 }
822}
823
824impl InlaySnapshot {
825 #[ztracing::instrument(skip_all)]
826 pub fn to_point(&self, offset: InlayOffset) -> InlayPoint {
827 let (start, _, item) = self.transforms.find::<Dimensions<
828 InlayOffset,
829 InlayPoint,
830 MultiBufferOffset,
831 >, _>((), &offset, Bias::Right);
832 let overshoot = offset.0 - start.0.0;
833 match item {
834 Some(Transform::Isomorphic(_)) => {
835 let buffer_offset_start = start.2;
836 let buffer_offset_end = buffer_offset_start + overshoot;
837 let buffer_start = self.buffer.offset_to_point(buffer_offset_start);
838 let buffer_end = self.buffer.offset_to_point(buffer_offset_end);
839 InlayPoint(start.1.0 + (buffer_end - buffer_start))
840 }
841 Some(Transform::Inlay(inlay)) => {
842 let overshoot = inlay.text().offset_to_point(overshoot);
843 InlayPoint(start.1.0 + overshoot)
844 }
845 None => self.max_point(),
846 }
847 }
848
849 #[ztracing::instrument(skip_all)]
850 pub fn len(&self) -> InlayOffset {
851 InlayOffset(self.transforms.summary().output.len)
852 }
853
854 #[ztracing::instrument(skip_all)]
855 pub fn max_point(&self) -> InlayPoint {
856 InlayPoint(self.transforms.summary().output.lines)
857 }
858
859 #[ztracing::instrument(skip_all, fields(point))]
860 pub fn to_offset(&self, point: InlayPoint) -> InlayOffset {
861 let (start, _, item) = self
862 .transforms
863 .find::<Dimensions<InlayPoint, InlayOffset, Point>, _>((), &point, Bias::Right);
864 let overshoot = point.0 - start.0.0;
865 match item {
866 Some(Transform::Isomorphic(_)) => {
867 let buffer_point_start = start.2;
868 let buffer_point_end = buffer_point_start + overshoot;
869 let buffer_offset_start = self.buffer.point_to_offset(buffer_point_start);
870 let buffer_offset_end = self.buffer.point_to_offset(buffer_point_end);
871 InlayOffset(start.1.0 + (buffer_offset_end - buffer_offset_start))
872 }
873 Some(Transform::Inlay(inlay)) => {
874 let overshoot = inlay.text().point_to_offset(overshoot);
875 InlayOffset(start.1.0 + overshoot)
876 }
877 None => self.len(),
878 }
879 }
880 #[ztracing::instrument(skip_all)]
881 pub fn to_buffer_point(&self, point: InlayPoint) -> Point {
882 let (start, _, item) =
883 self.transforms
884 .find::<Dimensions<InlayPoint, Point>, _>((), &point, Bias::Right);
885 match item {
886 Some(Transform::Isomorphic(_)) => {
887 let overshoot = point.0 - start.0.0;
888 start.1 + overshoot
889 }
890 Some(Transform::Inlay(_)) => start.1,
891 None => self.buffer.max_point(),
892 }
893 }
894 #[ztracing::instrument(skip_all)]
895 pub fn to_buffer_offset(&self, offset: InlayOffset) -> MultiBufferOffset {
896 let (start, _, item) = self
897 .transforms
898 .find::<Dimensions<InlayOffset, MultiBufferOffset>, _>((), &offset, Bias::Right);
899 match item {
900 Some(Transform::Isomorphic(_)) => {
901 let overshoot = offset - start.0;
902 start.1 + overshoot
903 }
904 Some(Transform::Inlay(_)) => start.1,
905 None => self.buffer.len(),
906 }
907 }
908
909 #[ztracing::instrument(skip_all)]
910 pub fn to_inlay_offset(&self, offset: MultiBufferOffset) -> InlayOffset {
911 let mut cursor = self
912 .transforms
913 .cursor::<Dimensions<MultiBufferOffset, InlayOffset>>(());
914 cursor.seek(&offset, Bias::Left);
915 loop {
916 match cursor.item() {
917 Some(Transform::Isomorphic(_)) => {
918 if offset == cursor.end().0 {
919 while let Some(Transform::Inlay(inlay)) = cursor.next_item() {
920 if inlay.position.bias() == Bias::Right {
921 break;
922 } else {
923 cursor.next();
924 }
925 }
926 return cursor.end().1;
927 } else {
928 let overshoot = offset - cursor.start().0;
929 return InlayOffset(cursor.start().1.0 + overshoot);
930 }
931 }
932 Some(Transform::Inlay(inlay)) => {
933 if inlay.position.bias() == Bias::Left {
934 cursor.next();
935 } else {
936 return cursor.start().1;
937 }
938 }
939 None => {
940 return self.len();
941 }
942 }
943 }
944 }
945
946 #[ztracing::instrument(skip_all)]
947 pub fn to_inlay_point(&self, point: Point) -> InlayPoint {
948 self.inlay_point_cursor().map(point, Bias::Left)
949 }
950
951 /// Converts a buffer offset range into one or more `InlayOffset` ranges that
952 /// cover only the actual buffer text, skipping any inlay hint text that falls
953 /// within the range. When there are no inlays the returned vec contains a
954 /// single element identical to the input mapped into inlay-offset space.
955 pub fn buffer_offset_to_inlay_ranges(
956 &self,
957 range: Range<MultiBufferOffset>,
958 ) -> impl Iterator<Item = Range<InlayOffset>> {
959 let mut cursor = self
960 .transforms
961 .cursor::<Dimensions<MultiBufferOffset, InlayOffset>>(());
962 cursor.seek(&range.start, Bias::Right);
963
964 std::iter::from_fn(move || {
965 loop {
966 match cursor.item()? {
967 Transform::Isomorphic(_) => {
968 let seg_buffer_start = cursor.start().0;
969 let seg_buffer_end = cursor.end().0;
970 let seg_inlay_start = cursor.start().1;
971
972 let overlap_start = cmp::max(range.start, seg_buffer_start);
973 let overlap_end = cmp::min(range.end, seg_buffer_end);
974
975 let past_end = seg_buffer_end >= range.end;
976 cursor.next();
977
978 if overlap_start < overlap_end {
979 let inlay_start =
980 InlayOffset(seg_inlay_start.0 + (overlap_start - seg_buffer_start));
981 let inlay_end =
982 InlayOffset(seg_inlay_start.0 + (overlap_end - seg_buffer_start));
983 return Some(inlay_start..inlay_end);
984 }
985
986 if past_end {
987 return None;
988 }
989 }
990 Transform::Inlay(_) => cursor.next(),
991 }
992 }
993 })
994 }
995
996 #[ztracing::instrument(skip_all)]
997 pub fn inlay_point_cursor(&self) -> InlayPointCursor<'_> {
998 let cursor = self.transforms.cursor::<Dimensions<Point, InlayPoint>>(());
999 InlayPointCursor {
1000 cursor,
1001 transforms: &self.transforms,
1002 }
1003 }
1004
1005 #[ztracing::instrument(skip_all)]
1006 pub fn clip_point(&self, mut point: InlayPoint, mut bias: Bias) -> InlayPoint {
1007 let mut cursor = self.transforms.cursor::<Dimensions<InlayPoint, Point>>(());
1008 cursor.seek(&point, Bias::Left);
1009 loop {
1010 match cursor.item() {
1011 Some(Transform::Isomorphic(transform)) => {
1012 if cursor.start().0 == point {
1013 if let Some(Transform::Inlay(inlay)) = cursor.prev_item() {
1014 if inlay.position.bias() == Bias::Left {
1015 return point;
1016 } else if bias == Bias::Left {
1017 cursor.prev();
1018 } else if transform.first_line_chars == 0 {
1019 point.0 += Point::new(1, 0);
1020 } else {
1021 point.0 += Point::new(0, 1);
1022 }
1023 } else {
1024 return point;
1025 }
1026 } else if cursor.end().0 == point {
1027 if let Some(Transform::Inlay(inlay)) = cursor.next_item() {
1028 if inlay.position.bias() == Bias::Right {
1029 return point;
1030 } else if bias == Bias::Right {
1031 cursor.next();
1032 } else if point.0.column == 0 {
1033 point.0.row -= 1;
1034 point.0.column = self.line_len(point.0.row);
1035 } else {
1036 point.0.column -= 1;
1037 }
1038 } else {
1039 return point;
1040 }
1041 } else {
1042 let overshoot = point.0 - cursor.start().0.0;
1043 let buffer_point = cursor.start().1 + overshoot;
1044 let clipped_buffer_point = self.buffer.clip_point(buffer_point, bias);
1045 let clipped_overshoot = clipped_buffer_point - cursor.start().1;
1046 let clipped_point = InlayPoint(cursor.start().0.0 + clipped_overshoot);
1047 if clipped_point == point {
1048 return clipped_point;
1049 } else {
1050 point = clipped_point;
1051 }
1052 }
1053 }
1054 Some(Transform::Inlay(inlay)) => {
1055 if point == cursor.start().0 && inlay.position.bias() == Bias::Right {
1056 match cursor.prev_item() {
1057 Some(Transform::Inlay(inlay)) => {
1058 if inlay.position.bias() == Bias::Left {
1059 return point;
1060 }
1061 }
1062 _ => return point,
1063 }
1064 } else if point == cursor.end().0 && inlay.position.bias() == Bias::Left {
1065 match cursor.next_item() {
1066 Some(Transform::Inlay(inlay)) => {
1067 if inlay.position.bias() == Bias::Right {
1068 return point;
1069 }
1070 }
1071 _ => return point,
1072 }
1073 }
1074
1075 if bias == Bias::Left {
1076 point = cursor.start().0;
1077 cursor.prev();
1078 } else {
1079 cursor.next();
1080 point = cursor.start().0;
1081 }
1082 }
1083 None => {
1084 bias = bias.invert();
1085 if bias == Bias::Left {
1086 point = cursor.start().0;
1087 cursor.prev();
1088 } else {
1089 cursor.next();
1090 point = cursor.start().0;
1091 }
1092 }
1093 }
1094 }
1095 }
1096
1097 #[ztracing::instrument(skip_all)]
1098 pub fn text_summary(&self) -> MBTextSummary {
1099 self.transforms.summary().output
1100 }
1101
1102 #[ztracing::instrument(skip_all)]
1103 pub fn text_summary_for_range(&self, range: Range<InlayOffset>) -> MBTextSummary {
1104 let mut summary = MBTextSummary::default();
1105
1106 let mut cursor = self
1107 .transforms
1108 .cursor::<Dimensions<InlayOffset, MultiBufferOffset>>(());
1109 cursor.seek(&range.start, Bias::Right);
1110
1111 let overshoot = range.start.0 - cursor.start().0.0;
1112 match cursor.item() {
1113 Some(Transform::Isomorphic(_)) => {
1114 let buffer_start = cursor.start().1;
1115 let suffix_start = buffer_start + overshoot;
1116 let suffix_end =
1117 buffer_start + (cmp::min(cursor.end().0, range.end).0 - cursor.start().0.0);
1118 summary = self.buffer.text_summary_for_range(suffix_start..suffix_end);
1119 cursor.next();
1120 }
1121 Some(Transform::Inlay(inlay)) => {
1122 let suffix_start = overshoot;
1123 let suffix_end = cmp::min(cursor.end().0, range.end).0 - cursor.start().0.0;
1124 summary = MBTextSummary::from(
1125 inlay
1126 .text()
1127 .cursor(suffix_start)
1128 .summary::<TextSummary>(suffix_end),
1129 );
1130 cursor.next();
1131 }
1132 None => {}
1133 }
1134
1135 if range.end > cursor.start().0 {
1136 summary += cursor
1137 .summary::<_, TransformSummary>(&range.end, Bias::Right)
1138 .output;
1139
1140 let overshoot = range.end.0 - cursor.start().0.0;
1141 match cursor.item() {
1142 Some(Transform::Isomorphic(_)) => {
1143 let prefix_start = cursor.start().1;
1144 let prefix_end = prefix_start + overshoot;
1145 summary += self
1146 .buffer
1147 .text_summary_for_range::<MBTextSummary, _>(prefix_start..prefix_end);
1148 }
1149 Some(Transform::Inlay(inlay)) => {
1150 let prefix_end = overshoot;
1151 summary += inlay.text().cursor(0).summary::<TextSummary>(prefix_end);
1152 }
1153 None => {}
1154 }
1155 }
1156
1157 summary
1158 }
1159
1160 #[ztracing::instrument(skip_all)]
1161 pub fn row_infos(&self, row: u32) -> InlayBufferRows<'_> {
1162 let mut cursor = self.transforms.cursor::<Dimensions<InlayPoint, Point>>(());
1163 let inlay_point = InlayPoint::new(row, 0);
1164 cursor.seek(&inlay_point, Bias::Left);
1165
1166 let max_buffer_row = self.buffer.max_row();
1167 let mut buffer_point = cursor.start().1;
1168 let buffer_row = if row == 0 {
1169 MultiBufferRow(0)
1170 } else {
1171 match cursor.item() {
1172 Some(Transform::Isomorphic(_)) => {
1173 buffer_point += inlay_point.0 - cursor.start().0.0;
1174 MultiBufferRow(buffer_point.row)
1175 }
1176 _ => cmp::min(MultiBufferRow(buffer_point.row + 1), max_buffer_row),
1177 }
1178 };
1179
1180 InlayBufferRows {
1181 transforms: cursor,
1182 inlay_row: inlay_point.row(),
1183 buffer_rows: self.buffer.row_infos(buffer_row),
1184 max_buffer_row,
1185 }
1186 }
1187
1188 #[ztracing::instrument(skip_all)]
1189 pub fn line_len(&self, row: u32) -> u32 {
1190 let line_start = self.to_offset(InlayPoint::new(row, 0)).0;
1191 let line_end = if row >= self.max_point().row() {
1192 self.len().0
1193 } else {
1194 self.to_offset(InlayPoint::new(row + 1, 0)).0 - 1
1195 };
1196 (line_end - line_start) as u32
1197 }
1198
1199 #[ztracing::instrument(skip_all)]
1200 pub(crate) fn chunks<'a>(
1201 &'a self,
1202 range: Range<InlayOffset>,
1203 language_aware: LanguageAwareStyling,
1204 highlights: Highlights<'a>,
1205 ) -> InlayChunks<'a> {
1206 let mut cursor = self
1207 .transforms
1208 .cursor::<Dimensions<InlayOffset, MultiBufferOffset>>(());
1209 cursor.seek(&range.start, Bias::Right);
1210
1211 let buffer_range = self.to_buffer_offset(range.start)..self.to_buffer_offset(range.end);
1212 let buffer_chunks = CustomHighlightsChunks::new(
1213 buffer_range,
1214 language_aware,
1215 highlights.text_highlights,
1216 highlights.semantic_token_highlights,
1217 &self.buffer,
1218 );
1219
1220 InlayChunks {
1221 transforms: cursor,
1222 buffer_chunks,
1223 inlay_chunks: None,
1224 inlay_chunk: None,
1225 buffer_chunk: None,
1226 output_offset: range.start,
1227 max_output_offset: range.end,
1228 highlight_styles: highlights.styles,
1229 highlights,
1230 snapshot: self,
1231 }
1232 }
1233
1234 #[cfg(test)]
1235 #[ztracing::instrument(skip_all)]
1236 pub fn text(&self) -> String {
1237 self.chunks(
1238 Default::default()..self.len(),
1239 LanguageAwareStyling {
1240 tree_sitter: false,
1241 diagnostics: false,
1242 },
1243 Highlights::default(),
1244 )
1245 .map(|chunk| chunk.chunk.text)
1246 .collect()
1247 }
1248
1249 #[ztracing::instrument(skip_all)]
1250 fn check_invariants(&self) {
1251 #[cfg(any(debug_assertions, feature = "test-support"))]
1252 {
1253 assert_eq!(self.transforms.summary().input, self.buffer.text_summary());
1254 let mut transforms = self.transforms.iter().peekable();
1255 while let Some(transform) = transforms.next() {
1256 let transform_is_isomorphic = matches!(transform, Transform::Isomorphic(_));
1257 if let Some(next_transform) = transforms.peek() {
1258 let next_transform_is_isomorphic =
1259 matches!(next_transform, Transform::Isomorphic(_));
1260 assert!(
1261 !transform_is_isomorphic || !next_transform_is_isomorphic,
1262 "two adjacent isomorphic transforms"
1263 );
1264 }
1265 }
1266 }
1267 }
1268}
1269
1270pub struct InlayPointCursor<'transforms> {
1271 cursor: Cursor<'transforms, 'static, Transform, Dimensions<Point, InlayPoint>>,
1272 transforms: &'transforms SumTree<Transform>,
1273}
1274
1275impl InlayPointCursor<'_> {
1276 #[ztracing::instrument(skip_all)]
1277 pub fn map(&mut self, point: Point, bias: Bias) -> InlayPoint {
1278 let cursor = &mut self.cursor;
1279 if cursor.did_seek() {
1280 cursor.seek_forward(&point, Bias::Left);
1281 } else {
1282 cursor.seek(&point, Bias::Left);
1283 }
1284 loop {
1285 match cursor.item() {
1286 Some(Transform::Isomorphic(_)) => {
1287 if point == cursor.end().0 {
1288 while let Some(Transform::Inlay(inlay)) = cursor.next_item() {
1289 if bias == Bias::Left && inlay.position.bias() == Bias::Right {
1290 break;
1291 } else {
1292 cursor.next();
1293 }
1294 }
1295 return cursor.end().1;
1296 } else {
1297 let overshoot = point - cursor.start().0;
1298 return InlayPoint(cursor.start().1.0 + overshoot);
1299 }
1300 }
1301 Some(Transform::Inlay(inlay)) => {
1302 if inlay.position.bias() == Bias::Left || bias == Bias::Right {
1303 cursor.next();
1304 } else {
1305 return cursor.start().1;
1306 }
1307 }
1308 None => {
1309 return InlayPoint(self.transforms.summary().output.lines);
1310 }
1311 }
1312 }
1313 }
1314}
1315
1316fn push_isomorphic(sum_tree: &mut SumTree<Transform>, summary: MBTextSummary) {
1317 if summary.len == MultiBufferOffset(0) {
1318 return;
1319 }
1320
1321 let mut summary = Some(summary);
1322 sum_tree.update_last(
1323 |transform| {
1324 if let Transform::Isomorphic(transform) = transform {
1325 *transform += summary.take().unwrap();
1326 }
1327 },
1328 (),
1329 );
1330
1331 if let Some(summary) = summary {
1332 sum_tree.push(Transform::Isomorphic(summary), ());
1333 }
1334}
1335
1336#[cfg(test)]
1337mod tests {
1338 use super::*;
1339 use crate::{
1340 MultiBuffer,
1341 display_map::{HighlightKey, InlayHighlights},
1342 hover_links::InlayHighlight,
1343 };
1344 use collections::HashMap;
1345 use gpui::{App, HighlightStyle};
1346 use multi_buffer::Anchor;
1347 use project::{InlayHint, InlayHintLabel, ResolveState};
1348 use rand::prelude::*;
1349 use settings::SettingsStore;
1350 use std::{cmp::Reverse, env, sync::Arc};
1351 use sum_tree::TreeMap;
1352 use text::{BufferId, Patch, Rope};
1353 use util::RandomCharIter;
1354 use util::post_inc;
1355
1356 #[test]
1357 fn test_inlay_properties_label_padding() {
1358 assert_eq!(
1359 Inlay::hint(
1360 InlayId::Hint(0),
1361 Anchor::Min,
1362 &InlayHint {
1363 label: InlayHintLabel::String("a".to_string()),
1364 position: text::Anchor::min_for_buffer(BufferId::new(1).unwrap()),
1365 padding_left: false,
1366 padding_right: false,
1367 tooltip: None,
1368 kind: None,
1369 resolve_state: ResolveState::Resolved,
1370 },
1371 )
1372 .text()
1373 .to_string(),
1374 "a",
1375 "Should not pad label if not requested"
1376 );
1377
1378 assert_eq!(
1379 Inlay::hint(
1380 InlayId::Hint(0),
1381 Anchor::Min,
1382 &InlayHint {
1383 label: InlayHintLabel::String("a".to_string()),
1384 position: text::Anchor::min_for_buffer(BufferId::new(1).unwrap()),
1385 padding_left: true,
1386 padding_right: true,
1387 tooltip: None,
1388 kind: None,
1389 resolve_state: ResolveState::Resolved,
1390 },
1391 )
1392 .text()
1393 .to_string(),
1394 " a ",
1395 "Should pad label for every side requested"
1396 );
1397
1398 assert_eq!(
1399 Inlay::hint(
1400 InlayId::Hint(0),
1401 Anchor::Min,
1402 &InlayHint {
1403 label: InlayHintLabel::String(" a ".to_string()),
1404 position: text::Anchor::min_for_buffer(BufferId::new(1).unwrap()),
1405 padding_left: false,
1406 padding_right: false,
1407 tooltip: None,
1408 kind: None,
1409 resolve_state: ResolveState::Resolved,
1410 },
1411 )
1412 .text()
1413 .to_string(),
1414 " a ",
1415 "Should not change already padded label"
1416 );
1417
1418 assert_eq!(
1419 Inlay::hint(
1420 InlayId::Hint(0),
1421 Anchor::Min,
1422 &InlayHint {
1423 label: InlayHintLabel::String(" a ".to_string()),
1424 position: text::Anchor::min_for_buffer(BufferId::new(1).unwrap()),
1425 padding_left: true,
1426 padding_right: true,
1427 tooltip: None,
1428 kind: None,
1429 resolve_state: ResolveState::Resolved,
1430 },
1431 )
1432 .text()
1433 .to_string(),
1434 " a ",
1435 "Should not change already padded label"
1436 );
1437 }
1438
1439 #[gpui::test]
1440 fn test_inlay_hint_padding_with_multibyte_chars() {
1441 assert_eq!(
1442 Inlay::hint(
1443 InlayId::Hint(0),
1444 Anchor::Min,
1445 &InlayHint {
1446 label: InlayHintLabel::String("🎨".to_string()),
1447 position: text::Anchor::min_for_buffer(BufferId::new(1).unwrap()),
1448 padding_left: true,
1449 padding_right: true,
1450 tooltip: None,
1451 kind: None,
1452 resolve_state: ResolveState::Resolved,
1453 },
1454 )
1455 .text()
1456 .to_string(),
1457 " 🎨 ",
1458 "Should pad single emoji correctly"
1459 );
1460 }
1461
1462 #[gpui::test]
1463 fn test_basic_inlays(cx: &mut App) {
1464 let buffer = MultiBuffer::build_simple("abcdefghi", cx);
1465 let buffer_edits = buffer.update(cx, |buffer, _| buffer.subscribe());
1466 let (mut inlay_map, inlay_snapshot) = InlayMap::new(buffer.read(cx).snapshot(cx));
1467 assert_eq!(inlay_snapshot.text(), "abcdefghi");
1468 let mut next_inlay_id = 0;
1469
1470 let (inlay_snapshot, _) = inlay_map.splice(
1471 &[],
1472 vec![Inlay::mock_hint(
1473 post_inc(&mut next_inlay_id),
1474 buffer
1475 .read(cx)
1476 .snapshot(cx)
1477 .anchor_after(MultiBufferOffset(3)),
1478 "|123|",
1479 )],
1480 );
1481 assert_eq!(inlay_snapshot.text(), "abc|123|defghi");
1482 assert_eq!(
1483 inlay_snapshot.to_inlay_point(Point::new(0, 0)),
1484 InlayPoint::new(0, 0)
1485 );
1486 assert_eq!(
1487 inlay_snapshot.to_inlay_point(Point::new(0, 1)),
1488 InlayPoint::new(0, 1)
1489 );
1490 assert_eq!(
1491 inlay_snapshot.to_inlay_point(Point::new(0, 2)),
1492 InlayPoint::new(0, 2)
1493 );
1494 assert_eq!(
1495 inlay_snapshot.to_inlay_point(Point::new(0, 3)),
1496 InlayPoint::new(0, 3)
1497 );
1498 assert_eq!(
1499 inlay_snapshot.to_inlay_point(Point::new(0, 4)),
1500 InlayPoint::new(0, 9)
1501 );
1502 assert_eq!(
1503 inlay_snapshot.to_inlay_point(Point::new(0, 5)),
1504 InlayPoint::new(0, 10)
1505 );
1506 assert_eq!(
1507 inlay_snapshot.clip_point(InlayPoint::new(0, 0), Bias::Left),
1508 InlayPoint::new(0, 0)
1509 );
1510 assert_eq!(
1511 inlay_snapshot.clip_point(InlayPoint::new(0, 0), Bias::Right),
1512 InlayPoint::new(0, 0)
1513 );
1514 assert_eq!(
1515 inlay_snapshot.clip_point(InlayPoint::new(0, 3), Bias::Left),
1516 InlayPoint::new(0, 3)
1517 );
1518 assert_eq!(
1519 inlay_snapshot.clip_point(InlayPoint::new(0, 3), Bias::Right),
1520 InlayPoint::new(0, 3)
1521 );
1522 assert_eq!(
1523 inlay_snapshot.clip_point(InlayPoint::new(0, 4), Bias::Left),
1524 InlayPoint::new(0, 3)
1525 );
1526 assert_eq!(
1527 inlay_snapshot.clip_point(InlayPoint::new(0, 4), Bias::Right),
1528 InlayPoint::new(0, 9)
1529 );
1530
1531 // Edits before or after the inlay should not affect it.
1532 buffer.update(cx, |buffer, cx| {
1533 buffer.edit(
1534 [
1535 (MultiBufferOffset(2)..MultiBufferOffset(3), "x"),
1536 (MultiBufferOffset(3)..MultiBufferOffset(3), "y"),
1537 (MultiBufferOffset(4)..MultiBufferOffset(4), "z"),
1538 ],
1539 None,
1540 cx,
1541 )
1542 });
1543 let (inlay_snapshot, _) = inlay_map.sync(
1544 buffer.read(cx).snapshot(cx),
1545 buffer_edits.consume().into_inner(),
1546 );
1547 assert_eq!(inlay_snapshot.text(), "abxy|123|dzefghi");
1548
1549 // An edit surrounding the inlay should invalidate it.
1550 buffer.update(cx, |buffer, cx| {
1551 buffer.edit(
1552 [(MultiBufferOffset(4)..MultiBufferOffset(5), "D")],
1553 None,
1554 cx,
1555 )
1556 });
1557 let (inlay_snapshot, _) = inlay_map.sync(
1558 buffer.read(cx).snapshot(cx),
1559 buffer_edits.consume().into_inner(),
1560 );
1561 assert_eq!(inlay_snapshot.text(), "abxyDzefghi");
1562
1563 let (inlay_snapshot, _) = inlay_map.splice(
1564 &[],
1565 vec![
1566 Inlay::mock_hint(
1567 post_inc(&mut next_inlay_id),
1568 buffer
1569 .read(cx)
1570 .snapshot(cx)
1571 .anchor_before(MultiBufferOffset(3)),
1572 "|123|",
1573 ),
1574 Inlay::edit_prediction(
1575 post_inc(&mut next_inlay_id),
1576 buffer
1577 .read(cx)
1578 .snapshot(cx)
1579 .anchor_after(MultiBufferOffset(3)),
1580 "|456|",
1581 ),
1582 ],
1583 );
1584 assert_eq!(inlay_snapshot.text(), "abx|123||456|yDzefghi");
1585
1586 // Edits ending where the inlay starts should not move it if it has a left bias.
1587 buffer.update(cx, |buffer, cx| {
1588 buffer.edit(
1589 [(MultiBufferOffset(3)..MultiBufferOffset(3), "JKL")],
1590 None,
1591 cx,
1592 )
1593 });
1594 let (inlay_snapshot, _) = inlay_map.sync(
1595 buffer.read(cx).snapshot(cx),
1596 buffer_edits.consume().into_inner(),
1597 );
1598 assert_eq!(inlay_snapshot.text(), "abx|123|JKL|456|yDzefghi");
1599
1600 assert_eq!(
1601 inlay_snapshot.clip_point(InlayPoint::new(0, 0), Bias::Left),
1602 InlayPoint::new(0, 0)
1603 );
1604 assert_eq!(
1605 inlay_snapshot.clip_point(InlayPoint::new(0, 0), Bias::Right),
1606 InlayPoint::new(0, 0)
1607 );
1608
1609 assert_eq!(
1610 inlay_snapshot.clip_point(InlayPoint::new(0, 1), Bias::Left),
1611 InlayPoint::new(0, 1)
1612 );
1613 assert_eq!(
1614 inlay_snapshot.clip_point(InlayPoint::new(0, 1), Bias::Right),
1615 InlayPoint::new(0, 1)
1616 );
1617
1618 assert_eq!(
1619 inlay_snapshot.clip_point(InlayPoint::new(0, 2), Bias::Left),
1620 InlayPoint::new(0, 2)
1621 );
1622 assert_eq!(
1623 inlay_snapshot.clip_point(InlayPoint::new(0, 2), Bias::Right),
1624 InlayPoint::new(0, 2)
1625 );
1626
1627 assert_eq!(
1628 inlay_snapshot.clip_point(InlayPoint::new(0, 3), Bias::Left),
1629 InlayPoint::new(0, 2)
1630 );
1631 assert_eq!(
1632 inlay_snapshot.clip_point(InlayPoint::new(0, 3), Bias::Right),
1633 InlayPoint::new(0, 8)
1634 );
1635
1636 assert_eq!(
1637 inlay_snapshot.clip_point(InlayPoint::new(0, 4), Bias::Left),
1638 InlayPoint::new(0, 2)
1639 );
1640 assert_eq!(
1641 inlay_snapshot.clip_point(InlayPoint::new(0, 4), Bias::Right),
1642 InlayPoint::new(0, 8)
1643 );
1644
1645 assert_eq!(
1646 inlay_snapshot.clip_point(InlayPoint::new(0, 5), Bias::Left),
1647 InlayPoint::new(0, 2)
1648 );
1649 assert_eq!(
1650 inlay_snapshot.clip_point(InlayPoint::new(0, 5), Bias::Right),
1651 InlayPoint::new(0, 8)
1652 );
1653
1654 assert_eq!(
1655 inlay_snapshot.clip_point(InlayPoint::new(0, 6), Bias::Left),
1656 InlayPoint::new(0, 2)
1657 );
1658 assert_eq!(
1659 inlay_snapshot.clip_point(InlayPoint::new(0, 6), Bias::Right),
1660 InlayPoint::new(0, 8)
1661 );
1662
1663 assert_eq!(
1664 inlay_snapshot.clip_point(InlayPoint::new(0, 7), Bias::Left),
1665 InlayPoint::new(0, 2)
1666 );
1667 assert_eq!(
1668 inlay_snapshot.clip_point(InlayPoint::new(0, 7), Bias::Right),
1669 InlayPoint::new(0, 8)
1670 );
1671
1672 assert_eq!(
1673 inlay_snapshot.clip_point(InlayPoint::new(0, 8), Bias::Left),
1674 InlayPoint::new(0, 8)
1675 );
1676 assert_eq!(
1677 inlay_snapshot.clip_point(InlayPoint::new(0, 8), Bias::Right),
1678 InlayPoint::new(0, 8)
1679 );
1680
1681 assert_eq!(
1682 inlay_snapshot.clip_point(InlayPoint::new(0, 9), Bias::Left),
1683 InlayPoint::new(0, 9)
1684 );
1685 assert_eq!(
1686 inlay_snapshot.clip_point(InlayPoint::new(0, 9), Bias::Right),
1687 InlayPoint::new(0, 9)
1688 );
1689
1690 assert_eq!(
1691 inlay_snapshot.clip_point(InlayPoint::new(0, 10), Bias::Left),
1692 InlayPoint::new(0, 10)
1693 );
1694 assert_eq!(
1695 inlay_snapshot.clip_point(InlayPoint::new(0, 10), Bias::Right),
1696 InlayPoint::new(0, 10)
1697 );
1698
1699 assert_eq!(
1700 inlay_snapshot.clip_point(InlayPoint::new(0, 11), Bias::Left),
1701 InlayPoint::new(0, 11)
1702 );
1703 assert_eq!(
1704 inlay_snapshot.clip_point(InlayPoint::new(0, 11), Bias::Right),
1705 InlayPoint::new(0, 11)
1706 );
1707
1708 assert_eq!(
1709 inlay_snapshot.clip_point(InlayPoint::new(0, 12), Bias::Left),
1710 InlayPoint::new(0, 11)
1711 );
1712 assert_eq!(
1713 inlay_snapshot.clip_point(InlayPoint::new(0, 12), Bias::Right),
1714 InlayPoint::new(0, 17)
1715 );
1716
1717 assert_eq!(
1718 inlay_snapshot.clip_point(InlayPoint::new(0, 13), Bias::Left),
1719 InlayPoint::new(0, 11)
1720 );
1721 assert_eq!(
1722 inlay_snapshot.clip_point(InlayPoint::new(0, 13), Bias::Right),
1723 InlayPoint::new(0, 17)
1724 );
1725
1726 assert_eq!(
1727 inlay_snapshot.clip_point(InlayPoint::new(0, 14), Bias::Left),
1728 InlayPoint::new(0, 11)
1729 );
1730 assert_eq!(
1731 inlay_snapshot.clip_point(InlayPoint::new(0, 14), Bias::Right),
1732 InlayPoint::new(0, 17)
1733 );
1734
1735 assert_eq!(
1736 inlay_snapshot.clip_point(InlayPoint::new(0, 15), Bias::Left),
1737 InlayPoint::new(0, 11)
1738 );
1739 assert_eq!(
1740 inlay_snapshot.clip_point(InlayPoint::new(0, 15), Bias::Right),
1741 InlayPoint::new(0, 17)
1742 );
1743
1744 assert_eq!(
1745 inlay_snapshot.clip_point(InlayPoint::new(0, 16), Bias::Left),
1746 InlayPoint::new(0, 11)
1747 );
1748 assert_eq!(
1749 inlay_snapshot.clip_point(InlayPoint::new(0, 16), Bias::Right),
1750 InlayPoint::new(0, 17)
1751 );
1752
1753 assert_eq!(
1754 inlay_snapshot.clip_point(InlayPoint::new(0, 17), Bias::Left),
1755 InlayPoint::new(0, 17)
1756 );
1757 assert_eq!(
1758 inlay_snapshot.clip_point(InlayPoint::new(0, 17), Bias::Right),
1759 InlayPoint::new(0, 17)
1760 );
1761
1762 assert_eq!(
1763 inlay_snapshot.clip_point(InlayPoint::new(0, 18), Bias::Left),
1764 InlayPoint::new(0, 18)
1765 );
1766 assert_eq!(
1767 inlay_snapshot.clip_point(InlayPoint::new(0, 18), Bias::Right),
1768 InlayPoint::new(0, 18)
1769 );
1770
1771 // The inlays can be manually removed.
1772 let (inlay_snapshot, _) = inlay_map.splice(
1773 &inlay_map
1774 .inlays
1775 .iter()
1776 .map(|inlay| inlay.id)
1777 .collect::<Vec<InlayId>>(),
1778 Vec::new(),
1779 );
1780 assert_eq!(inlay_snapshot.text(), "abxJKLyDzefghi");
1781 }
1782
1783 #[gpui::test]
1784 fn test_inlay_buffer_rows(cx: &mut App) {
1785 let buffer = MultiBuffer::build_simple("abc\ndef\nghi", cx);
1786 let (mut inlay_map, inlay_snapshot) = InlayMap::new(buffer.read(cx).snapshot(cx));
1787 assert_eq!(inlay_snapshot.text(), "abc\ndef\nghi");
1788 let mut next_inlay_id = 0;
1789
1790 let (inlay_snapshot, _) = inlay_map.splice(
1791 &[],
1792 vec![
1793 Inlay::mock_hint(
1794 post_inc(&mut next_inlay_id),
1795 buffer
1796 .read(cx)
1797 .snapshot(cx)
1798 .anchor_before(MultiBufferOffset(0)),
1799 "|123|\n",
1800 ),
1801 Inlay::mock_hint(
1802 post_inc(&mut next_inlay_id),
1803 buffer
1804 .read(cx)
1805 .snapshot(cx)
1806 .anchor_before(MultiBufferOffset(4)),
1807 "|456|",
1808 ),
1809 Inlay::edit_prediction(
1810 post_inc(&mut next_inlay_id),
1811 buffer
1812 .read(cx)
1813 .snapshot(cx)
1814 .anchor_before(MultiBufferOffset(7)),
1815 "\n|567|\n",
1816 ),
1817 ],
1818 );
1819 assert_eq!(inlay_snapshot.text(), "|123|\nabc\n|456|def\n|567|\n\nghi");
1820 assert_eq!(
1821 inlay_snapshot
1822 .row_infos(0)
1823 .map(|info| info.buffer_row)
1824 .collect::<Vec<_>>(),
1825 vec![Some(0), None, Some(1), None, None, Some(2)]
1826 );
1827 }
1828
1829 #[gpui::test(iterations = 100)]
1830 fn test_random_inlays(cx: &mut App, mut rng: StdRng) {
1831 init_test(cx);
1832
1833 let operations = env::var("OPERATIONS")
1834 .map(|i| i.parse().expect("invalid `OPERATIONS` variable"))
1835 .unwrap_or(10);
1836
1837 let len = rng.random_range(0..30);
1838 let buffer = if rng.random() {
1839 let text = util::RandomCharIter::new(&mut rng)
1840 .take(len)
1841 .collect::<String>();
1842 MultiBuffer::build_simple(&text, cx)
1843 } else {
1844 MultiBuffer::build_random(&mut rng, cx)
1845 };
1846 let mut buffer_snapshot = buffer.read(cx).snapshot(cx);
1847 let mut next_inlay_id = 0;
1848 log::info!("buffer text: {:?}", buffer_snapshot.text());
1849 let (mut inlay_map, mut inlay_snapshot) = InlayMap::new(buffer_snapshot.clone());
1850 for _ in 0..operations {
1851 let mut inlay_edits = Patch::default();
1852
1853 let mut prev_inlay_text = inlay_snapshot.text();
1854 let mut buffer_edits = Vec::new();
1855 match rng.random_range(0..=100) {
1856 0..=50 => {
1857 let (snapshot, edits) = inlay_map.randomly_mutate(&mut next_inlay_id, &mut rng);
1858 log::info!("mutated text: {:?}", snapshot.text());
1859 inlay_edits = Patch::new(edits);
1860 }
1861 _ => buffer.update(cx, |buffer, cx| {
1862 let subscription = buffer.subscribe();
1863 let edit_count = rng.random_range(1..=5);
1864 buffer.randomly_mutate(&mut rng, edit_count, cx);
1865 buffer_snapshot = buffer.snapshot(cx);
1866 let edits = subscription.consume().into_inner();
1867 log::info!("editing {:?}", edits);
1868 buffer_edits.extend(edits);
1869 }),
1870 };
1871
1872 let (new_inlay_snapshot, new_inlay_edits) =
1873 inlay_map.sync(buffer_snapshot.clone(), buffer_edits);
1874 inlay_snapshot = new_inlay_snapshot;
1875 inlay_edits = inlay_edits.compose(new_inlay_edits);
1876
1877 log::info!("buffer text: {:?}", buffer_snapshot.text());
1878 log::info!("inlay text: {:?}", inlay_snapshot.text());
1879
1880 let inlays = inlay_map
1881 .inlays
1882 .iter()
1883 .filter(|inlay| inlay.position.is_valid(&buffer_snapshot))
1884 .map(|inlay| {
1885 let offset = inlay.position.to_offset(&buffer_snapshot);
1886 (offset, inlay.clone())
1887 })
1888 .collect::<Vec<_>>();
1889 let mut expected_text = Rope::from(&buffer_snapshot.text());
1890 for (offset, inlay) in inlays.iter().rev() {
1891 expected_text.replace(offset.0..offset.0, &inlay.text().to_string());
1892 }
1893 assert_eq!(inlay_snapshot.text(), expected_text.to_string());
1894
1895 let expected_buffer_rows = inlay_snapshot.row_infos(0).collect::<Vec<_>>();
1896 assert_eq!(
1897 expected_buffer_rows.len() as u32,
1898 expected_text.max_point().row + 1
1899 );
1900 for row_start in 0..expected_buffer_rows.len() {
1901 assert_eq!(
1902 inlay_snapshot
1903 .row_infos(row_start as u32)
1904 .collect::<Vec<_>>(),
1905 &expected_buffer_rows[row_start..],
1906 "incorrect buffer rows starting at {}",
1907 row_start
1908 );
1909 }
1910
1911 let mut text_highlights = HashMap::default();
1912 let text_highlight_count = rng.random_range(0_usize..10);
1913 let mut text_highlight_ranges = (0..text_highlight_count)
1914 .map(|_| buffer_snapshot.random_byte_range(MultiBufferOffset(0), &mut rng))
1915 .collect::<Vec<_>>();
1916 text_highlight_ranges.sort_by_key(|range| (range.start, Reverse(range.end)));
1917 log::info!("highlighting text ranges {text_highlight_ranges:?}");
1918 text_highlights.insert(
1919 HighlightKey::ColorizeBracket(0),
1920 Arc::new((
1921 HighlightStyle::default(),
1922 text_highlight_ranges
1923 .into_iter()
1924 .map(|range| {
1925 buffer_snapshot.anchor_before(range.start)
1926 ..buffer_snapshot.anchor_after(range.end)
1927 })
1928 .collect(),
1929 )),
1930 );
1931 let text_highlights = Arc::new(text_highlights);
1932
1933 let mut inlay_highlights = InlayHighlights::default();
1934 if !inlays.is_empty() {
1935 let inlay_highlight_count = rng.random_range(0..inlays.len());
1936 let mut inlay_indices = BTreeSet::default();
1937 while inlay_indices.len() < inlay_highlight_count {
1938 inlay_indices.insert(rng.random_range(0..inlays.len()));
1939 }
1940 let new_highlights = TreeMap::from_ordered_entries(
1941 inlay_indices
1942 .into_iter()
1943 .filter_map(|i| {
1944 let (_, inlay) = &inlays[i];
1945 let inlay_text_len = inlay.text().len();
1946 match inlay_text_len {
1947 0 => None,
1948 1 => Some(InlayHighlight {
1949 inlay: inlay.id,
1950 inlay_position: inlay.position,
1951 range: 0..1,
1952 }),
1953 n => {
1954 let inlay_text = inlay.text().to_string();
1955 let mut highlight_end = rng.random_range(1..n);
1956 let mut highlight_start = rng.random_range(0..highlight_end);
1957 while !inlay_text.is_char_boundary(highlight_end) {
1958 highlight_end += 1;
1959 }
1960 while !inlay_text.is_char_boundary(highlight_start) {
1961 highlight_start -= 1;
1962 }
1963 Some(InlayHighlight {
1964 inlay: inlay.id,
1965 inlay_position: inlay.position,
1966 range: highlight_start..highlight_end,
1967 })
1968 }
1969 }
1970 })
1971 .map(|highlight| (highlight.inlay, (HighlightStyle::default(), highlight))),
1972 );
1973 log::info!("highlighting inlay ranges {new_highlights:?}");
1974 inlay_highlights.insert(HighlightKey::Editor, new_highlights);
1975 }
1976
1977 for _ in 0..5 {
1978 let mut end = rng.random_range(0..=inlay_snapshot.len().0.0);
1979 end = expected_text.clip_offset(end, Bias::Right);
1980 let mut start = rng.random_range(0..=end);
1981 start = expected_text.clip_offset(start, Bias::Right);
1982
1983 let range =
1984 InlayOffset(MultiBufferOffset(start))..InlayOffset(MultiBufferOffset(end));
1985 log::info!("calling inlay_snapshot.chunks({range:?})");
1986 let actual_text = inlay_snapshot
1987 .chunks(
1988 range,
1989 LanguageAwareStyling {
1990 tree_sitter: false,
1991 diagnostics: false,
1992 },
1993 Highlights {
1994 text_highlights: Some(&text_highlights),
1995 inlay_highlights: Some(&inlay_highlights),
1996 ..Highlights::default()
1997 },
1998 )
1999 .map(|chunk| chunk.chunk.text)
2000 .collect::<String>();
2001 assert_eq!(
2002 actual_text,
2003 expected_text.slice(start..end).to_string(),
2004 "incorrect text in range {:?}",
2005 start..end
2006 );
2007
2008 assert_eq!(
2009 inlay_snapshot.text_summary_for_range(
2010 InlayOffset(MultiBufferOffset(start))..InlayOffset(MultiBufferOffset(end))
2011 ),
2012 MBTextSummary::from(expected_text.slice(start..end).summary())
2013 );
2014 }
2015
2016 for edit in inlay_edits {
2017 prev_inlay_text.replace_range(
2018 edit.new.start.0.0..edit.new.start.0.0 + edit.old_len(),
2019 &inlay_snapshot.text()[edit.new.start.0.0..edit.new.end.0.0],
2020 );
2021 }
2022 assert_eq!(prev_inlay_text, inlay_snapshot.text());
2023
2024 assert_eq!(expected_text.max_point(), inlay_snapshot.max_point().0);
2025 assert_eq!(expected_text.len(), inlay_snapshot.len().0.0);
2026
2027 let mut buffer_point = Point::default();
2028 let mut inlay_point = inlay_snapshot.to_inlay_point(buffer_point);
2029 let mut buffer_chars = buffer_snapshot.chars_at(MultiBufferOffset(0));
2030 loop {
2031 // Ensure conversion from buffer coordinates to inlay coordinates
2032 // is consistent.
2033 let buffer_offset = buffer_snapshot.point_to_offset(buffer_point);
2034 assert_eq!(
2035 inlay_snapshot.to_point(inlay_snapshot.to_inlay_offset(buffer_offset)),
2036 inlay_point
2037 );
2038
2039 // No matter which bias we clip an inlay point with, it doesn't move
2040 // because it was constructed from a buffer point.
2041 assert_eq!(
2042 inlay_snapshot.clip_point(inlay_point, Bias::Left),
2043 inlay_point,
2044 "invalid inlay point for buffer point {:?} when clipped left",
2045 buffer_point
2046 );
2047 assert_eq!(
2048 inlay_snapshot.clip_point(inlay_point, Bias::Right),
2049 inlay_point,
2050 "invalid inlay point for buffer point {:?} when clipped right",
2051 buffer_point
2052 );
2053
2054 if let Some(ch) = buffer_chars.next() {
2055 if ch == '\n' {
2056 buffer_point += Point::new(1, 0);
2057 } else {
2058 buffer_point += Point::new(0, ch.len_utf8() as u32);
2059 }
2060
2061 // Ensure that moving forward in the buffer always moves the inlay point forward as well.
2062 let new_inlay_point = inlay_snapshot.to_inlay_point(buffer_point);
2063 assert!(new_inlay_point > inlay_point);
2064 inlay_point = new_inlay_point;
2065 } else {
2066 break;
2067 }
2068 }
2069
2070 let mut inlay_point = InlayPoint::default();
2071 let mut inlay_offset = InlayOffset::default();
2072 for ch in expected_text.chars() {
2073 assert_eq!(
2074 inlay_snapshot.to_offset(inlay_point),
2075 inlay_offset,
2076 "invalid to_offset({:?})",
2077 inlay_point
2078 );
2079 assert_eq!(
2080 inlay_snapshot.to_point(inlay_offset),
2081 inlay_point,
2082 "invalid to_point({:?})",
2083 inlay_offset
2084 );
2085
2086 let mut bytes = [0; 4];
2087 for byte in ch.encode_utf8(&mut bytes).as_bytes() {
2088 inlay_offset.0 += 1;
2089 if *byte == b'\n' {
2090 inlay_point.0 += Point::new(1, 0);
2091 } else {
2092 inlay_point.0 += Point::new(0, 1);
2093 }
2094
2095 let clipped_left_point = inlay_snapshot.clip_point(inlay_point, Bias::Left);
2096 let clipped_right_point = inlay_snapshot.clip_point(inlay_point, Bias::Right);
2097 assert!(
2098 clipped_left_point <= clipped_right_point,
2099 "inlay point {:?} when clipped left is greater than when clipped right ({:?} > {:?})",
2100 inlay_point,
2101 clipped_left_point,
2102 clipped_right_point
2103 );
2104
2105 // Ensure the clipped points are at valid text locations.
2106 assert_eq!(
2107 clipped_left_point.0,
2108 expected_text.clip_point(clipped_left_point.0, Bias::Left)
2109 );
2110 assert_eq!(
2111 clipped_right_point.0,
2112 expected_text.clip_point(clipped_right_point.0, Bias::Right)
2113 );
2114
2115 // Ensure the clipped points never overshoot the end of the map.
2116 assert!(clipped_left_point <= inlay_snapshot.max_point());
2117 assert!(clipped_right_point <= inlay_snapshot.max_point());
2118
2119 // Ensure the clipped points are at valid buffer locations.
2120 assert_eq!(
2121 inlay_snapshot
2122 .to_inlay_point(inlay_snapshot.to_buffer_point(clipped_left_point)),
2123 clipped_left_point,
2124 "to_buffer_point({:?}) = {:?}",
2125 clipped_left_point,
2126 inlay_snapshot.to_buffer_point(clipped_left_point),
2127 );
2128 assert_eq!(
2129 inlay_snapshot
2130 .to_inlay_point(inlay_snapshot.to_buffer_point(clipped_right_point)),
2131 clipped_right_point,
2132 "to_buffer_point({:?}) = {:?}",
2133 clipped_right_point,
2134 inlay_snapshot.to_buffer_point(clipped_right_point),
2135 );
2136 }
2137 }
2138 }
2139 }
2140
2141 #[gpui::test(iterations = 100)]
2142 fn test_random_chunk_bitmaps(cx: &mut gpui::App, mut rng: StdRng) {
2143 init_test(cx);
2144
2145 // Generate random buffer using existing test infrastructure
2146 let text_len = rng.random_range(0..10000);
2147 let buffer = if rng.random() {
2148 let text = RandomCharIter::new(&mut rng)
2149 .take(text_len)
2150 .collect::<String>();
2151 MultiBuffer::build_simple(&text, cx)
2152 } else {
2153 MultiBuffer::build_random(&mut rng, cx)
2154 };
2155
2156 let buffer_snapshot = buffer.read(cx).snapshot(cx);
2157 let (mut inlay_map, _) = InlayMap::new(buffer_snapshot.clone());
2158
2159 // Perform random mutations to add inlays
2160 let mut next_inlay_id = 0;
2161 let mutation_count = rng.random_range(1..10);
2162 for _ in 0..mutation_count {
2163 inlay_map.randomly_mutate(&mut next_inlay_id, &mut rng);
2164 }
2165
2166 let (snapshot, _) = inlay_map.sync(buffer_snapshot, vec![]);
2167
2168 // Get all chunks and verify their bitmaps
2169 let chunks = snapshot.chunks(
2170 InlayOffset(MultiBufferOffset(0))..snapshot.len(),
2171 LanguageAwareStyling {
2172 tree_sitter: false,
2173 diagnostics: false,
2174 },
2175 Highlights::default(),
2176 );
2177
2178 for chunk in chunks.into_iter().map(|inlay_chunk| inlay_chunk.chunk) {
2179 let chunk_text = chunk.text;
2180 let chars_bitmap = chunk.chars;
2181 let tabs_bitmap = chunk.tabs;
2182
2183 // Check empty chunks have empty bitmaps
2184 if chunk_text.is_empty() {
2185 assert_eq!(
2186 chars_bitmap, 0,
2187 "Empty chunk should have empty chars bitmap"
2188 );
2189 assert_eq!(tabs_bitmap, 0, "Empty chunk should have empty tabs bitmap");
2190 continue;
2191 }
2192
2193 // Verify that chunk text doesn't exceed 128 bytes
2194 assert!(
2195 chunk_text.len() <= 128,
2196 "Chunk text length {} exceeds 128 bytes",
2197 chunk_text.len()
2198 );
2199
2200 // Verify chars bitmap
2201 let char_indices = chunk_text
2202 .char_indices()
2203 .map(|(i, _)| i)
2204 .collect::<Vec<_>>();
2205
2206 for byte_idx in 0..chunk_text.len() {
2207 let should_have_bit = char_indices.contains(&byte_idx);
2208 let has_bit = chars_bitmap & (1 << byte_idx) != 0;
2209
2210 if has_bit != should_have_bit {
2211 eprintln!("Chunk text bytes: {:?}", chunk_text.as_bytes());
2212 eprintln!("Char indices: {:?}", char_indices);
2213 eprintln!("Chars bitmap: {:#b}", chars_bitmap);
2214 assert_eq!(
2215 has_bit, should_have_bit,
2216 "Chars bitmap mismatch at byte index {} in chunk {:?}. Expected bit: {}, Got bit: {}",
2217 byte_idx, chunk_text, should_have_bit, has_bit
2218 );
2219 }
2220 }
2221
2222 // Verify tabs bitmap
2223 for (byte_idx, byte) in chunk_text.bytes().enumerate() {
2224 let is_tab = byte == b'\t';
2225 let has_bit = tabs_bitmap & (1 << byte_idx) != 0;
2226
2227 if has_bit != is_tab {
2228 eprintln!("Chunk text bytes: {:?}", chunk_text.as_bytes());
2229 eprintln!("Tabs bitmap: {:#b}", tabs_bitmap);
2230 assert_eq!(
2231 has_bit, is_tab,
2232 "Tabs bitmap mismatch at byte index {} in chunk {:?}. Byte: {:?}, Expected bit: {}, Got bit: {}",
2233 byte_idx, chunk_text, byte as char, is_tab, has_bit
2234 );
2235 }
2236 }
2237 }
2238 }
2239
2240 fn init_test(cx: &mut App) {
2241 let store = SettingsStore::test(cx);
2242 cx.set_global(store);
2243 theme_settings::init(theme::LoadThemes::JustBase, cx);
2244 }
2245
2246 /// Helper to create test highlights for an inlay
2247 fn create_inlay_highlights(
2248 inlay_id: InlayId,
2249 highlight_range: Range<usize>,
2250 position: Anchor,
2251 ) -> TreeMap<HighlightKey, TreeMap<InlayId, (HighlightStyle, InlayHighlight)>> {
2252 let mut inlay_highlights = TreeMap::default();
2253 let mut type_highlights = TreeMap::default();
2254 type_highlights.insert(
2255 inlay_id,
2256 (
2257 HighlightStyle::default(),
2258 InlayHighlight {
2259 inlay: inlay_id,
2260 range: highlight_range,
2261 inlay_position: position,
2262 },
2263 ),
2264 );
2265 inlay_highlights.insert(HighlightKey::Editor, type_highlights);
2266 inlay_highlights
2267 }
2268
2269 #[gpui::test]
2270 fn test_inlay_utf8_boundary_panic_fix(cx: &mut App) {
2271 init_test(cx);
2272
2273 // This test verifies that we handle UTF-8 character boundaries correctly
2274 // when splitting inlay text for highlighting. Previously, this would panic
2275 // when trying to split at byte 13, which is in the middle of the '…' character.
2276 //
2277 // See https://github.com/zed-industries/zed/issues/33641
2278 let buffer = MultiBuffer::build_simple("fn main() {}\n", cx);
2279 let (mut inlay_map, _) = InlayMap::new(buffer.read(cx).snapshot(cx));
2280
2281 // Create an inlay with text that contains a multi-byte character
2282 // The string "SortingDirec…" contains an ellipsis character '…' which is 3 bytes (E2 80 A6)
2283 let inlay_text = "SortingDirec…";
2284 let position = buffer.read(cx).snapshot(cx).anchor_before(Point::new(0, 5));
2285
2286 let inlay = Inlay {
2287 id: InlayId::Hint(0),
2288 position,
2289 content: InlayContent::Text(text::Rope::from(inlay_text)),
2290 };
2291
2292 let (inlay_snapshot, _) = inlay_map.splice(&[], vec![inlay]);
2293
2294 // Create highlights that request a split at byte 13, which is in the middle
2295 // of the '…' character (bytes 12..15). We include the full character.
2296 let inlay_highlights = create_inlay_highlights(InlayId::Hint(0), 0..13, position);
2297
2298 let highlights = crate::display_map::Highlights {
2299 text_highlights: None,
2300 inlay_highlights: Some(&inlay_highlights),
2301 semantic_token_highlights: None,
2302 styles: crate::display_map::HighlightStyles::default(),
2303 };
2304
2305 // Collect chunks - this previously would panic
2306 let chunks: Vec<_> = inlay_snapshot
2307 .chunks(
2308 InlayOffset(MultiBufferOffset(0))..inlay_snapshot.len(),
2309 LanguageAwareStyling {
2310 tree_sitter: false,
2311 diagnostics: false,
2312 },
2313 highlights,
2314 )
2315 .collect();
2316
2317 // Verify the chunks are correct
2318 let full_text: String = chunks.iter().map(|c| c.chunk.text).collect();
2319 assert_eq!(full_text, "fn maSortingDirec…in() {}\n");
2320
2321 // Verify the highlighted portion includes the complete ellipsis character
2322 let highlighted_chunks: Vec<_> = chunks
2323 .iter()
2324 .filter(|c| c.chunk.highlight_style.is_some() && c.chunk.is_inlay)
2325 .collect();
2326
2327 assert_eq!(highlighted_chunks.len(), 1);
2328 assert_eq!(highlighted_chunks[0].chunk.text, "SortingDirec…");
2329 }
2330
2331 #[gpui::test]
2332 fn test_inlay_utf8_boundaries(cx: &mut App) {
2333 init_test(cx);
2334
2335 struct TestCase {
2336 inlay_text: &'static str,
2337 highlight_range: Range<usize>,
2338 expected_highlighted: &'static str,
2339 description: &'static str,
2340 }
2341
2342 let test_cases = vec![
2343 TestCase {
2344 inlay_text: "Hello👋World",
2345 highlight_range: 0..7,
2346 expected_highlighted: "Hello👋",
2347 description: "Emoji boundary - rounds up to include full emoji",
2348 },
2349 TestCase {
2350 inlay_text: "Test→End",
2351 highlight_range: 0..5,
2352 expected_highlighted: "Test→",
2353 description: "Arrow boundary - rounds up to include full arrow",
2354 },
2355 TestCase {
2356 inlay_text: "café",
2357 highlight_range: 0..4,
2358 expected_highlighted: "café",
2359 description: "Accented char boundary - rounds up to include full é",
2360 },
2361 TestCase {
2362 inlay_text: "🎨🎭🎪",
2363 highlight_range: 0..5,
2364 expected_highlighted: "🎨🎭",
2365 description: "Multiple emojis - partial highlight",
2366 },
2367 TestCase {
2368 inlay_text: "普通话",
2369 highlight_range: 0..4,
2370 expected_highlighted: "普通",
2371 description: "Chinese characters - partial highlight",
2372 },
2373 TestCase {
2374 inlay_text: "Hello",
2375 highlight_range: 0..2,
2376 expected_highlighted: "He",
2377 description: "ASCII only - no adjustment needed",
2378 },
2379 TestCase {
2380 inlay_text: "👋",
2381 highlight_range: 0..1,
2382 expected_highlighted: "👋",
2383 description: "Single emoji - partial byte range includes whole char",
2384 },
2385 TestCase {
2386 inlay_text: "Test",
2387 highlight_range: 0..0,
2388 expected_highlighted: "",
2389 description: "Empty range",
2390 },
2391 TestCase {
2392 inlay_text: "🎨ABC",
2393 highlight_range: 2..5,
2394 expected_highlighted: "A",
2395 description: "Range starting mid-emoji skips the emoji",
2396 },
2397 ];
2398
2399 for test_case in test_cases {
2400 let buffer = MultiBuffer::build_simple("test", cx);
2401 let (mut inlay_map, _) = InlayMap::new(buffer.read(cx).snapshot(cx));
2402 let position = buffer.read(cx).snapshot(cx).anchor_before(Point::new(0, 2));
2403
2404 let inlay = Inlay {
2405 id: InlayId::Hint(0),
2406 position,
2407 content: InlayContent::Text(text::Rope::from(test_case.inlay_text)),
2408 };
2409
2410 let (inlay_snapshot, _) = inlay_map.splice(&[], vec![inlay]);
2411 let inlay_highlights = create_inlay_highlights(
2412 InlayId::Hint(0),
2413 test_case.highlight_range.clone(),
2414 position,
2415 );
2416
2417 let highlights = crate::display_map::Highlights {
2418 text_highlights: None,
2419 inlay_highlights: Some(&inlay_highlights),
2420 semantic_token_highlights: None,
2421 styles: crate::display_map::HighlightStyles::default(),
2422 };
2423
2424 let chunks: Vec<_> = inlay_snapshot
2425 .chunks(
2426 InlayOffset(MultiBufferOffset(0))..inlay_snapshot.len(),
2427 LanguageAwareStyling {
2428 tree_sitter: false,
2429 diagnostics: false,
2430 },
2431 highlights,
2432 )
2433 .collect();
2434
2435 // Verify we got chunks and they total to the expected text
2436 let full_text: String = chunks.iter().map(|c| c.chunk.text).collect();
2437 assert_eq!(
2438 full_text,
2439 format!("te{}st", test_case.inlay_text),
2440 "Full text mismatch for case: {}",
2441 test_case.description
2442 );
2443
2444 // Verify that the highlighted portion matches expectations
2445 let highlighted_text: String = chunks
2446 .iter()
2447 .filter(|c| c.chunk.highlight_style.is_some() && c.chunk.is_inlay)
2448 .map(|c| c.chunk.text)
2449 .collect();
2450 assert_eq!(
2451 highlighted_text, test_case.expected_highlighted,
2452 "Highlighted text mismatch for case: {} (text: '{}', range: {:?})",
2453 test_case.description, test_case.inlay_text, test_case.highlight_range
2454 );
2455 }
2456 }
2457}