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        // Bengali (https://en.wikipedia.org/wiki/Bengali_(Unicode_block))
240        matches!(c, '\u{0980}'..='\u{09FF}') ||
241
242        // Some other known special characters that should be treated as word characters,
243        // e.g. `a-b`, `var_name`, `I'm`, '@mention`, `#hashtag`, `100%`, `3.1415`,
244        // `2^3`, `a~b`, `a=1`, `Self::new`, etc.
245        matches!(c, '-' | '_' | '.' | '\'' | '$' | '%' | '@' | '#' | '^' | '~' | ',' | '=' | ':') ||
246        // `⋯` character is special used in Zed, to keep this at the end of the line.
247        matches!(c, '⋯')
248    }
249
250    #[inline(always)]
251    fn width_for_char(&mut self, c: char) -> Pixels {
252        if (c as u32) < 128 {
253            if let Some(cached_width) = self.cached_ascii_char_widths[c as usize] {
254                cached_width
255            } else {
256                let width = self
257                    .text_system
258                    .layout_width(self.font_id, self.font_size, c);
259                self.cached_ascii_char_widths[c as usize] = Some(width);
260                width
261            }
262        } else if let Some(cached_width) = self.cached_other_char_widths.get(&c) {
263            *cached_width
264        } else {
265            let width = self
266                .text_system
267                .layout_width(self.font_id, self.font_size, c);
268            self.cached_other_char_widths.insert(c, width);
269            width
270        }
271    }
272}
273
274fn update_runs_after_truncation(
275    result: &str,
276    ellipsis: &str,
277    runs: &mut Vec<TextRun>,
278    truncate_from: TruncateFrom,
279) {
280    let mut truncate_at = result.len() - ellipsis.len();
281    match truncate_from {
282        TruncateFrom::Start => {
283            for (run_index, run) in runs.iter_mut().enumerate().rev() {
284                if run.len <= truncate_at {
285                    truncate_at -= run.len;
286                } else {
287                    run.len = truncate_at + ellipsis.len();
288                    runs.splice(..run_index, std::iter::empty());
289                    break;
290                }
291            }
292        }
293        TruncateFrom::End => {
294            for (run_index, run) in runs.iter_mut().enumerate() {
295                if run.len <= truncate_at {
296                    truncate_at -= run.len;
297                } else {
298                    run.len = truncate_at + ellipsis.len();
299                    runs.truncate(run_index + 1);
300                    break;
301                }
302            }
303        }
304    }
305}
306
307/// A fragment of a line that can be wrapped.
308pub enum LineFragment<'a> {
309    /// A text fragment consisting of characters.
310    Text {
311        /// The text content of the fragment.
312        text: &'a str,
313    },
314    /// A non-text element with a fixed width.
315    Element {
316        /// The width of the element in pixels.
317        width: Pixels,
318        /// The UTF-8 encoded length of the element.
319        len_utf8: usize,
320    },
321}
322
323impl<'a> LineFragment<'a> {
324    /// Creates a new text fragment from the given text.
325    pub fn text(text: &'a str) -> Self {
326        LineFragment::Text { text }
327    }
328
329    /// Creates a new non-text element with the given width and UTF-8 encoded length.
330    pub fn element(width: Pixels, len_utf8: usize) -> Self {
331        LineFragment::Element { width, len_utf8 }
332    }
333
334    fn wrap_boundary_candidates(&self) -> impl Iterator<Item = WrapBoundaryCandidate> {
335        let text = match self {
336            LineFragment::Text { text } => text,
337            LineFragment::Element { .. } => "\0",
338        };
339        text.chars().map(move |character| {
340            if let LineFragment::Element { width, len_utf8 } = self {
341                WrapBoundaryCandidate::Element {
342                    width: *width,
343                    len_utf8: *len_utf8,
344                }
345            } else {
346                WrapBoundaryCandidate::Char { character }
347            }
348        })
349    }
350}
351
352enum WrapBoundaryCandidate {
353    Char { character: char },
354    Element { width: Pixels, len_utf8: usize },
355}
356
357impl WrapBoundaryCandidate {
358    pub fn len_utf8(&self) -> usize {
359        match self {
360            WrapBoundaryCandidate::Char { character } => character.len_utf8(),
361            WrapBoundaryCandidate::Element { len_utf8: len, .. } => *len,
362        }
363    }
364}
365
366/// A boundary between two lines of text.
367#[derive(Copy, Clone, Debug, PartialEq, Eq)]
368pub struct Boundary {
369    /// The index of the last character in a line
370    pub ix: usize,
371    /// The indent of the next line.
372    pub next_indent: u32,
373}
374
375impl Boundary {
376    fn new(ix: usize, next_indent: u32) -> Self {
377        Self { ix, next_indent }
378    }
379}
380
381#[cfg(test)]
382mod tests {
383    use super::*;
384    use crate::{Font, FontFeatures, FontStyle, FontWeight, TestAppContext, TestDispatcher, font};
385    #[cfg(target_os = "macos")]
386    use crate::{TextRun, WindowTextSystem, WrapBoundary};
387
388    fn build_wrapper() -> LineWrapper {
389        let dispatcher = TestDispatcher::new(0);
390        let cx = TestAppContext::build(dispatcher, None);
391        let id = cx.text_system().resolve_font(&font(".ZedMono"));
392        LineWrapper::new(id, px(16.), cx.text_system().clone())
393    }
394
395    fn generate_test_runs(input_run_len: &[usize]) -> Vec<TextRun> {
396        input_run_len
397            .iter()
398            .map(|run_len| TextRun {
399                len: *run_len,
400                font: Font {
401                    family: "Dummy".into(),
402                    features: FontFeatures::default(),
403                    fallbacks: None,
404                    weight: FontWeight::default(),
405                    style: FontStyle::Normal,
406                },
407                ..Default::default()
408            })
409            .collect()
410    }
411
412    #[test]
413    fn test_wrap_line() {
414        let mut wrapper = build_wrapper();
415
416        assert_eq!(
417            wrapper
418                .wrap_line(&[LineFragment::text("aa bbb cccc ddddd eeee")], px(72.))
419                .collect::<Vec<_>>(),
420            &[
421                Boundary::new(7, 0),
422                Boundary::new(12, 0),
423                Boundary::new(18, 0)
424            ],
425        );
426        assert_eq!(
427            wrapper
428                .wrap_line(&[LineFragment::text("aaa aaaaaaaaaaaaaaaaaa")], px(72.0))
429                .collect::<Vec<_>>(),
430            &[
431                Boundary::new(4, 0),
432                Boundary::new(11, 0),
433                Boundary::new(18, 0)
434            ],
435        );
436        assert_eq!(
437            wrapper
438                .wrap_line(&[LineFragment::text("     aaaaaaa")], px(72.))
439                .collect::<Vec<_>>(),
440            &[
441                Boundary::new(7, 5),
442                Boundary::new(9, 5),
443                Boundary::new(11, 5),
444            ]
445        );
446        assert_eq!(
447            wrapper
448                .wrap_line(
449                    &[LineFragment::text("                            ")],
450                    px(72.)
451                )
452                .collect::<Vec<_>>(),
453            &[
454                Boundary::new(7, 0),
455                Boundary::new(14, 0),
456                Boundary::new(21, 0)
457            ]
458        );
459        assert_eq!(
460            wrapper
461                .wrap_line(&[LineFragment::text("          aaaaaaaaaaaaaa")], px(72.))
462                .collect::<Vec<_>>(),
463            &[
464                Boundary::new(7, 0),
465                Boundary::new(14, 3),
466                Boundary::new(18, 3),
467                Boundary::new(22, 3),
468            ]
469        );
470
471        // Test wrapping multiple text fragments
472        assert_eq!(
473            wrapper
474                .wrap_line(
475                    &[
476                        LineFragment::text("aa bbb "),
477                        LineFragment::text("cccc ddddd eeee")
478                    ],
479                    px(72.)
480                )
481                .collect::<Vec<_>>(),
482            &[
483                Boundary::new(7, 0),
484                Boundary::new(12, 0),
485                Boundary::new(18, 0)
486            ],
487        );
488
489        // Test wrapping with a mix of text and element fragments
490        assert_eq!(
491            wrapper
492                .wrap_line(
493                    &[
494                        LineFragment::text("aa "),
495                        LineFragment::element(px(20.), 1),
496                        LineFragment::text(" bbb "),
497                        LineFragment::element(px(30.), 1),
498                        LineFragment::text(" cccc")
499                    ],
500                    px(72.)
501                )
502                .collect::<Vec<_>>(),
503            &[
504                Boundary::new(5, 0),
505                Boundary::new(9, 0),
506                Boundary::new(11, 0)
507            ],
508        );
509
510        // Test with element at the beginning and text afterward
511        assert_eq!(
512            wrapper
513                .wrap_line(
514                    &[
515                        LineFragment::element(px(50.), 1),
516                        LineFragment::text(" aaaa bbbb cccc dddd")
517                    ],
518                    px(72.)
519                )
520                .collect::<Vec<_>>(),
521            &[
522                Boundary::new(2, 0),
523                Boundary::new(7, 0),
524                Boundary::new(12, 0),
525                Boundary::new(17, 0)
526            ],
527        );
528
529        // Test with a large element that forces wrapping by itself
530        assert_eq!(
531            wrapper
532                .wrap_line(
533                    &[
534                        LineFragment::text("short text "),
535                        LineFragment::element(px(100.), 1),
536                        LineFragment::text(" more text")
537                    ],
538                    px(72.)
539                )
540                .collect::<Vec<_>>(),
541            &[
542                Boundary::new(6, 0),
543                Boundary::new(11, 0),
544                Boundary::new(12, 0),
545                Boundary::new(18, 0)
546            ],
547        );
548    }
549
550    #[test]
551    fn test_truncate_line_end() {
552        let mut wrapper = build_wrapper();
553
554        fn perform_test(
555            wrapper: &mut LineWrapper,
556            text: &'static str,
557            expected: &'static str,
558            ellipsis: &str,
559        ) {
560            let dummy_run_lens = vec![text.len()];
561            let dummy_runs = generate_test_runs(&dummy_run_lens);
562            let (result, dummy_runs) = wrapper.truncate_line(
563                text.into(),
564                px(220.),
565                ellipsis,
566                &dummy_runs,
567                TruncateFrom::End,
568            );
569            assert_eq!(result, expected);
570            assert_eq!(dummy_runs.first().unwrap().len, result.len());
571        }
572
573        perform_test(
574            &mut wrapper,
575            "aa bbb cccc ddddd eeee ffff gggg",
576            "aa bbb cccc ddddd eeee",
577            "",
578        );
579        perform_test(
580            &mut wrapper,
581            "aa bbb cccc ddddd eeee ffff gggg",
582            "aa bbb cccc ddddd eee…",
583            "",
584        );
585        perform_test(
586            &mut wrapper,
587            "aa bbb cccc ddddd eeee ffff gggg",
588            "aa bbb cccc dddd......",
589            "......",
590        );
591        perform_test(
592            &mut wrapper,
593            "aa bbb cccc 🦀🦀🦀🦀🦀 eeee ffff gggg",
594            "aa bbb cccc 🦀🦀🦀🦀…",
595            "",
596        );
597    }
598
599    #[test]
600    fn test_truncate_line_start() {
601        let mut wrapper = build_wrapper();
602
603        #[track_caller]
604        fn perform_test(
605            wrapper: &mut LineWrapper,
606            text: &'static str,
607            expected: &'static str,
608            ellipsis: &str,
609        ) {
610            let dummy_run_lens = vec![text.len()];
611            let dummy_runs = generate_test_runs(&dummy_run_lens);
612            let (result, dummy_runs) = wrapper.truncate_line(
613                text.into(),
614                px(220.),
615                ellipsis,
616                &dummy_runs,
617                TruncateFrom::Start,
618            );
619            assert_eq!(result, expected);
620            assert_eq!(dummy_runs.first().unwrap().len, result.len());
621        }
622
623        perform_test(
624            &mut wrapper,
625            "aaaa bbbb cccc ddddd eeee fff gg",
626            "cccc ddddd eeee fff gg",
627            "",
628        );
629        perform_test(
630            &mut wrapper,
631            "aaaa bbbb cccc ddddd eeee fff gg",
632            "…ccc ddddd eeee fff gg",
633            "",
634        );
635        perform_test(
636            &mut wrapper,
637            "aaaa bbbb cccc ddddd eeee fff gg",
638            "......dddd eeee fff gg",
639            "......",
640        );
641        perform_test(
642            &mut wrapper,
643            "aaaa bbbb cccc 🦀🦀🦀🦀🦀 eeee fff gg",
644            "…🦀🦀🦀🦀 eeee fff gg",
645            "",
646        );
647    }
648
649    #[test]
650    fn test_truncate_multiple_runs_end() {
651        let mut wrapper = build_wrapper();
652
653        fn perform_test(
654            wrapper: &mut LineWrapper,
655            text: &'static str,
656            expected: &str,
657            run_lens: &[usize],
658            result_run_len: &[usize],
659            line_width: Pixels,
660        ) {
661            let dummy_runs = generate_test_runs(run_lens);
662            let (result, dummy_runs) =
663                wrapper.truncate_line(text.into(), line_width, "", &dummy_runs, TruncateFrom::End);
664            assert_eq!(result, expected);
665            for (run, result_len) in dummy_runs.iter().zip(result_run_len) {
666                assert_eq!(run.len, *result_len);
667            }
668        }
669        // Case 0: Normal
670        // Text: abcdefghijkl
671        // Runs: Run0 { len: 12, ... }
672        //
673        // Truncate res: abcd… (truncate_at = 4)
674        // Run res: Run0 { string: abcd…, len: 7, ... }
675        perform_test(&mut wrapper, "abcdefghijkl", "abcd…", &[12], &[7], px(50.));
676        // Case 1: Drop some runs
677        // Text: abcdefghijkl
678        // Runs: Run0 { len: 4, ... }, Run1 { len: 4, ... }, Run2 { len: 4, ... }
679        //
680        // Truncate res: abcdef… (truncate_at = 6)
681        // Runs res: Run0 { string: abcd, len: 4, ... }, Run1 { string: ef…, len:
682        // 5, ... }
683        perform_test(
684            &mut wrapper,
685            "abcdefghijkl",
686            "abcdef…",
687            &[4, 4, 4],
688            &[4, 5],
689            px(70.),
690        );
691        // Case 2: Truncate at start of some run
692        // Text: abcdefghijkl
693        // Runs: Run0 { len: 4, ... }, Run1 { len: 4, ... }, Run2 { len: 4, ... }
694        //
695        // Truncate res: abcdefgh… (truncate_at = 8)
696        // Runs res: Run0 { string: abcd, len: 4, ... }, Run1 { string: efgh, len:
697        // 4, ... }, Run2 { string: …, len: 3, ... }
698        perform_test(
699            &mut wrapper,
700            "abcdefghijkl",
701            "abcdefgh…",
702            &[4, 4, 4],
703            &[4, 4, 3],
704            px(90.),
705        );
706    }
707
708    #[test]
709    fn test_truncate_multiple_runs_start() {
710        let mut wrapper = build_wrapper();
711
712        #[track_caller]
713        fn perform_test(
714            wrapper: &mut LineWrapper,
715            text: &'static str,
716            expected: &str,
717            run_lens: &[usize],
718            result_run_len: &[usize],
719            line_width: Pixels,
720        ) {
721            let dummy_runs = generate_test_runs(run_lens);
722            let (result, dummy_runs) = wrapper.truncate_line(
723                text.into(),
724                line_width,
725                "",
726                &dummy_runs,
727                TruncateFrom::Start,
728            );
729            assert_eq!(result, expected);
730            for (run, result_len) in dummy_runs.iter().zip(result_run_len) {
731                assert_eq!(run.len, *result_len);
732            }
733        }
734        // Case 0: Normal
735        // Text: abcdefghijkl
736        // Runs: Run0 { len: 12, ... }
737        //
738        // Truncate res: …ijkl (truncate_at = 9)
739        // Run res: Run0 { string: …ijkl, len: 7, ... }
740        perform_test(&mut wrapper, "abcdefghijkl", "…ijkl", &[12], &[7], px(50.));
741        // Case 1: Drop some runs
742        // Text: abcdefghijkl
743        // Runs: Run0 { len: 4, ... }, Run1 { len: 4, ... }, Run2 { len: 4, ... }
744        //
745        // Truncate res: …ghijkl (truncate_at = 7)
746        // Runs res: Run0 { string: …gh, len: 5, ... }, Run1 { string: ijkl, len:
747        // 4, ... }
748        perform_test(
749            &mut wrapper,
750            "abcdefghijkl",
751            "…ghijkl",
752            &[4, 4, 4],
753            &[5, 4],
754            px(70.),
755        );
756        // Case 2: Truncate at start of some run
757        // Text: abcdefghijkl
758        // Runs: Run0 { len: 4, ... }, Run1 { len: 4, ... }, Run2 { len: 4, ... }
759        //
760        // Truncate res: abcdefgh… (truncate_at = 3)
761        // Runs res: Run0 { string: …, len: 3, ... }, Run1 { string: efgh, len:
762        // 4, ... }, Run2 { string: ijkl, len: 4, ... }
763        perform_test(
764            &mut wrapper,
765            "abcdefghijkl",
766            "…efghijkl",
767            &[4, 4, 4],
768            &[3, 4, 4],
769            px(90.),
770        );
771    }
772
773    #[test]
774    fn test_update_run_after_truncation_end() {
775        fn perform_test(result: &str, run_lens: &[usize], result_run_lens: &[usize]) {
776            let mut dummy_runs = generate_test_runs(run_lens);
777            update_runs_after_truncation(result, "", &mut dummy_runs, TruncateFrom::End);
778            for (run, result_len) in dummy_runs.iter().zip(result_run_lens) {
779                assert_eq!(run.len, *result_len);
780            }
781        }
782        // Case 0: Normal
783        // Text: abcdefghijkl
784        // Runs: Run0 { len: 12, ... }
785        //
786        // Truncate res: abcd… (truncate_at = 4)
787        // Run res: Run0 { string: abcd…, len: 7, ... }
788        perform_test("abcd…", &[12], &[7]);
789        // Case 1: Drop some runs
790        // Text: abcdefghijkl
791        // Runs: Run0 { len: 4, ... }, Run1 { len: 4, ... }, Run2 { len: 4, ... }
792        //
793        // Truncate res: abcdef… (truncate_at = 6)
794        // Runs res: Run0 { string: abcd, len: 4, ... }, Run1 { string: ef…, len:
795        // 5, ... }
796        perform_test("abcdef…", &[4, 4, 4], &[4, 5]);
797        // Case 2: Truncate at start of some run
798        // Text: abcdefghijkl
799        // Runs: Run0 { len: 4, ... }, Run1 { len: 4, ... }, Run2 { len: 4, ... }
800        //
801        // Truncate res: abcdefgh… (truncate_at = 8)
802        // Runs res: Run0 { string: abcd, len: 4, ... }, Run1 { string: efgh, len:
803        // 4, ... }, Run2 { string: …, len: 3, ... }
804        perform_test("abcdefgh…", &[4, 4, 4], &[4, 4, 3]);
805    }
806
807    #[test]
808    fn test_is_word_char() {
809        #[track_caller]
810        fn assert_word(word: &str) {
811            for c in word.chars() {
812                assert!(
813                    LineWrapper::is_word_char(c),
814                    "assertion failed for '{}' (unicode 0x{:x})",
815                    c,
816                    c as u32
817                );
818            }
819        }
820
821        #[track_caller]
822        fn assert_not_word(word: &str) {
823            let found = word.chars().any(|c| !LineWrapper::is_word_char(c));
824            assert!(found, "assertion failed for '{}'", word);
825        }
826
827        assert_word("Hello123");
828        assert_word("non-English");
829        assert_word("var_name");
830        assert_word("123456");
831        assert_word("3.1415");
832        assert_word("10^2");
833        assert_word("1~2");
834        assert_word("100%");
835        assert_word("@mention");
836        assert_word("#hashtag");
837        assert_word("$variable");
838        assert_word("a=1");
839        assert_word("Self::is_word_char");
840        assert_word("more⋯");
841
842        // Space
843        assert_not_word("foo bar");
844
845        // URL case
846        assert_word("github.com");
847        assert_not_word("zed-industries/zed");
848        assert_not_word("zed-industries\\zed");
849        assert_not_word("a=1&b=2");
850        assert_not_word("foo?b=2");
851
852        // Latin-1 Supplement
853        assert_word("ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏ");
854        // Latin Extended-A
855        assert_word("ĀāĂ㥹ĆćĈĉĊċČčĎď");
856        // Latin Extended-B
857        assert_word("ƀƁƂƃƄƅƆƇƈƉƊƋƌƍƎƏ");
858        // Cyrillic
859        assert_word("АБВГДЕЖЗИЙКЛМНОП");
860        // Vietnamese (https://github.com/zed-industries/zed/issues/23245)
861        assert_word("ThậmchíđếnkhithuachạychúngcònnhẫntâmgiếtnốtsốđôngtùchínhtrịởYênBáivàCaoBằng");
862        // Bengali
863        assert_word("গিয়েছিলেন");
864        assert_word("ছেলে");
865        assert_word("হচ্ছিল");
866
867        // non-word characters
868        assert_not_word("你好");
869        assert_not_word("안녕하세요");
870        assert_not_word("こんにちは");
871        assert_not_word("😀😁😂");
872        assert_not_word("()[]{}<>");
873    }
874
875    // For compatibility with the test macro
876    #[cfg(target_os = "macos")]
877    use crate as gpui;
878
879    // These seem to vary wildly based on the text system.
880    #[cfg(target_os = "macos")]
881    #[crate::test]
882    fn test_wrap_shaped_line(cx: &mut TestAppContext) {
883        cx.update(|cx| {
884            let text_system = WindowTextSystem::new(cx.text_system().clone());
885
886            let normal = TextRun {
887                len: 0,
888                font: font("Helvetica"),
889                color: Default::default(),
890                underline: Default::default(),
891                ..Default::default()
892            };
893            let bold = TextRun {
894                len: 0,
895                font: font("Helvetica").bold(),
896                ..Default::default()
897            };
898
899            let text = "aa bbb cccc ddddd eeee".into();
900            let lines = text_system
901                .shape_text(
902                    text,
903                    px(16.),
904                    &[
905                        normal.with_len(4),
906                        bold.with_len(5),
907                        normal.with_len(6),
908                        bold.with_len(1),
909                        normal.with_len(7),
910                    ],
911                    Some(px(72.)),
912                    None,
913                )
914                .unwrap();
915
916            assert_eq!(
917                lines[0].layout.wrap_boundaries(),
918                &[
919                    WrapBoundary {
920                        run_ix: 0,
921                        glyph_ix: 7
922                    },
923                    WrapBoundary {
924                        run_ix: 0,
925                        glyph_ix: 12
926                    },
927                    WrapBoundary {
928                        run_ix: 0,
929                        glyph_ix: 18
930                    }
931                ],
932            );
933        });
934    }
935}