1use crate::{FontId, Pixels, SharedString, TextRun, TextSystem, px};
3use collections::HashMap;
4use std::{borrow::Cow, iter, sync::Arc};
5use unicode_segmentation::UnicodeSegmentation;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9pub enum TruncateFrom {
10 Start,
12 End,
14 Middle,
16}
17
18#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
20pub enum IndentAdjustment {
21 NoIndent,
23 #[default]
25 SameIndent,
26 ExtraColumns(u32),
28}
29
30pub struct LineWrapper {
32 text_system: Arc<TextSystem>,
33 pub(crate) font_id: FontId,
34 pub(crate) font_size: Pixels,
35 char_widths: CharWidthCache,
36 text_buffer: String,
37}
38
39pub(crate) struct CharWidthCache {
41 ascii: [Option<Pixels>; 128],
42 other: HashMap<char, Pixels>,
43}
44
45impl Default for CharWidthCache {
46 fn default() -> Self {
47 Self {
48 ascii: [None; 128],
49 other: HashMap::default(),
50 }
51 }
52}
53
54impl LineWrapper {
55 pub const MAX_INDENT: u32 = 256;
57
58 pub(crate) fn new(
59 font_id: FontId,
60 font_size: Pixels,
61 text_system: Arc<TextSystem>,
62 char_widths: CharWidthCache,
63 ) -> Self {
64 Self {
65 text_system,
66 font_id,
67 font_size,
68 char_widths,
69 text_buffer: String::new(),
70 }
71 }
72
73 pub(crate) fn into_char_widths(self) -> CharWidthCache {
74 self.char_widths
75 }
76
77 pub fn wrap_line<'a>(
79 &'a mut self,
80 fragments: &'a [LineFragment],
81 wrap_width: Pixels,
82 indent_adjustment: IndentAdjustment,
83 ) -> impl Iterator<Item = Boundary> + 'a {
84 let mut width = px(0.);
85 let mut first_non_whitespace_ix = None;
86 let mut base_indent = None;
87 let mut last_candidate_ix = 0;
88 let mut last_candidate_width = px(0.);
89 let mut last_wrap_ix = 0;
90 let mut index = 0;
91 let Self {
92 text_buffer: text,
93 char_widths,
94 text_system,
95 font_id,
96 font_size,
97 } = self;
98 text.clear();
99 for fragment in fragments {
100 match fragment {
101 LineFragment::Text {
102 text: fragment_text,
103 } => text.push_str(fragment_text),
104 LineFragment::Element { len_utf8, .. } => {
106 text.extend(iter::repeat_n('a', *len_utf8));
107 }
108 }
109 }
110 let mut break_indices = Self::break_indices(text).peekable();
111 let mut grapheme_indices = text
112 .grapheme_indices(true)
113 .map(|(index, _)| index)
114 .peekable();
115 let mut width_for_char = move |character| {
116 width_for_char(char_widths, character, text_system, *font_id, *font_size)
117 };
118 let mut grapheme_index = 0;
119 let mut width_before_grapheme = px(0.);
120 let mut candidates = fragments
121 .iter()
122 .flat_map(move |fragment| fragment.wrap_boundary_candidates())
123 .peekable();
124 iter::from_fn(move || {
125 for candidate in candidates.by_ref() {
126 let ix = index;
127 index += candidate.len_utf8();
128 while grapheme_indices
129 .next_if(|position| *position < ix)
130 .is_some()
131 {}
132 let is_grapheme_boundary = grapheme_indices.next_if_eq(&ix).is_some();
133 if is_grapheme_boundary {
134 grapheme_index = ix;
135 width_before_grapheme = width;
136 }
137 if break_indices.next_if_eq(&ix).is_some()
138 && is_grapheme_boundary
139 && first_non_whitespace_ix.is_some()
140 {
141 last_candidate_ix = ix;
142 last_candidate_width = width;
143 }
144 let item_width = match candidate {
145 WrapBoundaryCandidate::Char { character: c } => {
146 if c == '\n' {
147 continue;
148 }
149
150 if c != ' ' && first_non_whitespace_ix.is_none() {
151 first_non_whitespace_ix = Some(ix);
152 }
153
154 width_for_char(c)
155 }
156 WrapBoundaryCandidate::Element {
157 width: element_width,
158 ..
159 } => {
160 if first_non_whitespace_ix.is_none() {
161 first_non_whitespace_ix = Some(ix);
162 }
163
164 element_width
165 }
166 };
167
168 width += item_width;
169 if width > wrap_width && grapheme_index > last_wrap_ix {
170 let wrap_at_candidate = last_candidate_ix > last_wrap_ix
171 && width - last_candidate_width <= wrap_width;
172
173 let carried_width = if wrap_at_candidate {
174 width - last_candidate_width
175 } else {
176 width - width_before_grapheme
177 };
178
179 if base_indent.is_none() {
185 if let Some(first_non_whitespace_ix) = first_non_whitespace_ix {
186 base_indent =
187 Some(Self::MAX_INDENT.min(
188 first_non_whitespace_ix.saturating_sub(last_wrap_ix) as u32,
189 ));
190 }
191 }
192
193 let next_indent = match indent_adjustment {
194 IndentAdjustment::NoIndent => 0,
195 IndentAdjustment::SameIndent => base_indent.unwrap_or(0),
196 IndentAdjustment::ExtraColumns(extra) => {
197 if let Some(base_indent) = base_indent {
198 let candidate = base_indent.saturating_add(extra);
199 let candidate_indent_width = width_for_char(' ') * candidate as f32;
200 let min_headroom = carried_width.max(width_for_char(' ') * 2.0);
204 if candidate_indent_width + min_headroom > wrap_width {
205 0
206 } else {
207 Self::MAX_INDENT.min(candidate)
208 }
209 } else {
210 0
211 }
212 }
213 };
214
215 if wrap_at_candidate {
216 last_wrap_ix = last_candidate_ix;
217 width -= last_candidate_width;
218 width_before_grapheme -= last_candidate_width;
219 } else {
220 last_wrap_ix = grapheme_index;
222 width -= width_before_grapheme;
223 width_before_grapheme = px(0.);
224 }
225 last_candidate_ix = 0;
226
227 let indent_width = width_for_char(' ') * next_indent as f32;
228 width += indent_width;
229 width_before_grapheme += indent_width;
230
231 return Some(Boundary::new(last_wrap_ix, next_indent));
232 }
233 }
234
235 None
236 })
237 }
238
239 pub fn should_truncate_line(
243 &mut self,
244 line: &str,
245 truncate_width: Pixels,
246 truncation_affix: &str,
247 truncate_from: TruncateFrom,
248 ) -> Option<usize> {
249 let mut width = px(0.);
250 let suffix_width = truncation_affix
251 .chars()
252 .map(|c| self.width_for_char(c))
253 .fold(px(0.0), |a, x| a + x);
254 let mut truncate_ix = 0;
255
256 match truncate_from {
257 TruncateFrom::Start => {
258 for (ix, c) in line.char_indices().rev() {
259 if width + suffix_width < truncate_width {
260 truncate_ix = ix;
261 }
262
263 let char_width = self.width_for_char(c);
264 width += char_width;
265
266 if width.floor() > truncate_width {
267 return Some(truncate_ix);
268 }
269 }
270 }
271 TruncateFrom::End => {
272 for (ix, c) in line.char_indices() {
273 if width + suffix_width < truncate_width {
274 truncate_ix = ix;
275 }
276
277 let char_width = self.width_for_char(c);
278 width += char_width;
279
280 if width.floor() > truncate_width {
281 return Some(truncate_ix);
282 }
283 }
284 }
285 TruncateFrom::Middle => {}
286 }
287
288 None
289 }
290
291 fn should_truncate_line_middle(
292 &mut self,
293 line: &str,
294 truncate_width: Pixels,
295 truncation_affix: &str,
296 ) -> Option<(usize, usize)> {
297 let suffix_width = truncation_affix
298 .chars()
299 .map(|c| self.width_for_char(c))
300 .fold(px(0.0), |a, x| a + x);
301
302 let total_width: Pixels = line
303 .chars()
304 .map(|c| self.width_for_char(c))
305 .fold(px(0.0), |a, x| a + x);
306
307 if total_width <= truncate_width {
308 return None;
309 }
310
311 let content_budget = truncate_width - suffix_width;
312 if content_budget <= px(0.) {
313 return Some((0, line.len()));
314 }
315
316 let front_budget = content_budget * (2.0 / 3.0);
317 let back_budget = content_budget - front_budget;
318
319 let mut front_width = px(0.);
320 let mut front_end_ix = 0usize;
321 for (ix, c) in line.char_indices() {
322 let char_width = self.width_for_char(c);
323 if front_width + char_width > front_budget {
324 break;
325 }
326 front_width += char_width;
327 front_end_ix = ix + c.len_utf8();
328 }
329
330 let mut back_width = px(0.);
331 let mut back_start_ix = line.len();
332 for (ix, c) in line.char_indices().rev() {
333 let char_width = self.width_for_char(c);
334 if back_width + char_width > back_budget {
335 break;
336 }
337 back_width += char_width;
338 back_start_ix = ix;
339 }
340
341 if front_end_ix >= back_start_ix {
342 return Some((0, line.len()));
343 }
344
345 Some((front_end_ix, back_start_ix))
346 }
347
348 pub fn truncate_line<'a>(
350 &mut self,
351 line: SharedString,
352 truncate_width: Pixels,
353 truncation_affix: &str,
354 runs: &'a [TextRun],
355 truncate_from: TruncateFrom,
356 ) -> (SharedString, Cow<'a, [TextRun]>) {
357 if truncate_from == TruncateFrom::Middle {
358 if let Some((front_end_ix, back_start_ix)) =
359 self.should_truncate_line_middle(&line, truncate_width, truncation_affix)
360 {
361 let result = SharedString::from(format!(
362 "{}{truncation_affix}{}",
363 &line[..front_end_ix],
364 &line[back_start_ix..]
365 ));
366 let mut runs = runs.to_vec();
367 update_runs_after_middle_truncation(
368 truncation_affix,
369 &mut runs,
370 front_end_ix,
371 back_start_ix,
372 );
373 return (result, Cow::Owned(runs));
374 } else {
375 return (line, Cow::Borrowed(runs));
376 }
377 }
378
379 if let Some(truncate_ix) =
380 self.should_truncate_line(&line, truncate_width, truncation_affix, truncate_from)
381 {
382 let result = match truncate_from {
383 TruncateFrom::Start => SharedString::from(format!(
384 "{truncation_affix}{}",
385 &line[line.ceil_char_boundary(truncate_ix + 1)..]
386 )),
387 TruncateFrom::End => SharedString::from(format!(
388 "{}{truncation_affix}",
389 line[..truncate_ix]
390 .trim_end_matches(|c: char| c.is_whitespace() || c.is_ascii_punctuation())
391 )),
392 TruncateFrom::Middle => unreachable!("Middle truncation is handled above"),
393 };
394 let mut runs = runs.to_vec();
395 update_runs_after_truncation(&result, truncation_affix, &mut runs, truncate_from);
396 (result, Cow::Owned(runs))
397 } else {
398 (line, Cow::Borrowed(runs))
399 }
400 }
401
402 pub fn truncate_wrapped_line<'a>(
412 &mut self,
413 text: SharedString,
414 wrap_width: Pixels,
415 max_lines: usize,
416 truncation_affix: &str,
417 runs: &'a [TextRun],
418 truncate_from: TruncateFrom,
419 ) -> (SharedString, Cow<'a, [TextRun]>) {
420 if max_lines <= 1 || truncate_from == TruncateFrom::Start {
421 return self.truncate_line(
422 text,
423 wrap_width * max_lines,
424 truncation_affix,
425 runs,
426 truncate_from,
427 );
428 }
429 if truncate_from == TruncateFrom::Middle {
430 return self.truncate_line(text, wrap_width, truncation_affix, runs, truncate_from);
431 }
432
433 let affix_width: Pixels = truncation_affix
434 .chars()
435 .map(|c| self.width_for_char(c))
436 .sum();
437
438 let mut width = px(0.);
439 let mut line = 0usize;
440 let mut first_non_whitespace_ix = None;
441 let mut last_candidate_ix = 0usize;
442 let mut last_candidate_width = px(0.);
443 let mut last_wrap_ix = 0usize;
444 let mut indent: Option<u32> = None;
445 let mut truncate_ix = 0usize;
446
447 let mut break_indices = Self::break_indices(&text).peekable();
448 for (ix, c) in text.char_indices() {
449 let is_break = break_indices.next_if_eq(&ix).is_some();
452 if c == '\n' {
453 if line >= max_lines - 1 && !text[ix + 1..].trim().is_empty() {
454 let truncated = text[..truncate_ix]
457 .trim_end_matches(|c: char| c.is_whitespace() || c.is_ascii_punctuation());
458 let result = SharedString::from(format!("{truncated}{truncation_affix}"));
459 let mut runs = runs.to_vec();
460 update_runs_after_truncation(
461 &result,
462 truncation_affix,
463 &mut runs,
464 TruncateFrom::End,
465 );
466 return (result, Cow::Owned(runs));
467 }
468
469 line += 1;
471 width = px(0.);
472 first_non_whitespace_ix = None;
473 last_candidate_ix = 0;
474 last_candidate_width = px(0.);
475 last_wrap_ix = ix + 1;
476 indent = None;
477 truncate_ix = ix + 1;
478 continue;
479 }
480
481 let char_width = self.width_for_char(c);
482
483 if is_break && first_non_whitespace_ix.is_some() {
484 last_candidate_ix = ix;
485 last_candidate_width = width;
486 }
487
488 if c != ' ' && first_non_whitespace_ix.is_none() {
489 first_non_whitespace_ix = Some(ix);
490 }
491
492 width += char_width;
493
494 if line < max_lines - 1 {
495 if width > wrap_width && ix > last_wrap_ix {
497 if let (None, Some(first_nw)) = (indent, first_non_whitespace_ix) {
498 indent = Some(Self::MAX_INDENT.min((first_nw - last_wrap_ix) as u32));
499 }
500
501 if last_candidate_ix > last_wrap_ix {
502 last_wrap_ix = last_candidate_ix;
503 width -= last_candidate_width;
504 last_candidate_ix = 0;
505 } else {
506 last_wrap_ix = ix;
507 width = char_width;
508 }
509
510 if let Some(ind) = indent {
511 width += self.width_for_char(' ') * ind as f32;
512 }
513
514 line += 1;
515 truncate_ix = last_wrap_ix;
516 }
517 } else {
518 if width + affix_width <= wrap_width {
521 truncate_ix = ix + c.len_utf8();
522 }
523
524 if width > wrap_width {
525 let truncated = text[..truncate_ix]
526 .trim_end_matches(|c: char| c.is_whitespace() || c.is_ascii_punctuation());
527 let result = SharedString::from(format!("{truncated}{truncation_affix}"));
528 let mut runs = runs.to_vec();
529 update_runs_after_truncation(
530 &result,
531 truncation_affix,
532 &mut runs,
533 TruncateFrom::End,
534 );
535 return (result, Cow::Owned(runs));
536 }
537 }
538 }
539
540 drop(break_indices);
542 (text, Cow::Borrowed(runs))
543 }
544
545 pub(crate) fn break_indices(text: &str) -> impl Iterator<Item = usize> {
547 icu_segmenter::LineSegmenter::new_for_non_complex_scripts(Default::default())
548 .segment_str(text)
549 }
550
551 #[inline(always)]
552 fn width_for_char(&mut self, c: char) -> Pixels {
553 width_for_char(
554 &mut self.char_widths,
555 c,
556 &self.text_system,
557 self.font_id,
558 self.font_size,
559 )
560 }
561}
562
563#[inline(always)]
564fn width_for_char(
565 char_widths: &mut CharWidthCache,
566 c: char,
567 text_system: &TextSystem,
568 font_id: FontId,
569 font_size: Pixels,
570) -> Pixels {
571 if (c as u32) < 128 {
572 if let Some(cached_width) = char_widths.ascii[c as usize] {
573 cached_width
574 } else {
575 let width = text_system.layout_width(font_id, font_size, c);
576 char_widths.ascii[c as usize] = Some(width);
577 width
578 }
579 } else if let Some(cached_width) = char_widths.other.get(&c) {
580 *cached_width
581 } else {
582 let width = text_system.layout_width(font_id, font_size, c);
583 char_widths.other.insert(c, width);
584 width
585 }
586}
587
588pub(crate) fn update_runs_after_truncation(
589 result: &str,
590 ellipsis: &str,
591 runs: &mut Vec<TextRun>,
592 truncate_from: TruncateFrom,
593) {
594 let mut truncate_at = result.len() - ellipsis.len();
595 match truncate_from {
596 TruncateFrom::Start => {
597 for (run_index, run) in runs.iter_mut().enumerate().rev() {
598 if run.len <= truncate_at {
599 truncate_at -= run.len;
600 } else {
601 run.len = truncate_at + ellipsis.len();
602 runs.splice(..run_index, std::iter::empty());
603 break;
604 }
605 }
606 }
607 TruncateFrom::End => {
608 for (run_index, run) in runs.iter_mut().enumerate() {
609 if run.len <= truncate_at {
610 truncate_at -= run.len;
611 } else {
612 run.len = truncate_at + ellipsis.len();
613 runs.truncate(run_index + 1);
614 break;
615 }
616 }
617 }
618 TruncateFrom::Middle => {
619 unreachable!("Middle truncation calls this function with TruncateFrom::End directly")
620 }
621 }
622}
623
624pub(crate) fn update_runs_after_middle_truncation(
625 ellipsis: &str,
626 runs: &mut Vec<TextRun>,
627 front_end_ix: usize,
628 back_start_ix: usize,
629) {
630 let original_runs = std::mem::take(runs);
631 let mut result_runs: Vec<TextRun> = Vec::with_capacity(original_runs.len());
632
633 let mut front_remaining = front_end_ix;
637 let mut front_done = false;
638 for run in &original_runs {
639 if front_done {
640 break;
641 }
642 if run.len <= front_remaining {
643 result_runs.push(run.clone());
644 front_remaining -= run.len;
645 } else {
646 let mut partial = run.clone();
647 partial.len = front_remaining + ellipsis.len();
648 result_runs.push(partial);
649 front_done = true;
650 }
651 }
652 if !front_done {
653 if let Some(last) = result_runs.last_mut() {
656 last.len += ellipsis.len();
657 } else if let Some(first) = original_runs.first() {
658 let mut affix_run = first.clone();
659 affix_run.len = ellipsis.len();
660 result_runs.push(affix_run);
661 }
662 }
663
664 let mut byte_pos = 0usize;
667 for run in &original_runs {
668 let run_end = byte_pos + run.len;
669 if run_end > back_start_ix {
670 if byte_pos < back_start_ix {
671 let mut partial = run.clone();
673 partial.len = run_end - back_start_ix;
674 result_runs.push(partial);
675 } else {
676 result_runs.push(run.clone());
677 }
678 }
679 byte_pos = run_end;
680 }
681
682 *runs = result_runs;
683}
684
685pub enum LineFragment<'a> {
687 Text {
689 text: &'a str,
691 },
692 Element {
694 width: Pixels,
696 len_utf8: usize,
698 },
699}
700
701impl<'a> LineFragment<'a> {
702 pub fn text(text: &'a str) -> Self {
704 LineFragment::Text { text }
705 }
706
707 pub fn element(width: Pixels, len_utf8: usize) -> Self {
709 LineFragment::Element { width, len_utf8 }
710 }
711
712 fn wrap_boundary_candidates(&self) -> impl Iterator<Item = WrapBoundaryCandidate> {
713 let text = match self {
714 LineFragment::Text { text } => text,
715 LineFragment::Element { .. } => "\0",
716 };
717 text.chars().map(move |character| {
718 if let LineFragment::Element { width, len_utf8 } = self {
719 WrapBoundaryCandidate::Element {
720 width: *width,
721 len_utf8: *len_utf8,
722 }
723 } else {
724 WrapBoundaryCandidate::Char { character }
725 }
726 })
727 }
728}
729
730enum WrapBoundaryCandidate {
731 Char { character: char },
732 Element { width: Pixels, len_utf8: usize },
733}
734
735impl WrapBoundaryCandidate {
736 pub fn len_utf8(&self) -> usize {
737 match self {
738 WrapBoundaryCandidate::Char { character } => character.len_utf8(),
739 WrapBoundaryCandidate::Element { len_utf8: len, .. } => *len,
740 }
741 }
742}
743
744#[derive(Copy, Clone, Debug, PartialEq, Eq)]
746pub struct Boundary {
747 pub ix: usize,
749 pub next_indent: u32,
751}
752
753impl Boundary {
754 fn new(ix: usize, next_indent: u32) -> Self {
755 Self { ix, next_indent }
756 }
757}
758
759#[cfg(test)]
760mod tests {
761 use super::*;
762 use crate::{Font, FontFeatures, FontStyle, FontWeight, TestAppContext, TestDispatcher, font};
763 #[cfg(target_os = "macos")]
764 use crate::{TextRun, WindowTextSystem, WrapBoundary};
765
766 fn build_wrapper() -> LineWrapper {
767 let dispatcher = TestDispatcher::new(0);
768 let cx = TestAppContext::build(dispatcher, None);
769 let id = cx.text_system().resolve_font(&font(".ZedMono"));
770 LineWrapper::new(
771 id,
772 px(16.),
773 cx.text_system().clone(),
774 CharWidthCache::default(),
775 )
776 }
777
778 fn assert_emergency_wrap_preserves_graphemes(wrapper: &mut LineWrapper) {
779 use unicode_segmentation::UnicodeSegmentation;
780
781 let mut failures = Vec::new();
782 for cluster in ["e\u{301}", "か\u{3099}", "👍🏽", "🇯🇵", "👩\u{200d}💻"] {
783 let cluster_width: Pixels = cluster
784 .chars()
785 .map(|character| wrapper.width_for_char(character))
786 .sum();
787 let first_width = cluster
788 .chars()
789 .next()
790 .map(|character| wrapper.width_for_char(character))
791 .expect("nonempty fixture");
792 for text in [cluster.to_owned(), format!("x{cluster}y")] {
793 let grapheme_boundaries: Vec<_> = text
794 .grapheme_indices(true)
795 .map(|(index, _)| index)
796 .chain(std::iter::once(text.len()))
797 .collect();
798 assert!(
799 LineWrapper::break_indices(&text)
800 .all(|index| grapheme_boundaries.contains(&index)),
801 "UAX #14 boundary inside {text:?}"
802 );
803 let fragments = [LineFragment::text(&text)];
804 let full_width: Pixels = text
805 .chars()
806 .map(|character| wrapper.width_for_char(character))
807 .sum();
808 assert!(
809 wrapper
810 .wrap_line(&fragments, full_width + px(1.), IndentAdjustment::NoIndent)
811 .next()
812 .is_none()
813 );
814 for wrap_width in [
815 first_width - px(0.1),
816 first_width,
817 first_width + px(0.1),
818 cluster_width - px(0.1),
819 cluster_width,
820 cluster_width + px(0.1),
821 ] {
822 let boundaries: Vec<_> = wrapper
823 .wrap_line(&fragments, wrap_width, IndentAdjustment::NoIndent)
824 .map(|boundary| boundary.ix)
825 .collect();
826 let split_fragments: Vec<_> = text
827 .split_inclusive(|_: char| true)
828 .map(LineFragment::text)
829 .collect();
830 let split_boundaries: Vec<_> = wrapper
831 .wrap_line(&split_fragments, wrap_width, IndentAdjustment::NoIndent)
832 .map(|boundary| boundary.ix)
833 .collect();
834 assert_eq!(
835 boundaries, split_boundaries,
836 "fragmentation changed {text:?}"
837 );
838 let invalid: Vec<_> = boundaries
839 .iter()
840 .copied()
841 .filter(|index| !grapheme_boundaries.contains(index))
842 .collect();
843 if !invalid.is_empty() {
844 failures.push(format!("text={text:?}, width={wrap_width:?}, cluster_width={cluster_width:?}, breaks={boundaries:?}, inside_grapheme={invalid:?}"));
845 }
846 }
847 }
848 }
849 assert!(failures.is_empty(), "{}", failures.join("\n"));
850 }
851
852 #[test]
853 fn test_emergency_wrap_preserves_graphemes() {
854 assert_emergency_wrap_preserves_graphemes(&mut build_wrapper());
855 }
856
857 #[test]
858 fn test_emergency_wrap_exact_boundaries() {
859 let mut wrapper = build_wrapper();
860 wrapper.char_widths.ascii.fill(Some(px(1.)));
861 wrapper.char_widths.other.insert('\u{301}', px(1.));
862
863 for (text, width, adjustment, expected) in [
864 (
865 "xe\u{301}y",
866 1.,
867 IndentAdjustment::NoIndent,
868 vec![Boundary::new(1, 0), Boundary::new(4, 0)],
869 ),
870 (
871 " xe\u{301}yze\u{301}q",
872 5.,
873 IndentAdjustment::SameIndent,
874 vec![Boundary::new(6, 2), Boundary::new(8, 2)],
875 ),
876 (
877 " xe\u{301}yze\u{301}q",
878 5.,
879 IndentAdjustment::ExtraColumns(1),
880 vec![
881 Boundary::new(6, 3),
882 Boundary::new(8, 3),
883 Boundary::new(11, 3),
884 ],
885 ),
886 ] {
887 let fragments = [LineFragment::text(text)];
888 assert_eq!(
889 wrapper
890 .wrap_line(&fragments, px(width), adjustment)
891 .collect::<Vec<_>>(),
892 expected,
893 "{text:?}, {adjustment:?}",
894 );
895 }
896 }
897
898 #[test]
899 fn test_emergency_wrap_elements_and_graphemes() {
900 let mut wrapper = build_wrapper();
901 wrapper.char_widths.ascii.fill(Some(px(1.)));
902 wrapper.char_widths.other.insert('\u{301}', px(1.));
903
904 for element_length in [1, 3, 32] {
905 let fragments = [
906 LineFragment::text("xe\u{301}"),
907 LineFragment::element(px(4.), element_length),
908 LineFragment::text("e\u{301}yz"),
909 ];
910 assert_eq!(
911 wrapper
912 .wrap_line(&fragments, px(2.), IndentAdjustment::NoIndent)
913 .collect::<Vec<_>>(),
914 vec![
915 Boundary::new(1, 0),
916 Boundary::new(4, 0),
917 Boundary::new(4 + element_length, 0),
918 Boundary::new(7 + element_length, 0),
919 ],
920 "element_length={element_length}",
921 );
922 }
923 }
924
925 fn generate_test_runs(input_run_len: &[usize]) -> Vec<TextRun> {
926 input_run_len
927 .iter()
928 .map(|run_len| TextRun {
929 len: *run_len,
930 font: Font {
931 family: "Dummy".into(),
932 features: FontFeatures::default(),
933 fallbacks: None,
934 weight: FontWeight::default(),
935 style: FontStyle::Normal,
936 },
937 ..Default::default()
938 })
939 .collect()
940 }
941
942 #[test]
943 fn test_wrap_line() {
944 let mut wrapper = build_wrapper();
945
946 assert_eq!(
947 wrapper
948 .wrap_line(
949 &[LineFragment::text("aa bbb cccc ddddd eeee")],
950 px(72.),
951 IndentAdjustment::default()
952 )
953 .collect::<Vec<_>>(),
954 &[
955 Boundary::new(7, 0),
956 Boundary::new(12, 0),
957 Boundary::new(18, 0)
958 ],
959 );
960 assert_eq!(
961 wrapper
962 .wrap_line(
963 &[LineFragment::text("aaa aaaaaaaaaaaaaaaaaa")],
964 px(72.0),
965 IndentAdjustment::default()
966 )
967 .collect::<Vec<_>>(),
968 &[
969 Boundary::new(4, 0),
970 Boundary::new(11, 0),
971 Boundary::new(18, 0)
972 ],
973 );
974 assert_eq!(
975 wrapper
976 .wrap_line(
977 &[LineFragment::text(" aaaaaaa")],
978 px(72.),
979 IndentAdjustment::default()
980 )
981 .collect::<Vec<_>>(),
982 &[
983 Boundary::new(7, 5),
984 Boundary::new(9, 5),
985 Boundary::new(11, 5),
986 ]
987 );
988 assert_eq!(
989 wrapper
990 .wrap_line(
991 &[LineFragment::text(" ")],
992 px(72.),
993 IndentAdjustment::default()
994 )
995 .collect::<Vec<_>>(),
996 &[
997 Boundary::new(7, 0),
998 Boundary::new(14, 0),
999 Boundary::new(21, 0)
1000 ]
1001 );
1002 assert_eq!(
1003 wrapper
1004 .wrap_line(
1005 &[LineFragment::text(" aaaaaaaaaaaaaa")],
1006 px(72.),
1007 IndentAdjustment::default()
1008 )
1009 .collect::<Vec<_>>(),
1010 &[
1011 Boundary::new(7, 0),
1012 Boundary::new(14, 3),
1013 Boundary::new(18, 3),
1014 Boundary::new(22, 3),
1015 ]
1016 );
1017
1018 assert_eq!(
1020 wrapper
1021 .wrap_line(
1022 &[
1023 LineFragment::text("aa bbb "),
1024 LineFragment::text("cccc ddddd eeee")
1025 ],
1026 px(72.),
1027 IndentAdjustment::default()
1028 )
1029 .collect::<Vec<_>>(),
1030 &[
1031 Boundary::new(7, 0),
1032 Boundary::new(12, 0),
1033 Boundary::new(18, 0)
1034 ],
1035 );
1036
1037 assert_eq!(
1039 wrapper
1040 .wrap_line(
1041 &[
1042 LineFragment::text("aa "),
1043 LineFragment::element(px(20.), 1),
1044 LineFragment::text(" bbb "),
1045 LineFragment::element(px(30.), 1),
1046 LineFragment::text(" cccc")
1047 ],
1048 px(72.),
1049 IndentAdjustment::default()
1050 )
1051 .collect::<Vec<_>>(),
1052 &[
1053 Boundary::new(5, 0),
1054 Boundary::new(9, 0),
1055 Boundary::new(11, 0)
1056 ],
1057 );
1058
1059 assert_eq!(
1061 wrapper
1062 .wrap_line(
1063 &[
1064 LineFragment::element(px(50.), 1),
1065 LineFragment::text(" aaaa bbbb cccc dddd")
1066 ],
1067 px(72.),
1068 IndentAdjustment::default()
1069 )
1070 .collect::<Vec<_>>(),
1071 &[
1072 Boundary::new(2, 0),
1073 Boundary::new(7, 0),
1074 Boundary::new(12, 0),
1075 Boundary::new(17, 0)
1076 ],
1077 );
1078
1079 assert_eq!(
1081 wrapper
1082 .wrap_line(
1083 &[
1084 LineFragment::text("short text "),
1085 LineFragment::element(px(100.), 1),
1086 LineFragment::text(" more text")
1087 ],
1088 px(72.),
1089 IndentAdjustment::default()
1090 )
1091 .collect::<Vec<_>>(),
1092 &[
1093 Boundary::new(6, 0),
1094 Boundary::new(11, 0),
1095 Boundary::new(12, 0),
1096 Boundary::new(18, 0)
1097 ],
1098 );
1099
1100 assert_eq!(
1102 wrapper
1103 .wrap_line(
1104 &[LineFragment::text("a\u{202F}b\u{00A0}c\u{2011}d e")],
1105 px(72.0),
1106 IndentAdjustment::default()
1107 )
1108 .collect::<Vec<_>>(),
1109 &[Boundary::new(12, 0),], );
1111 }
1112
1113 #[test]
1114 fn test_wrap_line_break_opportunities() {
1115 let mut wrapper = build_wrapper();
1116 for (text, fitting_prefix, expected_index) in [
1117 ("aaa required!", "aaa required", 4),
1118 ("aaa foo/bar", "aaa foo/", 8),
1119 ("aaa (hello)", "aaa (hello", 4),
1120 ("aaa αβγδε", "aaa αβγδ", 4),
1121 ("aaa a\u{a0}b", "aaa a\u{a0}", 4),
1122 ("aaa 你好!", "aaa 你好", 7),
1123 ] {
1124 let wrap_width = fitting_prefix
1125 .chars()
1126 .map(|character| wrapper.width_for_char(character))
1127 .sum();
1128 assert_eq!(
1129 wrapper
1130 .wrap_line(
1131 &[LineFragment::text(text)],
1132 wrap_width,
1133 IndentAdjustment::default()
1134 )
1135 .collect::<Vec<_>>(),
1136 [Boundary::new(expected_index, 0)],
1137 "{text:?}",
1138 );
1139 }
1140 }
1141
1142 #[test]
1143 fn test_truncate_line_end() {
1144 let mut wrapper = build_wrapper();
1145
1146 fn perform_test(
1147 wrapper: &mut LineWrapper,
1148 text: &'static str,
1149 expected: &'static str,
1150 ellipsis: &str,
1151 ) {
1152 let dummy_run_lens = vec![text.len()];
1153 let dummy_runs = generate_test_runs(&dummy_run_lens);
1154 let (result, dummy_runs) = wrapper.truncate_line(
1155 text.into(),
1156 px(220.),
1157 ellipsis,
1158 &dummy_runs,
1159 TruncateFrom::End,
1160 );
1161 assert_eq!(result, expected);
1162 assert_eq!(dummy_runs.first().unwrap().len, result.len());
1163 }
1164
1165 perform_test(
1166 &mut wrapper,
1167 "aa bbb cccc ddddd eeee ffff gggg",
1168 "aa bbb cccc ddddd eeee",
1169 "",
1170 );
1171 perform_test(
1172 &mut wrapper,
1173 "aa bbb cccc ddddd eeee ffff gggg",
1174 "aa bbb cccc ddddd eee…",
1175 "…",
1176 );
1177 perform_test(
1178 &mut wrapper,
1179 "aa bbb cccc ddddd eeee ffff gggg",
1180 "aa bbb cccc dddd......",
1181 "......",
1182 );
1183 perform_test(
1184 &mut wrapper,
1185 "aa bbb cccc 🦀🦀🦀🦀🦀 eeee ffff gggg",
1186 "aa bbb cccc 🦀🦀🦀🦀…",
1187 "…",
1188 );
1189 }
1190
1191 #[test]
1192 fn test_truncate_line_start() {
1193 let mut wrapper = build_wrapper();
1194
1195 #[track_caller]
1196 fn perform_test(
1197 wrapper: &mut LineWrapper,
1198 text: &'static str,
1199 expected: &'static str,
1200 ellipsis: &str,
1201 ) {
1202 let dummy_run_lens = vec![text.len()];
1203 let dummy_runs = generate_test_runs(&dummy_run_lens);
1204 let (result, dummy_runs) = wrapper.truncate_line(
1205 text.into(),
1206 px(220.),
1207 ellipsis,
1208 &dummy_runs,
1209 TruncateFrom::Start,
1210 );
1211 assert_eq!(result, expected);
1212 assert_eq!(dummy_runs.first().unwrap().len, result.len());
1213 }
1214
1215 perform_test(
1216 &mut wrapper,
1217 "aaaa bbbb cccc ddddd eeee fff gg",
1218 "cccc ddddd eeee fff gg",
1219 "",
1220 );
1221 perform_test(
1222 &mut wrapper,
1223 "aaaa bbbb cccc ddddd eeee fff gg",
1224 "…ccc ddddd eeee fff gg",
1225 "…",
1226 );
1227 perform_test(
1228 &mut wrapper,
1229 "aaaa bbbb cccc ddddd eeee fff gg",
1230 "......dddd eeee fff gg",
1231 "......",
1232 );
1233 perform_test(
1234 &mut wrapper,
1235 "aaaa bbbb cccc 🦀🦀🦀🦀🦀 eeee fff gg",
1236 "…🦀🦀🦀🦀 eeee fff gg",
1237 "…",
1238 );
1239 }
1240
1241 #[test]
1242 fn test_truncate_multiple_runs_end() {
1243 let mut wrapper = build_wrapper();
1244
1245 fn perform_test(
1246 wrapper: &mut LineWrapper,
1247 text: &'static str,
1248 expected: &str,
1249 run_lens: &[usize],
1250 result_run_len: &[usize],
1251 line_width: Pixels,
1252 ) {
1253 let dummy_runs = generate_test_runs(run_lens);
1254 let (result, dummy_runs) =
1255 wrapper.truncate_line(text.into(), line_width, "…", &dummy_runs, TruncateFrom::End);
1256 assert_eq!(result, expected);
1257 for (run, result_len) in dummy_runs.iter().zip(result_run_len) {
1258 assert_eq!(run.len, *result_len);
1259 }
1260 }
1261 perform_test(&mut wrapper, "abcdefghijkl", "abcd…", &[12], &[7], px(50.));
1268 perform_test(
1276 &mut wrapper,
1277 "abcdefghijkl",
1278 "abcdef…",
1279 &[4, 4, 4],
1280 &[4, 5],
1281 px(70.),
1282 );
1283 perform_test(
1291 &mut wrapper,
1292 "abcdefghijkl",
1293 "abcdefgh…",
1294 &[4, 4, 4],
1295 &[4, 4, 3],
1296 px(90.),
1297 );
1298 }
1299
1300 #[test]
1301 fn test_truncate_multiple_runs_start() {
1302 let mut wrapper = build_wrapper();
1303
1304 #[track_caller]
1305 fn perform_test(
1306 wrapper: &mut LineWrapper,
1307 text: &'static str,
1308 expected: &str,
1309 run_lens: &[usize],
1310 result_run_len: &[usize],
1311 line_width: Pixels,
1312 ) {
1313 let dummy_runs = generate_test_runs(run_lens);
1314 let (result, dummy_runs) = wrapper.truncate_line(
1315 text.into(),
1316 line_width,
1317 "…",
1318 &dummy_runs,
1319 TruncateFrom::Start,
1320 );
1321 assert_eq!(result, expected);
1322 for (run, result_len) in dummy_runs.iter().zip(result_run_len) {
1323 assert_eq!(run.len, *result_len);
1324 }
1325 }
1326 perform_test(&mut wrapper, "abcdefghijkl", "…ijkl", &[12], &[7], px(50.));
1333 perform_test(
1341 &mut wrapper,
1342 "abcdefghijkl",
1343 "…ghijkl",
1344 &[4, 4, 4],
1345 &[5, 4],
1346 px(70.),
1347 );
1348 perform_test(
1356 &mut wrapper,
1357 "abcdefghijkl",
1358 "…efghijkl",
1359 &[4, 4, 4],
1360 &[3, 4, 4],
1361 px(90.),
1362 );
1363 }
1364
1365 #[test]
1366 fn test_update_run_after_truncation_end() {
1367 fn perform_test(result: &str, run_lens: &[usize], result_run_lens: &[usize]) {
1368 let mut dummy_runs = generate_test_runs(run_lens);
1369 update_runs_after_truncation(result, "…", &mut dummy_runs, TruncateFrom::End);
1370 for (run, result_len) in dummy_runs.iter().zip(result_run_lens) {
1371 assert_eq!(run.len, *result_len);
1372 }
1373 }
1374 perform_test("abcd…", &[12], &[7]);
1381 perform_test("abcdef…", &[4, 4, 4], &[4, 5]);
1389 perform_test("abcdefgh…", &[4, 4, 4], &[4, 4, 3]);
1397 }
1398
1399 #[cfg(target_os = "macos")]
1401 use crate as gpui;
1402
1403 #[cfg(target_os = "macos")]
1405 #[crate::test]
1406 fn test_wrap_shaped_line(cx: &mut TestAppContext) {
1407 cx.update(|cx| {
1408 let text_system = WindowTextSystem::new(cx.text_system().clone());
1409
1410 let normal = TextRun {
1411 len: 0,
1412 font: font("Helvetica"),
1413 color: Default::default(),
1414 underline: Default::default(),
1415 ..Default::default()
1416 };
1417 let bold = TextRun {
1418 len: 0,
1419 font: font("Helvetica").bold(),
1420 ..Default::default()
1421 };
1422
1423 let text = "aa bbb cccc ddddd eeee".into();
1424 let lines = text_system
1425 .shape_text(
1426 text,
1427 px(16.),
1428 &[
1429 normal.with_len(4),
1430 bold.with_len(5),
1431 normal.with_len(6),
1432 bold.with_len(1),
1433 normal.with_len(7),
1434 ],
1435 Some(px(72.)),
1436 None,
1437 )
1438 .unwrap();
1439
1440 assert_eq!(
1441 lines[0].layout.wrap_boundaries(),
1442 &[
1443 WrapBoundary {
1444 run_ix: 0,
1445 glyph_ix: 7
1446 },
1447 WrapBoundary {
1448 run_ix: 0,
1449 glyph_ix: 12
1450 },
1451 WrapBoundary {
1452 run_ix: 0,
1453 glyph_ix: 18
1454 }
1455 ],
1456 );
1457 });
1458 }
1459
1460 #[test]
1461 fn test_multiline_truncation_fits_within_wrapped_lines() {
1462 let mut wrapper = build_wrapper();
1463
1464 let text: &str = "aa bbbbbb cccccc dddddd eeee ffff";
1476 let wrap_width = px(72.);
1477 let max_lines: usize = 2;
1478
1479 let runs = generate_test_runs(&[text.len()]);
1480 let (truncated, _) = wrapper.truncate_wrapped_line(
1481 text.into(),
1482 wrap_width,
1483 max_lines,
1484 "\u{2026}",
1485 &runs,
1486 TruncateFrom::End,
1487 );
1488
1489 let wrap_count = wrapper
1491 .wrap_line(
1492 &[LineFragment::text(&truncated)],
1493 wrap_width,
1494 IndentAdjustment::default(),
1495 )
1496 .count();
1497
1498 assert!(
1499 wrap_count < max_lines,
1500 "Truncated text '{}' wraps into {} visual lines, expected at most {}",
1501 truncated,
1502 wrap_count + 1,
1503 max_lines
1504 );
1505
1506 assert!(
1508 truncated.ends_with('\u{2026}'),
1509 "Truncated text '{}' should end with ellipsis",
1510 truncated
1511 );
1512 }
1513
1514 #[test]
1515 fn test_multiline_truncation_no_truncation_needed() {
1516 let mut wrapper = build_wrapper();
1517
1518 let text: &str = "aa bbb cccccc";
1521 let wrap_width = px(72.);
1522 let max_lines: usize = 2;
1523
1524 let runs = generate_test_runs(&[text.len()]);
1525 let (result, _) = wrapper.truncate_wrapped_line(
1526 text.into(),
1527 wrap_width,
1528 max_lines,
1529 "\u{2026}",
1530 &runs,
1531 TruncateFrom::End,
1532 );
1533
1534 assert_eq!(
1535 result.as_ref(),
1536 text,
1537 "Text that fits should not be modified"
1538 );
1539 }
1540
1541 #[test]
1542 fn test_multiline_truncation_three_lines() {
1543 let mut wrapper = build_wrapper();
1544
1545 let text: &str = "aa bbb cccc ddddd eeee ffff gggg hhhh iiii jjjj";
1546 let wrap_width = px(72.);
1547 let max_lines: usize = 3;
1548
1549 let runs = generate_test_runs(&[text.len()]);
1550 let (truncated, _) = wrapper.truncate_wrapped_line(
1551 text.into(),
1552 wrap_width,
1553 max_lines,
1554 "\u{2026}",
1555 &runs,
1556 TruncateFrom::End,
1557 );
1558
1559 let wrap_count = wrapper
1560 .wrap_line(
1561 &[LineFragment::text(&truncated)],
1562 wrap_width,
1563 IndentAdjustment::default(),
1564 )
1565 .count();
1566
1567 assert!(
1568 wrap_count < max_lines,
1569 "Truncated text '{}' wraps into {} visual lines, expected at most {}",
1570 truncated,
1571 wrap_count + 1,
1572 max_lines
1573 );
1574
1575 assert!(
1576 truncated.ends_with('\u{2026}'),
1577 "Truncated text '{}' should end with ellipsis",
1578 truncated
1579 );
1580 }
1581
1582 #[test]
1583 fn test_multiline_truncation_preserves_breaks_after_empty_lines() {
1584 let mut wrapper = build_wrapper();
1585 for prefix in ["", "\n", "\n\n", "hello\n\n"] {
1586 let text = format!("{prefix}aaa required! more words follow");
1587 let runs = generate_test_runs(&[text.len()]);
1588 let max_lines = prefix.bytes().filter(|byte| *byte == b'\n').count() + 2;
1589 let (truncated, _) = wrapper.truncate_wrapped_line(
1590 text.into(),
1591 px(72.),
1592 max_lines,
1593 "…",
1594 &runs,
1595 TruncateFrom::End,
1596 );
1597 assert_eq!(
1598 truncated.as_ref(),
1599 format!("{prefix}aaa {}…", &"required!"[..6]),
1600 "prefix: {prefix:?}",
1601 );
1602 }
1603 }
1604
1605 #[test]
1606 fn test_multiline_truncation_with_newlines() {
1607 let mut wrapper = build_wrapper();
1608
1609 let text: &str = "hello\nworld foo bar baz";
1614 let wrap_width = px(72.);
1615 let max_lines: usize = 2;
1616
1617 let runs = generate_test_runs(&[text.len()]);
1618 let (truncated, _) = wrapper.truncate_wrapped_line(
1619 text.into(),
1620 wrap_width,
1621 max_lines,
1622 "\u{2026}",
1623 &runs,
1624 TruncateFrom::End,
1625 );
1626
1627 let parts: Vec<&str> = truncated.splitn(2, '\n').collect();
1629 assert_eq!(
1630 parts.len(),
1631 2,
1632 "Newline should be preserved: '{}'",
1633 truncated
1634 );
1635 assert_eq!(parts[0], "hello");
1636
1637 let second_line_width: Pixels = parts[1].chars().map(|c| wrapper.width_for_char(c)).sum();
1639 assert!(
1640 second_line_width <= wrap_width,
1641 "Second line '{}' ({}px) exceeds wrap_width ({}px)",
1642 parts[1],
1643 second_line_width,
1644 wrap_width
1645 );
1646 assert!(
1647 truncated.ends_with('\u{2026}'),
1648 "Should end with ellipsis: '{}'",
1649 truncated
1650 );
1651 }
1652
1653 #[test]
1654 fn test_multiline_truncation_newline_on_last_line() {
1655 let mut wrapper = build_wrapper();
1656
1657 let text: &str = "hello\nworld\nmore";
1661 let wrap_width = px(72.);
1662 let max_lines: usize = 2;
1663
1664 let runs = generate_test_runs(&[text.len()]);
1665 let (truncated, _) = wrapper.truncate_wrapped_line(
1666 text.into(),
1667 wrap_width,
1668 max_lines,
1669 "\u{2026}",
1670 &runs,
1671 TruncateFrom::End,
1672 );
1673
1674 let parts: Vec<&str> = truncated.splitn(2, '\n').collect();
1675 assert_eq!(parts[0], "hello");
1676 assert!(
1677 truncated.ends_with('\u{2026}'),
1678 "Should end with ellipsis since there's more content: '{}'",
1679 truncated
1680 );
1681 }
1682
1683 #[test]
1684 fn test_truncate_line_middle() {
1685 let mut wrapper = build_wrapper();
1686
1687 let short_text = "hello world";
1689 let runs = generate_test_runs(&[short_text.len()]);
1690 let (result, result_runs) = wrapper.truncate_line(
1691 short_text.into(),
1692 px(10000.),
1693 "…",
1694 &runs,
1695 TruncateFrom::Middle,
1696 );
1697 assert_eq!(result.as_ref(), short_text);
1698 assert_eq!(result_runs.len(), 1);
1699 assert_eq!(result_runs[0].len, short_text.len());
1700
1701 let long_text = "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz";
1703 let runs = generate_test_runs(&[long_text.len()]);
1704 let (result, _result_runs) =
1705 wrapper.truncate_line(long_text.into(), px(100.), "…", &runs, TruncateFrom::Middle);
1706 assert!(
1707 result.contains('…'),
1708 "Middle-truncated result should contain '…', got: '{}'",
1709 result
1710 );
1711 assert!(
1712 result.chars().count() < long_text.chars().count(),
1713 "Middle-truncated result should be shorter than original"
1714 );
1715 assert_eq!(
1716 result.chars().next(),
1717 long_text.chars().next(),
1718 "Result should start with the same first character as original"
1719 );
1720 assert_eq!(
1721 result.chars().last(),
1722 long_text.chars().last(),
1723 "Result should end with the same last character as original"
1724 );
1725
1726 let text = "abcdef";
1729 let runs = generate_test_runs(&[text.len()]);
1730 let (result, result_runs) =
1731 wrapper.truncate_line(text.into(), px(1.), "…", &runs, TruncateFrom::Middle);
1732 assert_eq!(result.as_ref(), "…");
1733 assert_eq!(result_runs.len(), 1);
1734 assert_eq!(result_runs[0].len, "…".len());
1735
1736 let multi_run_text = "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz";
1739 let run_lens = [20, 20, multi_run_text.len() - 40];
1740 let runs = generate_test_runs(&run_lens);
1741 let (result, result_runs) = wrapper.truncate_line(
1742 multi_run_text.into(),
1743 px(100.),
1744 "…",
1745 &runs,
1746 TruncateFrom::Middle,
1747 );
1748 let total_run_len: usize = result_runs.iter().map(|r| r.len).sum();
1749 assert_eq!(
1750 total_run_len,
1751 result.len(),
1752 "Sum of run lengths ({}) should equal result byte length ({})",
1753 total_run_len,
1754 result.len()
1755 );
1756 }
1757
1758 #[test]
1759 fn test_multiline_truncation_trailing_newline() {
1760 let mut wrapper = build_wrapper();
1761
1762 let text: &str = "hello\nworld\n";
1765 let wrap_width = px(72.);
1766 let max_lines: usize = 2;
1767
1768 let runs = generate_test_runs(&[text.len()]);
1769 let (result, _) = wrapper.truncate_wrapped_line(
1770 text.into(),
1771 wrap_width,
1772 max_lines,
1773 "\u{2026}",
1774 &runs,
1775 TruncateFrom::End,
1776 );
1777
1778 assert!(
1779 !result.ends_with('\u{2026}'),
1780 "Trailing newline with no content should not add ellipsis: '{}'",
1781 result
1782 );
1783 }
1784
1785 #[test]
1786 fn test_multiline_truncation_newline_fits_exactly() {
1787 let mut wrapper = build_wrapper();
1788
1789 let text: &str = "hello\nworld";
1792 let wrap_width = px(72.);
1793 let max_lines: usize = 2;
1794
1795 let runs = generate_test_runs(&[text.len()]);
1796 let (result, _) = wrapper.truncate_wrapped_line(
1797 text.into(),
1798 wrap_width,
1799 max_lines,
1800 "\u{2026}",
1801 &runs,
1802 TruncateFrom::End,
1803 );
1804
1805 assert_eq!(
1806 result.as_ref(),
1807 text,
1808 "Text that fits exactly should not be modified: '{}'",
1809 result
1810 );
1811 }
1812
1813 #[test]
1814 fn test_extra_columns_overflow_guard() {
1815 let mut wrapper = build_wrapper();
1816 let space_width = wrapper.width_for_char(' ');
1817
1818 let text = " ab cd ef gh";
1820 let wrap_width = space_width * 10.0;
1821
1822 assert_eq!(
1829 wrapper
1830 .wrap_line(
1831 &[LineFragment::text(text)],
1832 wrap_width,
1833 IndentAdjustment::ExtraColumns(8),
1834 )
1835 .collect::<Vec<_>>(),
1836 &[Boundary::new(9, 0)]
1837 );
1838
1839 assert_eq!(
1852 wrapper
1853 .wrap_line(
1854 &[LineFragment::text(text)],
1855 wrap_width,
1856 IndentAdjustment::ExtraColumns(2),
1857 )
1858 .collect::<Vec<_>>(),
1859 &[
1860 Boundary::new(9, 8),
1861 Boundary::new(11, 8),
1862 Boundary::new(12, 8),
1863 Boundary::new(14, 8),
1864 Boundary::new(15, 8),
1865 ]
1866 );
1867
1868 let full_width_text = " 🦀🦀🦀🦀🦀🦀🦀🦀";
1881 assert_eq!(
1882 wrapper
1883 .wrap_line(
1884 &[LineFragment::text(full_width_text)],
1885 wrap_width,
1886 IndentAdjustment::ExtraColumns(8),
1887 )
1888 .collect::<Vec<_>>(),
1889 &[Boundary::new(17, 0)]
1890 );
1891
1892 assert_eq!(
1899 wrapper
1900 .wrap_line(
1901 &[LineFragment::text("a abcdefghi")],
1902 wrap_width,
1903 IndentAdjustment::ExtraColumns(1),
1904 )
1905 .collect::<Vec<_>>(),
1906 &[Boundary::new(2, 1)]
1907 );
1908
1909 assert_eq!(
1916 wrapper
1917 .wrap_line(
1918 &[LineFragment::text("a abcdefghi")],
1919 wrap_width,
1920 IndentAdjustment::ExtraColumns(2),
1921 )
1922 .collect::<Vec<_>>(),
1923 &[Boundary::new(2, 0)]
1924 );
1925
1926 let carried_text = "a abcdefghij";
1938 assert_eq!(
1939 wrapper
1940 .wrap_line(
1941 &[LineFragment::text(carried_text)],
1942 wrap_width,
1943 IndentAdjustment::ExtraColumns(8),
1944 )
1945 .collect::<Vec<_>>(),
1946 &[Boundary::new(2, 0)]
1947 );
1948
1949 let element_fragments = [
1967 LineFragment::text("abcdefghijk "),
1968 LineFragment::element(space_width * 8.0, 1),
1969 LineFragment::text("z 12"),
1970 ];
1971 assert_eq!(
1972 wrapper
1973 .wrap_line(
1974 &element_fragments,
1975 wrap_width,
1976 IndentAdjustment::ExtraColumns(8),
1977 )
1978 .collect::<Vec<_>>(),
1979 &[
1980 Boundary::new(10, 8),
1981 Boundary::new(12, 0),
1982 Boundary::new(15, 8),
1983 ]
1984 );
1985
1986 let spaces = " "; assert_eq!(
1990 wrapper
1991 .wrap_line(
1992 &[LineFragment::text(spaces)],
1993 wrap_width,
1994 IndentAdjustment::ExtraColumns(8),
1995 )
1996 .collect::<Vec<_>>(),
1997 &[Boundary::new(10, 0)]
1998 );
1999
2000 let multiline_indent_text = " ab cd ef gh";
2009 assert_eq!(
2010 wrapper
2011 .wrap_line(
2012 &[LineFragment::text(multiline_indent_text)],
2013 wrap_width,
2014 IndentAdjustment::ExtraColumns(2),
2015 )
2016 .collect::<Vec<_>>(),
2017 &[
2018 Boundary::new(10, 0),
2019 Boundary::new(20, 6),
2020 Boundary::new(23, 6),
2021 ]
2022 );
2023
2024 let oversized_suffix_fragments = [
2041 LineFragment::text("aaaaaaaaaaaaaaaaaaa a"),
2042 LineFragment::element(space_width * 10.0, 1),
2043 ];
2044 assert_eq!(
2045 wrapper
2046 .wrap_line(
2047 &oversized_suffix_fragments,
2048 wrap_width,
2049 IndentAdjustment::ExtraColumns(2),
2050 )
2051 .collect::<Vec<_>>(),
2052 &[
2053 Boundary::new(10, 2),
2054 Boundary::new(18, 2),
2055 Boundary::new(21, 0),
2056 ]
2057 );
2058
2059 assert_eq!(
2060 wrapper
2061 .wrap_line(
2062 &[LineFragment::text(" aaaaaaaa")],
2063 space_width * 4.0,
2064 IndentAdjustment::ExtraColumns(u32::MAX),
2065 )
2066 .collect::<Vec<_>>(),
2067 &[Boundary::new(4, 0), Boundary::new(8, 0)]
2068 );
2069 }
2070}