wrap_map.rs

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