wrap_map.rs

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