mirror of
https://github.com/openai/codex.git
synced 2026-11-26 23:40:43 +00:00
Fix Unicode highlighting in fuzzy file search (#64214)
## Why The fuzzy matcher reports grapheme positions, but the terminal file picker uses Unicode scalar offsets. When a path contains a joined emoji or a combining mark, the picker can highlight the wrong characters. ## What changed Let callers choose the unit for match indices. The terminal and CLI use scalar offsets that cover every scalar in each matched grapheme. The terminal's file picker and mention renderer now consume the same offsets. The app server returns grapheme offsets for both one-shot and streaming searches. ## Testing Add regression coverage for basename and full-path matches with joined emoji and combining marks, grapheme offsets in both app-server search modes, and terminal highlights in file and mention results. GitOrigin-RevId: 2da357a404396550333c785640d2c4121cb2ed3f
This commit is contained in:
@@ -216,24 +216,26 @@ mod tests {
|
||||
.into_iter()
|
||||
.map(|name| parent.join(name))
|
||||
.collect();
|
||||
let root = tempfile::tempdir().unwrap();
|
||||
std::fs::create_dir_all(root.path().join(&parent)).unwrap();
|
||||
for path in &paths {
|
||||
std::fs::write(root.path().join(path), "").unwrap();
|
||||
}
|
||||
let mut matches = codex_file_search::run(
|
||||
"parser",
|
||||
vec![root.path().to_path_buf()],
|
||||
codex_file_search::FileSearchOptions {
|
||||
index_unit: Some(codex_file_search::IndexUnit::Scalar),
|
||||
..Default::default()
|
||||
},
|
||||
/*cancel_flag*/ None,
|
||||
)
|
||||
.unwrap()
|
||||
.matches;
|
||||
matches.sort_by(|a, b| a.path.cmp(&b.path));
|
||||
let mut popup = FileSearchPopup::new();
|
||||
popup.set_query("parser");
|
||||
popup.set_matches(
|
||||
"parser",
|
||||
paths
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(index, path)| {
|
||||
let mut matched = file_match(index);
|
||||
matched.path = path.clone();
|
||||
matched.indices =
|
||||
fuzzy_match(&path.to_string_lossy(), "parser").map(|(indices, _)| {
|
||||
indices.into_iter().map(|index| index as u32).collect()
|
||||
});
|
||||
matched
|
||||
})
|
||||
.collect(),
|
||||
);
|
||||
popup.set_matches("parser", matches);
|
||||
for (selected, path) in paths.iter().enumerate() {
|
||||
for width in [28, 40, 80] {
|
||||
let area = Rect::new(
|
||||
@@ -262,18 +264,12 @@ mod tests {
|
||||
);
|
||||
assert_eq!(popup.selected_match().map(|m| &m.path), Some(path));
|
||||
let unselected_y = if selected == 0 { 2 } else { 1 };
|
||||
let parser_x = (0..width.saturating_sub(/*rhs*/ 5))
|
||||
.find(|&x| {
|
||||
(x..x + 6)
|
||||
.map(|column| buf[(column, unselected_y)].symbol())
|
||||
.eq(["p", "a", "r", "s", "e", "r"])
|
||||
})
|
||||
.expect("visible filename match");
|
||||
assert!(
|
||||
buf[(parser_x, unselected_y)]
|
||||
.modifier
|
||||
.contains(ratatui::style::Modifier::BOLD)
|
||||
);
|
||||
let highlighted: String = (0..width)
|
||||
.map(|x| &buf[(x, unselected_y)])
|
||||
.filter(|cell| cell.modifier.contains(ratatui::style::Modifier::BOLD))
|
||||
.map(ratatui::buffer::Cell::symbol)
|
||||
.collect();
|
||||
assert_eq!(highlighted, "parser");
|
||||
if selected == 0 {
|
||||
snapshots.push(format!("{components:?}, width {width}\n{text}"));
|
||||
}
|
||||
|
||||
@@ -289,36 +289,21 @@ 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() {
|
||||
highlighted_spans(
|
||||
&row.display_name,
|
||||
indices,
|
||||
name_style,
|
||||
MatchIndexUnit::Scalar,
|
||||
)
|
||||
.collect()
|
||||
highlighted_spans(&row.display_name, indices, name_style).collect()
|
||||
} else {
|
||||
vec![row.display_name.clone().set_style(name_style)]
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
};
|
||||
index_end += grapheme.chars().count();
|
||||
let mut matched = false;
|
||||
while indices.next_if(|index| **index < index_end).is_some() {
|
||||
matched = true;
|
||||
@@ -368,12 +353,7 @@ fn path_spans(row: &SearchResult, base_style: Style) -> Vec<Span<'static>> {
|
||||
.unwrap_or(row.display_name.len());
|
||||
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,
|
||||
));
|
||||
spans.extend(highlighted_spans(path, indices, path_style));
|
||||
} else {
|
||||
spans.push(path.to_string().set_style(path_style));
|
||||
}
|
||||
|
||||
@@ -184,7 +184,7 @@ fn fuzzy_matches_preserve_graphemes_when_truncated() {
|
||||
path: path.into(),
|
||||
full_path: path.into(),
|
||||
},
|
||||
match_indices: Some(vec![0, 1]),
|
||||
match_indices: Some(vec![0, 1, 2, 3]),
|
||||
score: 0,
|
||||
};
|
||||
let mut rendered = Vec::new();
|
||||
@@ -208,6 +208,34 @@ fn fuzzy_matches_preserve_graphemes_when_truncated() {
|
||||
");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parent_path_highlight_after_joined_emoji() {
|
||||
let path = "👩💻notes/report_beta.rs";
|
||||
let row = SearchResult {
|
||||
display_name: path.to_string(),
|
||||
description: None,
|
||||
mention_type: MentionType::File,
|
||||
label_style: ThreadLabelStyle::Plain,
|
||||
selection: Selection::File {
|
||||
path: path.into(),
|
||||
full_path: path.into(),
|
||||
},
|
||||
match_indices: Some((3..12).collect()),
|
||||
score: 0,
|
||||
};
|
||||
let mut buffer = Buffer::empty(Rect::new(
|
||||
/*x*/ 0, /*y*/ 0, /*width*/ 60, /*height*/ 1,
|
||||
));
|
||||
render_rows(buffer.area, &mut buffer, &[row], &ScrollState::new(), "");
|
||||
let bold: String = buffer
|
||||
.content
|
||||
.iter()
|
||||
.filter(|cell| cell.modifier.contains(ratatui::style::Modifier::BOLD))
|
||||
.map(ratatui::buffer::Cell::symbol)
|
||||
.collect();
|
||||
insta::assert_snapshot!(bold, @"notes/");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn long_mentions_leave_room_for_distinguishing_parent_paths() {
|
||||
let mut rows = rows();
|
||||
|
||||
@@ -83,7 +83,7 @@ impl FileSearchManager {
|
||||
let session = file_search::create_session(
|
||||
vec![self.search_dir.clone()],
|
||||
file_search::FileSearchOptions {
|
||||
compute_indices: true,
|
||||
index_unit: Some(file_search::IndexUnit::Scalar),
|
||||
..Default::default()
|
||||
},
|
||||
reporter,
|
||||
|
||||
Reference in New Issue
Block a user