diff --git a/codex-rs/tui/src/bottom_pane/mentions_v2/render.rs b/codex-rs/tui/src/bottom_pane/mentions_v2/render.rs index 4cf35452eb..33be248205 100644 --- a/codex-rs/tui/src/bottom_pane/mentions_v2/render.rs +++ b/codex-rs/tui/src/bottom_pane/mentions_v2/render.rs @@ -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> { 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> { 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> + '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> { 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> { .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)); } diff --git a/codex-rs/tui/src/bottom_pane/mentions_v2/render_tests.rs b/codex-rs/tui/src/bottom_pane/mentions_v2/render_tests.rs index 3b60d48b25..38af3a810b 100644 --- a/codex-rs/tui/src/bottom_pane/mentions_v2/render_tests.rs +++ b/codex-rs/tui/src/bottom_pane/mentions_v2/render_tests.rs @@ -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();