inlay_map.rs

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