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_point(&self, point: WrapPoint, bias: Bias) -> Point {
 661        self.tab_snapshot.to_point(self.to_tab_point(point), bias)
 662    }
 663
 664    pub fn from_point(&self, point: Point, bias: Bias) -> WrapPoint {
 665        self.from_tab_point(self.tab_snapshot.from_point(point, bias))
 666    }
 667
 668    pub fn from_tab_point(&self, point: TabPoint) -> WrapPoint {
 669        let mut cursor = self.transforms.cursor::<(TabPoint, WrapPoint)>();
 670        cursor.seek(&point, Bias::Right, &());
 671        WrapPoint(cursor.start().1 .0 + (point.0 - cursor.start().0 .0))
 672    }
 673
 674    pub fn clip_point(&self, mut point: WrapPoint, bias: Bias) -> WrapPoint {
 675        if bias == Bias::Left {
 676            let mut cursor = self.transforms.cursor::<WrapPoint>();
 677            cursor.seek(&point, Bias::Right, &());
 678            if cursor.item().map_or(false, |t| !t.is_isomorphic()) {
 679                point = *cursor.start();
 680                *point.column_mut() -= 1;
 681            }
 682        }
 683
 684        self.from_tab_point(self.tab_snapshot.clip_point(self.to_tab_point(point), bias))
 685    }
 686
 687    fn check_invariants(&self) {
 688        #[cfg(test)]
 689        {
 690            assert_eq!(
 691                TabPoint::from(self.transforms.summary().input.lines),
 692                self.tab_snapshot.max_point()
 693            );
 694
 695            {
 696                let mut transforms = self.transforms.cursor::<()>().peekable();
 697                while let Some(transform) = transforms.next() {
 698                    if let Some(next_transform) = transforms.peek() {
 699                        assert!(transform.is_isomorphic() != next_transform.is_isomorphic());
 700                    }
 701                }
 702            }
 703
 704            let mut expected_buffer_rows = Vec::new();
 705            let mut buffer_row = 0;
 706            let mut prev_tab_row = 0;
 707            for display_row in 0..=self.max_point().row() {
 708                let tab_point = self.to_tab_point(WrapPoint::new(display_row, 0));
 709                let soft_wrapped;
 710                if tab_point.row() == prev_tab_row {
 711                    soft_wrapped = display_row != 0;
 712                } else {
 713                    let fold_point = self.tab_snapshot.to_fold_point(tab_point, Bias::Left).0;
 714                    let buffer_point = fold_point.to_buffer_point(&self.tab_snapshot.fold_snapshot);
 715                    buffer_row = buffer_point.row;
 716                    prev_tab_row = tab_point.row();
 717                    soft_wrapped = false;
 718                }
 719                expected_buffer_rows.push((buffer_row, soft_wrapped));
 720            }
 721
 722            for start_display_row in 0..expected_buffer_rows.len() {
 723                assert_eq!(
 724                    self.buffer_rows(start_display_row as u32)
 725                        .collect::<Vec<_>>(),
 726                    &expected_buffer_rows[start_display_row..],
 727                    "invalid buffer_rows({}..)",
 728                    start_display_row
 729                );
 730            }
 731        }
 732    }
 733}
 734
 735impl<'a> Iterator for Chunks<'a> {
 736    type Item = &'a str;
 737
 738    fn next(&mut self) -> Option<Self::Item> {
 739        let transform = self.transforms.item()?;
 740        if let Some(display_text) = transform.display_text {
 741            if self.output_position > self.transforms.start().0 {
 742                self.output_position.0.column += transform.summary.output.lines.column;
 743                self.transforms.next(&());
 744                return Some(&display_text[1..]);
 745            } else {
 746                self.output_position.0 += transform.summary.output.lines;
 747                self.transforms.next(&());
 748                return Some(display_text);
 749            }
 750        }
 751
 752        if self.input_chunk.is_empty() {
 753            self.input_chunk = self.input_chunks.next().unwrap();
 754        }
 755
 756        let mut input_len = 0;
 757        let transform_end = self.transforms.end(&()).0;
 758        for c in self.input_chunk.chars() {
 759            let char_len = c.len_utf8();
 760            input_len += char_len;
 761            if c == '\n' {
 762                *self.output_position.row_mut() += 1;
 763                *self.output_position.column_mut() = 0;
 764            } else {
 765                *self.output_position.column_mut() += char_len as u32;
 766            }
 767
 768            if self.output_position >= transform_end {
 769                self.transforms.next(&());
 770                break;
 771            }
 772        }
 773
 774        let (prefix, suffix) = self.input_chunk.split_at(input_len);
 775        self.input_chunk = suffix;
 776        Some(prefix)
 777    }
 778}
 779
 780impl<'a> Iterator for HighlightedChunks<'a> {
 781    type Item = HighlightedChunk<'a>;
 782
 783    fn next(&mut self) -> Option<Self::Item> {
 784        if self.output_position.row() >= self.max_output_row {
 785            return None;
 786        }
 787
 788        let transform = self.transforms.item()?;
 789        if let Some(display_text) = transform.display_text {
 790            let mut start_ix = 0;
 791            let mut end_ix = display_text.len();
 792            let mut summary = transform.summary.output.lines;
 793
 794            if self.output_position > self.transforms.start().0 {
 795                // Exclude newline starting prior to the desired row.
 796                start_ix = 1;
 797                summary.row = 0;
 798            } else if self.output_position.row() + 1 >= self.max_output_row {
 799                // Exclude soft indentation ending after the desired row.
 800                end_ix = 1;
 801                summary.column = 0;
 802            }
 803
 804            self.output_position.0 += summary;
 805            self.transforms.next(&());
 806            return Some(HighlightedChunk {
 807                text: &display_text[start_ix..end_ix],
 808                ..self.input_chunk
 809            });
 810        }
 811
 812        if self.input_chunk.text.is_empty() {
 813            self.input_chunk = self.input_chunks.next().unwrap();
 814        }
 815
 816        let mut input_len = 0;
 817        let transform_end = self.transforms.end(&()).0;
 818        for c in self.input_chunk.text.chars() {
 819            let char_len = c.len_utf8();
 820            input_len += char_len;
 821            if c == '\n' {
 822                *self.output_position.row_mut() += 1;
 823                *self.output_position.column_mut() = 0;
 824            } else {
 825                *self.output_position.column_mut() += char_len as u32;
 826            }
 827
 828            if self.output_position >= transform_end {
 829                self.transforms.next(&());
 830                break;
 831            }
 832        }
 833
 834        let (prefix, suffix) = self.input_chunk.text.split_at(input_len);
 835        self.input_chunk.text = suffix;
 836        Some(HighlightedChunk {
 837            text: prefix,
 838            ..self.input_chunk
 839        })
 840    }
 841}
 842
 843impl<'a> Iterator for BufferRows<'a> {
 844    type Item = (u32, bool);
 845
 846    fn next(&mut self) -> Option<Self::Item> {
 847        if self.output_row > self.max_output_row {
 848            return None;
 849        }
 850
 851        let buffer_row = self.input_buffer_row;
 852        let soft_wrapped = self.soft_wrapped;
 853
 854        self.output_row += 1;
 855        self.transforms
 856            .seek_forward(&WrapPoint::new(self.output_row, 0), Bias::Left, &());
 857        if self.transforms.item().map_or(false, |t| t.is_isomorphic()) {
 858            self.input_buffer_row = self.input_buffer_rows.next().unwrap();
 859            self.soft_wrapped = false;
 860        } else {
 861            self.soft_wrapped = true;
 862        }
 863
 864        Some((buffer_row, soft_wrapped))
 865    }
 866}
 867
 868impl Transform {
 869    fn isomorphic(summary: TextSummary) -> Self {
 870        #[cfg(test)]
 871        assert!(!summary.lines.is_zero());
 872
 873        Self {
 874            summary: TransformSummary {
 875                input: summary.clone(),
 876                output: summary,
 877            },
 878            display_text: None,
 879        }
 880    }
 881
 882    fn wrap(indent: u32) -> Self {
 883        lazy_static! {
 884            static ref WRAP_TEXT: String = {
 885                let mut wrap_text = String::new();
 886                wrap_text.push('\n');
 887                wrap_text.extend((0..LineWrapper::MAX_INDENT as usize).map(|_| ' '));
 888                wrap_text
 889            };
 890        }
 891
 892        Self {
 893            summary: TransformSummary {
 894                input: TextSummary::default(),
 895                output: TextSummary {
 896                    lines: Point::new(1, indent),
 897                    first_line_chars: 0,
 898                    last_line_chars: indent,
 899                    longest_row: 1,
 900                    longest_row_chars: indent,
 901                },
 902            },
 903            display_text: Some(&WRAP_TEXT[..1 + indent as usize]),
 904        }
 905    }
 906
 907    fn is_isomorphic(&self) -> bool {
 908        self.display_text.is_none()
 909    }
 910}
 911
 912impl sum_tree::Item for Transform {
 913    type Summary = TransformSummary;
 914
 915    fn summary(&self) -> Self::Summary {
 916        self.summary.clone()
 917    }
 918}
 919
 920fn push_isomorphic(transforms: &mut Vec<Transform>, summary: TextSummary) {
 921    if let Some(last_transform) = transforms.last_mut() {
 922        if last_transform.is_isomorphic() {
 923            last_transform.summary.input += &summary;
 924            last_transform.summary.output += &summary;
 925            return;
 926        }
 927    }
 928    transforms.push(Transform::isomorphic(summary));
 929}
 930
 931trait SumTreeExt {
 932    fn push_or_extend(&mut self, transform: Transform);
 933}
 934
 935impl SumTreeExt for SumTree<Transform> {
 936    fn push_or_extend(&mut self, transform: Transform) {
 937        let mut transform = Some(transform);
 938        self.update_last(
 939            |last_transform| {
 940                if last_transform.is_isomorphic() && transform.as_ref().unwrap().is_isomorphic() {
 941                    let transform = transform.take().unwrap();
 942                    last_transform.summary.input += &transform.summary.input;
 943                    last_transform.summary.output += &transform.summary.output;
 944                }
 945            },
 946            &(),
 947        );
 948
 949        if let Some(transform) = transform {
 950            self.push(transform, &());
 951        }
 952    }
 953}
 954
 955impl WrapPoint {
 956    pub fn new(row: u32, column: u32) -> Self {
 957        Self(super::Point::new(row, column))
 958    }
 959
 960    pub fn row(self) -> u32 {
 961        self.0.row
 962    }
 963
 964    pub fn column(self) -> u32 {
 965        self.0.column
 966    }
 967
 968    pub fn row_mut(&mut self) -> &mut u32 {
 969        &mut self.0.row
 970    }
 971
 972    pub fn column_mut(&mut self) -> &mut u32 {
 973        &mut self.0.column
 974    }
 975}
 976
 977impl sum_tree::Summary for TransformSummary {
 978    type Context = ();
 979
 980    fn add_summary(&mut self, other: &Self, _: &()) {
 981        self.input += &other.input;
 982        self.output += &other.output;
 983    }
 984}
 985
 986impl<'a> sum_tree::Dimension<'a, TransformSummary> for TabPoint {
 987    fn add_summary(&mut self, summary: &'a TransformSummary, _: &()) {
 988        self.0 += summary.input.lines;
 989    }
 990}
 991
 992impl<'a> sum_tree::SeekTarget<'a, TransformSummary, TransformSummary> for TabPoint {
 993    fn cmp(&self, cursor_location: &TransformSummary, _: &()) -> std::cmp::Ordering {
 994        Ord::cmp(&self.0, &cursor_location.input.lines)
 995    }
 996}
 997
 998impl<'a> sum_tree::Dimension<'a, TransformSummary> for WrapPoint {
 999    fn add_summary(&mut self, summary: &'a TransformSummary, _: &()) {
1000        self.0 += summary.output.lines;
1001    }
1002}
1003
1004fn consolidate_wrap_edits(edits: &mut Vec<Edit>) {
1005    let mut i = 1;
1006    while i < edits.len() {
1007        let edit = edits[i].clone();
1008        let prev_edit = &mut edits[i - 1];
1009        if prev_edit.old.end >= edit.old.start {
1010            prev_edit.old.end = edit.old.end;
1011            prev_edit.new.end = edit.new.end;
1012            edits.remove(i);
1013            continue;
1014        }
1015        i += 1;
1016    }
1017}
1018
1019#[cfg(test)]
1020mod tests {
1021    use super::*;
1022    use crate::{
1023        display_map::{fold_map::FoldMap, tab_map::TabMap},
1024        test::Observer,
1025    };
1026    use buffer::Rope;
1027    use language::{Buffer, RandomCharIter};
1028    use rand::prelude::*;
1029    use std::{cmp, env};
1030
1031    #[gpui::test(iterations = 100)]
1032    async fn test_random_wraps(mut cx: gpui::TestAppContext, mut rng: StdRng) {
1033        cx.foreground().set_block_on_ticks(0..=50);
1034        cx.foreground().forbid_parking();
1035        let operations = env::var("OPERATIONS")
1036            .map(|i| i.parse().expect("invalid `OPERATIONS` variable"))
1037            .unwrap_or(10);
1038
1039        let font_cache = cx.font_cache().clone();
1040        let font_system = cx.platform().fonts();
1041        let mut wrap_width = if rng.gen_bool(0.1) {
1042            None
1043        } else {
1044            Some(rng.gen_range(0.0..=1000.0))
1045        };
1046        let tab_size = rng.gen_range(1..=4);
1047        let family_id = font_cache.load_family(&["Helvetica"]).unwrap();
1048        let font_id = font_cache
1049            .select_font(family_id, &Default::default())
1050            .unwrap();
1051        let font_size = 14.0;
1052
1053        log::info!("Tab size: {}", tab_size);
1054        log::info!("Wrap width: {:?}", wrap_width);
1055
1056        let buffer = cx.add_model(|cx| {
1057            let len = rng.gen_range(0..10);
1058            let text = RandomCharIter::new(&mut rng).take(len).collect::<String>();
1059            Buffer::new(0, text, cx)
1060        });
1061        let (mut fold_map, folds_snapshot) = cx.read(|cx| FoldMap::new(buffer.clone(), cx));
1062        let (tab_map, tabs_snapshot) = TabMap::new(folds_snapshot.clone(), tab_size);
1063        log::info!(
1064            "Unwrapped text (no folds): {:?}",
1065            buffer.read_with(&cx, |buf, _| buf.text())
1066        );
1067        log::info!(
1068            "Unwrapped text (unexpanded tabs): {:?}",
1069            folds_snapshot.text()
1070        );
1071        log::info!("Unwrapped text (expanded tabs): {:?}", tabs_snapshot.text());
1072
1073        let mut line_wrapper = LineWrapper::new(font_id, font_size, font_system);
1074        let unwrapped_text = tabs_snapshot.text();
1075        let expected_text = wrap_text(&unwrapped_text, wrap_width, &mut line_wrapper);
1076
1077        let (wrap_map, initial_snapshot) =
1078            cx.update(|cx| WrapMap::new(tabs_snapshot.clone(), font_id, font_size, wrap_width, cx));
1079        let (_observer, notifications) = Observer::new(&wrap_map, &mut cx);
1080
1081        if wrap_map.read_with(&cx, |map, _| map.is_rewrapping()) {
1082            notifications.recv().await.unwrap();
1083        }
1084
1085        let actual_text = initial_snapshot.text();
1086        assert_eq!(
1087            actual_text, expected_text,
1088            "unwrapped text is: {:?}",
1089            unwrapped_text
1090        );
1091        log::info!("Wrapped text: {:?}", actual_text);
1092
1093        let mut edits = Vec::new();
1094        for _i in 0..operations {
1095            match rng.gen_range(0..=100) {
1096                0..=19 => {
1097                    wrap_width = if rng.gen_bool(0.2) {
1098                        None
1099                    } else {
1100                        Some(rng.gen_range(0.0..=1000.0))
1101                    };
1102                    log::info!("Setting wrap width to {:?}", wrap_width);
1103                    wrap_map.update(&mut cx, |map, cx| map.set_wrap_width(wrap_width, cx));
1104                }
1105                20..=39 => {
1106                    for (folds_snapshot, fold_edits) in
1107                        cx.read(|cx| fold_map.randomly_mutate(&mut rng, cx))
1108                    {
1109                        let (tabs_snapshot, tab_edits) = tab_map.sync(folds_snapshot, fold_edits);
1110                        let (mut snapshot, wrap_edits) = wrap_map
1111                            .update(&mut cx, |map, cx| map.sync(tabs_snapshot, tab_edits, cx));
1112                        snapshot.check_invariants();
1113                        snapshot.verify_chunks(&mut rng);
1114                        edits.push((snapshot, wrap_edits));
1115                    }
1116                }
1117                _ => {
1118                    buffer.update(&mut cx, |buffer, _| buffer.randomly_mutate(&mut rng));
1119                }
1120            }
1121
1122            log::info!(
1123                "Unwrapped text (no folds): {:?}",
1124                buffer.read_with(&cx, |buf, _| buf.text())
1125            );
1126            let (folds_snapshot, fold_edits) = cx.read(|cx| fold_map.read(cx));
1127            log::info!(
1128                "Unwrapped text (unexpanded tabs): {:?}",
1129                folds_snapshot.text()
1130            );
1131            let (tabs_snapshot, tab_edits) = tab_map.sync(folds_snapshot, fold_edits);
1132            log::info!("Unwrapped text (expanded tabs): {:?}", tabs_snapshot.text());
1133
1134            let unwrapped_text = tabs_snapshot.text();
1135            let expected_text = wrap_text(&unwrapped_text, wrap_width, &mut line_wrapper);
1136            let (mut snapshot, wrap_edits) = wrap_map.update(&mut cx, |map, cx| {
1137                map.sync(tabs_snapshot.clone(), tab_edits, cx)
1138            });
1139            snapshot.check_invariants();
1140            snapshot.verify_chunks(&mut rng);
1141            edits.push((snapshot, wrap_edits));
1142
1143            if wrap_map.read_with(&cx, |map, _| map.is_rewrapping()) && rng.gen_bool(0.4) {
1144                log::info!("Waiting for wrapping to finish");
1145                while wrap_map.read_with(&cx, |map, _| map.is_rewrapping()) {
1146                    notifications.recv().await.unwrap();
1147                }
1148            }
1149
1150            if !wrap_map.read_with(&cx, |map, _| map.is_rewrapping()) {
1151                let (mut wrapped_snapshot, wrap_edits) =
1152                    wrap_map.update(&mut cx, |map, cx| map.sync(tabs_snapshot, Vec::new(), cx));
1153                let actual_text = wrapped_snapshot.text();
1154                log::info!("Wrapping finished: {:?}", actual_text);
1155                wrapped_snapshot.check_invariants();
1156                wrapped_snapshot.verify_chunks(&mut rng);
1157                edits.push((wrapped_snapshot, wrap_edits));
1158                assert_eq!(
1159                    actual_text, expected_text,
1160                    "unwrapped text is: {:?}",
1161                    unwrapped_text
1162                );
1163            }
1164        }
1165
1166        let mut initial_text = Rope::from(initial_snapshot.text().as_str());
1167        for (snapshot, patch) in edits {
1168            let snapshot_text = Rope::from(snapshot.text().as_str());
1169            for edit in &patch {
1170                let old_start = initial_text.point_to_offset(Point::new(edit.new.start, 0));
1171                let old_end = initial_text.point_to_offset(cmp::min(
1172                    Point::new(edit.new.start + edit.old.len() as u32, 0),
1173                    initial_text.max_point(),
1174                ));
1175                let new_start = snapshot_text.point_to_offset(Point::new(edit.new.start, 0));
1176                let new_end = snapshot_text.point_to_offset(cmp::min(
1177                    Point::new(edit.new.end, 0),
1178                    snapshot_text.max_point(),
1179                ));
1180                let new_text = snapshot_text
1181                    .chunks_in_range(new_start..new_end)
1182                    .collect::<String>();
1183
1184                initial_text.replace(old_start..old_end, &new_text);
1185            }
1186            assert_eq!(initial_text.to_string(), snapshot_text.to_string());
1187        }
1188    }
1189
1190    fn wrap_text(
1191        unwrapped_text: &str,
1192        wrap_width: Option<f32>,
1193        line_wrapper: &mut LineWrapper,
1194    ) -> String {
1195        if let Some(wrap_width) = wrap_width {
1196            let mut wrapped_text = String::new();
1197            for (row, line) in unwrapped_text.split('\n').enumerate() {
1198                if row > 0 {
1199                    wrapped_text.push('\n')
1200                }
1201
1202                let mut prev_ix = 0;
1203                for boundary in line_wrapper.wrap_line(line, wrap_width) {
1204                    wrapped_text.push_str(&line[prev_ix..boundary.ix]);
1205                    wrapped_text.push('\n');
1206                    wrapped_text.push_str(&" ".repeat(boundary.next_indent as usize));
1207                    prev_ix = boundary.ix;
1208                }
1209                wrapped_text.push_str(&line[prev_ix..]);
1210            }
1211            wrapped_text
1212        } else {
1213            unwrapped_text.to_string()
1214        }
1215    }
1216
1217    impl Snapshot {
1218        pub fn text(&self) -> String {
1219            self.chunks_at(0).collect()
1220        }
1221
1222        fn verify_chunks(&mut self, rng: &mut impl Rng) {
1223            for _ in 0..5 {
1224                let mut end_row = rng.gen_range(0..=self.max_point().row());
1225                let start_row = rng.gen_range(0..=end_row);
1226                end_row += 1;
1227
1228                let mut expected_text = self.chunks_at(start_row).collect::<String>();
1229                if expected_text.ends_with("\n") {
1230                    expected_text.push('\n');
1231                }
1232                let mut expected_text = expected_text
1233                    .lines()
1234                    .take((end_row - start_row) as usize)
1235                    .collect::<Vec<_>>()
1236                    .join("\n");
1237                if end_row <= self.max_point().row() {
1238                    expected_text.push('\n');
1239                }
1240
1241                let actual_text = self
1242                    .highlighted_chunks_for_rows(start_row..end_row)
1243                    .map(|c| c.text)
1244                    .collect::<String>();
1245                assert_eq!(
1246                    expected_text,
1247                    actual_text,
1248                    "chunks != highlighted_chunks for rows {:?}",
1249                    start_row..end_row
1250                );
1251            }
1252        }
1253    }
1254}