mirror of
https://github.com/openai/codex.git
synced 2026-11-16 22:00:36 +00:00
Fix Unicode grapheme highlighting in history search (#61725)
## Why Ctrl+R history search highlights need to cover whole grapheme clusters so that matches on combining marks highlight the visible character. ## What changed - Map case-insensitive matches to grapheme boundaries and deduplicate identical highlight ranges. - Segment the separately rendered `!` bash prompt independently from the textarea so a prompt-only match does not highlight the command body. ## Testing Extend match-range tests for repeated combining marks and rendering coverage for combining-mark matches and prompt-only matches. GitOrigin-RevId: d8f5c5ebb06b1130153b3cfbc8631342a57cfa1c
This commit is contained in:
@@ -35,6 +35,7 @@ use ratatui::layout::Rect;
|
||||
use ratatui::style::Stylize;
|
||||
use ratatui::text::Line;
|
||||
use ratatui::text::Span;
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
|
||||
use super::super::chat_composer_history::HistorySearchDirection;
|
||||
use super::super::chat_composer_history::HistorySearchResult;
|
||||
@@ -432,7 +433,7 @@ impl ChatComposer {
|
||||
return Vec::new();
|
||||
}
|
||||
let prefix_len = usize::from(self.draft.is_bash_mode);
|
||||
Self::case_insensitive_match_ranges(&self.current_text(), &search.query)
|
||||
Self::case_insensitive_match_ranges(&self.current_text(), &search.query, prefix_len)
|
||||
.into_iter()
|
||||
.filter_map(|range| {
|
||||
let start = range.start.saturating_sub(prefix_len);
|
||||
@@ -442,7 +443,11 @@ impl ChatComposer {
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn case_insensitive_match_ranges(text: &str, query: &str) -> Vec<Range<usize>> {
|
||||
fn case_insensitive_match_ranges(
|
||||
text: &str,
|
||||
query: &str,
|
||||
prefix_len: usize,
|
||||
) -> Vec<Range<usize>> {
|
||||
if query.is_empty() {
|
||||
return Vec::new();
|
||||
}
|
||||
@@ -457,9 +462,15 @@ impl ChatComposer {
|
||||
|
||||
let mut folded = String::new();
|
||||
let mut folded_spans: Vec<(Range<usize>, Range<usize>)> = Vec::new();
|
||||
for (original_start, ch) in text.char_indices() {
|
||||
let original_range = original_start..original_start + ch.len_utf8();
|
||||
for lower in ch.to_lowercase() {
|
||||
// The bash prompt is rendered separately and cannot share a grapheme with the textarea.
|
||||
let graphemes = text[..prefix_len].grapheme_indices(true).chain(
|
||||
text[prefix_len..]
|
||||
.grapheme_indices(true)
|
||||
.map(|(start, grapheme)| (prefix_len + start, grapheme)),
|
||||
);
|
||||
for (original_start, grapheme) in graphemes {
|
||||
let original_range = original_start..original_start + grapheme.len();
|
||||
for lower in grapheme.chars().flat_map(char::to_lowercase) {
|
||||
let folded_start = folded.len();
|
||||
folded.push(lower);
|
||||
folded_spans.push((folded_start..folded.len(), original_range.clone()));
|
||||
@@ -482,7 +493,10 @@ impl ChatComposer {
|
||||
while folded_spans[end_span].0.end < folded_end {
|
||||
end_span += 1;
|
||||
}
|
||||
ranges.push(folded_spans[start_span].1.start..folded_spans[end_span].1.end);
|
||||
let range = folded_spans[start_span].1.start..folded_spans[end_span].1.end;
|
||||
if ranges.last() != Some(&range) {
|
||||
ranges.push(range);
|
||||
}
|
||||
search_from = folded_end;
|
||||
}
|
||||
ranges
|
||||
@@ -744,14 +758,20 @@ mod tests {
|
||||
#[test]
|
||||
fn history_search_match_ranges_are_case_insensitive() {
|
||||
assert_eq!(
|
||||
ChatComposer::case_insensitive_match_ranges("git status git", "GIT"),
|
||||
ChatComposer::case_insensitive_match_ranges(
|
||||
"git status git",
|
||||
"GIT",
|
||||
/*prefix_len*/ 0
|
||||
),
|
||||
vec![0..3, 11..14]
|
||||
);
|
||||
assert_eq!(
|
||||
ChatComposer::case_insensitive_match_ranges("aİ i", "i"),
|
||||
ChatComposer::case_insensitive_match_ranges("aİ i", "i", /*prefix_len*/ 0),
|
||||
vec![1..3, 4..5]
|
||||
);
|
||||
assert!(ChatComposer::case_insensitive_match_ranges("git", "").is_empty());
|
||||
assert!(
|
||||
ChatComposer::case_insensitive_match_ranges("git", "", /*prefix_len*/ 0).is_empty()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -761,12 +781,17 @@ mod tests {
|
||||
("İİ", "\u{307}", vec![0..2, 2..4]),
|
||||
("İİİ", "\u{307}i", vec![0..4, 2..6]),
|
||||
("éÉ é", "É", vec![0..2, 2..4, 5..7]),
|
||||
(
|
||||
"a\u{301}\u{301}b\u{301}\u{301}",
|
||||
"\u{301}",
|
||||
vec![0..5, 5..10],
|
||||
),
|
||||
("aaaaa", "aa", vec![0..2, 2..4]),
|
||||
("", "x", vec![]),
|
||||
("abc", "z", vec![]),
|
||||
] {
|
||||
assert_eq!(
|
||||
ChatComposer::case_insensitive_match_ranges(text, query),
|
||||
ChatComposer::case_insensitive_match_ranges(text, query, /*prefix_len*/ 0),
|
||||
expected,
|
||||
"text: {text:?}, query: {query:?}"
|
||||
);
|
||||
@@ -1040,6 +1065,44 @@ mod tests {
|
||||
}
|
||||
assert_eq!(composer.current_text(), "!git status");
|
||||
assert_eq!(composer.history_search_highlight_ranges(), vec![0..3]);
|
||||
|
||||
let _ = composer.handle_key_event(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE));
|
||||
composer
|
||||
.history
|
||||
.record_local_submission(HistoryEntry::new("!🏽echo".to_string()));
|
||||
let _ = composer.handle_key_event(KeyEvent::new(KeyCode::Char('r'), KeyModifiers::CONTROL));
|
||||
let _ = composer.handle_key_event(KeyEvent::new(KeyCode::Char('!'), KeyModifiers::NONE));
|
||||
assert!(composer.history_search_highlight_ranges().is_empty());
|
||||
let mut prompt_buf = Buffer::empty(area);
|
||||
composer.render(area, &mut prompt_buf);
|
||||
let prompt_highlighted = prompt_buf[(x, y)]
|
||||
.style()
|
||||
.add_modifier
|
||||
.contains(Modifier::REVERSED);
|
||||
|
||||
let _ = composer.handle_key_event(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE));
|
||||
composer
|
||||
.history
|
||||
.record_local_submission(HistoryEntry::new("a\u{301}".to_string()));
|
||||
let _ = composer.handle_key_event(KeyEvent::new(KeyCode::Char('r'), KeyModifiers::CONTROL));
|
||||
let _ =
|
||||
composer.handle_key_event(KeyEvent::new(KeyCode::Char('\u{301}'), KeyModifiers::NONE));
|
||||
let mut buf = Buffer::empty(area);
|
||||
composer.render(area, &mut buf);
|
||||
let cell = &buf[(x, y)];
|
||||
let modifiers = cell.style().add_modifier;
|
||||
insta::assert_snapshot!(
|
||||
format!(
|
||||
"prompt-only match highlights body: {prompt_highlighted}\n{}: bold={}, reversed={}",
|
||||
cell.symbol(),
|
||||
modifiers.contains(Modifier::BOLD),
|
||||
modifiers.contains(Modifier::REVERSED)
|
||||
),
|
||||
@"
|
||||
prompt-only match highlights body: false
|
||||
á: bold=true, reversed=true
|
||||
"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user