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
982 let overshoot = range.start.0 - cursor.start().0.0;
983 match cursor.item() {
984 Some(Transform::Isomorphic(_)) => {
985 let buffer_start = cursor.start().1;
986 let suffix_start = buffer_start + overshoot;
987 let suffix_end =
988 buffer_start + (cmp::min(cursor.end().0, range.end).0 - cursor.start().0.0);
989 summary = self.buffer.text_summary_for_range(suffix_start..suffix_end);
990 cursor.next();
991 }
992 Some(Transform::Inlay(inlay)) => {
993 let suffix_start = overshoot;
994 let suffix_end = cmp::min(cursor.end().0, range.end).0 - cursor.start().0.0;
995 summary = inlay.text().cursor(suffix_start).summary(suffix_end);
996 cursor.next();
997 }
998 None => {}
999 }
1000
1001 if range.end > cursor.start().0 {
1002 summary += cursor
1003 .summary::<_, TransformSummary>(&range.end, Bias::Right)
1004 .output;
1005
1006 let overshoot = range.end.0 - cursor.start().0.0;
1007 match cursor.item() {
1008 Some(Transform::Isomorphic(_)) => {
1009 let prefix_start = cursor.start().1;
1010 let prefix_end = prefix_start + overshoot;
1011 summary += self
1012 .buffer
1013 .text_summary_for_range::<TextSummary, _>(prefix_start..prefix_end);
1014 }
1015 Some(Transform::Inlay(inlay)) => {
1016 let prefix_end = overshoot;
1017 summary += inlay.text().cursor(0).summary::<TextSummary>(prefix_end);
1018 }
1019 None => {}
1020 }
1021 }
1022
1023 summary
1024 }
1025
1026 pub fn row_infos(&self, row: u32) -> InlayBufferRows<'_> {
1027 let mut cursor = self.transforms.cursor::<Dimensions<InlayPoint, Point>>(());
1028 let inlay_point = InlayPoint::new(row, 0);
1029 cursor.seek(&inlay_point, Bias::Left);
1030
1031 let max_buffer_row = self.buffer.max_row();
1032 let mut buffer_point = cursor.start().1;
1033 let buffer_row = if row == 0 {
1034 MultiBufferRow(0)
1035 } else {
1036 match cursor.item() {
1037 Some(Transform::Isomorphic(_)) => {
1038 buffer_point += inlay_point.0 - cursor.start().0.0;
1039 MultiBufferRow(buffer_point.row)
1040 }
1041 _ => cmp::min(MultiBufferRow(buffer_point.row + 1), max_buffer_row),
1042 }
1043 };
1044
1045 InlayBufferRows {
1046 transforms: cursor,
1047 inlay_row: inlay_point.row(),
1048 buffer_rows: self.buffer.row_infos(buffer_row),
1049 max_buffer_row,
1050 }
1051 }
1052
1053 pub fn line_len(&self, row: u32) -> u32 {
1054 let line_start = self.to_offset(InlayPoint::new(row, 0)).0;
1055 let line_end = if row >= self.max_point().row() {
1056 self.len().0
1057 } else {
1058 self.to_offset(InlayPoint::new(row + 1, 0)).0 - 1
1059 };
1060 (line_end - line_start) as u32
1061 }
1062
1063 pub(crate) fn chunks<'a>(
1064 &'a self,
1065 range: Range<InlayOffset>,
1066 language_aware: bool,
1067 highlights: Highlights<'a>,
1068 ) -> InlayChunks<'a> {
1069 let mut cursor = self.transforms.cursor::<Dimensions<InlayOffset, usize>>(());
1070 cursor.seek(&range.start, Bias::Right);
1071
1072 let buffer_range = self.to_buffer_offset(range.start)..self.to_buffer_offset(range.end);
1073 let buffer_chunks = CustomHighlightsChunks::new(
1074 buffer_range,
1075 language_aware,
1076 highlights.text_highlights,
1077 &self.buffer,
1078 );
1079
1080 InlayChunks {
1081 transforms: cursor,
1082 buffer_chunks,
1083 inlay_chunks: None,
1084 inlay_chunk: None,
1085 buffer_chunk: None,
1086 output_offset: range.start,
1087 max_output_offset: range.end,
1088 highlight_styles: highlights.styles,
1089 highlights,
1090 snapshot: self,
1091 }
1092 }
1093
1094 #[cfg(test)]
1095 pub fn text(&self) -> String {
1096 self.chunks(Default::default()..self.len(), false, Highlights::default())
1097 .map(|chunk| chunk.chunk.text)
1098 .collect()
1099 }
1100
1101 fn check_invariants(&self) {
1102 #[cfg(any(debug_assertions, feature = "test-support"))]
1103 {
1104 assert_eq!(self.transforms.summary().input, self.buffer.text_summary());
1105 let mut transforms = self.transforms.iter().peekable();
1106 while let Some(transform) = transforms.next() {
1107 let transform_is_isomorphic = matches!(transform, Transform::Isomorphic(_));
1108 if let Some(next_transform) = transforms.peek() {
1109 let next_transform_is_isomorphic =
1110 matches!(next_transform, Transform::Isomorphic(_));
1111 assert!(
1112 !transform_is_isomorphic || !next_transform_is_isomorphic,
1113 "two adjacent isomorphic transforms"
1114 );
1115 }
1116 }
1117 }
1118 }
1119}
1120
1121fn push_isomorphic(sum_tree: &mut SumTree<Transform>, summary: TextSummary) {
1122 if summary.len == 0 {
1123 return;
1124 }
1125
1126 let mut summary = Some(summary);
1127 sum_tree.update_last(
1128 |transform| {
1129 if let Transform::Isomorphic(transform) = transform {
1130 *transform += summary.take().unwrap();
1131 }
1132 },
1133 (),
1134 );
1135
1136 if let Some(summary) = summary {
1137 sum_tree.push(Transform::Isomorphic(summary), ());
1138 }
1139}
1140
1141/// Given a byte index that is NOT a UTF-8 boundary, find the next one.
1142/// Assumes: 0 < byte_index < text.len() and !text.is_char_boundary(byte_index)
1143#[inline(always)]
1144fn find_next_utf8_boundary(text: &str, byte_index: usize) -> usize {
1145 let bytes = text.as_bytes();
1146 let mut idx = byte_index + 1;
1147
1148 // Scan forward until we find a boundary
1149 while idx < text.len() {
1150 if is_utf8_char_boundary(bytes[idx]) {
1151 return idx;
1152 }
1153 idx += 1;
1154 }
1155
1156 // Hit the end, return the full length
1157 text.len()
1158}
1159
1160// Private helper function taken from Rust's core::num module (which is both Apache2 and MIT licensed)
1161const fn is_utf8_char_boundary(byte: u8) -> bool {
1162 // This is bit magic equivalent to: b < 128 || b >= 192
1163 (byte as i8) >= -0x40
1164}
1165
1166#[cfg(test)]
1167mod tests {
1168 use super::*;
1169 use crate::{
1170 MultiBuffer,
1171 display_map::{HighlightKey, InlayHighlights, TextHighlights},
1172 hover_links::InlayHighlight,
1173 };
1174 use gpui::{App, HighlightStyle};
1175 use multi_buffer::Anchor;
1176 use project::{InlayHint, InlayHintLabel, ResolveState};
1177 use rand::prelude::*;
1178 use settings::SettingsStore;
1179 use std::{any::TypeId, cmp::Reverse, env, sync::Arc};
1180 use sum_tree::TreeMap;
1181 use text::{Patch, Rope};
1182 use util::RandomCharIter;
1183 use util::post_inc;
1184
1185 #[test]
1186 fn test_inlay_properties_label_padding() {
1187 assert_eq!(
1188 Inlay::hint(
1189 InlayId::Hint(0),
1190 Anchor::min(),
1191 &InlayHint {
1192 label: InlayHintLabel::String("a".to_string()),
1193 position: text::Anchor::MIN,
1194 padding_left: false,
1195 padding_right: false,
1196 tooltip: None,
1197 kind: None,
1198 resolve_state: ResolveState::Resolved,
1199 },
1200 )
1201 .text()
1202 .to_string(),
1203 "a",
1204 "Should not pad label if not requested"
1205 );
1206
1207 assert_eq!(
1208 Inlay::hint(
1209 InlayId::Hint(0),
1210 Anchor::min(),
1211 &InlayHint {
1212 label: InlayHintLabel::String("a".to_string()),
1213 position: text::Anchor::MIN,
1214 padding_left: true,
1215 padding_right: true,
1216 tooltip: None,
1217 kind: None,
1218 resolve_state: ResolveState::Resolved,
1219 },
1220 )
1221 .text()
1222 .to_string(),
1223 " a ",
1224 "Should pad label for every side requested"
1225 );
1226
1227 assert_eq!(
1228 Inlay::hint(
1229 InlayId::Hint(0),
1230 Anchor::min(),
1231 &InlayHint {
1232 label: InlayHintLabel::String(" a ".to_string()),
1233 position: text::Anchor::MIN,
1234 padding_left: false,
1235 padding_right: false,
1236 tooltip: None,
1237 kind: None,
1238 resolve_state: ResolveState::Resolved,
1239 },
1240 )
1241 .text()
1242 .to_string(),
1243 " a ",
1244 "Should not change already padded label"
1245 );
1246
1247 assert_eq!(
1248 Inlay::hint(
1249 InlayId::Hint(0),
1250 Anchor::min(),
1251 &InlayHint {
1252 label: InlayHintLabel::String(" a ".to_string()),
1253 position: text::Anchor::MIN,
1254 padding_left: true,
1255 padding_right: true,
1256 tooltip: None,
1257 kind: None,
1258 resolve_state: ResolveState::Resolved,
1259 },
1260 )
1261 .text()
1262 .to_string(),
1263 " a ",
1264 "Should not change already padded label"
1265 );
1266 }
1267
1268 #[gpui::test]
1269 fn test_inlay_hint_padding_with_multibyte_chars() {
1270 assert_eq!(
1271 Inlay::hint(
1272 InlayId::Hint(0),
1273 Anchor::min(),
1274 &InlayHint {
1275 label: InlayHintLabel::String("🎨".to_string()),
1276 position: text::Anchor::MIN,
1277 padding_left: true,
1278 padding_right: true,
1279 tooltip: None,
1280 kind: None,
1281 resolve_state: ResolveState::Resolved,
1282 },
1283 )
1284 .text()
1285 .to_string(),
1286 " 🎨 ",
1287 "Should pad single emoji correctly"
1288 );
1289 }
1290
1291 #[gpui::test]
1292 fn test_basic_inlays(cx: &mut App) {
1293 let buffer = MultiBuffer::build_simple("abcdefghi", cx);
1294 let buffer_edits = buffer.update(cx, |buffer, _| buffer.subscribe());
1295 let (mut inlay_map, inlay_snapshot) = InlayMap::new(buffer.read(cx).snapshot(cx));
1296 assert_eq!(inlay_snapshot.text(), "abcdefghi");
1297 let mut next_inlay_id = 0;
1298
1299 let (inlay_snapshot, _) = inlay_map.splice(
1300 &[],
1301 vec![Inlay::mock_hint(
1302 post_inc(&mut next_inlay_id),
1303 buffer.read(cx).snapshot(cx).anchor_after(3),
1304 "|123|",
1305 )],
1306 );
1307 assert_eq!(inlay_snapshot.text(), "abc|123|defghi");
1308 assert_eq!(
1309 inlay_snapshot.to_inlay_point(Point::new(0, 0)),
1310 InlayPoint::new(0, 0)
1311 );
1312 assert_eq!(
1313 inlay_snapshot.to_inlay_point(Point::new(0, 1)),
1314 InlayPoint::new(0, 1)
1315 );
1316 assert_eq!(
1317 inlay_snapshot.to_inlay_point(Point::new(0, 2)),
1318 InlayPoint::new(0, 2)
1319 );
1320 assert_eq!(
1321 inlay_snapshot.to_inlay_point(Point::new(0, 3)),
1322 InlayPoint::new(0, 3)
1323 );
1324 assert_eq!(
1325 inlay_snapshot.to_inlay_point(Point::new(0, 4)),
1326 InlayPoint::new(0, 9)
1327 );
1328 assert_eq!(
1329 inlay_snapshot.to_inlay_point(Point::new(0, 5)),
1330 InlayPoint::new(0, 10)
1331 );
1332 assert_eq!(
1333 inlay_snapshot.clip_point(InlayPoint::new(0, 0), Bias::Left),
1334 InlayPoint::new(0, 0)
1335 );
1336 assert_eq!(
1337 inlay_snapshot.clip_point(InlayPoint::new(0, 0), Bias::Right),
1338 InlayPoint::new(0, 0)
1339 );
1340 assert_eq!(
1341 inlay_snapshot.clip_point(InlayPoint::new(0, 3), Bias::Left),
1342 InlayPoint::new(0, 3)
1343 );
1344 assert_eq!(
1345 inlay_snapshot.clip_point(InlayPoint::new(0, 3), Bias::Right),
1346 InlayPoint::new(0, 3)
1347 );
1348 assert_eq!(
1349 inlay_snapshot.clip_point(InlayPoint::new(0, 4), Bias::Left),
1350 InlayPoint::new(0, 3)
1351 );
1352 assert_eq!(
1353 inlay_snapshot.clip_point(InlayPoint::new(0, 4), Bias::Right),
1354 InlayPoint::new(0, 9)
1355 );
1356
1357 // Edits before or after the inlay should not affect it.
1358 buffer.update(cx, |buffer, cx| {
1359 buffer.edit([(2..3, "x"), (3..3, "y"), (4..4, "z")], None, cx)
1360 });
1361 let (inlay_snapshot, _) = inlay_map.sync(
1362 buffer.read(cx).snapshot(cx),
1363 buffer_edits.consume().into_inner(),
1364 );
1365 assert_eq!(inlay_snapshot.text(), "abxy|123|dzefghi");
1366
1367 // An edit surrounding the inlay should invalidate it.
1368 buffer.update(cx, |buffer, cx| buffer.edit([(4..5, "D")], None, cx));
1369 let (inlay_snapshot, _) = inlay_map.sync(
1370 buffer.read(cx).snapshot(cx),
1371 buffer_edits.consume().into_inner(),
1372 );
1373 assert_eq!(inlay_snapshot.text(), "abxyDzefghi");
1374
1375 let (inlay_snapshot, _) = inlay_map.splice(
1376 &[],
1377 vec![
1378 Inlay::mock_hint(
1379 post_inc(&mut next_inlay_id),
1380 buffer.read(cx).snapshot(cx).anchor_before(3),
1381 "|123|",
1382 ),
1383 Inlay::edit_prediction(
1384 post_inc(&mut next_inlay_id),
1385 buffer.read(cx).snapshot(cx).anchor_after(3),
1386 "|456|",
1387 ),
1388 ],
1389 );
1390 assert_eq!(inlay_snapshot.text(), "abx|123||456|yDzefghi");
1391
1392 // Edits ending where the inlay starts should not move it if it has a left bias.
1393 buffer.update(cx, |buffer, cx| buffer.edit([(3..3, "JKL")], None, cx));
1394 let (inlay_snapshot, _) = inlay_map.sync(
1395 buffer.read(cx).snapshot(cx),
1396 buffer_edits.consume().into_inner(),
1397 );
1398 assert_eq!(inlay_snapshot.text(), "abx|123|JKL|456|yDzefghi");
1399
1400 assert_eq!(
1401 inlay_snapshot.clip_point(InlayPoint::new(0, 0), Bias::Left),
1402 InlayPoint::new(0, 0)
1403 );
1404 assert_eq!(
1405 inlay_snapshot.clip_point(InlayPoint::new(0, 0), Bias::Right),
1406 InlayPoint::new(0, 0)
1407 );
1408
1409 assert_eq!(
1410 inlay_snapshot.clip_point(InlayPoint::new(0, 1), Bias::Left),
1411 InlayPoint::new(0, 1)
1412 );
1413 assert_eq!(
1414 inlay_snapshot.clip_point(InlayPoint::new(0, 1), Bias::Right),
1415 InlayPoint::new(0, 1)
1416 );
1417
1418 assert_eq!(
1419 inlay_snapshot.clip_point(InlayPoint::new(0, 2), Bias::Left),
1420 InlayPoint::new(0, 2)
1421 );
1422 assert_eq!(
1423 inlay_snapshot.clip_point(InlayPoint::new(0, 2), Bias::Right),
1424 InlayPoint::new(0, 2)
1425 );
1426
1427 assert_eq!(
1428 inlay_snapshot.clip_point(InlayPoint::new(0, 3), Bias::Left),
1429 InlayPoint::new(0, 2)
1430 );
1431 assert_eq!(
1432 inlay_snapshot.clip_point(InlayPoint::new(0, 3), Bias::Right),
1433 InlayPoint::new(0, 8)
1434 );
1435
1436 assert_eq!(
1437 inlay_snapshot.clip_point(InlayPoint::new(0, 4), Bias::Left),
1438 InlayPoint::new(0, 2)
1439 );
1440 assert_eq!(
1441 inlay_snapshot.clip_point(InlayPoint::new(0, 4), Bias::Right),
1442 InlayPoint::new(0, 8)
1443 );
1444
1445 assert_eq!(
1446 inlay_snapshot.clip_point(InlayPoint::new(0, 5), Bias::Left),
1447 InlayPoint::new(0, 2)
1448 );
1449 assert_eq!(
1450 inlay_snapshot.clip_point(InlayPoint::new(0, 5), Bias::Right),
1451 InlayPoint::new(0, 8)
1452 );
1453
1454 assert_eq!(
1455 inlay_snapshot.clip_point(InlayPoint::new(0, 6), Bias::Left),
1456 InlayPoint::new(0, 2)
1457 );
1458 assert_eq!(
1459 inlay_snapshot.clip_point(InlayPoint::new(0, 6), Bias::Right),
1460 InlayPoint::new(0, 8)
1461 );
1462
1463 assert_eq!(
1464 inlay_snapshot.clip_point(InlayPoint::new(0, 7), Bias::Left),
1465 InlayPoint::new(0, 2)
1466 );
1467 assert_eq!(
1468 inlay_snapshot.clip_point(InlayPoint::new(0, 7), Bias::Right),
1469 InlayPoint::new(0, 8)
1470 );
1471
1472 assert_eq!(
1473 inlay_snapshot.clip_point(InlayPoint::new(0, 8), Bias::Left),
1474 InlayPoint::new(0, 8)
1475 );
1476 assert_eq!(
1477 inlay_snapshot.clip_point(InlayPoint::new(0, 8), Bias::Right),
1478 InlayPoint::new(0, 8)
1479 );
1480
1481 assert_eq!(
1482 inlay_snapshot.clip_point(InlayPoint::new(0, 9), Bias::Left),
1483 InlayPoint::new(0, 9)
1484 );
1485 assert_eq!(
1486 inlay_snapshot.clip_point(InlayPoint::new(0, 9), Bias::Right),
1487 InlayPoint::new(0, 9)
1488 );
1489
1490 assert_eq!(
1491 inlay_snapshot.clip_point(InlayPoint::new(0, 10), Bias::Left),
1492 InlayPoint::new(0, 10)
1493 );
1494 assert_eq!(
1495 inlay_snapshot.clip_point(InlayPoint::new(0, 10), Bias::Right),
1496 InlayPoint::new(0, 10)
1497 );
1498
1499 assert_eq!(
1500 inlay_snapshot.clip_point(InlayPoint::new(0, 11), Bias::Left),
1501 InlayPoint::new(0, 11)
1502 );
1503 assert_eq!(
1504 inlay_snapshot.clip_point(InlayPoint::new(0, 11), Bias::Right),
1505 InlayPoint::new(0, 11)
1506 );
1507
1508 assert_eq!(
1509 inlay_snapshot.clip_point(InlayPoint::new(0, 12), Bias::Left),
1510 InlayPoint::new(0, 11)
1511 );
1512 assert_eq!(
1513 inlay_snapshot.clip_point(InlayPoint::new(0, 12), Bias::Right),
1514 InlayPoint::new(0, 17)
1515 );
1516
1517 assert_eq!(
1518 inlay_snapshot.clip_point(InlayPoint::new(0, 13), Bias::Left),
1519 InlayPoint::new(0, 11)
1520 );
1521 assert_eq!(
1522 inlay_snapshot.clip_point(InlayPoint::new(0, 13), Bias::Right),
1523 InlayPoint::new(0, 17)
1524 );
1525
1526 assert_eq!(
1527 inlay_snapshot.clip_point(InlayPoint::new(0, 14), Bias::Left),
1528 InlayPoint::new(0, 11)
1529 );
1530 assert_eq!(
1531 inlay_snapshot.clip_point(InlayPoint::new(0, 14), Bias::Right),
1532 InlayPoint::new(0, 17)
1533 );
1534
1535 assert_eq!(
1536 inlay_snapshot.clip_point(InlayPoint::new(0, 15), Bias::Left),
1537 InlayPoint::new(0, 11)
1538 );
1539 assert_eq!(
1540 inlay_snapshot.clip_point(InlayPoint::new(0, 15), Bias::Right),
1541 InlayPoint::new(0, 17)
1542 );
1543
1544 assert_eq!(
1545 inlay_snapshot.clip_point(InlayPoint::new(0, 16), Bias::Left),
1546 InlayPoint::new(0, 11)
1547 );
1548 assert_eq!(
1549 inlay_snapshot.clip_point(InlayPoint::new(0, 16), Bias::Right),
1550 InlayPoint::new(0, 17)
1551 );
1552
1553 assert_eq!(
1554 inlay_snapshot.clip_point(InlayPoint::new(0, 17), Bias::Left),
1555 InlayPoint::new(0, 17)
1556 );
1557 assert_eq!(
1558 inlay_snapshot.clip_point(InlayPoint::new(0, 17), Bias::Right),
1559 InlayPoint::new(0, 17)
1560 );
1561
1562 assert_eq!(
1563 inlay_snapshot.clip_point(InlayPoint::new(0, 18), Bias::Left),
1564 InlayPoint::new(0, 18)
1565 );
1566 assert_eq!(
1567 inlay_snapshot.clip_point(InlayPoint::new(0, 18), Bias::Right),
1568 InlayPoint::new(0, 18)
1569 );
1570
1571 // The inlays can be manually removed.
1572 let (inlay_snapshot, _) = inlay_map.splice(
1573 &inlay_map
1574 .inlays
1575 .iter()
1576 .map(|inlay| inlay.id)
1577 .collect::<Vec<InlayId>>(),
1578 Vec::new(),
1579 );
1580 assert_eq!(inlay_snapshot.text(), "abxJKLyDzefghi");
1581 }
1582
1583 #[gpui::test]
1584 fn test_inlay_buffer_rows(cx: &mut App) {
1585 let buffer = MultiBuffer::build_simple("abc\ndef\nghi", cx);
1586 let (mut inlay_map, inlay_snapshot) = InlayMap::new(buffer.read(cx).snapshot(cx));
1587 assert_eq!(inlay_snapshot.text(), "abc\ndef\nghi");
1588 let mut next_inlay_id = 0;
1589
1590 let (inlay_snapshot, _) = inlay_map.splice(
1591 &[],
1592 vec![
1593 Inlay::mock_hint(
1594 post_inc(&mut next_inlay_id),
1595 buffer.read(cx).snapshot(cx).anchor_before(0),
1596 "|123|\n",
1597 ),
1598 Inlay::mock_hint(
1599 post_inc(&mut next_inlay_id),
1600 buffer.read(cx).snapshot(cx).anchor_before(4),
1601 "|456|",
1602 ),
1603 Inlay::edit_prediction(
1604 post_inc(&mut next_inlay_id),
1605 buffer.read(cx).snapshot(cx).anchor_before(7),
1606 "\n|567|\n",
1607 ),
1608 ],
1609 );
1610 assert_eq!(inlay_snapshot.text(), "|123|\nabc\n|456|def\n|567|\n\nghi");
1611 assert_eq!(
1612 inlay_snapshot
1613 .row_infos(0)
1614 .map(|info| info.buffer_row)
1615 .collect::<Vec<_>>(),
1616 vec![Some(0), None, Some(1), None, None, Some(2)]
1617 );
1618 }
1619
1620 #[gpui::test(iterations = 100)]
1621 fn test_random_inlays(cx: &mut App, mut rng: StdRng) {
1622 init_test(cx);
1623
1624 let operations = env::var("OPERATIONS")
1625 .map(|i| i.parse().expect("invalid `OPERATIONS` variable"))
1626 .unwrap_or(10);
1627
1628 let len = rng.random_range(0..30);
1629 let buffer = if rng.random() {
1630 let text = util::RandomCharIter::new(&mut rng)
1631 .take(len)
1632 .collect::<String>();
1633 MultiBuffer::build_simple(&text, cx)
1634 } else {
1635 MultiBuffer::build_random(&mut rng, cx)
1636 };
1637 let mut buffer_snapshot = buffer.read(cx).snapshot(cx);
1638 let mut next_inlay_id = 0;
1639 log::info!("buffer text: {:?}", buffer_snapshot.text());
1640 let (mut inlay_map, mut inlay_snapshot) = InlayMap::new(buffer_snapshot.clone());
1641 for _ in 0..operations {
1642 let mut inlay_edits = Patch::default();
1643
1644 let mut prev_inlay_text = inlay_snapshot.text();
1645 let mut buffer_edits = Vec::new();
1646 match rng.random_range(0..=100) {
1647 0..=50 => {
1648 let (snapshot, edits) = inlay_map.randomly_mutate(&mut next_inlay_id, &mut rng);
1649 log::info!("mutated text: {:?}", snapshot.text());
1650 inlay_edits = Patch::new(edits);
1651 }
1652 _ => buffer.update(cx, |buffer, cx| {
1653 let subscription = buffer.subscribe();
1654 let edit_count = rng.random_range(1..=5);
1655 buffer.randomly_mutate(&mut rng, edit_count, cx);
1656 buffer_snapshot = buffer.snapshot(cx);
1657 let edits = subscription.consume().into_inner();
1658 log::info!("editing {:?}", edits);
1659 buffer_edits.extend(edits);
1660 }),
1661 };
1662
1663 let (new_inlay_snapshot, new_inlay_edits) =
1664 inlay_map.sync(buffer_snapshot.clone(), buffer_edits);
1665 inlay_snapshot = new_inlay_snapshot;
1666 inlay_edits = inlay_edits.compose(new_inlay_edits);
1667
1668 log::info!("buffer text: {:?}", buffer_snapshot.text());
1669 log::info!("inlay text: {:?}", inlay_snapshot.text());
1670
1671 let inlays = inlay_map
1672 .inlays
1673 .iter()
1674 .filter(|inlay| inlay.position.is_valid(&buffer_snapshot))
1675 .map(|inlay| {
1676 let offset = inlay.position.to_offset(&buffer_snapshot);
1677 (offset, inlay.clone())
1678 })
1679 .collect::<Vec<_>>();
1680 let mut expected_text = Rope::from(&buffer_snapshot.text());
1681 for (offset, inlay) in inlays.iter().rev() {
1682 expected_text.replace(*offset..*offset, &inlay.text().to_string());
1683 }
1684 assert_eq!(inlay_snapshot.text(), expected_text.to_string());
1685
1686 let expected_buffer_rows = inlay_snapshot.row_infos(0).collect::<Vec<_>>();
1687 assert_eq!(
1688 expected_buffer_rows.len() as u32,
1689 expected_text.max_point().row + 1
1690 );
1691 for row_start in 0..expected_buffer_rows.len() {
1692 assert_eq!(
1693 inlay_snapshot
1694 .row_infos(row_start as u32)
1695 .collect::<Vec<_>>(),
1696 &expected_buffer_rows[row_start..],
1697 "incorrect buffer rows starting at {}",
1698 row_start
1699 );
1700 }
1701
1702 let mut text_highlights = TextHighlights::default();
1703 let text_highlight_count = rng.random_range(0_usize..10);
1704 let mut text_highlight_ranges = (0..text_highlight_count)
1705 .map(|_| buffer_snapshot.random_byte_range(0, &mut rng))
1706 .collect::<Vec<_>>();
1707 text_highlight_ranges.sort_by_key(|range| (range.start, Reverse(range.end)));
1708 log::info!("highlighting text ranges {text_highlight_ranges:?}");
1709 text_highlights.insert(
1710 HighlightKey::Type(TypeId::of::<()>()),
1711 Arc::new((
1712 HighlightStyle::default(),
1713 text_highlight_ranges
1714 .into_iter()
1715 .map(|range| {
1716 buffer_snapshot.anchor_before(range.start)
1717 ..buffer_snapshot.anchor_after(range.end)
1718 })
1719 .collect(),
1720 )),
1721 );
1722
1723 let mut inlay_highlights = InlayHighlights::default();
1724 if !inlays.is_empty() {
1725 let inlay_highlight_count = rng.random_range(0..inlays.len());
1726 let mut inlay_indices = BTreeSet::default();
1727 while inlay_indices.len() < inlay_highlight_count {
1728 inlay_indices.insert(rng.random_range(0..inlays.len()));
1729 }
1730 let new_highlights = TreeMap::from_ordered_entries(
1731 inlay_indices
1732 .into_iter()
1733 .filter_map(|i| {
1734 let (_, inlay) = &inlays[i];
1735 let inlay_text_len = inlay.text().len();
1736 match inlay_text_len {
1737 0 => None,
1738 1 => Some(InlayHighlight {
1739 inlay: inlay.id,
1740 inlay_position: inlay.position,
1741 range: 0..1,
1742 }),
1743 n => {
1744 let inlay_text = inlay.text().to_string();
1745 let mut highlight_end = rng.random_range(1..n);
1746 let mut highlight_start = rng.random_range(0..highlight_end);
1747 while !inlay_text.is_char_boundary(highlight_end) {
1748 highlight_end += 1;
1749 }
1750 while !inlay_text.is_char_boundary(highlight_start) {
1751 highlight_start -= 1;
1752 }
1753 Some(InlayHighlight {
1754 inlay: inlay.id,
1755 inlay_position: inlay.position,
1756 range: highlight_start..highlight_end,
1757 })
1758 }
1759 }
1760 })
1761 .map(|highlight| (highlight.inlay, (HighlightStyle::default(), highlight))),
1762 );
1763 log::info!("highlighting inlay ranges {new_highlights:?}");
1764 inlay_highlights.insert(TypeId::of::<()>(), new_highlights);
1765 }
1766
1767 for _ in 0..5 {
1768 let mut end = rng.random_range(0..=inlay_snapshot.len().0);
1769 end = expected_text.clip_offset(end, Bias::Right);
1770 let mut start = rng.random_range(0..=end);
1771 start = expected_text.clip_offset(start, Bias::Right);
1772
1773 let range = InlayOffset(start)..InlayOffset(end);
1774 log::info!("calling inlay_snapshot.chunks({range:?})");
1775 let actual_text = inlay_snapshot
1776 .chunks(
1777 range,
1778 false,
1779 Highlights {
1780 text_highlights: Some(&text_highlights),
1781 inlay_highlights: Some(&inlay_highlights),
1782 ..Highlights::default()
1783 },
1784 )
1785 .map(|chunk| chunk.chunk.text)
1786 .collect::<String>();
1787 assert_eq!(
1788 actual_text,
1789 expected_text.slice(start..end).to_string(),
1790 "incorrect text in range {:?}",
1791 start..end
1792 );
1793
1794 assert_eq!(
1795 inlay_snapshot.text_summary_for_range(InlayOffset(start)..InlayOffset(end)),
1796 expected_text.slice(start..end).summary()
1797 );
1798 }
1799
1800 for edit in inlay_edits {
1801 prev_inlay_text.replace_range(
1802 edit.new.start.0..edit.new.start.0 + edit.old_len().0,
1803 &inlay_snapshot.text()[edit.new.start.0..edit.new.end.0],
1804 );
1805 }
1806 assert_eq!(prev_inlay_text, inlay_snapshot.text());
1807
1808 assert_eq!(expected_text.max_point(), inlay_snapshot.max_point().0);
1809 assert_eq!(expected_text.len(), inlay_snapshot.len().0);
1810
1811 let mut buffer_point = Point::default();
1812 let mut inlay_point = inlay_snapshot.to_inlay_point(buffer_point);
1813 let mut buffer_chars = buffer_snapshot.chars_at(0);
1814 loop {
1815 // Ensure conversion from buffer coordinates to inlay coordinates
1816 // is consistent.
1817 let buffer_offset = buffer_snapshot.point_to_offset(buffer_point);
1818 assert_eq!(
1819 inlay_snapshot.to_point(inlay_snapshot.to_inlay_offset(buffer_offset)),
1820 inlay_point
1821 );
1822
1823 // No matter which bias we clip an inlay point with, it doesn't move
1824 // because it was constructed from a buffer point.
1825 assert_eq!(
1826 inlay_snapshot.clip_point(inlay_point, Bias::Left),
1827 inlay_point,
1828 "invalid inlay point for buffer point {:?} when clipped left",
1829 buffer_point
1830 );
1831 assert_eq!(
1832 inlay_snapshot.clip_point(inlay_point, Bias::Right),
1833 inlay_point,
1834 "invalid inlay point for buffer point {:?} when clipped right",
1835 buffer_point
1836 );
1837
1838 if let Some(ch) = buffer_chars.next() {
1839 if ch == '\n' {
1840 buffer_point += Point::new(1, 0);
1841 } else {
1842 buffer_point += Point::new(0, ch.len_utf8() as u32);
1843 }
1844
1845 // Ensure that moving forward in the buffer always moves the inlay point forward as well.
1846 let new_inlay_point = inlay_snapshot.to_inlay_point(buffer_point);
1847 assert!(new_inlay_point > inlay_point);
1848 inlay_point = new_inlay_point;
1849 } else {
1850 break;
1851 }
1852 }
1853
1854 let mut inlay_point = InlayPoint::default();
1855 let mut inlay_offset = InlayOffset::default();
1856 for ch in expected_text.chars() {
1857 assert_eq!(
1858 inlay_snapshot.to_offset(inlay_point),
1859 inlay_offset,
1860 "invalid to_offset({:?})",
1861 inlay_point
1862 );
1863 assert_eq!(
1864 inlay_snapshot.to_point(inlay_offset),
1865 inlay_point,
1866 "invalid to_point({:?})",
1867 inlay_offset
1868 );
1869
1870 let mut bytes = [0; 4];
1871 for byte in ch.encode_utf8(&mut bytes).as_bytes() {
1872 inlay_offset.0 += 1;
1873 if *byte == b'\n' {
1874 inlay_point.0 += Point::new(1, 0);
1875 } else {
1876 inlay_point.0 += Point::new(0, 1);
1877 }
1878
1879 let clipped_left_point = inlay_snapshot.clip_point(inlay_point, Bias::Left);
1880 let clipped_right_point = inlay_snapshot.clip_point(inlay_point, Bias::Right);
1881 assert!(
1882 clipped_left_point <= clipped_right_point,
1883 "inlay point {:?} when clipped left is greater than when clipped right ({:?} > {:?})",
1884 inlay_point,
1885 clipped_left_point,
1886 clipped_right_point
1887 );
1888
1889 // Ensure the clipped points are at valid text locations.
1890 assert_eq!(
1891 clipped_left_point.0,
1892 expected_text.clip_point(clipped_left_point.0, Bias::Left)
1893 );
1894 assert_eq!(
1895 clipped_right_point.0,
1896 expected_text.clip_point(clipped_right_point.0, Bias::Right)
1897 );
1898
1899 // Ensure the clipped points never overshoot the end of the map.
1900 assert!(clipped_left_point <= inlay_snapshot.max_point());
1901 assert!(clipped_right_point <= inlay_snapshot.max_point());
1902
1903 // Ensure the clipped points are at valid buffer locations.
1904 assert_eq!(
1905 inlay_snapshot
1906 .to_inlay_point(inlay_snapshot.to_buffer_point(clipped_left_point)),
1907 clipped_left_point,
1908 "to_buffer_point({:?}) = {:?}",
1909 clipped_left_point,
1910 inlay_snapshot.to_buffer_point(clipped_left_point),
1911 );
1912 assert_eq!(
1913 inlay_snapshot
1914 .to_inlay_point(inlay_snapshot.to_buffer_point(clipped_right_point)),
1915 clipped_right_point,
1916 "to_buffer_point({:?}) = {:?}",
1917 clipped_right_point,
1918 inlay_snapshot.to_buffer_point(clipped_right_point),
1919 );
1920 }
1921 }
1922 }
1923 }
1924
1925 #[gpui::test(iterations = 100)]
1926 fn test_random_chunk_bitmaps(cx: &mut gpui::App, mut rng: StdRng) {
1927 init_test(cx);
1928
1929 // Generate random buffer using existing test infrastructure
1930 let text_len = rng.random_range(0..10000);
1931 let buffer = if rng.random() {
1932 let text = RandomCharIter::new(&mut rng)
1933 .take(text_len)
1934 .collect::<String>();
1935 MultiBuffer::build_simple(&text, cx)
1936 } else {
1937 MultiBuffer::build_random(&mut rng, cx)
1938 };
1939
1940 let buffer_snapshot = buffer.read(cx).snapshot(cx);
1941 let (mut inlay_map, _) = InlayMap::new(buffer_snapshot.clone());
1942
1943 // Perform random mutations to add inlays
1944 let mut next_inlay_id = 0;
1945 let mutation_count = rng.random_range(1..10);
1946 for _ in 0..mutation_count {
1947 inlay_map.randomly_mutate(&mut next_inlay_id, &mut rng);
1948 }
1949
1950 let (snapshot, _) = inlay_map.sync(buffer_snapshot, vec![]);
1951
1952 // Get all chunks and verify their bitmaps
1953 let chunks = snapshot.chunks(
1954 InlayOffset(0)..InlayOffset(snapshot.len().0),
1955 false,
1956 Highlights::default(),
1957 );
1958
1959 for chunk in chunks.into_iter().map(|inlay_chunk| inlay_chunk.chunk) {
1960 let chunk_text = chunk.text;
1961 let chars_bitmap = chunk.chars;
1962 let tabs_bitmap = chunk.tabs;
1963
1964 // Check empty chunks have empty bitmaps
1965 if chunk_text.is_empty() {
1966 assert_eq!(
1967 chars_bitmap, 0,
1968 "Empty chunk should have empty chars bitmap"
1969 );
1970 assert_eq!(tabs_bitmap, 0, "Empty chunk should have empty tabs bitmap");
1971 continue;
1972 }
1973
1974 // Verify that chunk text doesn't exceed 128 bytes
1975 assert!(
1976 chunk_text.len() <= 128,
1977 "Chunk text length {} exceeds 128 bytes",
1978 chunk_text.len()
1979 );
1980
1981 // Verify chars bitmap
1982 let char_indices = chunk_text
1983 .char_indices()
1984 .map(|(i, _)| i)
1985 .collect::<Vec<_>>();
1986
1987 for byte_idx in 0..chunk_text.len() {
1988 let should_have_bit = char_indices.contains(&byte_idx);
1989 let has_bit = chars_bitmap & (1 << byte_idx) != 0;
1990
1991 if has_bit != should_have_bit {
1992 eprintln!("Chunk text bytes: {:?}", chunk_text.as_bytes());
1993 eprintln!("Char indices: {:?}", char_indices);
1994 eprintln!("Chars bitmap: {:#b}", chars_bitmap);
1995 assert_eq!(
1996 has_bit, should_have_bit,
1997 "Chars bitmap mismatch at byte index {} in chunk {:?}. Expected bit: {}, Got bit: {}",
1998 byte_idx, chunk_text, should_have_bit, has_bit
1999 );
2000 }
2001 }
2002
2003 // Verify tabs bitmap
2004 for (byte_idx, byte) in chunk_text.bytes().enumerate() {
2005 let is_tab = byte == b'\t';
2006 let has_bit = tabs_bitmap & (1 << byte_idx) != 0;
2007
2008 if has_bit != is_tab {
2009 eprintln!("Chunk text bytes: {:?}", chunk_text.as_bytes());
2010 eprintln!("Tabs bitmap: {:#b}", tabs_bitmap);
2011 assert_eq!(
2012 has_bit, is_tab,
2013 "Tabs bitmap mismatch at byte index {} in chunk {:?}. Byte: {:?}, Expected bit: {}, Got bit: {}",
2014 byte_idx, chunk_text, byte as char, is_tab, has_bit
2015 );
2016 }
2017 }
2018 }
2019 }
2020
2021 fn init_test(cx: &mut App) {
2022 let store = SettingsStore::test(cx);
2023 cx.set_global(store);
2024 theme::init(theme::LoadThemes::JustBase, cx);
2025 }
2026
2027 /// Helper to create test highlights for an inlay
2028 fn create_inlay_highlights(
2029 inlay_id: InlayId,
2030 highlight_range: Range<usize>,
2031 position: Anchor,
2032 ) -> TreeMap<TypeId, TreeMap<InlayId, (HighlightStyle, InlayHighlight)>> {
2033 let mut inlay_highlights = TreeMap::default();
2034 let mut type_highlights = TreeMap::default();
2035 type_highlights.insert(
2036 inlay_id,
2037 (
2038 HighlightStyle::default(),
2039 InlayHighlight {
2040 inlay: inlay_id,
2041 range: highlight_range,
2042 inlay_position: position,
2043 },
2044 ),
2045 );
2046 inlay_highlights.insert(TypeId::of::<()>(), type_highlights);
2047 inlay_highlights
2048 }
2049
2050 #[gpui::test]
2051 fn test_inlay_utf8_boundary_panic_fix(cx: &mut App) {
2052 init_test(cx);
2053
2054 // This test verifies that we handle UTF-8 character boundaries correctly
2055 // when splitting inlay text for highlighting. Previously, this would panic
2056 // when trying to split at byte 13, which is in the middle of the '…' character.
2057 //
2058 // See https://github.com/zed-industries/zed/issues/33641
2059 let buffer = MultiBuffer::build_simple("fn main() {}\n", cx);
2060 let (mut inlay_map, _) = InlayMap::new(buffer.read(cx).snapshot(cx));
2061
2062 // Create an inlay with text that contains a multi-byte character
2063 // The string "SortingDirec…" contains an ellipsis character '…' which is 3 bytes (E2 80 A6)
2064 let inlay_text = "SortingDirec…";
2065 let position = buffer.read(cx).snapshot(cx).anchor_before(Point::new(0, 5));
2066
2067 let inlay = Inlay {
2068 id: InlayId::Hint(0),
2069 position,
2070 content: InlayContent::Text(text::Rope::from(inlay_text)),
2071 };
2072
2073 let (inlay_snapshot, _) = inlay_map.splice(&[], vec![inlay]);
2074
2075 // Create highlights that request a split at byte 13, which is in the middle
2076 // of the '…' character (bytes 12..15). We include the full character.
2077 let inlay_highlights = create_inlay_highlights(InlayId::Hint(0), 0..13, position);
2078
2079 let highlights = crate::display_map::Highlights {
2080 text_highlights: None,
2081 inlay_highlights: Some(&inlay_highlights),
2082 styles: crate::display_map::HighlightStyles::default(),
2083 };
2084
2085 // Collect chunks - this previously would panic
2086 let chunks: Vec<_> = inlay_snapshot
2087 .chunks(
2088 InlayOffset(0)..InlayOffset(inlay_snapshot.len().0),
2089 false,
2090 highlights,
2091 )
2092 .collect();
2093
2094 // Verify the chunks are correct
2095 let full_text: String = chunks.iter().map(|c| c.chunk.text).collect();
2096 assert_eq!(full_text, "fn maSortingDirec…in() {}\n");
2097
2098 // Verify the highlighted portion includes the complete ellipsis character
2099 let highlighted_chunks: Vec<_> = chunks
2100 .iter()
2101 .filter(|c| c.chunk.highlight_style.is_some() && c.chunk.is_inlay)
2102 .collect();
2103
2104 assert_eq!(highlighted_chunks.len(), 1);
2105 assert_eq!(highlighted_chunks[0].chunk.text, "SortingDirec…");
2106 }
2107
2108 #[gpui::test]
2109 fn test_inlay_utf8_boundaries(cx: &mut App) {
2110 init_test(cx);
2111
2112 struct TestCase {
2113 inlay_text: &'static str,
2114 highlight_range: Range<usize>,
2115 expected_highlighted: &'static str,
2116 description: &'static str,
2117 }
2118
2119 let test_cases = vec![
2120 TestCase {
2121 inlay_text: "Hello👋World",
2122 highlight_range: 0..7,
2123 expected_highlighted: "Hello👋",
2124 description: "Emoji boundary - rounds up to include full emoji",
2125 },
2126 TestCase {
2127 inlay_text: "Test→End",
2128 highlight_range: 0..5,
2129 expected_highlighted: "Test→",
2130 description: "Arrow boundary - rounds up to include full arrow",
2131 },
2132 TestCase {
2133 inlay_text: "café",
2134 highlight_range: 0..4,
2135 expected_highlighted: "café",
2136 description: "Accented char boundary - rounds up to include full é",
2137 },
2138 TestCase {
2139 inlay_text: "🎨🎭🎪",
2140 highlight_range: 0..5,
2141 expected_highlighted: "🎨🎭",
2142 description: "Multiple emojis - partial highlight",
2143 },
2144 TestCase {
2145 inlay_text: "普通话",
2146 highlight_range: 0..4,
2147 expected_highlighted: "普通",
2148 description: "Chinese characters - partial highlight",
2149 },
2150 TestCase {
2151 inlay_text: "Hello",
2152 highlight_range: 0..2,
2153 expected_highlighted: "He",
2154 description: "ASCII only - no adjustment needed",
2155 },
2156 TestCase {
2157 inlay_text: "👋",
2158 highlight_range: 0..1,
2159 expected_highlighted: "👋",
2160 description: "Single emoji - partial byte range includes whole char",
2161 },
2162 TestCase {
2163 inlay_text: "Test",
2164 highlight_range: 0..0,
2165 expected_highlighted: "",
2166 description: "Empty range",
2167 },
2168 TestCase {
2169 inlay_text: "🎨ABC",
2170 highlight_range: 2..5,
2171 expected_highlighted: "A",
2172 description: "Range starting mid-emoji skips the emoji",
2173 },
2174 ];
2175
2176 for test_case in test_cases {
2177 let buffer = MultiBuffer::build_simple("test", cx);
2178 let (mut inlay_map, _) = InlayMap::new(buffer.read(cx).snapshot(cx));
2179 let position = buffer.read(cx).snapshot(cx).anchor_before(Point::new(0, 2));
2180
2181 let inlay = Inlay {
2182 id: InlayId::Hint(0),
2183 position,
2184 content: InlayContent::Text(text::Rope::from(test_case.inlay_text)),
2185 };
2186
2187 let (inlay_snapshot, _) = inlay_map.splice(&[], vec![inlay]);
2188 let inlay_highlights = create_inlay_highlights(
2189 InlayId::Hint(0),
2190 test_case.highlight_range.clone(),
2191 position,
2192 );
2193
2194 let highlights = crate::display_map::Highlights {
2195 text_highlights: None,
2196 inlay_highlights: Some(&inlay_highlights),
2197 styles: crate::display_map::HighlightStyles::default(),
2198 };
2199
2200 let chunks: Vec<_> = inlay_snapshot
2201 .chunks(
2202 InlayOffset(0)..InlayOffset(inlay_snapshot.len().0),
2203 false,
2204 highlights,
2205 )
2206 .collect();
2207
2208 // Verify we got chunks and they total to the expected text
2209 let full_text: String = chunks.iter().map(|c| c.chunk.text).collect();
2210 assert_eq!(
2211 full_text,
2212 format!("te{}st", test_case.inlay_text),
2213 "Full text mismatch for case: {}",
2214 test_case.description
2215 );
2216
2217 // Verify that the highlighted portion matches expectations
2218 let highlighted_text: String = chunks
2219 .iter()
2220 .filter(|c| c.chunk.highlight_style.is_some() && c.chunk.is_inlay)
2221 .map(|c| c.chunk.text)
2222 .collect();
2223 assert_eq!(
2224 highlighted_text, test_case.expected_highlighted,
2225 "Highlighted text mismatch for case: {} (text: '{}', range: {:?})",
2226 test_case.description, test_case.inlay_text, test_case.highlight_range
2227 );
2228 }
2229 }
2230}