wrap_map.rs

   1use super::{
   2    fold_map,
   3    tab_map::{self, Edit as TabEdit, Snapshot as TabSnapshot, TabPoint, TextSummary},
   4};
   5use gpui::{fonts::FontId, text_layout::LineWrapper, Entity, ModelContext, Task};
   6use language::{HighlightedChunk, Point};
   7use lazy_static::lazy_static;
   8use smol::future::yield_now;
   9use std::{collections::VecDeque, ops::Range, time::Duration};
  10use sum_tree::{Bias, Cursor, SumTree};
  11
  12pub type Edit = buffer::Edit<u32>;
  13
  14pub struct WrapMap {
  15    snapshot: Snapshot,
  16    pending_edits: VecDeque<(TabSnapshot, Vec<TabEdit>)>,
  17    wrap_width: Option<f32>,
  18    background_task: Option<Task<()>>,
  19    font: (FontId, f32),
  20}
  21
  22impl Entity for WrapMap {
  23    type Event = ();
  24}
  25
  26#[derive(Clone)]
  27pub struct Snapshot {
  28    tab_snapshot: TabSnapshot,
  29    transforms: SumTree<Transform>,
  30    interpolated: bool,
  31}
  32
  33#[derive(Clone, Debug, Default, Eq, PartialEq)]
  34struct Transform {
  35    summary: TransformSummary,
  36    display_text: Option<&'static str>,
  37}
  38
  39#[derive(Clone, Debug, Default, Eq, PartialEq)]
  40struct TransformSummary {
  41    input: TextSummary,
  42    output: TextSummary,
  43}
  44
  45#[derive(Copy, Clone, Debug, Default, Eq, Ord, PartialOrd, PartialEq)]
  46pub struct WrapPoint(super::Point);
  47
  48pub struct Chunks<'a> {
  49    input_chunks: tab_map::Chunks<'a>,
  50    input_chunk: &'a str,
  51    output_position: WrapPoint,
  52    transforms: Cursor<'a, Transform, (WrapPoint, TabPoint)>,
  53}
  54
  55pub struct HighlightedChunks<'a> {
  56    input_chunks: tab_map::HighlightedChunks<'a>,
  57    input_chunk: HighlightedChunk<'a>,
  58    output_position: WrapPoint,
  59    max_output_row: u32,
  60    transforms: Cursor<'a, Transform, (WrapPoint, TabPoint)>,
  61}
  62
  63pub struct BufferRows<'a> {
  64    input_buffer_rows: fold_map::BufferRows<'a>,
  65    input_buffer_row: u32,
  66    output_row: u32,
  67    soft_wrapped: bool,
  68    max_output_row: u32,
  69    transforms: Cursor<'a, Transform, (WrapPoint, TabPoint)>,
  70}
  71
  72impl WrapMap {
  73    pub fn new(
  74        tab_snapshot: TabSnapshot,
  75        font_id: FontId,
  76        font_size: f32,
  77        wrap_width: Option<f32>,
  78        cx: &mut ModelContext<Self>,
  79    ) -> Self {
  80        let mut this = Self {
  81            font: (font_id, font_size),
  82            wrap_width: None,
  83            pending_edits: Default::default(),
  84            snapshot: Snapshot::new(tab_snapshot),
  85            background_task: None,
  86        };
  87        this.set_wrap_width(wrap_width, cx);
  88
  89        this
  90    }
  91
  92    #[cfg(test)]
  93    pub fn is_rewrapping(&self) -> bool {
  94        self.background_task.is_some()
  95    }
  96
  97    pub fn sync(
  98        &mut self,
  99        tab_snapshot: TabSnapshot,
 100        edits: Vec<TabEdit>,
 101        cx: &mut ModelContext<Self>,
 102    ) -> Snapshot {
 103        self.pending_edits.push_back((tab_snapshot, edits));
 104        self.flush_edits(cx);
 105        self.snapshot.clone()
 106    }
 107
 108    pub fn set_font(&mut self, font_id: FontId, font_size: f32, cx: &mut ModelContext<Self>) {
 109        if (font_id, font_size) != self.font {
 110            self.font = (font_id, font_size);
 111            self.rewrap(cx)
 112        }
 113    }
 114
 115    pub fn set_wrap_width(&mut self, wrap_width: Option<f32>, cx: &mut ModelContext<Self>) -> bool {
 116        if wrap_width == self.wrap_width {
 117            return false;
 118        }
 119
 120        self.wrap_width = wrap_width;
 121        self.rewrap(cx);
 122        true
 123    }
 124
 125    fn rewrap(&mut self, cx: &mut ModelContext<Self>) {
 126        self.background_task.take();
 127
 128        if let Some(wrap_width) = self.wrap_width {
 129            let mut new_snapshot = self.snapshot.clone();
 130            let font_cache = cx.font_cache().clone();
 131            let (font_id, font_size) = self.font;
 132            let task = cx.background().spawn(async move {
 133                let mut line_wrapper = font_cache.line_wrapper(font_id, font_size);
 134                let tab_snapshot = new_snapshot.tab_snapshot.clone();
 135                let range = TabPoint::zero()..tab_snapshot.max_point();
 136                new_snapshot
 137                    .update(
 138                        tab_snapshot,
 139                        &[TabEdit {
 140                            old_lines: range.clone(),
 141                            new_lines: range.clone(),
 142                        }],
 143                        wrap_width,
 144                        &mut line_wrapper,
 145                    )
 146                    .await;
 147                new_snapshot
 148            });
 149
 150            match cx
 151                .background()
 152                .block_with_timeout(Duration::from_millis(5), task)
 153            {
 154                Ok(snapshot) => {
 155                    self.snapshot = snapshot;
 156                    cx.notify();
 157                }
 158                Err(wrap_task) => {
 159                    self.background_task = Some(cx.spawn(|this, mut cx| async move {
 160                        let snapshot = wrap_task.await;
 161                        this.update(&mut cx, |this, cx| {
 162                            this.snapshot = snapshot;
 163                            this.background_task = None;
 164                            this.flush_edits(cx);
 165                            cx.notify();
 166                        });
 167                    }));
 168                }
 169            }
 170        } else {
 171            self.snapshot.transforms = SumTree::new();
 172            let summary = self.snapshot.tab_snapshot.text_summary();
 173            if !summary.lines.is_zero() {
 174                self.snapshot
 175                    .transforms
 176                    .push(Transform::isomorphic(summary), &());
 177            }
 178        }
 179    }
 180
 181    fn flush_edits(&mut self, cx: &mut ModelContext<Self>) {
 182        if !self.snapshot.interpolated {
 183            let mut to_remove_len = 0;
 184            for (tab_snapshot, _) in &self.pending_edits {
 185                if tab_snapshot.version() <= self.snapshot.tab_snapshot.version() {
 186                    to_remove_len += 1;
 187                } else {
 188                    break;
 189                }
 190            }
 191            self.pending_edits.drain(..to_remove_len);
 192        }
 193
 194        if self.pending_edits.is_empty() {
 195            return;
 196        }
 197
 198        if let Some(wrap_width) = self.wrap_width {
 199            if self.background_task.is_none() {
 200                let pending_edits = self.pending_edits.clone();
 201                let mut snapshot = self.snapshot.clone();
 202                let font_cache = cx.font_cache().clone();
 203                let (font_id, font_size) = self.font;
 204                let update_task = cx.background().spawn(async move {
 205                    let mut line_wrapper = font_cache.line_wrapper(font_id, font_size);
 206
 207                    for (tab_snapshot, edits) in pending_edits {
 208                        snapshot
 209                            .update(tab_snapshot, &edits, wrap_width, &mut line_wrapper)
 210                            .await;
 211                    }
 212                    snapshot
 213                });
 214
 215                match cx
 216                    .background()
 217                    .block_with_timeout(Duration::from_millis(1), update_task)
 218                {
 219                    Ok(snapshot) => {
 220                        self.snapshot = snapshot;
 221                    }
 222                    Err(update_task) => {
 223                        self.background_task = Some(cx.spawn(|this, mut cx| async move {
 224                            let snapshot = update_task.await;
 225                            this.update(&mut cx, |this, cx| {
 226                                this.snapshot = snapshot;
 227                                this.background_task = None;
 228                                this.flush_edits(cx);
 229                                cx.notify();
 230                            });
 231                        }));
 232                    }
 233                }
 234            }
 235        }
 236
 237        let was_interpolated = self.snapshot.interpolated;
 238        let mut to_remove_len = 0;
 239        for (tab_snapshot, edits) in &self.pending_edits {
 240            if tab_snapshot.version() <= self.snapshot.tab_snapshot.version() {
 241                to_remove_len += 1;
 242            } else {
 243                self.snapshot.interpolate(tab_snapshot.clone(), &edits);
 244            }
 245        }
 246
 247        if !was_interpolated {
 248            self.pending_edits.drain(..to_remove_len);
 249        }
 250    }
 251}
 252
 253impl Snapshot {
 254    fn new(tab_snapshot: TabSnapshot) -> Self {
 255        let mut transforms = SumTree::new();
 256        let extent = tab_snapshot.text_summary();
 257        if !extent.lines.is_zero() {
 258            transforms.push(Transform::isomorphic(extent), &());
 259        }
 260        Self {
 261            transforms,
 262            tab_snapshot,
 263            interpolated: true,
 264        }
 265    }
 266
 267    fn interpolate(&mut self, new_tab_snapshot: TabSnapshot, edits: &[TabEdit]) {
 268        let mut new_transforms;
 269        if edits.is_empty() {
 270            new_transforms = self.transforms.clone();
 271        } else {
 272            let mut old_cursor = self.transforms.cursor::<TabPoint>();
 273            let mut edits = edits.into_iter().peekable();
 274            new_transforms =
 275                old_cursor.slice(&edits.peek().unwrap().old_lines.start, Bias::Right, &());
 276
 277            while let Some(edit) = edits.next() {
 278                if edit.new_lines.start > TabPoint::from(new_transforms.summary().input.lines) {
 279                    let summary = new_tab_snapshot.text_summary_for_range(
 280                        TabPoint::from(new_transforms.summary().input.lines)..edit.new_lines.start,
 281                    );
 282                    new_transforms.push_or_extend(Transform::isomorphic(summary));
 283                }
 284
 285                if !edit.new_lines.is_empty() {
 286                    new_transforms.push_or_extend(Transform::isomorphic(
 287                        new_tab_snapshot.text_summary_for_range(edit.new_lines.clone()),
 288                    ));
 289                }
 290
 291                old_cursor.seek_forward(&edit.old_lines.end, Bias::Right, &());
 292                if let Some(next_edit) = edits.peek() {
 293                    if next_edit.old_lines.start > old_cursor.end(&()) {
 294                        if old_cursor.end(&()) > edit.old_lines.end {
 295                            let summary = self
 296                                .tab_snapshot
 297                                .text_summary_for_range(edit.old_lines.end..old_cursor.end(&()));
 298                            new_transforms.push_or_extend(Transform::isomorphic(summary));
 299                        }
 300                        old_cursor.next(&());
 301                        new_transforms.push_tree(
 302                            old_cursor.slice(&next_edit.old_lines.start, Bias::Right, &()),
 303                            &(),
 304                        );
 305                    }
 306                } else {
 307                    if old_cursor.end(&()) > edit.old_lines.end {
 308                        let summary = self
 309                            .tab_snapshot
 310                            .text_summary_for_range(edit.old_lines.end..old_cursor.end(&()));
 311                        new_transforms.push_or_extend(Transform::isomorphic(summary));
 312                    }
 313                    old_cursor.next(&());
 314                    new_transforms.push_tree(old_cursor.suffix(&()), &());
 315                }
 316            }
 317        }
 318
 319        self.transforms = new_transforms;
 320        self.tab_snapshot = new_tab_snapshot;
 321        self.interpolated = true;
 322        self.check_invariants();
 323    }
 324
 325    async fn update(
 326        &mut self,
 327        new_tab_snapshot: TabSnapshot,
 328        edits: &[TabEdit],
 329        wrap_width: f32,
 330        line_wrapper: &mut LineWrapper,
 331    ) {
 332        #[derive(Debug)]
 333        struct RowEdit {
 334            old_rows: Range<u32>,
 335            new_rows: Range<u32>,
 336        }
 337
 338        let mut edits = edits.into_iter().peekable();
 339        let mut row_edits = Vec::new();
 340        while let Some(edit) = edits.next() {
 341            let mut row_edit = RowEdit {
 342                old_rows: edit.old_lines.start.row()..edit.old_lines.end.row() + 1,
 343                new_rows: edit.new_lines.start.row()..edit.new_lines.end.row() + 1,
 344            };
 345
 346            while let Some(next_edit) = edits.peek() {
 347                if next_edit.old_lines.start.row() <= row_edit.old_rows.end {
 348                    row_edit.old_rows.end = next_edit.old_lines.end.row() + 1;
 349                    row_edit.new_rows.end = next_edit.new_lines.end.row() + 1;
 350                    edits.next();
 351                } else {
 352                    break;
 353                }
 354            }
 355
 356            row_edits.push(row_edit);
 357        }
 358
 359        let mut new_transforms;
 360        if row_edits.is_empty() {
 361            new_transforms = self.transforms.clone();
 362        } else {
 363            let mut row_edits = row_edits.into_iter().peekable();
 364            let mut old_cursor = self.transforms.cursor::<TabPoint>();
 365
 366            new_transforms = old_cursor.slice(
 367                &TabPoint::new(row_edits.peek().unwrap().old_rows.start, 0),
 368                Bias::Right,
 369                &(),
 370            );
 371
 372            while let Some(edit) = row_edits.next() {
 373                if edit.new_rows.start > new_transforms.summary().input.lines.row {
 374                    let summary = new_tab_snapshot.text_summary_for_range(
 375                        TabPoint::new(new_transforms.summary().input.lines.row, 0)
 376                            ..TabPoint::new(edit.new_rows.start, 0),
 377                    );
 378                    new_transforms.push_or_extend(Transform::isomorphic(summary));
 379                }
 380
 381                let mut line = String::new();
 382                let mut remaining = None;
 383                let mut chunks = new_tab_snapshot.chunks_at(TabPoint::new(edit.new_rows.start, 0));
 384                let mut edit_transforms = Vec::<Transform>::new();
 385                for _ in edit.new_rows.start..edit.new_rows.end {
 386                    while let Some(chunk) = remaining.take().or_else(|| chunks.next()) {
 387                        if let Some(ix) = chunk.find('\n') {
 388                            line.push_str(&chunk[..ix + 1]);
 389                            remaining = Some(&chunk[ix + 1..]);
 390                            break;
 391                        } else {
 392                            line.push_str(chunk)
 393                        }
 394                    }
 395
 396                    if line.is_empty() {
 397                        break;
 398                    }
 399
 400                    let mut prev_boundary_ix = 0;
 401                    for boundary in line_wrapper.wrap_line(&line, wrap_width) {
 402                        let wrapped = &line[prev_boundary_ix..boundary.ix];
 403                        push_isomorphic(&mut edit_transforms, TextSummary::from(wrapped));
 404                        edit_transforms.push(Transform::wrap(boundary.next_indent));
 405                        prev_boundary_ix = boundary.ix;
 406                    }
 407
 408                    if prev_boundary_ix < line.len() {
 409                        push_isomorphic(
 410                            &mut edit_transforms,
 411                            TextSummary::from(&line[prev_boundary_ix..]),
 412                        );
 413                    }
 414
 415                    line.clear();
 416                    yield_now().await;
 417                }
 418
 419                let mut edit_transforms = edit_transforms.into_iter();
 420                if let Some(transform) = edit_transforms.next() {
 421                    new_transforms.push_or_extend(transform);
 422                }
 423                new_transforms.extend(edit_transforms, &());
 424
 425                old_cursor.seek_forward(&TabPoint::new(edit.old_rows.end, 0), Bias::Right, &());
 426                if let Some(next_edit) = row_edits.peek() {
 427                    if next_edit.old_rows.start > old_cursor.end(&()).row() {
 428                        if old_cursor.end(&()) > TabPoint::new(edit.old_rows.end, 0) {
 429                            let summary = self.tab_snapshot.text_summary_for_range(
 430                                TabPoint::new(edit.old_rows.end, 0)..old_cursor.end(&()),
 431                            );
 432                            new_transforms.push_or_extend(Transform::isomorphic(summary));
 433                        }
 434                        old_cursor.next(&());
 435                        new_transforms.push_tree(
 436                            old_cursor.slice(
 437                                &TabPoint::new(next_edit.old_rows.start, 0),
 438                                Bias::Right,
 439                                &(),
 440                            ),
 441                            &(),
 442                        );
 443                    }
 444                } else {
 445                    if old_cursor.end(&()) > TabPoint::new(edit.old_rows.end, 0) {
 446                        let summary = self.tab_snapshot.text_summary_for_range(
 447                            TabPoint::new(edit.old_rows.end, 0)..old_cursor.end(&()),
 448                        );
 449                        new_transforms.push_or_extend(Transform::isomorphic(summary));
 450                    }
 451                    old_cursor.next(&());
 452                    new_transforms.push_tree(old_cursor.suffix(&()), &());
 453                }
 454            }
 455        }
 456
 457        self.transforms = new_transforms;
 458        self.tab_snapshot = new_tab_snapshot;
 459        self.interpolated = false;
 460        self.check_invariants();
 461    }
 462
 463    pub fn chunks_at(&self, wrap_row: u32) -> Chunks {
 464        let point = WrapPoint::new(wrap_row, 0);
 465        let mut transforms = self.transforms.cursor::<(WrapPoint, TabPoint)>();
 466        transforms.seek(&point, Bias::Right, &());
 467        let mut input_position = TabPoint(transforms.start().1 .0);
 468        if transforms.item().map_or(false, |t| t.is_isomorphic()) {
 469            input_position.0 += point.0 - transforms.start().0 .0;
 470        }
 471        let input_chunks = self.tab_snapshot.chunks_at(input_position);
 472        Chunks {
 473            input_chunks,
 474            transforms,
 475            output_position: point,
 476            input_chunk: "",
 477        }
 478    }
 479
 480    pub fn highlighted_chunks_for_rows(&mut self, rows: Range<u32>) -> HighlightedChunks {
 481        let output_start = WrapPoint::new(rows.start, 0);
 482        let output_end = WrapPoint::new(rows.end, 0);
 483        let mut transforms = self.transforms.cursor::<(WrapPoint, TabPoint)>();
 484        transforms.seek(&output_start, Bias::Right, &());
 485        let mut input_start = TabPoint(transforms.start().1 .0);
 486        if transforms.item().map_or(false, |t| t.is_isomorphic()) {
 487            input_start.0 += output_start.0 - transforms.start().0 .0;
 488        }
 489        let input_end = self
 490            .to_tab_point(output_end)
 491            .min(self.tab_snapshot.max_point());
 492        HighlightedChunks {
 493            input_chunks: self.tab_snapshot.highlighted_chunks(input_start..input_end),
 494            input_chunk: Default::default(),
 495            output_position: output_start,
 496            max_output_row: rows.end,
 497            transforms,
 498        }
 499    }
 500
 501    pub fn max_point(&self) -> WrapPoint {
 502        self.to_wrap_point(self.tab_snapshot.max_point())
 503    }
 504
 505    pub fn line_len(&self, row: u32) -> u32 {
 506        let mut len = 0;
 507        for chunk in self.chunks_at(row) {
 508            if let Some(newline_ix) = chunk.find('\n') {
 509                len += newline_ix;
 510                break;
 511            } else {
 512                len += chunk.len();
 513            }
 514        }
 515        len as u32
 516    }
 517
 518    pub fn soft_wrap_indent(&self, row: u32) -> Option<u32> {
 519        let mut cursor = self.transforms.cursor::<WrapPoint>();
 520        cursor.seek(&WrapPoint::new(row + 1, 0), Bias::Right, &());
 521        cursor.item().and_then(|transform| {
 522            if transform.is_isomorphic() {
 523                None
 524            } else {
 525                Some(transform.summary.output.lines.column)
 526            }
 527        })
 528    }
 529
 530    pub fn longest_row(&self) -> u32 {
 531        self.transforms.summary().output.longest_row
 532    }
 533
 534    pub fn buffer_rows(&self, start_row: u32) -> BufferRows {
 535        let mut transforms = self.transforms.cursor::<(WrapPoint, TabPoint)>();
 536        transforms.seek(&WrapPoint::new(start_row, 0), Bias::Left, &());
 537        let mut input_row = transforms.start().1.row();
 538        if transforms.item().map_or(false, |t| t.is_isomorphic()) {
 539            input_row += start_row - transforms.start().0.row();
 540        }
 541        let soft_wrapped = transforms.item().map_or(false, |t| !t.is_isomorphic());
 542        let mut input_buffer_rows = self.tab_snapshot.buffer_rows(input_row);
 543        let input_buffer_row = input_buffer_rows.next().unwrap();
 544        BufferRows {
 545            transforms,
 546            input_buffer_row,
 547            input_buffer_rows,
 548            output_row: start_row,
 549            soft_wrapped,
 550            max_output_row: self.max_point().row(),
 551        }
 552    }
 553
 554    pub fn to_tab_point(&self, point: WrapPoint) -> TabPoint {
 555        let mut cursor = self.transforms.cursor::<(WrapPoint, TabPoint)>();
 556        cursor.seek(&point, Bias::Right, &());
 557        let mut tab_point = cursor.start().1 .0;
 558        if cursor.item().map_or(false, |t| t.is_isomorphic()) {
 559            tab_point += point.0 - cursor.start().0 .0;
 560        }
 561        TabPoint(tab_point)
 562    }
 563
 564    pub fn to_wrap_point(&self, point: TabPoint) -> WrapPoint {
 565        let mut cursor = self.transforms.cursor::<(TabPoint, WrapPoint)>();
 566        cursor.seek(&point, Bias::Right, &());
 567        WrapPoint(cursor.start().1 .0 + (point.0 - cursor.start().0 .0))
 568    }
 569
 570    pub fn clip_point(&self, mut point: WrapPoint, bias: Bias) -> WrapPoint {
 571        if bias == Bias::Left {
 572            let mut cursor = self.transforms.cursor::<WrapPoint>();
 573            cursor.seek(&point, Bias::Right, &());
 574            if cursor.item().map_or(false, |t| !t.is_isomorphic()) {
 575                point = *cursor.start();
 576                *point.column_mut() -= 1;
 577            }
 578        }
 579
 580        self.to_wrap_point(self.tab_snapshot.clip_point(self.to_tab_point(point), bias))
 581    }
 582
 583    fn check_invariants(&self) {
 584        #[cfg(test)]
 585        {
 586            assert_eq!(
 587                TabPoint::from(self.transforms.summary().input.lines),
 588                self.tab_snapshot.max_point()
 589            );
 590
 591            {
 592                let mut transforms = self.transforms.cursor::<()>().peekable();
 593                while let Some(transform) = transforms.next() {
 594                    if let Some(next_transform) = transforms.peek() {
 595                        assert!(transform.is_isomorphic() != next_transform.is_isomorphic());
 596                    }
 597                }
 598            }
 599
 600            let mut expected_buffer_rows = Vec::new();
 601            let mut buffer_row = 0;
 602            let mut prev_tab_row = 0;
 603            for display_row in 0..=self.max_point().row() {
 604                let tab_point = self.to_tab_point(WrapPoint::new(display_row, 0));
 605                let soft_wrapped;
 606                if tab_point.row() == prev_tab_row {
 607                    soft_wrapped = display_row != 0;
 608                } else {
 609                    let fold_point = self.tab_snapshot.to_fold_point(tab_point, Bias::Left).0;
 610                    let buffer_point = fold_point.to_buffer_point(&self.tab_snapshot.fold_snapshot);
 611                    buffer_row = buffer_point.row;
 612                    prev_tab_row = tab_point.row();
 613                    soft_wrapped = false;
 614                }
 615                expected_buffer_rows.push((buffer_row, soft_wrapped));
 616            }
 617
 618            for start_display_row in 0..expected_buffer_rows.len() {
 619                assert_eq!(
 620                    self.buffer_rows(start_display_row as u32)
 621                        .collect::<Vec<_>>(),
 622                    &expected_buffer_rows[start_display_row..],
 623                    "invalid buffer_rows({}..)",
 624                    start_display_row
 625                );
 626            }
 627        }
 628    }
 629}
 630
 631impl<'a> Iterator for Chunks<'a> {
 632    type Item = &'a str;
 633
 634    fn next(&mut self) -> Option<Self::Item> {
 635        let transform = self.transforms.item()?;
 636        if let Some(display_text) = transform.display_text {
 637            if self.output_position > self.transforms.start().0 {
 638                self.output_position.0.column += transform.summary.output.lines.column;
 639                self.transforms.next(&());
 640                return Some(&display_text[1..]);
 641            } else {
 642                self.output_position.0 += transform.summary.output.lines;
 643                self.transforms.next(&());
 644                return Some(display_text);
 645            }
 646        }
 647
 648        if self.input_chunk.is_empty() {
 649            self.input_chunk = self.input_chunks.next().unwrap();
 650        }
 651
 652        let mut input_len = 0;
 653        let transform_end = self.transforms.end(&()).0;
 654        for c in self.input_chunk.chars() {
 655            let char_len = c.len_utf8();
 656            input_len += char_len;
 657            if c == '\n' {
 658                *self.output_position.row_mut() += 1;
 659                *self.output_position.column_mut() = 0;
 660            } else {
 661                *self.output_position.column_mut() += char_len as u32;
 662            }
 663
 664            if self.output_position >= transform_end {
 665                self.transforms.next(&());
 666                break;
 667            }
 668        }
 669
 670        let (prefix, suffix) = self.input_chunk.split_at(input_len);
 671        self.input_chunk = suffix;
 672        Some(prefix)
 673    }
 674}
 675
 676impl<'a> Iterator for HighlightedChunks<'a> {
 677    type Item = HighlightedChunk<'a>;
 678
 679    fn next(&mut self) -> Option<Self::Item> {
 680        if self.output_position.row() >= self.max_output_row {
 681            return None;
 682        }
 683
 684        let transform = self.transforms.item()?;
 685        if let Some(display_text) = transform.display_text {
 686            let mut start_ix = 0;
 687            let mut end_ix = display_text.len();
 688            let mut summary = transform.summary.output.lines;
 689
 690            if self.output_position > self.transforms.start().0 {
 691                // Exclude newline starting prior to the desired row.
 692                start_ix = 1;
 693                summary.row = 0;
 694            } else if self.output_position.row() + 1 >= self.max_output_row {
 695                // Exclude soft indentation ending after the desired row.
 696                end_ix = 1;
 697                summary.column = 0;
 698            }
 699
 700            self.output_position.0 += summary;
 701            self.transforms.next(&());
 702            return Some(HighlightedChunk {
 703                text: &display_text[start_ix..end_ix],
 704                ..self.input_chunk
 705            });
 706        }
 707
 708        if self.input_chunk.text.is_empty() {
 709            self.input_chunk = self.input_chunks.next().unwrap();
 710        }
 711
 712        let mut input_len = 0;
 713        let transform_end = self.transforms.end(&()).0;
 714        for c in self.input_chunk.text.chars() {
 715            let char_len = c.len_utf8();
 716            input_len += char_len;
 717            if c == '\n' {
 718                *self.output_position.row_mut() += 1;
 719                *self.output_position.column_mut() = 0;
 720            } else {
 721                *self.output_position.column_mut() += char_len as u32;
 722            }
 723
 724            if self.output_position >= transform_end {
 725                self.transforms.next(&());
 726                break;
 727            }
 728        }
 729
 730        let (prefix, suffix) = self.input_chunk.text.split_at(input_len);
 731        self.input_chunk.text = suffix;
 732        Some(HighlightedChunk {
 733            text: prefix,
 734            ..self.input_chunk
 735        })
 736    }
 737}
 738
 739impl<'a> Iterator for BufferRows<'a> {
 740    type Item = (u32, bool);
 741
 742    fn next(&mut self) -> Option<Self::Item> {
 743        if self.output_row > self.max_output_row {
 744            return None;
 745        }
 746
 747        let buffer_row = self.input_buffer_row;
 748        let soft_wrapped = self.soft_wrapped;
 749
 750        self.output_row += 1;
 751        self.transforms
 752            .seek_forward(&WrapPoint::new(self.output_row, 0), Bias::Left, &());
 753        if self.transforms.item().map_or(false, |t| t.is_isomorphic()) {
 754            self.input_buffer_row = self.input_buffer_rows.next().unwrap();
 755            self.soft_wrapped = false;
 756        } else {
 757            self.soft_wrapped = true;
 758        }
 759
 760        Some((buffer_row, soft_wrapped))
 761    }
 762}
 763
 764impl Transform {
 765    fn isomorphic(summary: TextSummary) -> Self {
 766        #[cfg(test)]
 767        assert!(!summary.lines.is_zero());
 768
 769        Self {
 770            summary: TransformSummary {
 771                input: summary.clone(),
 772                output: summary,
 773            },
 774            display_text: None,
 775        }
 776    }
 777
 778    fn wrap(indent: u32) -> Self {
 779        lazy_static! {
 780            static ref WRAP_TEXT: String = {
 781                let mut wrap_text = String::new();
 782                wrap_text.push('\n');
 783                wrap_text.extend((0..LineWrapper::MAX_INDENT as usize).map(|_| ' '));
 784                wrap_text
 785            };
 786        }
 787
 788        Self {
 789            summary: TransformSummary {
 790                input: TextSummary::default(),
 791                output: TextSummary {
 792                    lines: Point::new(1, indent),
 793                    first_line_chars: 0,
 794                    last_line_chars: indent,
 795                    longest_row: 1,
 796                    longest_row_chars: indent,
 797                },
 798            },
 799            display_text: Some(&WRAP_TEXT[..1 + indent as usize]),
 800        }
 801    }
 802
 803    fn is_isomorphic(&self) -> bool {
 804        self.display_text.is_none()
 805    }
 806}
 807
 808impl sum_tree::Item for Transform {
 809    type Summary = TransformSummary;
 810
 811    fn summary(&self) -> Self::Summary {
 812        self.summary.clone()
 813    }
 814}
 815
 816fn push_isomorphic(transforms: &mut Vec<Transform>, summary: TextSummary) {
 817    if let Some(last_transform) = transforms.last_mut() {
 818        if last_transform.is_isomorphic() {
 819            last_transform.summary.input += &summary;
 820            last_transform.summary.output += &summary;
 821            return;
 822        }
 823    }
 824    transforms.push(Transform::isomorphic(summary));
 825}
 826
 827trait SumTreeExt {
 828    fn push_or_extend(&mut self, transform: Transform);
 829}
 830
 831impl SumTreeExt for SumTree<Transform> {
 832    fn push_or_extend(&mut self, transform: Transform) {
 833        let mut transform = Some(transform);
 834        self.update_last(
 835            |last_transform| {
 836                if last_transform.is_isomorphic() && transform.as_ref().unwrap().is_isomorphic() {
 837                    let transform = transform.take().unwrap();
 838                    last_transform.summary.input += &transform.summary.input;
 839                    last_transform.summary.output += &transform.summary.output;
 840                }
 841            },
 842            &(),
 843        );
 844
 845        if let Some(transform) = transform {
 846            self.push(transform, &());
 847        }
 848    }
 849}
 850
 851impl WrapPoint {
 852    pub fn new(row: u32, column: u32) -> Self {
 853        Self(super::Point::new(row, column))
 854    }
 855
 856    #[cfg(test)]
 857    pub fn is_zero(&self) -> bool {
 858        self.0.is_zero()
 859    }
 860
 861    pub fn row(self) -> u32 {
 862        self.0.row
 863    }
 864
 865    pub fn column(self) -> u32 {
 866        self.0.column
 867    }
 868
 869    pub fn row_mut(&mut self) -> &mut u32 {
 870        &mut self.0.row
 871    }
 872
 873    pub fn column_mut(&mut self) -> &mut u32 {
 874        &mut self.0.column
 875    }
 876}
 877
 878impl sum_tree::Summary for TransformSummary {
 879    type Context = ();
 880
 881    fn add_summary(&mut self, other: &Self, _: &()) {
 882        self.input += &other.input;
 883        self.output += &other.output;
 884    }
 885}
 886
 887impl<'a> sum_tree::Dimension<'a, TransformSummary> for TabPoint {
 888    fn add_summary(&mut self, summary: &'a TransformSummary, _: &()) {
 889        self.0 += summary.input.lines;
 890    }
 891}
 892
 893impl<'a> sum_tree::Dimension<'a, TransformSummary> for WrapPoint {
 894    fn add_summary(&mut self, summary: &'a TransformSummary, _: &()) {
 895        self.0 += summary.output.lines;
 896    }
 897}
 898
 899#[cfg(test)]
 900mod tests {
 901    use super::*;
 902    use crate::{
 903        display_map::{fold_map::FoldMap, tab_map::TabMap},
 904        test::Observer,
 905    };
 906    use language::{Buffer, RandomCharIter};
 907    use rand::prelude::*;
 908    use std::env;
 909
 910    #[gpui::test(iterations = 100)]
 911    async fn test_random_wraps(mut cx: gpui::TestAppContext, mut rng: StdRng) {
 912        cx.foreground().set_block_on_ticks(0..=50);
 913        cx.foreground().forbid_parking();
 914        let operations = env::var("OPERATIONS")
 915            .map(|i| i.parse().expect("invalid `OPERATIONS` variable"))
 916            .unwrap_or(10);
 917
 918        let font_cache = cx.font_cache().clone();
 919        let font_system = cx.platform().fonts();
 920        let mut wrap_width = if rng.gen_bool(0.1) {
 921            None
 922        } else {
 923            Some(rng.gen_range(0.0..=1000.0))
 924        };
 925        let tab_size = rng.gen_range(1..=4);
 926        let family_id = font_cache.load_family(&["Helvetica"]).unwrap();
 927        let font_id = font_cache
 928            .select_font(family_id, &Default::default())
 929            .unwrap();
 930        let font_size = 14.0;
 931
 932        log::info!("Tab size: {}", tab_size);
 933        log::info!("Wrap width: {:?}", wrap_width);
 934
 935        let buffer = cx.add_model(|cx| {
 936            let len = rng.gen_range(0..10);
 937            let text = RandomCharIter::new(&mut rng).take(len).collect::<String>();
 938            Buffer::new(0, text, cx)
 939        });
 940        let (mut fold_map, folds_snapshot) = cx.read(|cx| FoldMap::new(buffer.clone(), cx));
 941        let (tab_map, tabs_snapshot) = TabMap::new(folds_snapshot.clone(), tab_size);
 942        log::info!(
 943            "Unwrapped text (no folds): {:?}",
 944            buffer.read_with(&cx, |buf, _| buf.text())
 945        );
 946        log::info!(
 947            "Unwrapped text (unexpanded tabs): {:?}",
 948            folds_snapshot.text()
 949        );
 950        log::info!("Unwrapped text (expanded tabs): {:?}", tabs_snapshot.text());
 951
 952        let mut line_wrapper = LineWrapper::new(font_id, font_size, font_system);
 953        let unwrapped_text = tabs_snapshot.text();
 954        let expected_text = wrap_text(&unwrapped_text, wrap_width, &mut line_wrapper);
 955
 956        let wrap_map = cx.add_model(|cx| {
 957            WrapMap::new(tabs_snapshot.clone(), font_id, font_size, wrap_width, cx)
 958        });
 959        let (_observer, notifications) = Observer::new(&wrap_map, &mut cx);
 960
 961        if wrap_map.read_with(&cx, |map, _| map.is_rewrapping()) {
 962            notifications.recv().await.unwrap();
 963        }
 964
 965        let snapshot = wrap_map.update(&mut cx, |map, cx| map.sync(tabs_snapshot, Vec::new(), cx));
 966        let actual_text = snapshot.text();
 967        assert_eq!(
 968            actual_text, expected_text,
 969            "unwrapped text is: {:?}",
 970            unwrapped_text
 971        );
 972        log::info!("Wrapped text: {:?}", actual_text);
 973
 974        for _i in 0..operations {
 975            match rng.gen_range(0..=100) {
 976                0..=19 => {
 977                    wrap_width = if rng.gen_bool(0.2) {
 978                        None
 979                    } else {
 980                        Some(rng.gen_range(0.0..=1000.0))
 981                    };
 982                    log::info!("Setting wrap width to {:?}", wrap_width);
 983                    wrap_map.update(&mut cx, |map, cx| map.set_wrap_width(wrap_width, cx));
 984                }
 985                20..=39 => {
 986                    for (folds_snapshot, edits) in
 987                        cx.read(|cx| fold_map.randomly_mutate(&mut rng, cx))
 988                    {
 989                        let (tabs_snapshot, edits) = tab_map.sync(folds_snapshot, edits);
 990                        let mut snapshot =
 991                            wrap_map.update(&mut cx, |map, cx| map.sync(tabs_snapshot, edits, cx));
 992                        snapshot.check_invariants();
 993                        snapshot.verify_chunks(&mut rng);
 994                    }
 995                }
 996                _ => {
 997                    buffer.update(&mut cx, |buffer, _| buffer.randomly_mutate(&mut rng));
 998                }
 999            }
1000
1001            log::info!(
1002                "Unwrapped text (no folds): {:?}",
1003                buffer.read_with(&cx, |buf, _| buf.text())
1004            );
1005            let (folds_snapshot, edits) = cx.read(|cx| fold_map.read(cx));
1006            log::info!(
1007                "Unwrapped text (unexpanded tabs): {:?}",
1008                folds_snapshot.text()
1009            );
1010            let (tabs_snapshot, edits) = tab_map.sync(folds_snapshot, edits);
1011            log::info!("Unwrapped text (expanded tabs): {:?}", tabs_snapshot.text());
1012
1013            let unwrapped_text = tabs_snapshot.text();
1014            let expected_text = wrap_text(&unwrapped_text, wrap_width, &mut line_wrapper);
1015            let mut snapshot = wrap_map.update(&mut cx, |map, cx| {
1016                map.sync(tabs_snapshot.clone(), edits, cx)
1017            });
1018            snapshot.check_invariants();
1019            snapshot.verify_chunks(&mut rng);
1020
1021            if wrap_map.read_with(&cx, |map, _| map.is_rewrapping()) && rng.gen_bool(0.4) {
1022                log::info!("Waiting for wrapping to finish");
1023                while wrap_map.read_with(&cx, |map, _| map.is_rewrapping()) {
1024                    notifications.recv().await.unwrap();
1025                }
1026            }
1027
1028            if !wrap_map.read_with(&cx, |map, _| map.is_rewrapping()) {
1029                let mut wrapped_snapshot =
1030                    wrap_map.update(&mut cx, |map, cx| map.sync(tabs_snapshot, Vec::new(), cx));
1031                let actual_text = wrapped_snapshot.text();
1032                log::info!("Wrapping finished: {:?}", actual_text);
1033                wrapped_snapshot.check_invariants();
1034                wrapped_snapshot.verify_chunks(&mut rng);
1035                assert_eq!(
1036                    actual_text, expected_text,
1037                    "unwrapped text is: {:?}",
1038                    unwrapped_text
1039                );
1040            }
1041        }
1042    }
1043
1044    fn wrap_text(
1045        unwrapped_text: &str,
1046        wrap_width: Option<f32>,
1047        line_wrapper: &mut LineWrapper,
1048    ) -> String {
1049        if let Some(wrap_width) = wrap_width {
1050            let mut wrapped_text = String::new();
1051            for (row, line) in unwrapped_text.split('\n').enumerate() {
1052                if row > 0 {
1053                    wrapped_text.push('\n')
1054                }
1055
1056                let mut prev_ix = 0;
1057                for boundary in line_wrapper.wrap_line(line, wrap_width) {
1058                    wrapped_text.push_str(&line[prev_ix..boundary.ix]);
1059                    wrapped_text.push('\n');
1060                    wrapped_text.push_str(&" ".repeat(boundary.next_indent as usize));
1061                    prev_ix = boundary.ix;
1062                }
1063                wrapped_text.push_str(&line[prev_ix..]);
1064            }
1065            wrapped_text
1066        } else {
1067            unwrapped_text.to_string()
1068        }
1069    }
1070
1071    impl Snapshot {
1072        fn text(&self) -> String {
1073            self.chunks_at(0).collect()
1074        }
1075
1076        fn verify_chunks(&mut self, rng: &mut impl Rng) {
1077            for _ in 0..5 {
1078                let mut end_row = rng.gen_range(0..=self.max_point().row());
1079                let start_row = rng.gen_range(0..=end_row);
1080                end_row += 1;
1081
1082                let mut expected_text = self.chunks_at(start_row).collect::<String>();
1083                if expected_text.ends_with("\n") {
1084                    expected_text.push('\n');
1085                }
1086                let mut expected_text = expected_text
1087                    .lines()
1088                    .take((end_row - start_row) as usize)
1089                    .collect::<Vec<_>>()
1090                    .join("\n");
1091                if end_row <= self.max_point().row() {
1092                    expected_text.push('\n');
1093                }
1094
1095                let actual_text = self
1096                    .highlighted_chunks_for_rows(start_row..end_row)
1097                    .map(|c| c.text)
1098                    .collect::<String>();
1099                assert_eq!(
1100                    expected_text,
1101                    actual_text,
1102                    "chunks != highlighted_chunks for rows {:?}",
1103                    start_row..end_row
1104                );
1105            }
1106        }
1107    }
1108}