Skip to main content

wgpui_base/input/editor/display_map/
text_wrapper.rs

1use super::inline_line::InputLine;
2use std::borrow::Cow;
3use std::ops::Range;
4use std::rc::Rc;
5use wgpui::Half;
6
7use ropey::Rope;
8use smallvec::SmallVec;
9use sum_tree::{Bias, Dimensions, SumTree};
10use unicode_segmentation::UnicodeSegmentation as _;
11use wgpui::{
12    App, Font, LineFragment, Pixels, Point, ShapedLine, Size, TextAlign, Window, point, px, size,
13};
14
15use crate::input::{
16    Point as TreeSitterPoint, RopeExt, TabSize,
17    layout::{LastLayout, WhitespaceIndicators},
18    tab_stops::{expanded_width, tab_stop_width},
19};
20
21/// Controls how soft-wrapped continuation lines are indented.
22#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
23pub enum WrappingIndent {
24    /// Continuation lines start flush-left at the full editor width.
25    None,
26    /// Continuation lines keep the same indentation as the first line.
27    #[default]
28    Same,
29}
30
31/// Choose Unicode line-break opportunities using the same shaped widths as
32/// painting. Oversized words fall back to complete graphemes, never UTF-8 bytes.
33fn measured_wrap_boundaries(
34    text: &str,
35    width: Pixels,
36    wrapping_indent: WrappingIndent,
37    mut measure: impl FnMut(&str) -> Pixels,
38) -> Vec<wgpui::Boundary> {
39    let indent = if wrapping_indent == WrappingIndent::Same {
40        text.chars()
41            .take_while(|&c| c == ' ')
42            .count()
43            .min(wgpui::LineWrapper::MAX_INDENT as usize)
44    } else {
45        0
46    };
47    let indent_width = measure(&text[..indent]);
48    let ends: Vec<usize> = text
49        .grapheme_indices(true)
50        .map(|(ix, grapheme)| ix + grapheme.len())
51        .collect();
52    let opportunities: Vec<usize> = unicode_linebreak::linebreaks(text)
53        .map(|(ix, _)| ix)
54        .filter(|ix| ends.binary_search(ix).is_ok())
55        .collect();
56    let mut result = Vec::new();
57    let mut first = 0;
58    let mut start = 0;
59    while first < ends.len() {
60        let available = if start == 0 {
61            width
62        } else {
63            width - indent_width
64        };
65        // Find the fitting prefix locally. Exponential probing avoids shaping
66        // the entire remaining logical line for every visual row of a long paste.
67        let remaining = ends.len() - first;
68        let mut low = 0;
69        let mut high = 1;
70        while measure(&text[start..ends[first + high - 1]]) <= available {
71            low = high;
72            if high == remaining {
73                break;
74            }
75            high = (high * 2).min(remaining);
76        }
77        while low + 1 < high {
78            let mid = (low + high) / 2;
79            if measure(&text[start..ends[first + mid - 1]]) <= available {
80                low = mid;
81            } else {
82                high = mid;
83            }
84        }
85        // An indivisible grapheme wider than the viewport still consumes a row.
86        let fitting_end = ends[first + low.max(1) - 1];
87        if fitting_end == text.len() {
88            break;
89        }
90        let candidate = opportunities.partition_point(|&ix| ix <= fitting_end);
91        let end = candidate
92            .checked_sub(1)
93            .map(|ix| opportunities[ix])
94            .filter(|&ix| ix > start && (start != 0 || ix > indent))
95            .unwrap_or(fitting_end);
96        result.push(wgpui::Boundary {
97            ix: end,
98            next_indent: indent as u32,
99        });
100        first = ends.partition_point(|&ix| ix <= end);
101        start = end;
102    }
103    result
104}
105
106/// A line with soft wrapped lines info.
107#[derive(Debug, Clone)]
108pub(crate) struct LineItem {
109    /// The byte length of the line, without the end `\n`.
110    len: usize,
111    /// Number of leading characters of the line reserved as indentation for continuation wrapped
112    /// lines, when [`WrappingIndent::Same`] is used.
113    ///
114    /// Zero when [`WrappingIndent::None`] is used or the line is not wrapped.
115    pub(crate) indent: u32,
116    /// The soft wrapped lines relative byte range (0..len) of this line (Include first line).
117    ///
118    /// Not contains the line end `\n`.
119    pub(crate) wrapped_lines: SmallVec<[Range<usize>; 1]>,
120}
121
122impl LineItem {
123    /// Get the bytes length of this line.
124    #[inline]
125    pub(crate) fn len(&self) -> usize {
126        self.len
127    }
128
129    /// Get number of soft wrapped lines of this line (include the first line).
130    #[inline]
131    pub(crate) fn lines_len(&self) -> usize {
132        self.wrapped_lines.len()
133    }
134}
135
136/// Summary of a subtree of [`LineItem`]s, maintained incrementally by the [`SumTree`].
137#[derive(Debug, Clone)]
138pub(crate) struct LineSummary {
139    /// Number of buffer lines.
140    buffer_rows: usize,
141    /// Number of wrap rows (sum of each line's `lines_len()`).
142    wrap_rows: usize,
143    /// Sum of byte lengths of the buffer lines (without the trailing `\n`).
144    bytes: usize,
145    /// Byte length of the longest line in this subtree.
146    max_line_len: usize,
147    /// Buffer row (relative to this subtree) of the first line achieving `max_line_len`.
148    longest_row: usize,
149}
150
151impl sum_tree::Summary for LineSummary {
152    type Context<'a> = &'a ();
153
154    fn zero(_: &()) -> Self {
155        LineSummary {
156            buffer_rows: 0,
157            wrap_rows: 0,
158            bytes: 0,
159            max_line_len: 0,
160            longest_row: 0,
161        }
162    }
163
164    fn add_summary(&mut self, other: &Self, _: &()) {
165        // Keep the leftmost row that achieves a strictly greater length
166        if other.max_line_len > self.max_line_len {
167            self.longest_row = self.buffer_rows + other.longest_row;
168            self.max_line_len = other.max_line_len;
169        }
170        self.buffer_rows += other.buffer_rows;
171        self.wrap_rows += other.wrap_rows;
172        self.bytes += other.bytes;
173    }
174}
175
176impl sum_tree::Item for LineItem {
177    type Summary = LineSummary;
178
179    fn summary(&self, _: &()) -> LineSummary {
180        LineSummary {
181            buffer_rows: 1,
182            wrap_rows: self.lines_len(),
183            bytes: self.len(),
184            max_line_len: self.len(),
185            longest_row: 0,
186        }
187    }
188}
189
190/// Cursor dimension counting buffer rows.
191#[derive(Clone, Copy, Default, Debug, PartialEq, Eq, PartialOrd, Ord)]
192pub(crate) struct BufferRows(pub usize);
193
194impl<'a> sum_tree::Dimension<'a, LineSummary> for BufferRows {
195    fn zero(_: &()) -> Self {
196        BufferRows(0)
197    }
198
199    fn add_summary(&mut self, summary: &'a LineSummary, _: &()) {
200        self.0 += summary.buffer_rows;
201    }
202}
203
204/// Cursor dimension counting wrap rows.
205#[derive(Clone, Copy, Default, Debug, PartialEq, Eq, PartialOrd, Ord)]
206pub(crate) struct WrapRows(pub usize);
207
208impl<'a> sum_tree::Dimension<'a, LineSummary> for WrapRows {
209    fn zero(_: &()) -> Self {
210        WrapRows(0)
211    }
212
213    fn add_summary(&mut self, summary: &'a LineSummary, _: &()) {
214        self.0 += summary.wrap_rows;
215    }
216}
217
218/// Used to prepare the text with soft wrap to be get lines to displayed in the Editor.
219///
220/// After use lines to calculate the scroll size of the Editor.
221pub(crate) struct TextWrapper {
222    text: Rope,
223    font: Font,
224    font_size: Pixels,
225    /// If is none, it means the text is not wrapped
226    wrap_width: Option<Pixels>,
227    wrapping_indent: WrappingIndent,
228    /// Spaces per tab stop, so tabs are measured as they are drawn.
229    tab_size: usize,
230    /// The lines by split \n
231    pub(crate) lines: SumTree<LineItem>,
232
233    inline_metrics: Rc<[(Range<usize>, Pixels)]>,
234    _initialized: bool,
235}
236
237#[allow(unused)]
238impl TextWrapper {
239    pub(crate) fn new(font: Font, font_size: Pixels, wrap_width: Option<Pixels>) -> Self {
240        Self {
241            text: Rope::new(),
242            font,
243            font_size,
244            wrap_width,
245            wrapping_indent: WrappingIndent::default(),
246            tab_size: TabSize::default().tab_size,
247            lines: SumTree::new(&()),
248            inline_metrics: Rc::from([]),
249            _initialized: false,
250        }
251    }
252
253    #[inline]
254    pub(crate) fn set_default_text(&mut self, text: &Rope) {
255        self.text = text.clone();
256    }
257
258    /// Get reference to the rope text.
259    #[inline]
260    pub(crate) fn text(&self) -> &Rope {
261        &self.text
262    }
263
264    /// Get the total number of lines including wrapped lines.
265    #[inline]
266    pub(crate) fn len(&self) -> usize {
267        self.lines.summary().wrap_rows
268    }
269
270    /// Get the total number of buffer lines.
271    #[inline]
272    pub(crate) fn lines_count(&self) -> usize {
273        self.lines.summary().buffer_rows
274    }
275
276    /// Get the 0-based row index of the longest line (by byte length).
277    #[inline]
278    pub(crate) fn longest_row(&self) -> usize {
279        self.lines.summary().longest_row
280    }
281
282    /// Get the line item by buffer row index.
283    #[inline]
284    pub(crate) fn line(&self, row: usize) -> Option<&LineItem> {
285        let mut cursor = self.lines.cursor::<BufferRows>(&());
286        cursor.seek(&BufferRows(row), Bias::Right);
287        cursor.item()
288    }
289
290    /// Iterate buffer lines in order.
291    #[inline]
292    pub(crate) fn iter_lines(&self) -> impl Iterator<Item = &LineItem> {
293        self.lines.iter()
294    }
295
296    /// First wrap row of buffer line `row`. Returns the total wrap row count if `row` is
297    /// out of range.
298    pub(crate) fn buffer_line_to_first_wrap_row(&self, row: usize) -> usize {
299        let mut cursor = self.lines.cursor::<Dimensions<BufferRows, WrapRows>>(&());
300        cursor.seek(&BufferRows(row), Bias::Right);
301        cursor.start().1.0
302    }
303
304    /// Wrap row range of buffer line `row`.
305    pub(crate) fn buffer_line_to_wrap_row_range(&self, row: usize) -> Range<usize> {
306        let mut cursor = self.lines.cursor::<Dimensions<BufferRows, WrapRows>>(&());
307        cursor.seek(&BufferRows(row), Bias::Right);
308        let start = cursor.start().1.0;
309        let len = cursor.item().map(|l| l.lines_len()).unwrap_or(0);
310        start..start + len
311    }
312
313    /// Buffer line containing wrap row `wrap_row`, clamped to the last line.
314    pub(crate) fn wrap_row_to_buffer_line(&self, wrap_row: usize) -> usize {
315        let mut cursor = self.lines.cursor::<Dimensions<WrapRows, BufferRows>>(&());
316        cursor.seek(&WrapRows(wrap_row), Bias::Right);
317        match cursor.item() {
318            Some(_) => cursor.start().1.0,
319            None => self.lines_count().saturating_sub(1),
320        }
321    }
322
323    pub(crate) fn set_wrap_width(&mut self, wrap_width: Option<Pixels>, cx: &mut App) {
324        if wrap_width == self.wrap_width {
325            return;
326        }
327
328        self.wrap_width = wrap_width;
329        self.update_all(&self.text.clone(), cx);
330    }
331
332    pub(crate) fn set_wrapping_indent(&mut self, wrapping_indent: WrappingIndent, cx: &mut App) {
333        if wrapping_indent == self.wrapping_indent {
334            return;
335        }
336
337        self.wrapping_indent = wrapping_indent;
338        self.update_all(&self.text.clone(), cx);
339    }
340
341    /// Returns whether the tab size changed, and so the wrapping.
342    pub(crate) fn set_tab_size(&mut self, tab_size: usize, cx: &mut App) -> bool {
343        if tab_size == self.tab_size {
344            return false;
345        }
346
347        self.tab_size = tab_size;
348        self.update_all(&self.text.clone(), cx);
349        true
350    }
351
352    pub(crate) fn set_font(&mut self, font: Font, font_size: Pixels, cx: &mut App) {
353        if self.font.eq(&font) && self.font_size == font_size {
354            return;
355        }
356
357        self.font = font;
358        self.font_size = font_size;
359        self.update_all(&self.text.clone(), cx);
360    }
361
362    pub(crate) fn prepare_if_need(&mut self, text: &Rope, cx: &mut App) -> bool {
363        if self._initialized {
364            return false;
365        }
366        self._initialized = true;
367        self.update_all(text, cx);
368        true
369    }
370
371    /// Update the text wrapper and recalculate the wrapped lines.
372    ///
373    /// If the `text` is the same as the current text, do nothing.
374    ///
375    /// - `changed_text`: The text [`Rope`] that has changed.
376    /// - `range`: The `selected_range` before change.
377    /// - `new_text`: The inserted text.
378    /// - `force`: Whether to force the update, if false, the update will be skipped if the text is the same.
379    /// - `cx`: The application context.
380    pub(crate) fn update(
381        &mut self,
382        changed_text: &Rope,
383        range: &Range<usize>,
384        new_text: &Rope,
385        cx: &mut App,
386    ) {
387        let mut line_wrapper = cx
388            .text_system()
389            .line_wrapper(self.font.clone(), self.font_size);
390        let metrics = self.inline_metrics.clone();
391        let text_system = wgpui::WindowTextSystem::new(cx.text_system().clone());
392        let font = self.font.clone();
393        let font_size = self.font_size;
394        let wrapping_indent = self.wrapping_indent;
395        let tab_stop = tab_stop_width(&text_system, &font, font_size, self.tab_size);
396        self._update(
397            changed_text,
398            range,
399            new_text,
400            &mut |line_str, wrap_width, line_start| {
401                let mut fragments = Vec::new();
402                let mut offset = 0;
403                let first = metrics.partition_point(|(r, _)| r.end <= line_start);
404                for (range, width) in &metrics[first..] {
405                    if range.start >= line_start + line_str.len() {
406                        break;
407                    }
408                    if range.start < line_start || range.end > line_start + line_str.len() {
409                        continue;
410                    }
411                    let range = range.start - line_start..range.end - line_start;
412                    if !line_str.is_char_boundary(range.start)
413                        || !line_str.is_char_boundary(range.end)
414                    {
415                        continue;
416                    }
417                    if offset < range.start {
418                        fragments.push(LineFragment::text(&line_str[offset..range.start]));
419                    }
420                    fragments.push(LineFragment::element(*width, range.len()));
421                    offset = range.end;
422                }
423                if fragments.is_empty() {
424                    return measured_wrap_boundaries(
425                        line_str,
426                        wrap_width,
427                        wrapping_indent,
428                        |text| {
429                            let layout = text_system.layout_line(
430                                text,
431                                font_size,
432                                &[wgpui::TextRun {
433                                    len: text.len(),
434                                    font: font.clone(),
435                                    color: wgpui::black(),
436                                    background_color: None,
437                                    underline: None,
438                                    strikethrough: None,
439                                }],
440                                None,
441                            );
442                            expanded_width(&layout, text, tab_stop)
443                        },
444                    );
445                }
446                if offset < line_str.len() {
447                    fragments.push(LineFragment::text(&line_str[offset..]));
448                }
449                line_wrapper
450                    .wrap_line(&fragments, wrap_width, wgpui::IndentAdjustment::SameIndent)
451                    .collect()
452            },
453        );
454    }
455
456    pub(crate) fn adjust_inline_metrics(&mut self, range: &Range<usize>, new_len: usize) {
457        if self.inline_metrics.is_empty() {
458            return;
459        }
460        let shift = new_len as isize - range.len() as isize;
461        self.inline_metrics = self
462            .inline_metrics
463            .iter()
464            .filter_map(|(token, width)| {
465                if token.start < range.end && range.start < token.end {
466                    return None;
467                }
468                let token = if token.start >= range.end {
469                    token.start.checked_add_signed(shift)?..token.end.checked_add_signed(shift)?
470                } else {
471                    token.clone()
472                };
473                Some((token, *width))
474            })
475            .collect();
476    }
477
478    pub(crate) fn set_inline_metrics(
479        &mut self,
480        metrics: Rc<[(Range<usize>, Pixels)]>,
481        cx: &mut App,
482    ) {
483        if self.inline_metrics == metrics {
484            return;
485        }
486        // Only rows whose element geometry changed need another wrap pass.
487        let mut affected = Vec::new();
488        let (mut old, mut new) = (
489            self.inline_metrics.iter().peekable(),
490            metrics.iter().peekable(),
491        );
492        while old.peek().is_some() || new.peek().is_some() {
493            match (old.peek(), new.peek()) {
494                (Some(a), Some(b)) if a == b => {
495                    old.next();
496                    new.next();
497                }
498                (Some(a), Some(b)) if a.0.start <= b.0.start => {
499                    affected.push(a.0.clone());
500                    old.next();
501                }
502                (Some(_), Some(b)) | (None, Some(b)) => {
503                    affected.push(b.0.clone());
504                    new.next();
505                }
506                (Some(a), None) => {
507                    affected.push(a.0.clone());
508                    old.next();
509                }
510                (None, None) => break,
511            }
512        }
513        self.inline_metrics = metrics;
514        let text = self.text.clone();
515        let mut rows: Vec<usize> = affected
516            .iter()
517            .map(|r| text.offset_to_point(r.start.min(text.len_bytes())).row)
518            .collect();
519        rows.sort_unstable();
520        rows.dedup();
521        for row in rows {
522            let start = text.line_start_offset(row);
523            let end = text.line_end_offset(row);
524            self.update(
525                &text,
526                &(start..end),
527                &Rope::from(text.byte_slice(start..end).to_string()),
528                cx,
529            );
530        }
531    }
532
533    fn _update<F>(
534        &mut self,
535        changed_text: &Rope,
536        range: &Range<usize>,
537        new_text: &Rope,
538        wrap_line: &mut F,
539    ) where
540        F: FnMut(&str, Pixels, usize) -> Vec<wgpui::Boundary>,
541    {
542        // Remove the old changed lines.
543        let buffer_line_count = self.lines_count();
544        let start_row = self.text.offset_to_point(range.start).row;
545        let start_row = start_row.min(buffer_line_count.saturating_sub(1));
546        let end_row = self.text.offset_to_point(range.end).row;
547        let end_row = end_row.min(buffer_line_count.saturating_sub(1));
548
549        // To add the new lines.
550        let new_start_row = changed_text.offset_to_point(range.start).row;
551        let new_end_row = changed_text
552            .offset_to_point(range.start + new_text.len_bytes())
553            .row;
554
555        let mut new_lines = Vec::with_capacity(new_end_row.saturating_sub(new_start_row) + 1);
556        let wrap_width = self.wrap_width;
557
558        // line not contains `\n`.
559        for row in new_start_row..=new_end_row {
560            let line = changed_text.slice_line(row);
561            let mut wrapped_lines = SmallVec::<[Range<usize>; 1]>::new();
562            let mut prev_boundary_ix = 0;
563            let mut indent_chars = 0;
564
565            // If wrap_width is Pixels::MAX, skip wrapping to disable word wrap
566            if let Some(wrap_width) = wrap_width {
567                // Borrowed for lines within a single rope chunk.
568                let line_str: Cow<str> = line.into();
569                match self.wrapping_indent {
570                    WrappingIndent::Same => {
571                        // Here only have wrapped line, if there is no wrap meet, the `line_wraps`
572                        // result will empty.
573                        for boundary in
574                            wrap_line(&line_str, wrap_width, changed_text.line_start_offset(row))
575                        {
576                            wrapped_lines.push(prev_boundary_ix..boundary.ix);
577                            prev_boundary_ix = boundary.ix;
578                            indent_chars = boundary.next_indent;
579                        }
580                    }
581                    WrappingIndent::None => {
582                        // The first visual line keeps the line's leading indentation, so it is
583                        // wrapped as is.
584                        let boundaries =
585                            wrap_line(&line_str, wrap_width, changed_text.line_start_offset(row));
586                        if let Some(first_ix) = boundaries.first().map(|b| b.ix) {
587                            wrapped_lines.push(prev_boundary_ix..first_ix);
588                            prev_boundary_ix = first_ix;
589
590                            for boundary in wrap_line(
591                                &line_str[first_ix..],
592                                wrap_width,
593                                changed_text.line_start_offset(row) + first_ix,
594                            ) {
595                                let ix = first_ix + boundary.ix;
596                                wrapped_lines.push(prev_boundary_ix..ix);
597                                prev_boundary_ix = ix;
598                            }
599                        }
600                    }
601                }
602            }
603
604            // Reset of the line
605            if prev_boundary_ix < line.len_bytes() || prev_boundary_ix == 0 {
606                wrapped_lines.push(prev_boundary_ix..line.len_bytes());
607            }
608
609            new_lines.push(LineItem {
610                len: line.len_bytes(),
611                indent: indent_chars,
612                wrapped_lines,
613            });
614        }
615
616        if self.lines.is_empty() {
617            self.lines = SumTree::from_iter(new_lines, &());
618        } else {
619            let mut cursor = self.lines.cursor::<BufferRows>(&());
620            let mut new_tree = cursor.slice(&BufferRows(start_row), Bias::Right);
621            // Skip the replaced rows
622            cursor.seek_forward(&BufferRows(end_row + 1), Bias::Right);
623            new_tree.extend(new_lines, &());
624            // Untouched rows after the edit
625            new_tree.append(cursor.suffix(), &());
626            drop(cursor);
627            self.lines = new_tree;
628        }
629
630        self.text = changed_text.clone();
631    }
632
633    /// Update the text wrapper and recalculate the wrapped lines.
634    ///
635    /// If the `text` is the same as the current text, do nothing.
636    fn update_all(&mut self, text: &Rope, cx: &mut App) {
637        self.update(text, &(0..text.len_bytes()), &text, cx);
638    }
639
640    /// Return display point (with soft wrap) from the given byte offset in the text.
641    ///
642    /// Panics if the `offset` is out of bounds.
643    pub(crate) fn offset_to_display_point(&self, offset: usize) -> WrapDisplayPoint {
644        self.offset_to_display_point_with_affinity(offset, false)
645    }
646
647    /// Like [`Self::offset_to_display_point`], but honours the caret's line-end affinity.
648    ///
649    /// A soft wrap boundary is one offset shared by two visual rows. Without affinity it always
650    /// resolves to the start of the second row, which is wrong for a caret that is being drawn at
651    /// the end of the first one -- vertical movement would then step from the row below the one
652    /// the user can see.
653    pub(crate) fn offset_to_display_point_with_affinity(
654        &self,
655        offset: usize,
656        line_end_affinity: bool,
657    ) -> WrapDisplayPoint {
658        let row = self.text.offset_to_point(offset).row;
659        let start = self.text.line_start_offset(row);
660
661        // Seek to buffer row
662        let mut cursor = self.lines.cursor::<Dimensions<BufferRows, WrapRows>>(&());
663        cursor.seek(&BufferRows(row), Bias::Right);
664        let wrapped_row = cursor.start().1.0;
665        let Some(line) = cursor.item() else {
666            return WrapDisplayPoint::new(wrapped_row, 0, 0);
667        };
668
669        let local_offset = offset.saturating_sub(start);
670        for (ix, range) in line.wrapped_lines.iter().enumerate() {
671            // With affinity the boundary offset closes the current row instead of opening the
672            // next one, so the range is matched inclusively.
673            let matches =
674                range.contains(&local_offset) || (line_end_affinity && local_offset == range.end);
675            if matches {
676                return WrapDisplayPoint::new(
677                    wrapped_row + ix,
678                    ix,
679                    local_offset.saturating_sub(range.start),
680                );
681            }
682        }
683
684        // Otherwise return the eof of the line.
685        let last_range = line.wrapped_lines.last().unwrap_or(&(0..0));
686        let ix = line.lines_len().saturating_sub(1);
687        return WrapDisplayPoint::new(wrapped_row + ix, ix, last_range.len());
688    }
689
690    /// Return byte offset in the text from the given display point (with soft wrap).
691    ///
692    /// Panics if the `point.row` is out of bounds.
693    pub(crate) fn display_point_to_offset(&self, point: WrapDisplayPoint) -> usize {
694        // Seek to wrap row `point.row`
695        let mut cursor = self.lines.cursor::<Dimensions<WrapRows, BufferRows>>(&());
696        cursor.seek(&WrapRows(point.row), Bias::Right);
697        let Some(line) = cursor.item() else {
698            return self.text.len_bytes();
699        };
700        let wrapped_row = cursor.start().0.0;
701        let row = cursor.start().1.0;
702
703        let line_start = self.text.line_start_offset(row);
704        let local_row = point.row.saturating_sub(wrapped_row);
705        if let Some(range) = line.wrapped_lines.get(local_row) {
706            line_start + (range.start + point.column).min(range.end)
707        } else {
708            // If not found, return the end of the line.
709            line_start + line.len()
710        }
711    }
712
713    pub(crate) fn display_point_to_point(&self, point: WrapDisplayPoint) -> TreeSitterPoint {
714        let offset = self.display_point_to_offset(point);
715        self.text.offset_to_point(offset)
716    }
717
718    pub(crate) fn point_to_display_point(&self, point: TreeSitterPoint) -> WrapDisplayPoint {
719        let offset = self.text.point_to_offset(point);
720        self.offset_to_display_point(offset)
721    }
722}
723
724/// A display point within the soft-wrapped text.
725///
726/// This represents a position in the text after soft-wrapping,
727/// with an additional `local_row` field tracking the wrap line
728/// within the original buffer line.
729#[derive(Debug, Clone, Copy, PartialEq, Eq)]
730pub(crate) struct WrapDisplayPoint {
731    /// The 0-based soft wrapped row index in the text.
732    pub row: usize,
733    /// The 0-based row index in local line (include first line).
734    ///
735    /// This value only valid when return from [`TextWrapper::offset_to_display_point`], otherwise it will be ignored.
736    pub local_row: usize,
737    /// The 0-based column byte index in the display line (with soft wrap).
738    pub column: usize,
739}
740
741impl WrapDisplayPoint {
742    pub(crate) fn new(row: usize, local_row: usize, column: usize) -> Self {
743        Self {
744            row,
745            local_row,
746            column,
747        }
748    }
749}
750
751/// The layout info of a line with soft wrapped lines.
752pub(crate) struct LineLayout {
753    /// Total bytes length of this line.
754    len: usize,
755    /// The soft wrapped lines of this line (Include the first line).
756    pub(crate) wrapped_lines: SmallVec<[InputLine; 1]>,
757    /// Extra left offset applied to continuation wrapped lines, used to reserve the first line's
758    /// indentation when [`WrappingIndent::Same`] is used.
759    pub(crate) wrap_indent: Pixels,
760    pub(crate) longest_width: Pixels,
761    pub(crate) whitespace_indicators: Option<WhitespaceIndicators>,
762    /// Whitespace indicators: (line_index, x_position, is_tab)
763    pub(crate) whitespace_chars: Vec<(usize, Pixels, bool)>,
764    /// Whether any run of this line carries a background color, so [`Self::paint_background`]
765    /// can skip the glyph walk for the common case of a line without highlights.
766    has_background: bool,
767}
768
769impl LineLayout {
770    pub(crate) fn new() -> Self {
771        Self {
772            len: 0,
773            longest_width: px(0.),
774            wrapped_lines: SmallVec::new(),
775            wrap_indent: px(0.),
776            whitespace_chars: Vec::new(),
777            whitespace_indicators: None,
778            has_background: false,
779        }
780    }
781
782    /// Record whether any run of this line carries a background color.
783    pub(crate) fn with_background(mut self, has_background: bool) -> Self {
784        self.has_background = has_background;
785        self
786    }
787
788    /// Set the left offset reserved for continuation wrapped lines.
789    pub(crate) fn wrap_indent(mut self, wrap_indent: Pixels) -> Self {
790        self.wrap_indent = wrap_indent;
791        self
792    }
793
794    /// The pixel indent applied to the given visual line, relative to the line's
795    /// leading text. Only continuation lines (index > 0) are indented.
796    #[inline]
797    fn line_indent(&self, line_index: usize) -> Pixels {
798        if line_index == 0 {
799            px(0.)
800        } else {
801            self.wrap_indent
802        }
803    }
804
805    pub(crate) fn lines(mut self, wrapped_lines: SmallVec<[ShapedLine; 1]>) -> Self {
806        self.set_wrapped_lines(wrapped_lines);
807        self
808    }
809
810    pub(crate) fn set_wrapped_lines(&mut self, wrapped_lines: SmallVec<[ShapedLine; 1]>) {
811        self.len = wrapped_lines.iter().map(|l| l.len).sum();
812        let width = wrapped_lines
813            .iter()
814            .map(|l| l.width)
815            .max()
816            .unwrap_or_default();
817        self.longest_width = width;
818        self.wrapped_lines = wrapped_lines.into_iter().map(InputLine::from).collect();
819    }
820
821    pub(crate) fn inline_lines(mut self, lines: SmallVec<[InputLine; 1]>) -> Self {
822        self.len = lines.iter().map(|line| line.len).sum();
823        self.longest_width = lines
824            .iter()
825            .map(|line| line.width)
826            .max()
827            .unwrap_or_default();
828        self.wrapped_lines = lines;
829        self
830    }
831
832    pub(crate) fn with_whitespaces(mut self, indicators: Option<WhitespaceIndicators>) -> Self {
833        self.whitespace_indicators = indicators;
834        let Some(indicators) = self.whitespace_indicators.as_ref() else {
835            return self;
836        };
837
838        let space_indicator_offset = indicators.space.width.half();
839
840        for (line_index, wrapped_line) in self.wrapped_lines.iter().enumerate() {
841            for (relative_offset, c) in wrapped_line.text.char_indices() {
842                if matches!(c, ' ' | '\t') {
843                    let is_tab = c == '\t';
844                    let start_x = wrapped_line.x_for_index(relative_offset);
845                    let end_x = wrapped_line.x_for_index(relative_offset + c.len_utf8());
846                    // Center the indicator in the actual character's space
847                    let x_position = if c == ' ' {
848                        (start_x + end_x).half() - space_indicator_offset
849                    } else {
850                        start_x
851                    };
852
853                    self.whitespace_chars.push((line_index, x_position, is_tab));
854                }
855            }
856        }
857        self
858    }
859
860    #[inline]
861    pub(crate) fn len(&self) -> usize {
862        self.len
863    }
864
865    /// Get the position (x, y) for the given index in this line layout.
866    ///
867    /// - The `offset` is a local byte index in this line layout.
868    /// - When `line_end_affinity` is true, an offset at a soft wrap boundary is placed at
869    ///   the end of the current visual line rather than the start of the next one.
870    /// - The return value is relative to the top-left corner of this line layout, start from (0, 0)
871    pub(crate) fn position_for_index(
872        &self,
873        offset: usize,
874        last_layout: &LastLayout,
875        line_end_affinity: bool,
876    ) -> Option<Point<Pixels>> {
877        let mut acc_len = 0;
878        let mut offset_y = px(0.);
879
880        let x_offset = last_layout.alignment_offset(self.longest_width);
881
882        for (i, line) in self.wrapped_lines.iter().enumerate() {
883            let is_last = i + 1 == self.wrapped_lines.len();
884
885            let matches = if line.len == 0 {
886                // Empty visual lines still own their boundary offset.
887                offset == acc_len
888            } else if is_last || line_end_affinity {
889                // Inclusive: cursor can sit at end of this visual line.
890                offset >= acc_len && offset <= acc_len + line.len
891            } else {
892                // Exclusive: boundary offset belongs to the next visual line.
893                offset >= acc_len && offset < acc_len + line.len
894            };
895
896            if matches {
897                let x = line.x_for_index(offset.saturating_sub(acc_len))
898                    + x_offset
899                    + self.line_indent(i);
900                return Some(point(x, offset_y));
901            }
902
903            // Always advance by actual line length. The last line gets +1 so the
904            // cursor can be placed after the final character.
905            acc_len += if is_last { line.len + 1 } else { line.len };
906            offset_y += last_layout.line_height;
907        }
908
909        None
910    }
911
912    /// Get the closest index for the given x in this line layout.
913    ///
914    /// This ignores y, so it only makes sense for a layout that is known to occupy a single
915    /// visual line. Wrapped layouts must use [`Self::closest_index_for_position`], which also
916    /// reports the caret affinity that a wrap boundary needs.
917    pub(crate) fn closest_index_for_x(&self, x: Pixels, last_layout: &LastLayout) -> usize {
918        let mut acc_len = 0;
919        let x_offset = last_layout.alignment_offset(self.longest_width);
920        let x = x - x_offset;
921
922        for (i, line) in self.wrapped_lines.iter().enumerate() {
923            let line_indent = self.line_indent(i);
924            if x <= line_indent + line.width {
925                return acc_len + line.closest_index_for_x(x - line_indent);
926            }
927            acc_len += line.len;
928        }
929
930        acc_len
931    }
932
933    /// Resolve `pos` to the wrapped sub-line under it.
934    ///
935    /// Returns the sub-line index, the byte offset that sub-line starts at within this line
936    /// layout, and `pos.x` translated into that sub-line's own coordinate space.
937    fn wrapped_line_at(
938        &self,
939        pos: Point<Pixels>,
940        last_layout: &LastLayout,
941    ) -> Option<(usize, usize, Pixels)> {
942        let mut offset = 0;
943        let mut line_top = px(0.);
944        let x_offset = last_layout.alignment_offset(self.longest_width);
945
946        for (i, line) in self.wrapped_lines.iter().enumerate() {
947            let line_bottom = line_top + last_layout.line_height;
948            if pos.y >= line_top && pos.y < line_bottom {
949                return Some((i, offset, pos.x - x_offset - self.line_indent(i)));
950            }
951
952            offset += line.len;
953            line_top = line_bottom;
954        }
955
956        None
957    }
958
959    /// Get the index for the given position (x, y) in this line layout.
960    ///
961    /// The `pos` is relative to the top-left corner of this line layout, start from (0, 0).
962    ///
963    /// Returns a local byte index in this line layout (start from 0) together with the caret
964    /// affinity to use for it: `true` when the index landed on the wrap boundary of a non-final
965    /// sub-line. That boundary offset is shared by the end of one visual line and the start of
966    /// the next, so the affinity is what tells [`Self::position_for_index`] which of the two the
967    /// caret belongs to. Without it a click past the last glyph of a wrapped line would put a
968    /// visible caret on the following line.
969    pub(crate) fn closest_index_for_position(
970        &self,
971        pos: Point<Pixels>,
972        last_layout: &LastLayout,
973    ) -> Option<(usize, bool)> {
974        let (i, offset, x) = self.wrapped_line_at(pos, last_layout)?;
975        let line = &self.wrapped_lines[i];
976        let ix = line.closest_index_for_x(x);
977        let line_end_affinity = i + 1 < self.wrapped_lines.len() && ix == line.len;
978
979        Some((offset + ix, line_end_affinity))
980    }
981
982    /// How many columns the given position sits past the end of the line under it.
983    ///
984    /// Past the end of a line there is no glyph to hit-test against, so a position out
985    /// there resolves to the line end and loses how far right it really was. The extra
986    /// distance is reported here in whole spaces, letting a columnar selection keep its
987    /// width over a short row. Only the final visual line of a wrapped layout has that
988    /// trailing space; a continuation line ends at a wrap boundary, where the next glyph
989    /// merely lives on the following row.
990    ///
991    /// The `pos` is relative to the top-left corner of this line layout, start from (0, 0).
992    pub(crate) fn columns_past_line_end(
993        &self,
994        pos: Point<Pixels>,
995        last_layout: &LastLayout,
996    ) -> usize {
997        let Some((i, _, x)) = self.wrapped_line_at(pos, last_layout) else {
998            return 0;
999        };
1000
1001        if i + 1 < self.wrapped_lines.len() || last_layout.space_width <= px(0.) {
1002            return 0;
1003        }
1004
1005        let past_end = x - self.wrapped_lines[i].width;
1006        if past_end <= px(0.) {
1007            return 0;
1008        }
1009
1010        (past_end / last_layout.space_width).round() as usize
1011    }
1012
1013    pub(crate) fn index_for_position(
1014        &self,
1015        pos: Point<Pixels>,
1016        last_layout: &LastLayout,
1017    ) -> Option<usize> {
1018        let (i, offset, x) = self.wrapped_line_at(pos, last_layout)?;
1019
1020        Some(offset + self.wrapped_lines[i].index_for_x(x)?)
1021    }
1022
1023    pub(crate) fn size(&self, line_height: Pixels) -> Size<Pixels> {
1024        let width = self
1025            .wrapped_lines
1026            .iter()
1027            .enumerate()
1028            .map(|(ix, line)| line.width + self.line_indent(ix))
1029            .max()
1030            .unwrap_or(self.longest_width);
1031        size(width, self.wrapped_lines.len() * line_height)
1032    }
1033
1034    /// Paint only the glyph background quads of this line.
1035    ///
1036    /// WGPUI's [`ShapedLine::paint`] does not draw backgrounds, so every line painted with
1037    /// [`Self::paint`] needs this called first, with the same origin and align width.
1038    pub(crate) fn paint_background(
1039        &self,
1040        pos: Point<Pixels>,
1041        line_height: Pixels,
1042        text_align: TextAlign,
1043        align_width: Option<Pixels>,
1044        window: &mut Window,
1045        cx: &mut App,
1046    ) {
1047        // Painting a background walks every glyph and pushes a scene layer, so skip the
1048        // whole pass for lines that have no background color to paint.
1049        if !self.has_background {
1050            return;
1051        }
1052
1053        for (ix, line) in self.wrapped_lines.iter().enumerate() {
1054            _ = line.paint_background(
1055                pos + point(self.line_indent(ix), ix * line_height),
1056                line_height,
1057                text_align,
1058                align_width,
1059                window,
1060                cx,
1061            );
1062        }
1063    }
1064
1065    pub(crate) fn paint(
1066        &self,
1067        pos: Point<Pixels>,
1068        line_height: Pixels,
1069        text_align: TextAlign,
1070        align_width: Option<Pixels>,
1071        window: &mut Window,
1072        cx: &mut App,
1073    ) {
1074        for (ix, line) in self.wrapped_lines.iter().enumerate() {
1075            _ = line.paint(
1076                pos + point(self.line_indent(ix), ix * line_height),
1077                line_height,
1078                text_align,
1079                align_width,
1080                window,
1081                cx,
1082            );
1083        }
1084
1085        // Paint whitespace indicators
1086        if let Some(indicators) = self.whitespace_indicators.as_ref() {
1087            for (line_index, x_position, is_tab) in &self.whitespace_chars {
1088                let invisible = if *is_tab {
1089                    indicators.tab.clone()
1090                } else {
1091                    indicators.space.clone()
1092                };
1093
1094                let origin = point(
1095                    pos.x + *x_position + self.line_indent(*line_index),
1096                    pos.y + *line_index as f32 * line_height,
1097                );
1098
1099                _ = invisible.paint(origin, line_height, text_align, align_width, window, cx);
1100            }
1101        }
1102    }
1103}
1104
1105#[cfg(test)]
1106mod tests {
1107    use super::*;
1108    use std::rc::Rc;
1109
1110    #[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
1111    use wgpui::TestAppContext;
1112    use wgpui::{Boundary, FontFeatures, FontStyle, FontWeight, px};
1113
1114    #[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
1115    fn shaped_width(text: &str, font: &Font, font_size: Pixels, cx: &App) -> Pixels {
1116        wgpui::WindowTextSystem::new(cx.text_system().clone())
1117            .layout_line(
1118                text,
1119                font_size,
1120                &[wgpui::TextRun {
1121                    len: text.len(),
1122                    font: font.clone(),
1123                    color: wgpui::black(),
1124                    background_color: None,
1125                    underline: None,
1126                    strikethrough: None,
1127                }],
1128                None,
1129            )
1130            .width
1131    }
1132
1133    #[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
1134    fn shaping_test_context() -> TestAppContext {
1135        TestAppContext::build_with_text_system(
1136            wgpui::TestDispatcher::new(0),
1137            None,
1138            wgpui::try_current_platform_text_system().expect("native shaping service"),
1139        )
1140    }
1141
1142    #[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
1143    #[test]
1144    fn production_wrap_keeps_shaped_cjk_latin_boundary_during_edits() {
1145        let cx = shaping_test_context();
1146        cx.update(|cx| {
1147            let font = test_font();
1148            let font_size = px(14.);
1149            let prefix = "abcd的";
1150            let width = shaped_width(prefix, &font, font_size, cx);
1151            assert!(shaped_width("abcd的s", &font, font_size, cx) > width);
1152            let mut wrapper = TextWrapper::new(font.clone(), font_size, Some(width));
1153            let mut previous = Rope::new();
1154
1155            for value in ["abcd的", "abcd的s", "abcd的ss", "abcd的s", "abcd的"] {
1156                let text = Rope::from(value);
1157                let start = previous.len_bytes().min(text.len_bytes());
1158                let inserted = Rope::from(text.byte_slice(start..).to_string());
1159                wrapper.update(&text, &(start..previous.len_bytes()), &inserted, cx);
1160                let expected = if value.ends_with('s') {
1161                    vec![0..prefix.len(), prefix.len()..value.len()]
1162                } else {
1163                    vec![0..prefix.len()]
1164                };
1165                assert_eq!(wrapper.line(0).unwrap().wrapped_lines.as_slice(), expected);
1166                for range in &wrapper.line(0).unwrap().wrapped_lines {
1167                    assert!(shaped_width(&value[range.clone()], &font, font_size, cx) <= width);
1168                }
1169                assert_eq!(
1170                    wrapper.offset_to_display_point(value.len()).row,
1171                    usize::from(value.ends_with('s'))
1172                );
1173                previous = text;
1174            }
1175        });
1176    }
1177
1178    #[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
1179    #[test]
1180    fn production_wrap_preserves_words_and_complete_graphemes() {
1181        let cx = shaping_test_context();
1182        cx.update(|cx| {
1183            let font = test_font();
1184            let font_size = px(14.);
1185            for (value, prefix) in [("hello world", "hello "), ("a👩‍💻b", "a👩‍💻")] {
1186                // Either complete row must fit even when the native font makes
1187                // the second word wider than the first word and its space.
1188                let width = shaped_width(prefix, &font, font_size, cx).max(shaped_width(
1189                    &value[prefix.len()..],
1190                    &font,
1191                    font_size,
1192                    cx,
1193                ));
1194                assert!(shaped_width(value, &font, font_size, cx) > width);
1195                let text = Rope::from(value);
1196                let mut wrapper = TextWrapper::new(font.clone(), font_size, Some(width));
1197                wrapper.update(&text, &(0..0), &text, cx);
1198                assert_eq!(
1199                    wrapper.line(0).unwrap().wrapped_lines.as_slice(),
1200                    [0..prefix.len(), prefix.len()..value.len()]
1201                );
1202                for range in &wrapper.line(0).unwrap().wrapped_lines {
1203                    assert!(shaped_width(&value[range.clone()], &font, font_size, cx) <= width);
1204                }
1205            }
1206        });
1207    }
1208
1209    #[test]
1210    fn measured_wrap_keeps_cjk_latin_boundary_stable_during_edits() {
1211        let measure = |text: &str| {
1212            px(text
1213                .chars()
1214                .map(|c| if c.is_ascii() { 1. } else { 2. })
1215                .sum())
1216        };
1217        let mut wrapper = TextWrapper::new(wgpui::font("Arial"), px(14.), Some(px(6.)));
1218        let mut previous = Rope::new();
1219        for value in ["abcd的", "abcd的s", "abcd的ss", "abcd的s", "abcd的"] {
1220            let text = Rope::from(value);
1221            let start = previous.len_bytes().min(text.len_bytes());
1222            let inserted = Rope::from(text.byte_slice(start..).to_string());
1223            wrapper._update(
1224                &text,
1225                &(start..previous.len_bytes()),
1226                &inserted,
1227                &mut |line, width, _| {
1228                    measured_wrap_boundaries(line, width, WrappingIndent::None, measure)
1229                },
1230            );
1231            let expected = if value.ends_with('s') {
1232                vec![0..7, 7..value.len()]
1233            } else {
1234                vec![0..7]
1235            };
1236            assert_eq!(wrapper.line(0).unwrap().wrapped_lines.as_slice(), expected);
1237            let cursor = wrapper.offset_to_display_point(value.len());
1238            assert_eq!(cursor.row, usize::from(value.ends_with('s')));
1239            previous = text;
1240        }
1241    }
1242
1243    #[test]
1244    fn measured_wrap_preserves_words_graphemes_and_indentation() {
1245        let wrap = |text: &str, width, indent| {
1246            measured_wrap_boundaries(text, px(width), indent, |s| {
1247                px(s.graphemes(true).count() as f32)
1248            })
1249            .into_iter()
1250            .map(|b| b.ix)
1251            .collect::<Vec<_>>()
1252        };
1253        assert_eq!(wrap("hello world", 8., WrappingIndent::None), vec![6]);
1254        assert_eq!(wrap("a👩‍💻b", 2., WrappingIndent::None), vec!["a👩‍💻".len()]);
1255        assert_eq!(wrap("  abcdefgh", 5., WrappingIndent::Same), vec![5, 8]);
1256        assert_eq!(wrap("  abcdefgh", 5., WrappingIndent::None), vec![2, 7]);
1257        assert!(wrap("", 0., WrappingIndent::None).is_empty());
1258        assert_eq!(wrap("abc", 0., WrappingIndent::None), vec![1, 2]);
1259        // Closing punctuation stays with the preceding Chinese character.
1260        assert_eq!(wrap("你好,世界", 2., WrappingIndent::None), vec![3, 9]);
1261    }
1262
1263    #[test]
1264    fn test_update() {
1265        let font = wgpui::Font {
1266            family: "Arial".into(),
1267            weight: FontWeight::default(),
1268            style: FontStyle::Normal,
1269            features: FontFeatures::default(),
1270            fallbacks: None,
1271        };
1272
1273        let mut wrapper = TextWrapper::new(font, px(14.), None);
1274        let mut text = Rope::from(
1275            "Hello, 世界!\r\nThis is second line.\nThis is third line.\n这里是第 4 行。",
1276        );
1277
1278        fn fake_wrap_line(_line: &str, _wrap_width: Pixels, _: usize) -> Vec<Boundary> {
1279            vec![]
1280        }
1281
1282        #[track_caller]
1283        fn assert_wrapper_lines(text: &Rope, wrapper: &TextWrapper, expected_lines: &[&[&str]]) {
1284            let mut actual_lines = vec![];
1285            let mut offset = 0;
1286            for line in wrapper.iter_lines() {
1287                actual_lines.push(
1288                    line.wrapped_lines
1289                        .iter()
1290                        .map(|range| text.byte_slice(offset + range.start..offset + range.end))
1291                        .collect::<Vec<_>>(),
1292                );
1293                // +1 \n
1294                offset += line.len() + 1;
1295            }
1296            assert_eq!(actual_lines, expected_lines);
1297        }
1298
1299        wrapper._update(&text, &(0..text.len_bytes()), &text, &mut fake_wrap_line);
1300        assert_eq!(wrapper.lines_count(), 4);
1301        assert_wrapper_lines(
1302            &text,
1303            &wrapper,
1304            &[
1305                &["Hello, 世界!\r"],
1306                &["This is second line."],
1307                &["This is third line."],
1308                &["这里是第 4 行。"],
1309            ],
1310        );
1311
1312        // Add a new text to end
1313        let range = text.len_bytes()..text.len_bytes();
1314        let new_text = "New text";
1315        text.replace(range.clone(), new_text);
1316        wrapper._update(&text, &range, &Rope::from(new_text), &mut fake_wrap_line);
1317        assert_eq!(
1318            text.to_string(),
1319            "Hello, 世界!\r\nThis is second line.\nThis is third line.\n这里是第 4 行。New text"
1320        );
1321        assert_eq!(wrapper.lines_count(), 4);
1322        assert_eq!(wrapper.lines_count(), 4);
1323        assert_wrapper_lines(
1324            &text,
1325            &wrapper,
1326            &[
1327                &["Hello, 世界!\r"],
1328                &["This is second line."],
1329                &["This is third line."],
1330                &["这里是第 4 行。New text"],
1331            ],
1332        );
1333
1334        // Replace first line `Hello` to `AAA`
1335        let range = 0..5;
1336        let new_text = "AAA";
1337        text.replace(range.clone(), new_text);
1338        wrapper._update(&text, &range, &Rope::from(new_text), &mut fake_wrap_line);
1339        assert_eq!(
1340            text.to_string(),
1341            "AAA, 世界!\r\nThis is second line.\nThis is third line.\n这里是第 4 行。New text"
1342        );
1343        assert_eq!(wrapper.lines_count(), 4);
1344        assert_wrapper_lines(
1345            &text,
1346            &wrapper,
1347            &[
1348                &["AAA, 世界!\r"],
1349                &["This is second line."],
1350                &["This is third line."],
1351                &["这里是第 4 行。New text"],
1352            ],
1353        );
1354
1355        // Remove the second line
1356        let start_offset = text.line_start_offset(1);
1357        let end_offset = text.line_end_offset(1);
1358        let range = start_offset..end_offset + 1;
1359        text.replace(range.clone(), "");
1360        wrapper._update(&text, &range, &Rope::from(""), &mut fake_wrap_line);
1361        assert_eq!(
1362            text.to_string(),
1363            "AAA, 世界!\r\nThis is third line.\n这里是第 4 行。New text"
1364        );
1365        assert_eq!(wrapper.lines_count(), 3);
1366        assert_wrapper_lines(
1367            &text,
1368            &wrapper,
1369            &[
1370                &["AAA, 世界!\r"],
1371                &["This is third line."],
1372                &["这里是第 4 行。New text"],
1373            ],
1374        );
1375
1376        // Replace the first 2 lines to "This is a new line."
1377        let range = text.line_start_offset(0)..text.line_end_offset(1) + 1;
1378        let new_text = "This is a new line.\nThis is new line 2.\n";
1379        text.replace(range.clone(), new_text);
1380        wrapper._update(&text, &range, &Rope::from(new_text), &mut fake_wrap_line);
1381        assert_eq!(
1382            text.to_string(),
1383            "This is a new line.\nThis is new line 2.\n这里是第 4 行。New text"
1384        );
1385        assert_eq!(wrapper.lines_count(), 3);
1386        assert_wrapper_lines(
1387            &text,
1388            &wrapper,
1389            &[
1390                &["This is a new line."],
1391                &["This is new line 2."],
1392                &["这里是第 4 行。New text"],
1393            ],
1394        );
1395
1396        // Add a new line at the end
1397        let range = text.len_bytes()..text.len_bytes();
1398        let new_text = "\nThis is a new line at the end.";
1399        text.replace(range.clone(), new_text);
1400        wrapper._update(&text, &range, &Rope::from(new_text), &mut fake_wrap_line);
1401        assert_eq!(
1402            text.to_string(),
1403            "This is a new line.\nThis is new line 2.\n这里是第 4 行。New text\nThis is a new line at the end."
1404        );
1405        assert_eq!(wrapper.lines_count(), 4);
1406        assert_wrapper_lines(
1407            &text,
1408            &wrapper,
1409            &[
1410                &["This is a new line."],
1411                &["This is new line 2."],
1412                &["这里是第 4 行。New text"],
1413                &["This is a new line at the end."],
1414            ],
1415        );
1416
1417        // Add a new line at the beginning
1418        let range = 0..0;
1419        let new_text = "This is a new line at the beginning.\n";
1420        text.replace(range.clone(), new_text);
1421        wrapper._update(&text, &range, &Rope::from(new_text), &mut fake_wrap_line);
1422        assert_eq!(
1423            text.to_string(),
1424            "This is a new line at the beginning.\nThis is a new line.\nThis is new line 2.\n这里是第 4 行。New text\nThis is a new line at the end."
1425        );
1426        assert_eq!(wrapper.lines_count(), 5);
1427        assert_wrapper_lines(
1428            &text,
1429            &wrapper,
1430            &[
1431                &["This is a new line at the beginning."],
1432                &["This is a new line."],
1433                &["This is new line 2."],
1434                &["这里是第 4 行。New text"],
1435                &["This is a new line at the end."],
1436            ],
1437        );
1438
1439        // Remove all to at least one line in `lines`.
1440        let range = 0..text.len_bytes();
1441        let new_text = "";
1442        text.replace(range.clone(), new_text);
1443        wrapper._update(&text, &range, &Rope::from(new_text), &mut fake_wrap_line);
1444        assert_eq!(text.to_string(), "");
1445        assert_eq!(wrapper.lines_count(), 1);
1446        assert_eq!(wrapper.line(0).unwrap().wrapped_lines.as_slice(), [0..0]);
1447
1448        // Test update_all
1449        let range = 0..text.len_bytes();
1450        let new_text = "This is a full text.\nThis is a second line.";
1451        text.replace(range.clone(), new_text);
1452        wrapper._update(&text, &range, &text, &mut fake_wrap_line);
1453        assert_eq!(
1454            text.to_string(),
1455            "This is a full text.\nThis is a second line."
1456        );
1457        assert_eq!(wrapper.lines_count(), 2);
1458    }
1459
1460    fn test_font() -> wgpui::Font {
1461        wgpui::Font {
1462            family: "Arial".into(),
1463            weight: FontWeight::default(),
1464            style: FontStyle::Normal,
1465            features: FontFeatures::default(),
1466            fallbacks: None,
1467        }
1468    }
1469
1470    /// The longest-row summary stays exact when the previously-longest line is shrunk.
1471    #[test]
1472    fn test_longest_row_after_shrink() {
1473        let mut wrapper = TextWrapper::new(test_font(), px(14.), None);
1474        let mut text = Rope::from("aa\nthis is the longest line\nbb");
1475        wrapper._update(&text, &(0..text.len_bytes()), &text, &mut |_, _, _| vec![]);
1476        assert_eq!(wrapper.longest_row(), 1);
1477
1478        // Shrink line 1 so line 2-equivalent isn't longest.
1479        // Make line 0 the longest now.
1480        let start = text.line_start_offset(0);
1481        let end = text.line_end_offset(0);
1482        let range = start..end;
1483        let new_text = "a very very long first line now";
1484        text.replace(range.clone(), new_text);
1485        wrapper._update(&text, &range, &Rope::from(new_text), &mut |_, _, _| vec![]);
1486        assert_eq!(wrapper.longest_row(), 0);
1487    }
1488
1489    /// Editing the last line and deleting everything must keep the tree consistent.
1490    #[test]
1491    fn test_edit_last_line_and_full_delete() {
1492        let mut wrapper = TextWrapper::new(test_font(), px(14.), None);
1493        let mut text = Rope::from("one\ntwo\nthree");
1494        wrapper._update(&text, &(0..text.len_bytes()), &text, &mut |_, _, _| vec![]);
1495        assert_eq!(wrapper.lines_count(), 3);
1496
1497        // Replace the last line only.
1498        let start = text.line_start_offset(2);
1499        let range = start..text.len_bytes();
1500        let new_text = "THREE EDITED";
1501        text.replace(range.clone(), new_text);
1502        wrapper._update(&text, &range, &Rope::from(new_text), &mut |_, _, _| vec![]);
1503        assert_eq!(wrapper.lines_count(), 3);
1504        assert_eq!(wrapper.line(2).unwrap().len(), "THREE EDITED".len());
1505
1506        // Delete everything.
1507        let range = 0..text.len_bytes();
1508        text.replace(range.clone(), "");
1509        wrapper._update(&text, &range, &Rope::from(""), &mut |_, _, _| vec![]);
1510        assert_eq!(wrapper.lines_count(), 1);
1511        assert_eq!(wrapper.len(), 1);
1512        assert_eq!(wrapper.line(0).unwrap().wrapped_lines.as_slice(), [0..0]);
1513    }
1514
1515    #[test]
1516    fn test_wrap_row_buffer_line_boundaries() {
1517        let mut wrapper = TextWrapper::new(test_font(), px(14.), None);
1518        wrapper.text = Rope::from("aa\nbbbb\nc");
1519        wrapper.lines = SumTree::from_iter(
1520            vec![
1521                LineItem {
1522                    len: 2,
1523                    indent: 0,
1524                    wrapped_lines: smallvec::smallvec![0..2],
1525                },
1526                LineItem {
1527                    len: 4,
1528                    indent: 0,
1529                    wrapped_lines: smallvec::smallvec![0..2, 2..4],
1530                },
1531                LineItem {
1532                    len: 1,
1533                    indent: 0,
1534                    wrapped_lines: smallvec::smallvec![0..1],
1535                },
1536            ],
1537            &(),
1538        );
1539
1540        assert_eq!(wrapper.lines_count(), 3);
1541        assert_eq!(wrapper.len(), 4);
1542
1543        assert_eq!(wrapper.buffer_line_to_first_wrap_row(0), 0);
1544        assert_eq!(wrapper.buffer_line_to_first_wrap_row(1), 1);
1545        assert_eq!(wrapper.buffer_line_to_first_wrap_row(2), 3);
1546        assert_eq!(wrapper.buffer_line_to_first_wrap_row(3), 4);
1547
1548        assert_eq!(wrapper.buffer_line_to_wrap_row_range(0), 0..1);
1549        assert_eq!(wrapper.buffer_line_to_wrap_row_range(1), 1..3);
1550        assert_eq!(wrapper.buffer_line_to_wrap_row_range(2), 3..4);
1551        assert_eq!(wrapper.buffer_line_to_wrap_row_range(3), 4..4);
1552
1553        assert_eq!(wrapper.wrap_row_to_buffer_line(0), 0);
1554        assert_eq!(wrapper.wrap_row_to_buffer_line(1), 1);
1555        assert_eq!(wrapper.wrap_row_to_buffer_line(2), 1);
1556        assert_eq!(wrapper.wrap_row_to_buffer_line(3), 2);
1557        assert_eq!(wrapper.wrap_row_to_buffer_line(4), 2);
1558    }
1559
1560    #[test]
1561    fn test_wrap_row_queries_after_incremental_splice() {
1562        let mut wrapper = TextWrapper::new(test_font(), px(14.), Some(px(10.)));
1563        let mut text = Rope::from("aa\nbbbb\nc");
1564        let mut fake_wrap_line = |line: &str, _wrap_width: Pixels, _: usize| {
1565            if line.len() > 2 {
1566                vec![Boundary {
1567                    ix: 2,
1568                    next_indent: 0,
1569                }]
1570            } else {
1571                vec![]
1572            }
1573        };
1574
1575        wrapper._update(&text, &(0..text.len_bytes()), &text, &mut fake_wrap_line);
1576        assert_eq!(wrapper.buffer_line_to_wrap_row_range(0), 0..1);
1577        assert_eq!(wrapper.buffer_line_to_wrap_row_range(1), 1..3);
1578        assert_eq!(wrapper.buffer_line_to_wrap_row_range(2), 3..4);
1579
1580        let range = text.line_start_offset(1)..text.line_end_offset(1);
1581        let new_text = "dd\neeee";
1582        text.replace(range.clone(), new_text);
1583        wrapper._update(&text, &range, &Rope::from(new_text), &mut fake_wrap_line);
1584
1585        assert_eq!(wrapper.lines_count(), 4);
1586        assert_eq!(wrapper.len(), 5);
1587        assert_eq!(wrapper.buffer_line_to_wrap_row_range(0), 0..1);
1588        assert_eq!(wrapper.buffer_line_to_wrap_row_range(1), 1..2);
1589        assert_eq!(wrapper.buffer_line_to_wrap_row_range(2), 2..4);
1590        assert_eq!(wrapper.buffer_line_to_wrap_row_range(3), 4..5);
1591        assert_eq!(wrapper.wrap_row_to_buffer_line(0), 0);
1592        assert_eq!(wrapper.wrap_row_to_buffer_line(1), 1);
1593        assert_eq!(wrapper.wrap_row_to_buffer_line(2), 2);
1594        assert_eq!(wrapper.wrap_row_to_buffer_line(3), 2);
1595        assert_eq!(wrapper.wrap_row_to_buffer_line(4), 3);
1596    }
1597
1598    #[test]
1599    fn test_line_layout() {
1600        let mut line_layout = LineLayout::new();
1601
1602        let line1 = ShapedLine::default().with_len(100);
1603        let line2 = ShapedLine::default().with_len(50);
1604        let wrapped_lines = smallvec::smallvec![line1, line2];
1605        line_layout.set_wrapped_lines(wrapped_lines);
1606        assert_eq!(line_layout.len(), 150);
1607        assert_eq!(line_layout.wrapped_lines.len(), 2);
1608    }
1609
1610    /// A layout context whose only load-bearing field is the line height.
1611    fn test_last_layout(line_height: Pixels) -> LastLayout {
1612        LastLayout {
1613            visible_range: 0..1,
1614            visible_buffer_lines: vec![0],
1615            visible_line_byte_offsets: vec![0],
1616            visible_top: px(0.),
1617            visible_range_offset: 0..0,
1618            lines: Rc::new(vec![]),
1619            line_height,
1620            wrap_width: None,
1621            wrapping_indent: WrappingIndent::default(),
1622            line_number_width: px(0.),
1623            space_width: px(0.),
1624            cursor_bounds: None,
1625            text_align: TextAlign::Left,
1626            content_width: px(0.),
1627        }
1628    }
1629
1630    #[test]
1631    fn test_position_for_index_prefers_first_leading_empty_visual_line() {
1632        let mut line_layout = LineLayout::new();
1633        line_layout.set_wrapped_lines(smallvec::smallvec![
1634            ShapedLine::default(),
1635            ShapedLine::default(),
1636            ShapedLine::default().with_len(3),
1637        ]);
1638
1639        assert_eq!(
1640            line_layout.position_for_index(0, &test_last_layout(px(20.)), false),
1641            Some(point(px(0.), px(0.)))
1642        );
1643    }
1644
1645    #[test]
1646    fn clicking_past_a_wrapped_row_keeps_the_caret_on_that_row() {
1647        // One buffer line wrapped into two visual rows, splitting at byte 10.
1648        let mut line_layout = LineLayout::new();
1649        line_layout.set_wrapped_lines(smallvec::smallvec![
1650            ShapedLine::default().with_len(10),
1651            ShapedLine::default().with_len(5),
1652        ]);
1653        let last_layout = test_last_layout(px(20.));
1654
1655        // Clicking past the last glyph of the first row resolves to the wrap boundary, which is
1656        // also the first offset of the second row -- hence the affinity.
1657        let (ix, line_end_affinity) = line_layout
1658            .closest_index_for_position(point(px(999.), px(5.)), &last_layout)
1659            .unwrap();
1660        assert_eq!(ix, 10);
1661        assert!(line_end_affinity);
1662
1663        // Carrying that affinity is what keeps the caret on the row that was clicked; dropping it
1664        // is the bug -- the caret shows up one row below the pointer.
1665        assert_eq!(
1666            line_layout
1667                .position_for_index(ix, &last_layout, line_end_affinity)
1668                .map(|pos| pos.y),
1669            Some(px(0.))
1670        );
1671        assert_eq!(
1672            line_layout
1673                .position_for_index(ix, &last_layout, false)
1674                .map(|pos| pos.y),
1675            Some(px(20.))
1676        );
1677
1678        // The final row owns the end of the line outright, so there is nothing to disambiguate.
1679        let (ix, line_end_affinity) = line_layout
1680            .closest_index_for_position(point(px(999.), px(25.)), &last_layout)
1681            .unwrap();
1682        assert_eq!(ix, 15);
1683        assert!(!line_end_affinity);
1684    }
1685
1686    #[test]
1687    fn a_wrap_boundary_offset_resolves_to_the_row_the_caret_is_drawn_on() {
1688        let mut wrapper = TextWrapper::new(test_font(), px(14.), None);
1689        wrapper.text = Rope::from("first line\nthis one wraps");
1690        wrapper.lines = SumTree::from_iter(
1691            vec![
1692                LineItem {
1693                    len: Rope::from("first line").len_bytes(),
1694                    indent: 0,
1695                    wrapped_lines: smallvec::smallvec![0..10],
1696                },
1697                LineItem {
1698                    len: Rope::from("this one wraps").len_bytes(),
1699                    indent: 0,
1700                    wrapped_lines: smallvec::smallvec![0..9, 9..14],
1701                },
1702            ],
1703            &(),
1704        );
1705
1706        // Offset 20 is the wrap boundary of the second buffer line: 11 (line start) + 9.
1707        // It is the last offset of wrap row 1 and the first of wrap row 2 at the same time, so
1708        // only the affinity can say which row a vertical move should step away from.
1709        assert_eq!(
1710            wrapper.offset_to_display_point_with_affinity(20, true),
1711            WrapDisplayPoint::new(1, 0, 9)
1712        );
1713        assert_eq!(
1714            wrapper.offset_to_display_point_with_affinity(20, false),
1715            WrapDisplayPoint::new(2, 1, 0)
1716        );
1717        assert_eq!(
1718            wrapper.offset_to_display_point(20),
1719            wrapper.offset_to_display_point_with_affinity(20, false)
1720        );
1721
1722        // An offset that is not on a boundary is unaffected either way.
1723        for line_end_affinity in [false, true] {
1724            assert_eq!(
1725                wrapper.offset_to_display_point_with_affinity(23, line_end_affinity),
1726                WrapDisplayPoint::new(2, 1, 3)
1727            );
1728        }
1729    }
1730
1731    #[test]
1732    fn test_offset_to_display_point() {
1733        let font = wgpui::Font {
1734            family: "Arial".into(),
1735            weight: FontWeight::default(),
1736            style: FontStyle::Normal,
1737            features: FontFeatures::default(),
1738            fallbacks: None,
1739        };
1740
1741        let mut wrapper = TextWrapper::new(font, px(14.), None);
1742        wrapper.text = Rope::from(
1743            "Hello, 世界!\r\nThis is second line.\nThis is third line.\n这里是第 4 行。",
1744        );
1745        wrapper.lines = SumTree::from_iter(
1746            vec![
1747                // range: 0..15
1748                LineItem {
1749                    len: Rope::from("Hello, 世界!\r").len_bytes(),
1750                    indent: 0,
1751                    wrapped_lines: smallvec::smallvec![0..15],
1752                },
1753                // range: 16..36
1754                LineItem {
1755                    len: Rope::from("This is second line.\n").len_bytes(),
1756                    indent: 0,
1757                    wrapped_lines: smallvec::smallvec![0..10, 10..20],
1758                },
1759                // range: 37..56
1760                LineItem {
1761                    len: Rope::from("This is third line.\n").len_bytes(),
1762                    indent: 0,
1763                    wrapped_lines: smallvec::smallvec![0..9, 9..15, 15..20],
1764                },
1765                // range: 57..79
1766                LineItem {
1767                    len: Rope::from("这里是第 4 行。").len_bytes(),
1768                    indent: 0,
1769                    wrapped_lines: smallvec::smallvec![0..22],
1770                },
1771            ],
1772            &(),
1773        );
1774
1775        assert_eq!(
1776            wrapper.offset_to_display_point(12),
1777            WrapDisplayPoint::new(0, 0, 12)
1778        );
1779        assert_eq!(
1780            wrapper.offset_to_display_point(15),
1781            WrapDisplayPoint::new(0, 0, 15)
1782        );
1783
1784        assert_eq!(
1785            wrapper.offset_to_display_point(16),
1786            WrapDisplayPoint::new(1, 0, 0)
1787        );
1788        assert_eq!(
1789            wrapper.offset_to_display_point(21),
1790            WrapDisplayPoint::new(1, 0, 5)
1791        );
1792        assert_eq!(
1793            wrapper.offset_to_display_point(27),
1794            WrapDisplayPoint::new(2, 1, 1)
1795        );
1796        assert_eq!(
1797            wrapper.offset_to_display_point(37),
1798            WrapDisplayPoint::new(3, 0, 0)
1799        );
1800        assert_eq!(
1801            wrapper.offset_to_display_point(54),
1802            WrapDisplayPoint::new(5, 2, 2)
1803        );
1804        assert_eq!(
1805            wrapper.offset_to_display_point(59),
1806            WrapDisplayPoint::new(6, 0, 2)
1807        );
1808
1809        assert_eq!(
1810            wrapper.display_point_to_offset(WrapDisplayPoint::new(6, 0, 2)),
1811            59
1812        );
1813        assert_eq!(
1814            wrapper.display_point_to_offset(WrapDisplayPoint::new(5, 2, 2)),
1815            54
1816        );
1817        assert_eq!(
1818            wrapper.display_point_to_offset(WrapDisplayPoint::new(3, 0, 0)),
1819            37
1820        );
1821        assert_eq!(
1822            wrapper.display_point_to_offset(WrapDisplayPoint::new(2, 1, 1)),
1823            27
1824        );
1825        assert_eq!(
1826            wrapper.display_point_to_offset(WrapDisplayPoint::new(1, 0, 5)),
1827            21
1828        );
1829        assert_eq!(
1830            wrapper.display_point_to_offset(WrapDisplayPoint::new(1, 0, 0)),
1831            16
1832        );
1833        assert_eq!(
1834            wrapper.display_point_to_offset(WrapDisplayPoint::new(0, 0, 15)),
1835            15
1836        );
1837    }
1838
1839    #[test]
1840    fn test_wrapping_indent_same_keeps_indent_reserved() {
1841        let mut wrapper = TextWrapper::new(test_font(), px(14.0), Some(px(10.)));
1842        wrapper.wrapping_indent = WrappingIndent::Same;
1843        let text = Rope::from("  abcdefghijklmnopqrstuv");
1844        let mut fake_wrap_line = |line: &str, _wrap_width: Pixels, _: usize| {
1845            if line.starts_with(' ') {
1846                vec![Boundary {
1847                    ix: 5,
1848                    next_indent: 2,
1849                }]
1850            } else {
1851                let mut boundaries = vec![];
1852                let mut i = 8;
1853                while i < line.len() {
1854                    boundaries.push(Boundary {
1855                        ix: i,
1856                        next_indent: 0,
1857                    });
1858                    i += 8;
1859                }
1860                boundaries
1861            }
1862        };
1863
1864        wrapper._update(&text, &(0..text.len_bytes()), &text, &mut fake_wrap_line);
1865
1866        let line = wrapper.line(0).unwrap();
1867        assert_eq!(line.indent, 2);
1868        assert_eq!(line.wrapped_lines.as_slice(), [0..5, 5..24]);
1869    }
1870
1871    #[test]
1872    fn test_wrapping_indent_none_continuation_lines_wrapped_at_full_width() {
1873        let mut wrapper = TextWrapper::new(test_font(), px(14.0), Some(px(10.)));
1874        wrapper.wrapping_indent = WrappingIndent::None;
1875        let text = Rope::from("  abcdefghijklmnopqrstuv");
1876        let mut fake_wrap_line = |line: &str, _wrap_width: Pixels, _: usize| {
1877            if line.starts_with(' ') {
1878                vec![Boundary {
1879                    ix: 5,
1880                    next_indent: 2,
1881                }]
1882            } else {
1883                let mut boundaries = vec![];
1884                let mut i = 8;
1885                while i < line.len() {
1886                    boundaries.push(Boundary {
1887                        ix: i,
1888                        next_indent: 0,
1889                    });
1890                    i += 8;
1891                }
1892                boundaries
1893            }
1894        };
1895
1896        wrapper._update(&text, &(0..text.len_bytes()), &text, &mut fake_wrap_line);
1897
1898        let line = wrapper.line(0).unwrap();
1899        assert_eq!(line.indent, 0);
1900        assert_eq!(line.wrapped_lines.as_slice(), [0..5, 5..13, 13..21, 21..24]);
1901    }
1902
1903    #[test]
1904    fn test_wrap_indent_offsets_continuation_lines() {
1905        let mut line_layout = LineLayout::new();
1906        line_layout.set_wrapped_lines(smallvec::smallvec![
1907            ShapedLine::default().with_len(5),
1908            ShapedLine::default().with_len(10),
1909        ]);
1910
1911        line_layout = line_layout.wrap_indent(px(20.0));
1912
1913        let last_layout = LastLayout {
1914            visible_range: 0..1,
1915            visible_buffer_lines: vec![0],
1916            visible_line_byte_offsets: vec![0],
1917            visible_top: px(0.),
1918            visible_range_offset: 0..0,
1919            lines: Rc::new(vec![]),
1920            line_height: px(20.0),
1921            wrap_width: Some(px(10.)),
1922            wrapping_indent: WrappingIndent::Same,
1923            line_number_width: px(0.),
1924            space_width: px(0.),
1925            cursor_bounds: None,
1926            text_align: TextAlign::Left,
1927            content_width: px(0.),
1928        };
1929
1930        assert_eq!(
1931            line_layout.position_for_index(0, &last_layout, false),
1932            Some(point(px(0.), px(0.))),
1933        );
1934
1935        assert_eq!(
1936            line_layout.position_for_index(6, &last_layout, false),
1937            Some(point(px(20.), px(20.))),
1938        )
1939    }
1940}