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