inlay_map.rs

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