display_map.rs

   1mod block_map;
   2mod fold_map;
   3mod tab_map;
   4mod wrap_map;
   5
   6use crate::{Anchor, MultiBuffer, MultiBufferSnapshot, ToOffset, ToPoint};
   7use block_map::{BlockMap, BlockPoint};
   8use collections::{HashMap, HashSet};
   9use fold_map::FoldMap;
  10use gpui::{
  11    fonts::{FontId, HighlightStyle},
  12    Entity, ModelContext, ModelHandle,
  13};
  14use language::{Point, Subscription as BufferSubscription};
  15use std::{any::TypeId, fmt::Debug, ops::Range, sync::Arc};
  16use sum_tree::{Bias, TreeMap};
  17use tab_map::TabMap;
  18use wrap_map::WrapMap;
  19
  20pub use block_map::{
  21    BlockBufferRows as DisplayBufferRows, BlockChunks as DisplayChunks, BlockContext,
  22    BlockDisposition, BlockId, BlockProperties, RenderBlock, TransformBlock,
  23};
  24
  25pub trait ToDisplayPoint {
  26    fn to_display_point(&self, map: &DisplaySnapshot) -> DisplayPoint;
  27}
  28
  29type TextHighlights = TreeMap<Option<TypeId>, Arc<(HighlightStyle, Vec<Range<Anchor>>)>>;
  30
  31pub struct DisplayMap {
  32    buffer: ModelHandle<MultiBuffer>,
  33    buffer_subscription: BufferSubscription,
  34    fold_map: FoldMap,
  35    tab_map: TabMap,
  36    wrap_map: ModelHandle<WrapMap>,
  37    block_map: BlockMap,
  38    text_highlights: TextHighlights,
  39}
  40
  41impl Entity for DisplayMap {
  42    type Event = ();
  43}
  44
  45impl DisplayMap {
  46    pub fn new(
  47        buffer: ModelHandle<MultiBuffer>,
  48        tab_size: usize,
  49        font_id: FontId,
  50        font_size: f32,
  51        wrap_width: Option<f32>,
  52        buffer_header_height: u8,
  53        excerpt_header_height: u8,
  54        cx: &mut ModelContext<Self>,
  55    ) -> Self {
  56        let buffer_subscription = buffer.update(cx, |buffer, _| buffer.subscribe());
  57        let (fold_map, snapshot) = FoldMap::new(buffer.read(cx).snapshot(cx));
  58        let (tab_map, snapshot) = TabMap::new(snapshot, tab_size);
  59        let (wrap_map, snapshot) = WrapMap::new(snapshot, font_id, font_size, wrap_width, cx);
  60        let block_map = BlockMap::new(snapshot, buffer_header_height, excerpt_header_height);
  61        cx.observe(&wrap_map, |_, _, cx| cx.notify()).detach();
  62        DisplayMap {
  63            buffer,
  64            buffer_subscription,
  65            fold_map,
  66            tab_map,
  67            wrap_map,
  68            block_map,
  69            text_highlights: Default::default(),
  70        }
  71    }
  72
  73    pub fn snapshot(&self, cx: &mut ModelContext<Self>) -> DisplaySnapshot {
  74        let buffer_snapshot = self.buffer.read(cx).snapshot(cx);
  75        let edits = self.buffer_subscription.consume().into_inner();
  76        let (folds_snapshot, edits) = self.fold_map.read(buffer_snapshot, edits);
  77        let (tabs_snapshot, edits) = self.tab_map.sync(folds_snapshot.clone(), edits);
  78        let (wraps_snapshot, edits) = self
  79            .wrap_map
  80            .update(cx, |map, cx| map.sync(tabs_snapshot.clone(), edits, cx));
  81        let blocks_snapshot = self.block_map.read(wraps_snapshot.clone(), edits);
  82
  83        DisplaySnapshot {
  84            buffer_snapshot: self.buffer.read(cx).snapshot(cx),
  85            folds_snapshot,
  86            tabs_snapshot,
  87            wraps_snapshot,
  88            blocks_snapshot,
  89            text_highlights: self.text_highlights.clone(),
  90        }
  91    }
  92
  93    pub fn fold<T: ToOffset>(
  94        &mut self,
  95        ranges: impl IntoIterator<Item = Range<T>>,
  96        cx: &mut ModelContext<Self>,
  97    ) {
  98        let snapshot = self.buffer.read(cx).snapshot(cx);
  99        let edits = self.buffer_subscription.consume().into_inner();
 100        let (mut fold_map, snapshot, edits) = self.fold_map.write(snapshot, edits);
 101        let (snapshot, edits) = self.tab_map.sync(snapshot, edits);
 102        let (snapshot, edits) = self
 103            .wrap_map
 104            .update(cx, |map, cx| map.sync(snapshot, edits, cx));
 105        self.block_map.read(snapshot, edits);
 106        let (snapshot, edits) = fold_map.fold(ranges);
 107        let (snapshot, edits) = self.tab_map.sync(snapshot, edits);
 108        let (snapshot, edits) = self
 109            .wrap_map
 110            .update(cx, |map, cx| map.sync(snapshot, edits, cx));
 111        self.block_map.read(snapshot, edits);
 112    }
 113
 114    pub fn unfold<T: ToOffset>(
 115        &mut self,
 116        ranges: impl IntoIterator<Item = Range<T>>,
 117        inclusive: bool,
 118        cx: &mut ModelContext<Self>,
 119    ) {
 120        let snapshot = self.buffer.read(cx).snapshot(cx);
 121        let edits = self.buffer_subscription.consume().into_inner();
 122        let (mut fold_map, snapshot, edits) = self.fold_map.write(snapshot, edits);
 123        let (snapshot, edits) = self.tab_map.sync(snapshot, edits);
 124        let (snapshot, edits) = self
 125            .wrap_map
 126            .update(cx, |map, cx| map.sync(snapshot, edits, cx));
 127        self.block_map.read(snapshot, edits);
 128        let (snapshot, edits) = fold_map.unfold(ranges, inclusive);
 129        let (snapshot, edits) = self.tab_map.sync(snapshot, edits);
 130        let (snapshot, edits) = self
 131            .wrap_map
 132            .update(cx, |map, cx| map.sync(snapshot, edits, cx));
 133        self.block_map.read(snapshot, edits);
 134    }
 135
 136    pub fn insert_blocks(
 137        &mut self,
 138        blocks: impl IntoIterator<Item = BlockProperties<Anchor>>,
 139        cx: &mut ModelContext<Self>,
 140    ) -> Vec<BlockId> {
 141        let snapshot = self.buffer.read(cx).snapshot(cx);
 142        let edits = self.buffer_subscription.consume().into_inner();
 143        let (snapshot, edits) = self.fold_map.read(snapshot, edits);
 144        let (snapshot, edits) = self.tab_map.sync(snapshot, edits);
 145        let (snapshot, edits) = self
 146            .wrap_map
 147            .update(cx, |map, cx| map.sync(snapshot, edits, cx));
 148        let mut block_map = self.block_map.write(snapshot, edits);
 149        block_map.insert(blocks)
 150    }
 151
 152    pub fn replace_blocks(&mut self, styles: HashMap<BlockId, RenderBlock>) {
 153        self.block_map.replace(styles);
 154    }
 155
 156    pub fn remove_blocks(&mut self, ids: HashSet<BlockId>, cx: &mut ModelContext<Self>) {
 157        let snapshot = self.buffer.read(cx).snapshot(cx);
 158        let edits = self.buffer_subscription.consume().into_inner();
 159        let (snapshot, edits) = self.fold_map.read(snapshot, edits);
 160        let (snapshot, edits) = self.tab_map.sync(snapshot, edits);
 161        let (snapshot, edits) = self
 162            .wrap_map
 163            .update(cx, |map, cx| map.sync(snapshot, edits, cx));
 164        let mut block_map = self.block_map.write(snapshot, edits);
 165        block_map.remove(ids);
 166    }
 167
 168    pub fn highlight_text(
 169        &mut self,
 170        type_id: TypeId,
 171        ranges: Vec<Range<Anchor>>,
 172        style: HighlightStyle,
 173    ) {
 174        self.text_highlights
 175            .insert(Some(type_id), Arc::new((style, ranges)));
 176    }
 177
 178    pub fn clear_text_highlights(
 179        &mut self,
 180        type_id: TypeId,
 181    ) -> Option<Arc<(HighlightStyle, Vec<Range<Anchor>>)>> {
 182        self.text_highlights.remove(&Some(type_id))
 183    }
 184
 185    pub fn set_font(&self, font_id: FontId, font_size: f32, cx: &mut ModelContext<Self>) {
 186        self.wrap_map
 187            .update(cx, |map, cx| map.set_font(font_id, font_size, cx));
 188    }
 189
 190    pub fn set_wrap_width(&self, width: Option<f32>, cx: &mut ModelContext<Self>) -> bool {
 191        self.wrap_map
 192            .update(cx, |map, cx| map.set_wrap_width(width, cx))
 193    }
 194
 195    #[cfg(test)]
 196    pub fn is_rewrapping(&self, cx: &gpui::AppContext) -> bool {
 197        self.wrap_map.read(cx).is_rewrapping()
 198    }
 199}
 200
 201pub struct DisplaySnapshot {
 202    pub buffer_snapshot: MultiBufferSnapshot,
 203    folds_snapshot: fold_map::FoldSnapshot,
 204    tabs_snapshot: tab_map::TabSnapshot,
 205    wraps_snapshot: wrap_map::WrapSnapshot,
 206    blocks_snapshot: block_map::BlockSnapshot,
 207    text_highlights: TextHighlights,
 208}
 209
 210impl DisplaySnapshot {
 211    #[cfg(test)]
 212    pub fn fold_count(&self) -> usize {
 213        self.folds_snapshot.fold_count()
 214    }
 215
 216    pub fn is_empty(&self) -> bool {
 217        self.buffer_snapshot.len() == 0
 218    }
 219
 220    pub fn buffer_rows<'a>(&'a self, start_row: u32) -> DisplayBufferRows<'a> {
 221        self.blocks_snapshot.buffer_rows(start_row)
 222    }
 223
 224    pub fn max_buffer_row(&self) -> u32 {
 225        self.buffer_snapshot.max_buffer_row()
 226    }
 227
 228    pub fn prev_line_boundary(&self, mut point: Point) -> (Point, DisplayPoint) {
 229        loop {
 230            let mut fold_point = self.folds_snapshot.to_fold_point(point, Bias::Left);
 231            *fold_point.column_mut() = 0;
 232            point = fold_point.to_buffer_point(&self.folds_snapshot);
 233
 234            let mut display_point = self.point_to_display_point(point, Bias::Left);
 235            *display_point.column_mut() = 0;
 236            let next_point = self.display_point_to_point(display_point, Bias::Left);
 237            if next_point == point {
 238                return (point, display_point);
 239            }
 240            point = next_point;
 241        }
 242    }
 243
 244    pub fn next_line_boundary(&self, mut point: Point) -> (Point, DisplayPoint) {
 245        loop {
 246            let mut fold_point = self.folds_snapshot.to_fold_point(point, Bias::Right);
 247            *fold_point.column_mut() = self.folds_snapshot.line_len(fold_point.row());
 248            point = fold_point.to_buffer_point(&self.folds_snapshot);
 249
 250            let mut display_point = self.point_to_display_point(point, Bias::Right);
 251            *display_point.column_mut() = self.line_len(display_point.row());
 252            let next_point = self.display_point_to_point(display_point, Bias::Right);
 253            if next_point == point {
 254                return (point, display_point);
 255            }
 256            point = next_point;
 257        }
 258    }
 259
 260    fn point_to_display_point(&self, point: Point, bias: Bias) -> DisplayPoint {
 261        let fold_point = self.folds_snapshot.to_fold_point(point, bias);
 262        let tab_point = self.tabs_snapshot.to_tab_point(fold_point);
 263        let wrap_point = self.wraps_snapshot.from_tab_point(tab_point);
 264        let block_point = self.blocks_snapshot.to_block_point(wrap_point);
 265        DisplayPoint(block_point)
 266    }
 267
 268    fn display_point_to_point(&self, point: DisplayPoint, bias: Bias) -> Point {
 269        let block_point = point.0;
 270        let wrap_point = self.blocks_snapshot.to_wrap_point(block_point);
 271        let tab_point = self.wraps_snapshot.to_tab_point(wrap_point);
 272        let fold_point = self.tabs_snapshot.to_fold_point(tab_point, bias).0;
 273        fold_point.to_buffer_point(&self.folds_snapshot)
 274    }
 275
 276    pub fn max_point(&self) -> DisplayPoint {
 277        DisplayPoint(self.blocks_snapshot.max_point())
 278    }
 279
 280    pub fn text_chunks(&self, display_row: u32) -> impl Iterator<Item = &str> {
 281        self.blocks_snapshot
 282            .chunks(display_row..self.max_point().row() + 1, false, None)
 283            .map(|h| h.text)
 284    }
 285
 286    pub fn chunks<'a>(
 287        &'a self,
 288        display_rows: Range<u32>,
 289        language_aware: bool,
 290    ) -> DisplayChunks<'a> {
 291        self.blocks_snapshot
 292            .chunks(display_rows, language_aware, Some(&self.text_highlights))
 293    }
 294
 295    pub fn chars_at<'a>(&'a self, point: DisplayPoint) -> impl Iterator<Item = char> + 'a {
 296        let mut column = 0;
 297        let mut chars = self.text_chunks(point.row()).flat_map(str::chars);
 298        while column < point.column() {
 299            if let Some(c) = chars.next() {
 300                column += c.len_utf8() as u32;
 301            } else {
 302                break;
 303            }
 304        }
 305        chars
 306    }
 307
 308    pub fn column_to_chars(&self, display_row: u32, target: u32) -> u32 {
 309        let mut count = 0;
 310        let mut column = 0;
 311        for c in self.chars_at(DisplayPoint::new(display_row, 0)) {
 312            if column >= target {
 313                break;
 314            }
 315            count += 1;
 316            column += c.len_utf8() as u32;
 317        }
 318        count
 319    }
 320
 321    pub fn column_from_chars(&self, display_row: u32, char_count: u32) -> u32 {
 322        let mut count = 0;
 323        let mut column = 0;
 324        for c in self.chars_at(DisplayPoint::new(display_row, 0)) {
 325            if c == '\n' || count >= char_count {
 326                break;
 327            }
 328            count += 1;
 329            column += c.len_utf8() as u32;
 330        }
 331        column
 332    }
 333
 334    pub fn clip_point(&self, point: DisplayPoint, bias: Bias) -> DisplayPoint {
 335        DisplayPoint(self.blocks_snapshot.clip_point(point.0, bias))
 336    }
 337
 338    pub fn folds_in_range<'a, T>(
 339        &'a self,
 340        range: Range<T>,
 341    ) -> impl Iterator<Item = &'a Range<Anchor>>
 342    where
 343        T: ToOffset,
 344    {
 345        self.folds_snapshot.folds_in_range(range)
 346    }
 347
 348    pub fn blocks_in_range<'a>(
 349        &'a self,
 350        rows: Range<u32>,
 351    ) -> impl Iterator<Item = (u32, &'a TransformBlock)> {
 352        self.blocks_snapshot.blocks_in_range(rows)
 353    }
 354
 355    pub fn intersects_fold<T: ToOffset>(&self, offset: T) -> bool {
 356        self.folds_snapshot.intersects_fold(offset)
 357    }
 358
 359    pub fn is_line_folded(&self, display_row: u32) -> bool {
 360        let block_point = BlockPoint(Point::new(display_row, 0));
 361        let wrap_point = self.blocks_snapshot.to_wrap_point(block_point);
 362        let tab_point = self.wraps_snapshot.to_tab_point(wrap_point);
 363        self.folds_snapshot.is_line_folded(tab_point.row())
 364    }
 365
 366    pub fn is_block_line(&self, display_row: u32) -> bool {
 367        self.blocks_snapshot.is_block_line(display_row)
 368    }
 369
 370    pub fn soft_wrap_indent(&self, display_row: u32) -> Option<u32> {
 371        let wrap_row = self
 372            .blocks_snapshot
 373            .to_wrap_point(BlockPoint::new(display_row, 0))
 374            .row();
 375        self.wraps_snapshot.soft_wrap_indent(wrap_row)
 376    }
 377
 378    pub fn text(&self) -> String {
 379        self.text_chunks(0).collect()
 380    }
 381
 382    pub fn line(&self, display_row: u32) -> String {
 383        let mut result = String::new();
 384        for chunk in self.text_chunks(display_row) {
 385            if let Some(ix) = chunk.find('\n') {
 386                result.push_str(&chunk[0..ix]);
 387                break;
 388            } else {
 389                result.push_str(chunk);
 390            }
 391        }
 392        result
 393    }
 394
 395    pub fn line_indent(&self, display_row: u32) -> (u32, bool) {
 396        let mut indent = 0;
 397        let mut is_blank = true;
 398        for c in self.chars_at(DisplayPoint::new(display_row, 0)) {
 399            if c == ' ' {
 400                indent += 1;
 401            } else {
 402                is_blank = c == '\n';
 403                break;
 404            }
 405        }
 406        (indent, is_blank)
 407    }
 408
 409    pub fn line_len(&self, row: u32) -> u32 {
 410        self.blocks_snapshot.line_len(row)
 411    }
 412
 413    pub fn longest_row(&self) -> u32 {
 414        self.blocks_snapshot.longest_row()
 415    }
 416}
 417
 418#[derive(Copy, Clone, Default, Eq, Ord, PartialOrd, PartialEq)]
 419pub struct DisplayPoint(BlockPoint);
 420
 421impl Debug for DisplayPoint {
 422    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 423        f.write_fmt(format_args!(
 424            "DisplayPoint({}, {})",
 425            self.row(),
 426            self.column()
 427        ))
 428    }
 429}
 430
 431impl DisplayPoint {
 432    pub fn new(row: u32, column: u32) -> Self {
 433        Self(BlockPoint(Point::new(row, column)))
 434    }
 435
 436    pub fn zero() -> Self {
 437        Self::new(0, 0)
 438    }
 439
 440    pub fn is_zero(&self) -> bool {
 441        self.0.is_zero()
 442    }
 443
 444    pub fn row(self) -> u32 {
 445        self.0.row
 446    }
 447
 448    pub fn column(self) -> u32 {
 449        self.0.column
 450    }
 451
 452    pub fn row_mut(&mut self) -> &mut u32 {
 453        &mut self.0.row
 454    }
 455
 456    pub fn column_mut(&mut self) -> &mut u32 {
 457        &mut self.0.column
 458    }
 459
 460    pub fn to_point(self, map: &DisplaySnapshot) -> Point {
 461        map.display_point_to_point(self, Bias::Left)
 462    }
 463
 464    pub fn to_offset(self, map: &DisplaySnapshot, bias: Bias) -> usize {
 465        let unblocked_point = map.blocks_snapshot.to_wrap_point(self.0);
 466        let unwrapped_point = map.wraps_snapshot.to_tab_point(unblocked_point);
 467        let unexpanded_point = map.tabs_snapshot.to_fold_point(unwrapped_point, bias).0;
 468        unexpanded_point.to_buffer_offset(&map.folds_snapshot)
 469    }
 470}
 471
 472impl ToDisplayPoint for usize {
 473    fn to_display_point(&self, map: &DisplaySnapshot) -> DisplayPoint {
 474        map.point_to_display_point(self.to_point(&map.buffer_snapshot), Bias::Left)
 475    }
 476}
 477
 478impl ToDisplayPoint for Point {
 479    fn to_display_point(&self, map: &DisplaySnapshot) -> DisplayPoint {
 480        map.point_to_display_point(*self, Bias::Left)
 481    }
 482}
 483
 484impl ToDisplayPoint for Anchor {
 485    fn to_display_point(&self, map: &DisplaySnapshot) -> DisplayPoint {
 486        self.to_point(&map.buffer_snapshot).to_display_point(map)
 487    }
 488}
 489
 490#[cfg(test)]
 491mod tests {
 492    use super::*;
 493    use crate::movement;
 494    use gpui::{color::Color, elements::*, test::observe, MutableAppContext};
 495    use language::{Buffer, Language, LanguageConfig, RandomCharIter, SelectionGoal};
 496    use rand::{prelude::*, Rng};
 497    use smol::stream::StreamExt;
 498    use std::{env, sync::Arc};
 499    use theme::SyntaxTheme;
 500    use util::test::sample_text;
 501    use Bias::*;
 502
 503    #[gpui::test(iterations = 100)]
 504    async fn test_random_display_map(cx: &mut gpui::TestAppContext, mut rng: StdRng) {
 505        cx.foreground().set_block_on_ticks(0..=50);
 506        cx.foreground().forbid_parking();
 507        let operations = env::var("OPERATIONS")
 508            .map(|i| i.parse().expect("invalid `OPERATIONS` variable"))
 509            .unwrap_or(10);
 510
 511        let font_cache = cx.font_cache().clone();
 512        let tab_size = rng.gen_range(1..=4);
 513        let buffer_start_excerpt_header_height = rng.gen_range(1..=5);
 514        let excerpt_header_height = rng.gen_range(1..=5);
 515        let family_id = font_cache.load_family(&["Helvetica"]).unwrap();
 516        let font_id = font_cache
 517            .select_font(family_id, &Default::default())
 518            .unwrap();
 519        let font_size = 14.0;
 520        let max_wrap_width = 300.0;
 521        let mut wrap_width = if rng.gen_bool(0.1) {
 522            None
 523        } else {
 524            Some(rng.gen_range(0.0..=max_wrap_width))
 525        };
 526
 527        log::info!("tab size: {}", tab_size);
 528        log::info!("wrap width: {:?}", wrap_width);
 529
 530        let buffer = cx.update(|cx| {
 531            if rng.gen() {
 532                let len = rng.gen_range(0..10);
 533                let text = RandomCharIter::new(&mut rng).take(len).collect::<String>();
 534                MultiBuffer::build_simple(&text, cx)
 535            } else {
 536                MultiBuffer::build_random(&mut rng, cx)
 537            }
 538        });
 539
 540        let map = cx.add_model(|cx| {
 541            DisplayMap::new(
 542                buffer.clone(),
 543                tab_size,
 544                font_id,
 545                font_size,
 546                wrap_width,
 547                buffer_start_excerpt_header_height,
 548                excerpt_header_height,
 549                cx,
 550            )
 551        });
 552        let mut notifications = observe(&map, cx);
 553        let mut fold_count = 0;
 554        let mut blocks = Vec::new();
 555
 556        let snapshot = map.update(cx, |map, cx| map.snapshot(cx));
 557        log::info!("buffer text: {:?}", snapshot.buffer_snapshot.text());
 558        log::info!("fold text: {:?}", snapshot.folds_snapshot.text());
 559        log::info!("tab text: {:?}", snapshot.tabs_snapshot.text());
 560        log::info!("wrap text: {:?}", snapshot.wraps_snapshot.text());
 561        log::info!("block text: {:?}", snapshot.blocks_snapshot.text());
 562        log::info!("display text: {:?}", snapshot.text());
 563
 564        for _i in 0..operations {
 565            match rng.gen_range(0..100) {
 566                0..=19 => {
 567                    wrap_width = if rng.gen_bool(0.2) {
 568                        None
 569                    } else {
 570                        Some(rng.gen_range(0.0..=max_wrap_width))
 571                    };
 572                    log::info!("setting wrap width to {:?}", wrap_width);
 573                    map.update(cx, |map, cx| map.set_wrap_width(wrap_width, cx));
 574                }
 575                20..=44 => {
 576                    map.update(cx, |map, cx| {
 577                        if rng.gen() || blocks.is_empty() {
 578                            let buffer = map.snapshot(cx).buffer_snapshot;
 579                            let block_properties = (0..rng.gen_range(1..=1))
 580                                .map(|_| {
 581                                    let position =
 582                                        buffer.anchor_after(buffer.clip_offset(
 583                                            rng.gen_range(0..=buffer.len()),
 584                                            Bias::Left,
 585                                        ));
 586
 587                                    let disposition = if rng.gen() {
 588                                        BlockDisposition::Above
 589                                    } else {
 590                                        BlockDisposition::Below
 591                                    };
 592                                    let height = rng.gen_range(1..5);
 593                                    log::info!(
 594                                        "inserting block {:?} {:?} with height {}",
 595                                        disposition,
 596                                        position.to_point(&buffer),
 597                                        height
 598                                    );
 599                                    BlockProperties {
 600                                        position,
 601                                        height,
 602                                        disposition,
 603                                        render: Arc::new(|_| Empty::new().boxed()),
 604                                    }
 605                                })
 606                                .collect::<Vec<_>>();
 607                            blocks.extend(map.insert_blocks(block_properties, cx));
 608                        } else {
 609                            blocks.shuffle(&mut rng);
 610                            let remove_count = rng.gen_range(1..=4.min(blocks.len()));
 611                            let block_ids_to_remove = (0..remove_count)
 612                                .map(|_| blocks.remove(rng.gen_range(0..blocks.len())))
 613                                .collect();
 614                            log::info!("removing block ids {:?}", block_ids_to_remove);
 615                            map.remove_blocks(block_ids_to_remove, cx);
 616                        }
 617                    });
 618                }
 619                45..=79 => {
 620                    let mut ranges = Vec::new();
 621                    for _ in 0..rng.gen_range(1..=3) {
 622                        buffer.read_with(cx, |buffer, cx| {
 623                            let buffer = buffer.read(cx);
 624                            let end = buffer.clip_offset(rng.gen_range(0..=buffer.len()), Right);
 625                            let start = buffer.clip_offset(rng.gen_range(0..=end), Left);
 626                            ranges.push(start..end);
 627                        });
 628                    }
 629
 630                    if rng.gen() && fold_count > 0 {
 631                        log::info!("unfolding ranges: {:?}", ranges);
 632                        map.update(cx, |map, cx| {
 633                            map.unfold(ranges, true, cx);
 634                        });
 635                    } else {
 636                        log::info!("folding ranges: {:?}", ranges);
 637                        map.update(cx, |map, cx| {
 638                            map.fold(ranges, cx);
 639                        });
 640                    }
 641                }
 642                _ => {
 643                    buffer.update(cx, |buffer, cx| buffer.randomly_edit(&mut rng, 5, cx));
 644                }
 645            }
 646
 647            if map.read_with(cx, |map, cx| map.is_rewrapping(cx)) {
 648                notifications.next().await.unwrap();
 649            }
 650
 651            let snapshot = map.update(cx, |map, cx| map.snapshot(cx));
 652            fold_count = snapshot.fold_count();
 653            log::info!("buffer text: {:?}", snapshot.buffer_snapshot.text());
 654            log::info!("fold text: {:?}", snapshot.folds_snapshot.text());
 655            log::info!("tab text: {:?}", snapshot.tabs_snapshot.text());
 656            log::info!("wrap text: {:?}", snapshot.wraps_snapshot.text());
 657            log::info!("block text: {:?}", snapshot.blocks_snapshot.text());
 658            log::info!("display text: {:?}", snapshot.text());
 659
 660            // Line boundaries
 661            let buffer = &snapshot.buffer_snapshot;
 662            for _ in 0..5 {
 663                let row = rng.gen_range(0..=buffer.max_point().row);
 664                let column = rng.gen_range(0..=buffer.line_len(row));
 665                let point = buffer.clip_point(Point::new(row, column), Left);
 666
 667                let (prev_buffer_bound, prev_display_bound) = snapshot.prev_line_boundary(point);
 668                let (next_buffer_bound, next_display_bound) = snapshot.next_line_boundary(point);
 669
 670                assert!(prev_buffer_bound <= point);
 671                assert!(next_buffer_bound >= point);
 672                assert_eq!(prev_buffer_bound.column, 0);
 673                assert_eq!(prev_display_bound.column(), 0);
 674                if next_buffer_bound < buffer.max_point() {
 675                    assert_eq!(buffer.chars_at(next_buffer_bound).next(), Some('\n'));
 676                }
 677
 678                assert_eq!(
 679                    prev_display_bound,
 680                    prev_buffer_bound.to_display_point(&snapshot),
 681                    "row boundary before {:?}. reported buffer row boundary: {:?}",
 682                    point,
 683                    prev_buffer_bound
 684                );
 685                assert_eq!(
 686                    next_display_bound,
 687                    next_buffer_bound.to_display_point(&snapshot),
 688                    "display row boundary after {:?}. reported buffer row boundary: {:?}",
 689                    point,
 690                    next_buffer_bound
 691                );
 692                assert_eq!(
 693                    prev_buffer_bound,
 694                    prev_display_bound.to_point(&snapshot),
 695                    "row boundary before {:?}. reported display row boundary: {:?}",
 696                    point,
 697                    prev_display_bound
 698                );
 699                assert_eq!(
 700                    next_buffer_bound,
 701                    next_display_bound.to_point(&snapshot),
 702                    "row boundary after {:?}. reported display row boundary: {:?}",
 703                    point,
 704                    next_display_bound
 705                );
 706            }
 707
 708            // Movement
 709            let min_point = snapshot.clip_point(DisplayPoint::new(0, 0), Left);
 710            let max_point = snapshot.clip_point(snapshot.max_point(), Right);
 711            for _ in 0..5 {
 712                let row = rng.gen_range(0..=snapshot.max_point().row());
 713                let column = rng.gen_range(0..=snapshot.line_len(row));
 714                let point = snapshot.clip_point(DisplayPoint::new(row, column), Left);
 715
 716                log::info!("Moving from point {:?}", point);
 717
 718                let moved_right = movement::right(&snapshot, point);
 719                log::info!("Right {:?}", moved_right);
 720                if point < max_point {
 721                    assert!(moved_right > point);
 722                    if point.column() == snapshot.line_len(point.row())
 723                        || snapshot.soft_wrap_indent(point.row()).is_some()
 724                            && point.column() == snapshot.line_len(point.row()) - 1
 725                    {
 726                        assert!(moved_right.row() > point.row());
 727                    }
 728                } else {
 729                    assert_eq!(moved_right, point);
 730                }
 731
 732                let moved_left = movement::left(&snapshot, point);
 733                log::info!("Left {:?}", moved_left);
 734                if point > min_point {
 735                    assert!(moved_left < point);
 736                    if point.column() == 0 {
 737                        assert!(moved_left.row() < point.row());
 738                    }
 739                } else {
 740                    assert_eq!(moved_left, point);
 741                }
 742            }
 743        }
 744    }
 745
 746    #[gpui::test(retries = 5)]
 747    fn test_soft_wraps(cx: &mut MutableAppContext) {
 748        cx.foreground().set_block_on_ticks(usize::MAX..=usize::MAX);
 749        cx.foreground().forbid_parking();
 750
 751        let font_cache = cx.font_cache();
 752
 753        let tab_size = 4;
 754        let family_id = font_cache.load_family(&["Helvetica"]).unwrap();
 755        let font_id = font_cache
 756            .select_font(family_id, &Default::default())
 757            .unwrap();
 758        let font_size = 12.0;
 759        let wrap_width = Some(64.);
 760
 761        let text = "one two three four five\nsix seven eight";
 762        let buffer = MultiBuffer::build_simple(text, cx);
 763        let map = cx.add_model(|cx| {
 764            DisplayMap::new(
 765                buffer.clone(),
 766                tab_size,
 767                font_id,
 768                font_size,
 769                wrap_width,
 770                1,
 771                1,
 772                cx,
 773            )
 774        });
 775
 776        let snapshot = map.update(cx, |map, cx| map.snapshot(cx));
 777        assert_eq!(
 778            snapshot.text_chunks(0).collect::<String>(),
 779            "one two \nthree four \nfive\nsix seven \neight"
 780        );
 781        assert_eq!(
 782            snapshot.clip_point(DisplayPoint::new(0, 8), Bias::Left),
 783            DisplayPoint::new(0, 7)
 784        );
 785        assert_eq!(
 786            snapshot.clip_point(DisplayPoint::new(0, 8), Bias::Right),
 787            DisplayPoint::new(1, 0)
 788        );
 789        assert_eq!(
 790            movement::right(&snapshot, DisplayPoint::new(0, 7)),
 791            DisplayPoint::new(1, 0)
 792        );
 793        assert_eq!(
 794            movement::left(&snapshot, DisplayPoint::new(1, 0)),
 795            DisplayPoint::new(0, 7)
 796        );
 797        assert_eq!(
 798            movement::up(&snapshot, DisplayPoint::new(1, 10), SelectionGoal::None),
 799            (DisplayPoint::new(0, 7), SelectionGoal::Column(10))
 800        );
 801        assert_eq!(
 802            movement::down(
 803                &snapshot,
 804                DisplayPoint::new(0, 7),
 805                SelectionGoal::Column(10)
 806            ),
 807            (DisplayPoint::new(1, 10), SelectionGoal::Column(10))
 808        );
 809        assert_eq!(
 810            movement::down(
 811                &snapshot,
 812                DisplayPoint::new(1, 10),
 813                SelectionGoal::Column(10)
 814            ),
 815            (DisplayPoint::new(2, 4), SelectionGoal::Column(10))
 816        );
 817
 818        let ix = snapshot.buffer_snapshot.text().find("seven").unwrap();
 819        buffer.update(cx, |buffer, cx| {
 820            buffer.edit(vec![ix..ix], "and ", cx);
 821        });
 822
 823        let snapshot = map.update(cx, |map, cx| map.snapshot(cx));
 824        assert_eq!(
 825            snapshot.text_chunks(1).collect::<String>(),
 826            "three four \nfive\nsix and \nseven eight"
 827        );
 828
 829        // Re-wrap on font size changes
 830        map.update(cx, |map, cx| map.set_font(font_id, font_size + 3., cx));
 831
 832        let snapshot = map.update(cx, |map, cx| map.snapshot(cx));
 833        assert_eq!(
 834            snapshot.text_chunks(1).collect::<String>(),
 835            "three \nfour five\nsix and \nseven \neight"
 836        )
 837    }
 838
 839    #[gpui::test]
 840    fn test_text_chunks(cx: &mut gpui::MutableAppContext) {
 841        let text = sample_text(6, 6, 'a');
 842        let buffer = MultiBuffer::build_simple(&text, cx);
 843        let tab_size = 4;
 844        let family_id = cx.font_cache().load_family(&["Helvetica"]).unwrap();
 845        let font_id = cx
 846            .font_cache()
 847            .select_font(family_id, &Default::default())
 848            .unwrap();
 849        let font_size = 14.0;
 850        let map = cx.add_model(|cx| {
 851            DisplayMap::new(buffer.clone(), tab_size, font_id, font_size, None, 1, 1, cx)
 852        });
 853        buffer.update(cx, |buffer, cx| {
 854            buffer.edit(
 855                vec![
 856                    Point::new(1, 0)..Point::new(1, 0),
 857                    Point::new(1, 1)..Point::new(1, 1),
 858                    Point::new(2, 1)..Point::new(2, 1),
 859                ],
 860                "\t",
 861                cx,
 862            )
 863        });
 864
 865        assert_eq!(
 866            map.update(cx, |map, cx| map.snapshot(cx))
 867                .text_chunks(1)
 868                .collect::<String>()
 869                .lines()
 870                .next(),
 871            Some("    b   bbbbb")
 872        );
 873        assert_eq!(
 874            map.update(cx, |map, cx| map.snapshot(cx))
 875                .text_chunks(2)
 876                .collect::<String>()
 877                .lines()
 878                .next(),
 879            Some("c   ccccc")
 880        );
 881    }
 882
 883    #[gpui::test]
 884    async fn test_chunks(cx: &mut gpui::TestAppContext) {
 885        use unindent::Unindent as _;
 886
 887        let text = r#"
 888            fn outer() {}
 889
 890            mod module {
 891                fn inner() {}
 892            }"#
 893        .unindent();
 894
 895        let theme = SyntaxTheme::new(vec![
 896            ("mod.body".to_string(), Color::red().into()),
 897            ("fn.name".to_string(), Color::blue().into()),
 898        ]);
 899        let language = Arc::new(
 900            Language::new(
 901                LanguageConfig {
 902                    name: "Test".into(),
 903                    path_suffixes: vec![".test".to_string()],
 904                    ..Default::default()
 905                },
 906                Some(tree_sitter_rust::language()),
 907            )
 908            .with_highlights_query(
 909                r#"
 910                (mod_item name: (identifier) body: _ @mod.body)
 911                (function_item name: (identifier) @fn.name)
 912                "#,
 913            )
 914            .unwrap(),
 915        );
 916        language.set_theme(&theme);
 917
 918        let buffer = cx.add_model(|cx| Buffer::new(0, text, cx).with_language(language, cx));
 919        buffer.condition(&cx, |buf, _| !buf.is_parsing()).await;
 920        let buffer = cx.add_model(|cx| MultiBuffer::singleton(buffer, cx));
 921
 922        let tab_size = 2;
 923        let font_cache = cx.font_cache();
 924        let family_id = font_cache.load_family(&["Helvetica"]).unwrap();
 925        let font_id = font_cache
 926            .select_font(family_id, &Default::default())
 927            .unwrap();
 928        let font_size = 14.0;
 929
 930        let map = cx
 931            .add_model(|cx| DisplayMap::new(buffer, tab_size, font_id, font_size, None, 1, 1, cx));
 932        assert_eq!(
 933            cx.update(|cx| chunks(0..5, &map, &theme, cx)),
 934            vec![
 935                ("fn ".to_string(), None),
 936                ("outer".to_string(), Some(Color::blue())),
 937                ("() {}\n\nmod module ".to_string(), None),
 938                ("{\n    fn ".to_string(), Some(Color::red())),
 939                ("inner".to_string(), Some(Color::blue())),
 940                ("() {}\n}".to_string(), Some(Color::red())),
 941            ]
 942        );
 943        assert_eq!(
 944            cx.update(|cx| chunks(3..5, &map, &theme, cx)),
 945            vec![
 946                ("    fn ".to_string(), Some(Color::red())),
 947                ("inner".to_string(), Some(Color::blue())),
 948                ("() {}\n}".to_string(), Some(Color::red())),
 949            ]
 950        );
 951
 952        map.update(cx, |map, cx| {
 953            map.fold(vec![Point::new(0, 6)..Point::new(3, 2)], cx)
 954        });
 955        assert_eq!(
 956            cx.update(|cx| chunks(0..2, &map, &theme, cx)),
 957            vec![
 958                ("fn ".to_string(), None),
 959                ("out".to_string(), Some(Color::blue())),
 960                ("".to_string(), None),
 961                ("  fn ".to_string(), Some(Color::red())),
 962                ("inner".to_string(), Some(Color::blue())),
 963                ("() {}\n}".to_string(), Some(Color::red())),
 964            ]
 965        );
 966    }
 967
 968    #[gpui::test]
 969    async fn test_chunks_with_soft_wrapping(cx: &mut gpui::TestAppContext) {
 970        use unindent::Unindent as _;
 971
 972        cx.foreground().set_block_on_ticks(usize::MAX..=usize::MAX);
 973
 974        let text = r#"
 975            fn outer() {}
 976
 977            mod module {
 978                fn inner() {}
 979            }"#
 980        .unindent();
 981
 982        let theme = SyntaxTheme::new(vec![
 983            ("mod.body".to_string(), Color::red().into()),
 984            ("fn.name".to_string(), Color::blue().into()),
 985        ]);
 986        let language = Arc::new(
 987            Language::new(
 988                LanguageConfig {
 989                    name: "Test".into(),
 990                    path_suffixes: vec![".test".to_string()],
 991                    ..Default::default()
 992                },
 993                Some(tree_sitter_rust::language()),
 994            )
 995            .with_highlights_query(
 996                r#"
 997                (mod_item name: (identifier) body: _ @mod.body)
 998                (function_item name: (identifier) @fn.name)
 999                "#,
1000            )
1001            .unwrap(),
1002        );
1003        language.set_theme(&theme);
1004
1005        let buffer = cx.add_model(|cx| Buffer::new(0, text, cx).with_language(language, cx));
1006        buffer.condition(&cx, |buf, _| !buf.is_parsing()).await;
1007        let buffer = cx.add_model(|cx| MultiBuffer::singleton(buffer, cx));
1008
1009        let font_cache = cx.font_cache();
1010
1011        let tab_size = 4;
1012        let family_id = font_cache.load_family(&["Courier"]).unwrap();
1013        let font_id = font_cache
1014            .select_font(family_id, &Default::default())
1015            .unwrap();
1016        let font_size = 16.0;
1017
1018        let map = cx.add_model(|cx| {
1019            DisplayMap::new(buffer, tab_size, font_id, font_size, Some(40.0), 1, 1, cx)
1020        });
1021        assert_eq!(
1022            cx.update(|cx| chunks(0..5, &map, &theme, cx)),
1023            [
1024                ("fn \n".to_string(), None),
1025                ("oute\nr".to_string(), Some(Color::blue())),
1026                ("() \n{}\n\n".to_string(), None),
1027            ]
1028        );
1029        assert_eq!(
1030            cx.update(|cx| chunks(3..5, &map, &theme, cx)),
1031            [("{}\n\n".to_string(), None)]
1032        );
1033
1034        map.update(cx, |map, cx| {
1035            map.fold(vec![Point::new(0, 6)..Point::new(3, 2)], cx)
1036        });
1037        assert_eq!(
1038            cx.update(|cx| chunks(1..4, &map, &theme, cx)),
1039            [
1040                ("out".to_string(), Some(Color::blue())),
1041                ("\n".to_string(), None),
1042                ("  \nfn ".to_string(), Some(Color::red())),
1043                ("i\n".to_string(), Some(Color::blue()))
1044            ]
1045        );
1046    }
1047
1048    #[gpui::test]
1049    fn test_clip_point(cx: &mut gpui::MutableAppContext) {
1050        use Bias::{Left, Right};
1051
1052        let text = "\n'a', 'α',\t'✋',\t'❎', '🍐'\n";
1053        let display_text = "\n'a', 'α',   '✋',    '❎', '🍐'\n";
1054        let buffer = MultiBuffer::build_simple(text, cx);
1055
1056        let tab_size = 4;
1057        let font_cache = cx.font_cache();
1058        let family_id = font_cache.load_family(&["Helvetica"]).unwrap();
1059        let font_id = font_cache
1060            .select_font(family_id, &Default::default())
1061            .unwrap();
1062        let font_size = 14.0;
1063        let map = cx.add_model(|cx| {
1064            DisplayMap::new(buffer.clone(), tab_size, font_id, font_size, None, 1, 1, cx)
1065        });
1066        let map = map.update(cx, |map, cx| map.snapshot(cx));
1067
1068        assert_eq!(map.text(), display_text);
1069        for (input_column, bias, output_column) in vec![
1070            ("'a', '".len(), Left, "'a', '".len()),
1071            ("'a', '".len() + 1, Left, "'a', '".len()),
1072            ("'a', '".len() + 1, Right, "'a', 'α".len()),
1073            ("'a', 'α', ".len(), Left, "'a', 'α',".len()),
1074            ("'a', 'α', ".len(), Right, "'a', 'α',   ".len()),
1075            ("'a', 'α',   '".len() + 1, Left, "'a', 'α',   '".len()),
1076            ("'a', 'α',   '".len() + 1, Right, "'a', 'α',   '✋".len()),
1077            ("'a', 'α',   '✋',".len(), Right, "'a', 'α',   '✋',".len()),
1078            ("'a', 'α',   '✋', ".len(), Left, "'a', 'α',   '✋',".len()),
1079            (
1080                "'a', 'α',   '✋', ".len(),
1081                Right,
1082                "'a', 'α',   '✋',    ".len(),
1083            ),
1084        ] {
1085            assert_eq!(
1086                map.clip_point(DisplayPoint::new(1, input_column as u32), bias),
1087                DisplayPoint::new(1, output_column as u32),
1088                "clip_point(({}, {}))",
1089                1,
1090                input_column,
1091            );
1092        }
1093    }
1094
1095    #[gpui::test]
1096    fn test_tabs_with_multibyte_chars(cx: &mut gpui::MutableAppContext) {
1097        let text = "\t\tα\nβ\t\n🏀β\t\tγ";
1098        let buffer = MultiBuffer::build_simple(text, cx);
1099        let tab_size = 4;
1100        let font_cache = cx.font_cache();
1101        let family_id = font_cache.load_family(&["Helvetica"]).unwrap();
1102        let font_id = font_cache
1103            .select_font(family_id, &Default::default())
1104            .unwrap();
1105        let font_size = 14.0;
1106
1107        let map = cx.add_model(|cx| {
1108            DisplayMap::new(buffer.clone(), tab_size, font_id, font_size, None, 1, 1, cx)
1109        });
1110        let map = map.update(cx, |map, cx| map.snapshot(cx));
1111        assert_eq!(map.text(), "✅       α\nβ   \n🏀β      γ");
1112        assert_eq!(
1113            map.text_chunks(0).collect::<String>(),
1114            "✅       α\nβ   \n🏀β      γ"
1115        );
1116        assert_eq!(map.text_chunks(1).collect::<String>(), "β   \n🏀β      γ");
1117        assert_eq!(map.text_chunks(2).collect::<String>(), "🏀β      γ");
1118
1119        let point = Point::new(0, "\t\t".len() as u32);
1120        let display_point = DisplayPoint::new(0, "".len() as u32);
1121        assert_eq!(point.to_display_point(&map), display_point);
1122        assert_eq!(display_point.to_point(&map), point);
1123
1124        let point = Point::new(1, "β\t".len() as u32);
1125        let display_point = DisplayPoint::new(1, "β   ".len() as u32);
1126        assert_eq!(point.to_display_point(&map), display_point);
1127        assert_eq!(display_point.to_point(&map), point,);
1128
1129        let point = Point::new(2, "🏀β\t\t".len() as u32);
1130        let display_point = DisplayPoint::new(2, "🏀β      ".len() as u32);
1131        assert_eq!(point.to_display_point(&map), display_point);
1132        assert_eq!(display_point.to_point(&map), point,);
1133
1134        // Display points inside of expanded tabs
1135        assert_eq!(
1136            DisplayPoint::new(0, "".len() as u32).to_point(&map),
1137            Point::new(0, "\t".len() as u32),
1138        );
1139        assert_eq!(
1140            DisplayPoint::new(0, "".len() as u32).to_point(&map),
1141            Point::new(0, "".len() as u32),
1142        );
1143
1144        // Clipping display points inside of multi-byte characters
1145        assert_eq!(
1146            map.clip_point(DisplayPoint::new(0, "".len() as u32 - 1), Left),
1147            DisplayPoint::new(0, 0)
1148        );
1149        assert_eq!(
1150            map.clip_point(DisplayPoint::new(0, "".len() as u32 - 1), Bias::Right),
1151            DisplayPoint::new(0, "".len() as u32)
1152        );
1153    }
1154
1155    #[gpui::test]
1156    fn test_max_point(cx: &mut gpui::MutableAppContext) {
1157        let buffer = MultiBuffer::build_simple("aaa\n\t\tbbb", cx);
1158        let tab_size = 4;
1159        let font_cache = cx.font_cache();
1160        let family_id = font_cache.load_family(&["Helvetica"]).unwrap();
1161        let font_id = font_cache
1162            .select_font(family_id, &Default::default())
1163            .unwrap();
1164        let font_size = 14.0;
1165        let map = cx.add_model(|cx| {
1166            DisplayMap::new(buffer.clone(), tab_size, font_id, font_size, None, 1, 1, cx)
1167        });
1168        assert_eq!(
1169            map.update(cx, |map, cx| map.snapshot(cx)).max_point(),
1170            DisplayPoint::new(1, 11)
1171        )
1172    }
1173
1174    fn chunks<'a>(
1175        rows: Range<u32>,
1176        map: &ModelHandle<DisplayMap>,
1177        theme: &'a SyntaxTheme,
1178        cx: &mut MutableAppContext,
1179    ) -> Vec<(String, Option<Color>)> {
1180        let snapshot = map.update(cx, |map, cx| map.snapshot(cx));
1181        let mut chunks: Vec<(String, Option<Color>)> = Vec::new();
1182        for chunk in snapshot.chunks(rows, true) {
1183            let color = chunk
1184                .syntax_highlight_id
1185                .and_then(|id| id.style(theme)?.color);
1186            if let Some((last_chunk, last_color)) = chunks.last_mut() {
1187                if color == *last_color {
1188                    last_chunk.push_str(chunk.text);
1189                } else {
1190                    chunks.push((chunk.text.to_string(), color));
1191                }
1192            } else {
1193                chunks.push((chunk.text.to_string(), color));
1194            }
1195        }
1196        chunks
1197    }
1198}