fix: search result advancing highlight

This commit is contained in:
2026-06-02 00:35:40 +01:00
parent 6f3245f927
commit ccc93d2429
2 changed files with 39 additions and 25 deletions

View File

@@ -25,7 +25,6 @@ pub(crate) struct PullRequestDiffView {
diff_view_state_in_search_mode: Option<DiffViewState>,
diff_view_content: Option<DiffViewContent>,
diff_search_result: Option<DiffSearchResult>,
diff_search_result_cursor: Option<DiffSearchResultCursor>,
current_file_path: Option<Arc<str>>,
@@ -36,6 +35,7 @@ pub(crate) struct PullRequestDiffView {
struct DiffSearchHit {
diff_line_i: DiffLineIndex,
src_byte_range: std::ops::Range<usize>,
highlight_range: util::syntax_highlight::HighlightedRange,
}
struct DiffSearchResult {
@@ -62,7 +62,6 @@ impl PullRequestDiffView {
diff_view_content: None,
diff_view_state_in_search_mode: None,
diff_search_result: None,
diff_search_result_cursor: None,
current_file_path: None,
focus_handle: cx.focus_handle(),
@@ -239,7 +238,8 @@ impl PullRequestDiffView {
.diff_line_index_for_line(util::diff::DiffSide::Old, line_idx);
Some(DiffSearchHit {
diff_line_i,
src_byte_range: range,
src_byte_range: range.clone(),
highlight_range: (range, symbol_highlight_style(theme)),
})
})
.collect::<Vec<_>>();
@@ -255,23 +255,18 @@ impl PullRequestDiffView {
.diff_line_index_for_line(util::diff::DiffSide::New, line_idx);
Some(DiffSearchHit {
diff_line_i,
src_byte_range: range,
src_byte_range: range.clone(),
highlight_range: (range, symbol_highlight_style(theme)),
})
})
.collect::<Vec<_>>();
let old_side_highlights =
old_search_result
.iter()
.map(|hit| -> util::syntax_highlight::HighlightedRange {
(hit.src_byte_range.clone(), symbol_highlight_style(theme))
});
let new_side_highlights =
new_search_result
.iter()
.map(|hit| -> util::syntax_highlight::HighlightedRange {
(hit.src_byte_range.clone(), symbol_highlight_style(theme))
});
let old_side_highlights = old_search_result
.iter()
.map(|hit| -> &util::syntax_highlight::HighlightedRange { &hit.highlight_range });
let new_side_highlights = new_search_result
.iter()
.map(|hit| -> &util::syntax_highlight::HighlightedRange { &hit.highlight_range });
if let Some(h) = self
.diff_view_state
@@ -323,18 +318,38 @@ impl PullRequestDiffView {
return;
};
let (current_side, other_side) = match search_result.cursor.side {
| util::diff::DiffSide::Old => (&search_result.old_side, &search_result.new_side),
| util::diff::DiffSide::New => (&search_result.new_side, &search_result.old_side),
};
let theme = app::current_theme(cx);
let (current_side, other_side) = match search_result.cursor.side {
| util::diff::DiffSide::Old => {
// clear current cursor highlight
// by replacing it with the normal styled highlighted range
if let Some(mut highlights) = diff_view_state.old_side_highlights_mut() {
highlights.replace_highlight_range(
&search_result.old_side[search_result.cursor.index].highlight_range,
);
}
(&search_result.old_side, &search_result.new_side)
}
| util::diff::DiffSide::New => {
// clear current cursor highlight
// by replacing it with the normal styled highlighted range
if let Some(mut highlights) = diff_view_state.new_side_highlights_mut() {
highlights.replace_highlight_range(
&search_result.new_side[search_result.cursor.index].highlight_range,
);
}
(&search_result.new_side, &search_result.old_side)
}
};
let current_search_hit = &current_side[search_result.cursor.index];
let highlight_range = match current_side.get(search_result.cursor.index + 1) {
| Some(DiffSearchHit {
diff_line_i,
src_byte_range,
..
}) if *diff_line_i == current_search_hit.diff_line_i => {
// go to next search result on same side & same line
search_result.cursor.index += 1;
@@ -357,6 +372,7 @@ impl PullRequestDiffView {
while let Some(DiffSearchHit {
diff_line_i,
src_byte_range,
..
}) = &other_side.get(i)
{
if *diff_line_i == current_search_hit.diff_line_i

View File

@@ -122,9 +122,9 @@ impl HighlightedContent {
&self.0[line].line_range
}
pub(crate) fn extended_with_highlights(
pub(crate) fn extended_with_highlights<'a>(
mut self,
highlights: impl Iterator<Item = HighlightedRange>,
highlights: impl Iterator<Item = &'a HighlightedRange>,
) -> Self {
let total_line_count = self.0.len();
let mut current_line: usize = 0;
@@ -191,8 +191,6 @@ impl HighlightedContent {
pub(crate) fn replace_highlight_range(&mut self, highlighted_range: &HighlightedRange) {
let (range, _) = &highlighted_range;
println!("finding range to replace {:?}", range);
let line = self
.line_index_of_range(range)
.map(|line_i| &mut self.0[line_i]);