line_wrapper.rs

  1use crate::{FontId, Pixels, SharedString, TextRun, TextSystem, px};
  2use collections::HashMap;
  3use std::{borrow::Cow, iter, sync::Arc};
  4
  5/// Determines whether to truncate text from the start or end.
  6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
  7pub enum TruncateFrom {
  8    /// Truncate text from the start.
  9    Start,
 10    /// Truncate text from the end.
 11    End,
 12}
 13
 14/// The GPUI line wrapper, used to wrap lines of text to a given width.
 15pub struct LineWrapper {
 16    text_system: Arc<TextSystem>,
 17    pub(crate) font_id: FontId,
 18    pub(crate) font_size: Pixels,
 19    cached_ascii_char_widths: [Option<Pixels>; 128],
 20    cached_other_char_widths: HashMap<char, Pixels>,
 21}
 22
 23impl LineWrapper {
 24    /// The maximum indent that can be applied to a line.
 25    pub const MAX_INDENT: u32 = 256;
 26
 27    pub(crate) fn new(font_id: FontId, font_size: Pixels, text_system: Arc<TextSystem>) -> Self {
 28        Self {
 29            text_system,
 30            font_id,
 31            font_size,
 32            cached_ascii_char_widths: [None; 128],
 33            cached_other_char_widths: HashMap::default(),
 34        }
 35    }
 36
 37    /// Wrap a line of text to the given width with this wrapper's font and font size.
 38    pub fn wrap_line<'a>(
 39        &'a mut self,
 40        fragments: &'a [LineFragment],
 41        wrap_width: Pixels,
 42    ) -> impl Iterator<Item = Boundary> + 'a {
 43        let mut width = px(0.);
 44        let mut first_non_whitespace_ix = None;
 45        let mut indent = None;
 46        let mut last_candidate_ix = 0;
 47        let mut last_candidate_width = px(0.);
 48        let mut last_wrap_ix = 0;
 49        let mut prev_c = '\0';
 50        let mut index = 0;
 51        let mut candidates = fragments
 52            .iter()
 53            .flat_map(move |fragment| fragment.wrap_boundary_candidates())
 54            .peekable();
 55        iter::from_fn(move || {
 56            for candidate in candidates.by_ref() {
 57                let ix = index;
 58                index += candidate.len_utf8();
 59                let mut new_prev_c = prev_c;
 60                let item_width = match candidate {
 61                    WrapBoundaryCandidate::Char { character: c } => {
 62                        if c == '\n' {
 63                            continue;
 64                        }
 65
 66                        if Self::is_word_char(c) {
 67                            if prev_c == ' ' && c != ' ' && first_non_whitespace_ix.is_some() {
 68                                last_candidate_ix = ix;
 69                                last_candidate_width = width;
 70                            }
 71                        } else {
 72                            // CJK may not be space separated, e.g.: `Hello world你好世界`
 73                            if c != ' ' && first_non_whitespace_ix.is_some() {
 74                                last_candidate_ix = ix;
 75                                last_candidate_width = width;
 76                            }
 77                        }
 78
 79                        if c != ' ' && first_non_whitespace_ix.is_none() {
 80                            first_non_whitespace_ix = Some(ix);
 81                        }
 82
 83                        new_prev_c = c;
 84
 85                        self.width_for_char(c)
 86                    }
 87                    WrapBoundaryCandidate::Element {
 88                        width: element_width,
 89                        ..
 90                    } => {
 91                        if prev_c == ' ' && first_non_whitespace_ix.is_some() {
 92                            last_candidate_ix = ix;
 93                            last_candidate_width = width;
 94                        }
 95
 96                        if first_non_whitespace_ix.is_none() {
 97                            first_non_whitespace_ix = Some(ix);
 98                        }
 99
100                        element_width
101                    }
102                };
103
104                width += item_width;
105                if width > wrap_width && ix > last_wrap_ix {
106                    if let (None, Some(first_non_whitespace_ix)) = (indent, first_non_whitespace_ix)
107                    {
108                        indent = Some(
109                            Self::MAX_INDENT.min((first_non_whitespace_ix - last_wrap_ix) as u32),
110                        );
111                    }
112
113                    if last_candidate_ix > 0 {
114                        last_wrap_ix = last_candidate_ix;
115                        width -= last_candidate_width;
116                        last_candidate_ix = 0;
117                    } else {
118                        last_wrap_ix = ix;
119                        width = item_width;
120                    }
121
122                    if let Some(indent) = indent {
123                        width += self.width_for_char(' ') * indent as f32;
124                    }
125
126                    return Some(Boundary::new(last_wrap_ix, indent.unwrap_or(0)));
127                }
128
129                prev_c = new_prev_c;
130            }
131
132            None
133        })
134    }
135
136    /// Determines if a line should be truncated based on its width.
137    ///
138    /// Returns the truncation index in `line`.
139    pub fn should_truncate_line(
140        &mut self,
141        line: &str,
142        truncate_width: Pixels,
143        truncation_affix: &str,
144        truncate_from: TruncateFrom,
145    ) -> Option<usize> {
146        let mut width = px(0.);
147        let suffix_width = truncation_affix
148            .chars()
149            .map(|c| self.width_for_char(c))
150            .fold(px(0.0), |a, x| a + x);
151        let mut truncate_ix = 0;
152
153        match truncate_from {
154            TruncateFrom::Start => {
155                for (ix, c) in line.char_indices().rev() {
156                    if width + suffix_width < truncate_width {
157                        truncate_ix = ix;
158                    }
159
160                    let char_width = self.width_for_char(c);
161                    width += char_width;
162
163                    if width.floor() > truncate_width {
164                        return Some(truncate_ix);
165                    }
166                }
167            }
168            TruncateFrom::End => {
169                for (ix, c) in line.char_indices() {
170                    if width + suffix_width < truncate_width {
171                        truncate_ix = ix;
172                    }
173
174                    let char_width = self.width_for_char(c);
175                    width += char_width;
176
177                    if width.floor() > truncate_width {
178                        return Some(truncate_ix);
179                    }
180                }
181            }
182        }
183
184        None
185    }
186
187    /// Truncate a line of text to the given width with this wrapper's font and font size.
188    pub fn truncate_line<'a>(
189        &mut self,
190        line: SharedString,
191        truncate_width: Pixels,
192        truncation_affix: &str,
193        runs: &'a [TextRun],
194        truncate_from: TruncateFrom,
195    ) -> (SharedString, Cow<'a, [TextRun]>) {
196        if let Some(truncate_ix) =
197            self.should_truncate_line(&line, truncate_width, truncation_affix, truncate_from)
198        {
199            let result = match truncate_from {
200                TruncateFrom::Start => SharedString::from(format!(
201                    "{truncation_affix}{}",
202                    &line[line.ceil_char_boundary(truncate_ix + 1)..]
203                )),
204                TruncateFrom::End => {
205                    SharedString::from(format!("{}{truncation_affix}", &line[..truncate_ix]))
206                }
207            };
208            let mut runs = runs.to_vec();
209            update_runs_after_truncation(&result, truncation_affix, &mut runs, truncate_from);
210            (result, Cow::Owned(runs))
211        } else {
212            (line, Cow::Borrowed(runs))
213        }
214    }
215
216    /// Any character in this list should be treated as a word character,
217    /// meaning it can be part of a word that should not be wrapped.
218    pub(crate) fn is_word_char(c: char) -> bool {
219        // ASCII alphanumeric characters, for English, numbers: `Hello123`, etc.
220        c.is_ascii_alphanumeric() ||
221        // Latin script in Unicode for French, German, Spanish, etc.
222        // Latin-1 Supplement
223        // https://en.wikipedia.org/wiki/Latin-1_Supplement
224        matches!(c, '\u{00C0}'..='\u{00FF}') ||
225        // Latin Extended-A
226        // https://en.wikipedia.org/wiki/Latin_Extended-A
227        matches!(c, '\u{0100}'..='\u{017F}') ||
228        // Latin Extended-B
229        // https://en.wikipedia.org/wiki/Latin_Extended-B
230        matches!(c, '\u{0180}'..='\u{024F}') ||
231        // Cyrillic for Russian, Ukrainian, etc.
232        // https://en.wikipedia.org/wiki/Cyrillic_script_in_Unicode
233        matches!(c, '\u{0400}'..='\u{04FF}') ||
234
235        // Vietnamese (https://vietunicode.sourceforge.net/charset/)
236        matches!(c, '\u{1E00}'..='\u{1EFF}') || // Latin Extended Additional
237        matches!(c, '\u{0300}'..='\u{036F}') || // Combining Diacritical Marks
238
239        // Some other known special characters that should be treated as word characters,
240        // e.g. `a-b`, `var_name`, `I'm`, '@mention`, `#hashtag`, `100%`, `3.1415`,
241        // `2^3`, `a~b`, `a=1`, `Self::new`, etc.
242        matches!(c, '-' | '_' | '.' | '\'' | '$' | '%' | '@' | '#' | '^' | '~' | ',' | '=' | ':') ||
243        // `⋯` character is special used in Zed, to keep this at the end of the line.
244        matches!(c, '⋯')
245    }
246
247    #[inline(always)]
248    fn width_for_char(&mut self, c: char) -> Pixels {
249        if (c as u32) < 128 {
250            if let Some(cached_width) = self.cached_ascii_char_widths[c as usize] {
251                cached_width
252            } else {
253                let width = self
254                    .text_system
255                    .layout_width(self.font_id, self.font_size, c);
256                self.cached_ascii_char_widths[c as usize] = Some(width);
257                width
258            }
259        } else if let Some(cached_width) = self.cached_other_char_widths.get(&c) {
260            *cached_width
261        } else {
262            let width = self
263                .text_system
264                .layout_width(self.font_id, self.font_size, c);
265            self.cached_other_char_widths.insert(c, width);
266            width
267        }
268    }
269}
270
271fn update_runs_after_truncation(
272    result: &str,
273    ellipsis: &str,
274    runs: &mut Vec<TextRun>,
275    truncate_from: TruncateFrom,
276) {
277    let mut truncate_at = result.len() - ellipsis.len();
278    match truncate_from {
279        TruncateFrom::Start => {
280            for (run_index, run) in runs.iter_mut().enumerate().rev() {
281                if run.len <= truncate_at {
282                    truncate_at -= run.len;
283                } else {
284                    run.len = truncate_at + ellipsis.len();
285                    runs.splice(..run_index, std::iter::empty());
286                    break;
287                }
288            }
289        }
290        TruncateFrom::End => {
291            for (run_index, run) in runs.iter_mut().enumerate() {
292                if run.len <= truncate_at {
293                    truncate_at -= run.len;
294                } else {
295                    run.len = truncate_at + ellipsis.len();
296                    runs.truncate(run_index + 1);
297                    break;
298                }
299            }
300        }
301    }
302}
303
304/// A fragment of a line that can be wrapped.
305pub enum LineFragment<'a> {
306    /// A text fragment consisting of characters.
307    Text {
308        /// The text content of the fragment.
309        text: &'a str,
310    },
311    /// A non-text element with a fixed width.
312    Element {
313        /// The width of the element in pixels.
314        width: Pixels,
315        /// The UTF-8 encoded length of the element.
316        len_utf8: usize,
317    },
318}
319
320impl<'a> LineFragment<'a> {
321    /// Creates a new text fragment from the given text.
322    pub fn text(text: &'a str) -> Self {
323        LineFragment::Text { text }
324    }
325
326    /// Creates a new non-text element with the given width and UTF-8 encoded length.
327    pub fn element(width: Pixels, len_utf8: usize) -> Self {
328        LineFragment::Element { width, len_utf8 }
329    }
330
331    fn wrap_boundary_candidates(&self) -> impl Iterator<Item = WrapBoundaryCandidate> {
332        let text = match self {
333            LineFragment::Text { text } => text,
334            LineFragment::Element { .. } => "\0",
335        };
336        text.chars().map(move |character| {
337            if let LineFragment::Element { width, len_utf8 } = self {
338                WrapBoundaryCandidate::Element {
339                    width: *width,
340                    len_utf8: *len_utf8,
341                }
342            } else {
343                WrapBoundaryCandidate::Char { character }
344            }
345        })
346    }
347}
348
349enum WrapBoundaryCandidate {
350    Char { character: char },
351    Element { width: Pixels, len_utf8: usize },
352}
353
354impl WrapBoundaryCandidate {
355    pub fn len_utf8(&self) -> usize {
356        match self {
357            WrapBoundaryCandidate::Char { character } => character.len_utf8(),
358            WrapBoundaryCandidate::Element { len_utf8: len, .. } => *len,
359        }
360    }
361}
362
363/// A boundary between two lines of text.
364#[derive(Copy, Clone, Debug, PartialEq, Eq)]
365pub struct Boundary {
366    /// The index of the last character in a line
367    pub ix: usize,
368    /// The indent of the next line.
369    pub next_indent: u32,
370}
371
372impl Boundary {
373    fn new(ix: usize, next_indent: u32) -> Self {
374        Self { ix, next_indent }
375    }
376}
377
378#[cfg(test)]
379mod tests {
380    use super::*;
381    use crate::{Font, FontFeatures, FontStyle, FontWeight, TestAppContext, TestDispatcher, font};
382    #[cfg(target_os = "macos")]
383    use crate::{TextRun, WindowTextSystem, WrapBoundary};
384
385    fn build_wrapper() -> LineWrapper {
386        let dispatcher = TestDispatcher::new(0);
387        let cx = TestAppContext::build(dispatcher, None);
388        let id = cx.text_system().resolve_font(&font(".ZedMono"));
389        LineWrapper::new(id, px(16.), cx.text_system().clone())
390    }
391
392    fn generate_test_runs(input_run_len: &[usize]) -> Vec<TextRun> {
393        input_run_len
394            .iter()
395            .map(|run_len| TextRun {
396                len: *run_len,
397                font: Font {
398                    family: "Dummy".into(),
399                    features: FontFeatures::default(),
400                    fallbacks: None,
401                    weight: FontWeight::default(),
402                    style: FontStyle::Normal,
403                },
404                ..Default::default()
405            })
406            .collect()
407    }
408
409    #[test]
410    fn test_wrap_line() {
411        let mut wrapper = build_wrapper();
412
413        assert_eq!(
414            wrapper
415                .wrap_line(&[LineFragment::text("aa bbb cccc ddddd eeee")], px(72.))
416                .collect::<Vec<_>>(),
417            &[
418                Boundary::new(7, 0),
419                Boundary::new(12, 0),
420                Boundary::new(18, 0)
421            ],
422        );
423        assert_eq!(
424            wrapper
425                .wrap_line(&[LineFragment::text("aaa aaaaaaaaaaaaaaaaaa")], px(72.0))
426                .collect::<Vec<_>>(),
427            &[
428                Boundary::new(4, 0),
429                Boundary::new(11, 0),
430                Boundary::new(18, 0)
431            ],
432        );
433        assert_eq!(
434            wrapper
435                .wrap_line(&[LineFragment::text("     aaaaaaa")], px(72.))
436                .collect::<Vec<_>>(),
437            &[
438                Boundary::new(7, 5),
439                Boundary::new(9, 5),
440                Boundary::new(11, 5),
441            ]
442        );
443        assert_eq!(
444            wrapper
445                .wrap_line(
446                    &[LineFragment::text("                            ")],
447                    px(72.)
448                )
449                .collect::<Vec<_>>(),
450            &[
451                Boundary::new(7, 0),
452                Boundary::new(14, 0),
453                Boundary::new(21, 0)
454            ]
455        );
456        assert_eq!(
457            wrapper
458                .wrap_line(&[LineFragment::text("          aaaaaaaaaaaaaa")], px(72.))
459                .collect::<Vec<_>>(),
460            &[
461                Boundary::new(7, 0),
462                Boundary::new(14, 3),
463                Boundary::new(18, 3),
464                Boundary::new(22, 3),
465            ]
466        );
467
468        // Test wrapping multiple text fragments
469        assert_eq!(
470            wrapper
471                .wrap_line(
472                    &[
473                        LineFragment::text("aa bbb "),
474                        LineFragment::text("cccc ddddd eeee")
475                    ],
476                    px(72.)
477                )
478                .collect::<Vec<_>>(),
479            &[
480                Boundary::new(7, 0),
481                Boundary::new(12, 0),
482                Boundary::new(18, 0)
483            ],
484        );
485
486        // Test wrapping with a mix of text and element fragments
487        assert_eq!(
488            wrapper
489                .wrap_line(
490                    &[
491                        LineFragment::text("aa "),
492                        LineFragment::element(px(20.), 1),
493                        LineFragment::text(" bbb "),
494                        LineFragment::element(px(30.), 1),
495                        LineFragment::text(" cccc")
496                    ],
497                    px(72.)
498                )
499                .collect::<Vec<_>>(),
500            &[
501                Boundary::new(5, 0),
502                Boundary::new(9, 0),
503                Boundary::new(11, 0)
504            ],
505        );
506
507        // Test with element at the beginning and text afterward
508        assert_eq!(
509            wrapper
510                .wrap_line(
511                    &[
512                        LineFragment::element(px(50.), 1),
513                        LineFragment::text(" aaaa bbbb cccc dddd")
514                    ],
515                    px(72.)
516                )
517                .collect::<Vec<_>>(),
518            &[
519                Boundary::new(2, 0),
520                Boundary::new(7, 0),
521                Boundary::new(12, 0),
522                Boundary::new(17, 0)
523            ],
524        );
525
526        // Test with a large element that forces wrapping by itself
527        assert_eq!(
528            wrapper
529                .wrap_line(
530                    &[
531                        LineFragment::text("short text "),
532                        LineFragment::element(px(100.), 1),
533                        LineFragment::text(" more text")
534                    ],
535                    px(72.)
536                )
537                .collect::<Vec<_>>(),
538            &[
539                Boundary::new(6, 0),
540                Boundary::new(11, 0),
541                Boundary::new(12, 0),
542                Boundary::new(18, 0)
543            ],
544        );
545    }
546
547    #[test]
548    fn test_truncate_line_end() {
549        let mut wrapper = build_wrapper();
550
551        fn perform_test(
552            wrapper: &mut LineWrapper,
553            text: &'static str,
554            expected: &'static str,
555            ellipsis: &str,
556        ) {
557            let dummy_run_lens = vec![text.len()];
558            let dummy_runs = generate_test_runs(&dummy_run_lens);
559            let (result, dummy_runs) = wrapper.truncate_line(
560                text.into(),
561                px(220.),
562                ellipsis,
563                &dummy_runs,
564                TruncateFrom::End,
565            );
566            assert_eq!(result, expected);
567            assert_eq!(dummy_runs.first().unwrap().len, result.len());
568        }
569
570        perform_test(
571            &mut wrapper,
572            "aa bbb cccc ddddd eeee ffff gggg",
573            "aa bbb cccc ddddd eeee",
574            "",
575        );
576        perform_test(
577            &mut wrapper,
578            "aa bbb cccc ddddd eeee ffff gggg",
579            "aa bbb cccc ddddd eee…",
580            "",
581        );
582        perform_test(
583            &mut wrapper,
584            "aa bbb cccc ddddd eeee ffff gggg",
585            "aa bbb cccc dddd......",
586            "......",
587        );
588        perform_test(
589            &mut wrapper,
590            "aa bbb cccc 🦀🦀🦀🦀🦀 eeee ffff gggg",
591            "aa bbb cccc 🦀🦀🦀🦀…",
592            "",
593        );
594    }
595
596    #[test]
597    fn test_truncate_line_start() {
598        let mut wrapper = build_wrapper();
599
600        #[track_caller]
601        fn perform_test(
602            wrapper: &mut LineWrapper,
603            text: &'static str,
604            expected: &'static str,
605            ellipsis: &str,
606        ) {
607            let dummy_run_lens = vec![text.len()];
608            let dummy_runs = generate_test_runs(&dummy_run_lens);
609            let (result, dummy_runs) = wrapper.truncate_line(
610                text.into(),
611                px(220.),
612                ellipsis,
613                &dummy_runs,
614                TruncateFrom::Start,
615            );
616            assert_eq!(result, expected);
617            assert_eq!(dummy_runs.first().unwrap().len, result.len());
618        }
619
620        perform_test(
621            &mut wrapper,
622            "aaaa bbbb cccc ddddd eeee fff gg",
623            "cccc ddddd eeee fff gg",
624            "",
625        );
626        perform_test(
627            &mut wrapper,
628            "aaaa bbbb cccc ddddd eeee fff gg",
629            "…ccc ddddd eeee fff gg",
630            "",
631        );
632        perform_test(
633            &mut wrapper,
634            "aaaa bbbb cccc ddddd eeee fff gg",
635            "......dddd eeee fff gg",
636            "......",
637        );
638        perform_test(
639            &mut wrapper,
640            "aaaa bbbb cccc 🦀🦀🦀🦀🦀 eeee fff gg",
641            "…🦀🦀🦀🦀 eeee fff gg",
642            "",
643        );
644    }
645
646    #[test]
647    fn test_truncate_multiple_runs_end() {
648        let mut wrapper = build_wrapper();
649
650        fn perform_test(
651            wrapper: &mut LineWrapper,
652            text: &'static str,
653            expected: &str,
654            run_lens: &[usize],
655            result_run_len: &[usize],
656            line_width: Pixels,
657        ) {
658            let dummy_runs = generate_test_runs(run_lens);
659            let (result, dummy_runs) =
660                wrapper.truncate_line(text.into(), line_width, "", &dummy_runs, TruncateFrom::End);
661            assert_eq!(result, expected);
662            for (run, result_len) in dummy_runs.iter().zip(result_run_len) {
663                assert_eq!(run.len, *result_len);
664            }
665        }
666        // Case 0: Normal
667        // Text: abcdefghijkl
668        // Runs: Run0 { len: 12, ... }
669        //
670        // Truncate res: abcd… (truncate_at = 4)
671        // Run res: Run0 { string: abcd…, len: 7, ... }
672        perform_test(&mut wrapper, "abcdefghijkl", "abcd…", &[12], &[7], px(50.));
673        // Case 1: Drop some runs
674        // Text: abcdefghijkl
675        // Runs: Run0 { len: 4, ... }, Run1 { len: 4, ... }, Run2 { len: 4, ... }
676        //
677        // Truncate res: abcdef… (truncate_at = 6)
678        // Runs res: Run0 { string: abcd, len: 4, ... }, Run1 { string: ef…, len:
679        // 5, ... }
680        perform_test(
681            &mut wrapper,
682            "abcdefghijkl",
683            "abcdef…",
684            &[4, 4, 4],
685            &[4, 5],
686            px(70.),
687        );
688        // Case 2: Truncate at start of some run
689        // Text: abcdefghijkl
690        // Runs: Run0 { len: 4, ... }, Run1 { len: 4, ... }, Run2 { len: 4, ... }
691        //
692        // Truncate res: abcdefgh… (truncate_at = 8)
693        // Runs res: Run0 { string: abcd, len: 4, ... }, Run1 { string: efgh, len:
694        // 4, ... }, Run2 { string: …, len: 3, ... }
695        perform_test(
696            &mut wrapper,
697            "abcdefghijkl",
698            "abcdefgh…",
699            &[4, 4, 4],
700            &[4, 4, 3],
701            px(90.),
702        );
703    }
704
705    #[test]
706    fn test_truncate_multiple_runs_start() {
707        let mut wrapper = build_wrapper();
708
709        #[track_caller]
710        fn perform_test(
711            wrapper: &mut LineWrapper,
712            text: &'static str,
713            expected: &str,
714            run_lens: &[usize],
715            result_run_len: &[usize],
716            line_width: Pixels,
717        ) {
718            let dummy_runs = generate_test_runs(run_lens);
719            let (result, dummy_runs) = wrapper.truncate_line(
720                text.into(),
721                line_width,
722                "",
723                &dummy_runs,
724                TruncateFrom::Start,
725            );
726            assert_eq!(result, expected);
727            for (run, result_len) in dummy_runs.iter().zip(result_run_len) {
728                assert_eq!(run.len, *result_len);
729            }
730        }
731        // Case 0: Normal
732        // Text: abcdefghijkl
733        // Runs: Run0 { len: 12, ... }
734        //
735        // Truncate res: …ijkl (truncate_at = 9)
736        // Run res: Run0 { string: …ijkl, len: 7, ... }
737        perform_test(&mut wrapper, "abcdefghijkl", "…ijkl", &[12], &[7], px(50.));
738        // Case 1: Drop some runs
739        // Text: abcdefghijkl
740        // Runs: Run0 { len: 4, ... }, Run1 { len: 4, ... }, Run2 { len: 4, ... }
741        //
742        // Truncate res: …ghijkl (truncate_at = 7)
743        // Runs res: Run0 { string: …gh, len: 5, ... }, Run1 { string: ijkl, len:
744        // 4, ... }
745        perform_test(
746            &mut wrapper,
747            "abcdefghijkl",
748            "…ghijkl",
749            &[4, 4, 4],
750            &[5, 4],
751            px(70.),
752        );
753        // Case 2: Truncate at start of some run
754        // Text: abcdefghijkl
755        // Runs: Run0 { len: 4, ... }, Run1 { len: 4, ... }, Run2 { len: 4, ... }
756        //
757        // Truncate res: abcdefgh… (truncate_at = 3)
758        // Runs res: Run0 { string: …, len: 3, ... }, Run1 { string: efgh, len:
759        // 4, ... }, Run2 { string: ijkl, len: 4, ... }
760        perform_test(
761            &mut wrapper,
762            "abcdefghijkl",
763            "…efghijkl",
764            &[4, 4, 4],
765            &[3, 4, 4],
766            px(90.),
767        );
768    }
769
770    #[test]
771    fn test_update_run_after_truncation_end() {
772        fn perform_test(result: &str, run_lens: &[usize], result_run_lens: &[usize]) {
773            let mut dummy_runs = generate_test_runs(run_lens);
774            update_runs_after_truncation(result, "", &mut dummy_runs, TruncateFrom::End);
775            for (run, result_len) in dummy_runs.iter().zip(result_run_lens) {
776                assert_eq!(run.len, *result_len);
777            }
778        }
779        // Case 0: Normal
780        // Text: abcdefghijkl
781        // Runs: Run0 { len: 12, ... }
782        //
783        // Truncate res: abcd… (truncate_at = 4)
784        // Run res: Run0 { string: abcd…, len: 7, ... }
785        perform_test("abcd…", &[12], &[7]);
786        // Case 1: Drop some runs
787        // Text: abcdefghijkl
788        // Runs: Run0 { len: 4, ... }, Run1 { len: 4, ... }, Run2 { len: 4, ... }
789        //
790        // Truncate res: abcdef… (truncate_at = 6)
791        // Runs res: Run0 { string: abcd, len: 4, ... }, Run1 { string: ef…, len:
792        // 5, ... }
793        perform_test("abcdef…", &[4, 4, 4], &[4, 5]);
794        // Case 2: Truncate at start of some run
795        // Text: abcdefghijkl
796        // Runs: Run0 { len: 4, ... }, Run1 { len: 4, ... }, Run2 { len: 4, ... }
797        //
798        // Truncate res: abcdefgh… (truncate_at = 8)
799        // Runs res: Run0 { string: abcd, len: 4, ... }, Run1 { string: efgh, len:
800        // 4, ... }, Run2 { string: …, len: 3, ... }
801        perform_test("abcdefgh…", &[4, 4, 4], &[4, 4, 3]);
802    }
803
804    #[test]
805    fn test_is_word_char() {
806        #[track_caller]
807        fn assert_word(word: &str) {
808            for c in word.chars() {
809                assert!(
810                    LineWrapper::is_word_char(c),
811                    "assertion failed for '{}' (unicode 0x{:x})",
812                    c,
813                    c as u32
814                );
815            }
816        }
817
818        #[track_caller]
819        fn assert_not_word(word: &str) {
820            let found = word.chars().any(|c| !LineWrapper::is_word_char(c));
821            assert!(found, "assertion failed for '{}'", word);
822        }
823
824        assert_word("Hello123");
825        assert_word("non-English");
826        assert_word("var_name");
827        assert_word("123456");
828        assert_word("3.1415");
829        assert_word("10^2");
830        assert_word("1~2");
831        assert_word("100%");
832        assert_word("@mention");
833        assert_word("#hashtag");
834        assert_word("$variable");
835        assert_word("a=1");
836        assert_word("Self::is_word_char");
837        assert_word("more⋯");
838
839        // Space
840        assert_not_word("foo bar");
841
842        // URL case
843        assert_word("github.com");
844        assert_not_word("zed-industries/zed");
845        assert_not_word("zed-industries\\zed");
846        assert_not_word("a=1&b=2");
847        assert_not_word("foo?b=2");
848
849        // Latin-1 Supplement
850        assert_word("ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏ");
851        // Latin Extended-A
852        assert_word("ĀāĂ㥹ĆćĈĉĊċČčĎď");
853        // Latin Extended-B
854        assert_word("ƀƁƂƃƄƅƆƇƈƉƊƋƌƍƎƏ");
855        // Cyrillic
856        assert_word("АБВГДЕЖЗИЙКЛМНОП");
857        // Vietnamese (https://github.com/zed-industries/zed/issues/23245)
858        assert_word("ThậmchíđếnkhithuachạychúngcònnhẫntâmgiếtnốtsốđôngtùchínhtrịởYênBáivàCaoBằng");
859
860        // non-word characters
861        assert_not_word("你好");
862        assert_not_word("안녕하세요");
863        assert_not_word("こんにちは");
864        assert_not_word("😀😁😂");
865        assert_not_word("()[]{}<>");
866    }
867
868    // For compatibility with the test macro
869    #[cfg(target_os = "macos")]
870    use crate as gpui;
871
872    // These seem to vary wildly based on the text system.
873    #[cfg(target_os = "macos")]
874    #[crate::test]
875    fn test_wrap_shaped_line(cx: &mut TestAppContext) {
876        cx.update(|cx| {
877            let text_system = WindowTextSystem::new(cx.text_system().clone());
878
879            let normal = TextRun {
880                len: 0,
881                font: font("Helvetica"),
882                color: Default::default(),
883                underline: Default::default(),
884                ..Default::default()
885            };
886            let bold = TextRun {
887                len: 0,
888                font: font("Helvetica").bold(),
889                ..Default::default()
890            };
891
892            let text = "aa bbb cccc ddddd eeee".into();
893            let lines = text_system
894                .shape_text(
895                    text,
896                    px(16.),
897                    &[
898                        normal.with_len(4),
899                        bold.with_len(5),
900                        normal.with_len(6),
901                        bold.with_len(1),
902                        normal.with_len(7),
903                    ],
904                    Some(px(72.)),
905                    None,
906                )
907                .unwrap();
908
909            assert_eq!(
910                lines[0].layout.wrap_boundaries(),
911                &[
912                    WrapBoundary {
913                        run_ix: 0,
914                        glyph_ix: 7
915                    },
916                    WrapBoundary {
917                        run_ix: 0,
918                        glyph_ix: 12
919                    },
920                    WrapBoundary {
921                        run_ix: 0,
922                        glyph_ix: 18
923                    }
924                ],
925            );
926        });
927    }
928}