Preserve graphemes in mention search match highlighting (#61726)

## Why

Splitting fuzzy matches into individual Unicode scalars can break multi-codepoint emoji when mention results are truncated.

## What changed

Render highlighted names and parent paths as whole grapheme clusters, using scalar match indices for names and grapheme match indices for paths. Bold the entire grapheme when any part matches.

## Testing

Add a regression test covering truncated plugin names and file paths containing `👩‍💻`, checking both rendered text and bold match styling.

GitOrigin-RevId: 516d6668a9a22f79483adb0c576ed190b4f4a784
This commit is contained in:
Eric Traut
2026-11-16 02:41:09 +00:00
committed by copyberry
parent e649c6d74c
commit eff3036bf4
2 changed files with 83 additions and 27 deletions
@@ -13,6 +13,7 @@ use ratatui::style::Stylize;
use ratatui::text::Line;
use ratatui::text::Span;
use ratatui::widgets::Widget;
use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr;
use crate::line_truncation::truncate_line_with_ellipsis_if_overflow;
@@ -274,7 +275,6 @@ fn primary_spans(row: &SearchResult, base_style: Style) -> Vec<Span<'static>> {
return vec![file_name.to_string().set_style(style)];
}
let mut spans = Vec::with_capacity(row.display_name.len());
let name_style = match row.mention_type {
MentionType::Plugin => base_style.fg(crate::style::accent_color()),
MentionType::Skill => base_style.dim(),
@@ -282,20 +282,44 @@ fn primary_spans(row: &SearchResult, base_style: Style) -> Vec<Span<'static>> {
MentionType::File | MentionType::Directory => base_style,
};
if let Some(indices) = row.match_indices.as_ref() {
let mut idx_iter = indices.iter().peekable();
for (char_idx, ch) in row.display_name.chars().enumerate() {
let mut style = name_style;
if idx_iter.peek().is_some_and(|next| **next == char_idx) {
idx_iter.next();
style = style.bold();
}
spans.push(ch.to_string().set_style(style));
}
highlighted_spans(
&row.display_name,
indices,
name_style,
MatchIndexUnit::Scalar,
)
.collect()
} else {
spans.push(row.display_name.clone().set_style(name_style));
vec![row.display_name.clone().set_style(name_style)]
}
}
spans
enum MatchIndexUnit {
Scalar,
Grapheme,
}
fn highlighted_spans<'a>(
text: &'a str,
indices: &'a [usize],
style: Style,
unit: MatchIndexUnit,
) -> impl Iterator<Item = Span<'static>> + 'a {
let mut indices = indices.iter().peekable();
let mut index_end = 0;
text.graphemes(/*is_extended*/ true).map(move |grapheme| {
index_end += match unit {
MatchIndexUnit::Scalar => grapheme.chars().count(),
MatchIndexUnit::Grapheme => 1,
};
let mut matched = false;
while indices.next_if(|index| **index < index_end).is_some() {
matched = true;
}
grapheme
.to_owned()
.set_style(if matched { style.bold() } else { style })
})
}
fn secondary_line(
@@ -328,16 +352,6 @@ fn path_spans(row: &SearchResult, base_style: Style) -> Vec<Span<'static>> {
let path_style = base_style.dim();
if file_name_start == 0 {
spans.push("./".set_style(path_style));
} else if let Some(indices) = row.match_indices.as_ref() {
let mut idx_iter = indices.iter().peekable();
for (char_idx, ch) in row.display_name.chars().enumerate().take(file_name_start) {
let mut style = path_style;
if idx_iter.peek().is_some_and(|next| **next == char_idx) {
idx_iter.next();
style = style.bold();
}
spans.push(ch.to_string().set_style(style));
}
} else if file_name_start != usize::MAX {
let byte_start = row
.display_name
@@ -345,11 +359,17 @@ fn path_spans(row: &SearchResult, base_style: Style) -> Vec<Span<'static>> {
.nth(file_name_start)
.map(|(idx, _)| idx)
.unwrap_or(row.display_name.len());
spans.push(
row.display_name[..byte_start]
.to_string()
.set_style(path_style),
);
let path = &row.display_name[..byte_start];
if let Some(indices) = row.match_indices.as_ref() {
spans.extend(highlighted_spans(
path,
indices,
path_style,
MatchIndexUnit::Grapheme,
));
} else {
spans.push(path.to_string().set_style(path_style));
}
} else {
spans.push(row.display_name.clone().set_style(base_style));
}
@@ -162,6 +162,42 @@ fn filesystem_columns_use_terminal_cell_widths() {
assert_eq!(columns, vec![12, 12]);
}
#[test]
fn fuzzy_matches_preserve_graphemes_when_truncated() {
let mut plugin = rows().remove(0);
plugin.display_name = "👩‍💻abcdef".to_string();
plugin.description = None;
plugin.match_indices = Some(vec![2, 3]);
let path = "👩‍💻abcdef/f";
let file = SearchResult {
display_name: path.to_string(),
description: None,
mention_type: MentionType::File,
selection: Selection::File(path.into()),
match_indices: Some(vec![0, 1]),
score: 0,
};
let mut rendered = Vec::new();
for (row, width, bold_cells) in [(plugin, 13, &[2][..]), (file, 17, &[5, 7][..])] {
let mut buffer = Buffer::empty(Rect::new(
/*x*/ 0, /*y*/ 0, width, /*height*/ 1,
));
render_rows(buffer.area, &mut buffer, &[row], &ScrollState::new(), "");
for &x in bold_cells {
assert!(
buffer[(x, 0)]
.modifier
.contains(ratatui::style::Modifier::BOLD)
);
}
rendered.push(text(&buffer));
}
insta::assert_snapshot!(rendered.join("\n"), @"
👩‍💻… Plugin
f 👩‍💻a… File
");
}
#[test]
fn long_mentions_leave_room_for_distinguishing_parent_paths() {
let mut rows = rows();