use crate::text_system::{update_runs_after_middle_truncation, update_runs_after_truncation};
use crate::{Pixels, SharedString, TextRun, TruncateFrom, Window, WrappedLine};
use gpui_util::ResultExt as _;
use std::borrow::Cow;
#[derive(Clone, Copy, Debug)]
pub(crate) enum Room {
Line(Pixels),
Lines {
wrap_width: Pixels,
max_lines: usize,
},
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn fit<'a>(
text: &SharedString,
runs: &'a [TextRun],
truncated: (SharedString, Cow<'a, [TextRun]>),
affix: &str,
truncate_from: TruncateFrom,
room: Room,
font_size: Pixels,
window: &Window,
) -> (SharedString, Cow<'a, [TextRun]>) {
if fits(&truncated.0, &truncated.1, room, font_size, window) {
return truncated;
}
let mut overflows = if truncated.0 == *text {
text.chars().count()
} else {
truncated
.0
.chars()
.count()
.saturating_sub(affix.chars().count())
};
let mut fitting = 0;
while fitting + 1 < overflows {
let kept = (fitting + overflows) / 2;
let (candidate, candidate_runs) = keep(text, runs, kept, affix, truncate_from);
if fits(&candidate, &candidate_runs, room, font_size, window) {
fitting = kept;
} else {
overflows = kept;
}
}
let (text, runs) = keep(text, runs, fitting, affix, truncate_from);
(text, Cow::Owned(runs))
}
fn keep(
text: &str,
runs: &[TextRun],
kept: usize,
affix: &str,
truncate_from: TruncateFrom,
) -> (SharedString, Vec<TextRun>) {
let mut runs = runs.to_vec();
match truncate_from {
TruncateFrom::End => {
let kept = text[..first_chars(text, kept)]
.trim_end_matches(|c: char| c.is_whitespace() || c.is_ascii_punctuation());
let result = SharedString::from(format!("{kept}{affix}"));
update_runs_after_truncation(&result, affix, &mut runs, TruncateFrom::End);
(result, runs)
}
TruncateFrom::Start => {
let result = SharedString::from(format!("{affix}{}", &text[last_chars(text, kept)..]));
update_runs_after_truncation(&result, affix, &mut runs, TruncateFrom::Start);
(result, runs)
}
TruncateFrom::Middle => {
let front = (kept * 2).div_ceil(3);
let front_end = first_chars(text, front);
let back_start = last_chars(text, kept - front).max(front_end);
let result = SharedString::from(format!(
"{}{affix}{}",
&text[..front_end],
&text[back_start..]
));
update_runs_after_middle_truncation(affix, &mut runs, front_end, back_start);
(result, runs)
}
}
}
fn first_chars(text: &str, count: usize) -> usize {
text.char_indices()
.nth(count)
.map_or(text.len(), |(index, _)| index)
}
fn last_chars(text: &str, count: usize) -> usize {
match count.checked_sub(1) {
None => text.len(),
Some(skip) => text
.char_indices()
.rev()
.nth(skip)
.map_or(0, |(index, _)| index),
}
}
fn fits(
text: &SharedString,
runs: &[TextRun],
room: Room,
font_size: Pixels,
window: &Window,
) -> bool {
match room {
Room::Line(width) => window
.text_system()
.shape_text(text.clone(), font_size, runs, None, None)
.log_err()
.is_none_or(|lines| {
lines
.iter()
.all(|line| line.unwrapped_layout.width <= width)
}),
Room::Lines {
wrap_width,
max_lines,
} => window
.text_system()
.shape_text(
text.clone(),
font_size,
runs,
Some(wrap_width),
Some(max_lines),
)
.log_err()
.is_none_or(|lines| {
let count: usize = lines
.iter()
.map(|line| line.wrap_boundaries.len() + 1)
.sum();
count <= max_lines
&& lines
.last()
.is_none_or(|line| last_line_width(line) <= wrap_width)
}),
}
}
fn last_line_width(line: &WrappedLine) -> Pixels {
let layout = &line.unwrapped_layout;
let start = line
.wrap_boundaries
.last()
.map_or(Pixels::ZERO, |boundary| {
layout.runs[boundary.run_ix].glyphs[boundary.glyph_ix]
.position
.x
});
layout.width - start
}
#[cfg(test)]
mod tests {
use super::{first_chars, last_chars};
#[test]
fn character_counts_land_on_character_boundaries() {
let text = "a,b余";
assert_eq!(first_chars(text, 0), 0);
assert_eq!(first_chars(text, 2), "a,".len());
assert_eq!(first_chars(text, 9), text.len());
assert_eq!(last_chars(text, 0), text.len());
assert_eq!(last_chars(text, 1), text.len() - "余".len());
assert_eq!(last_chars(text, 9), 0);
}
}