diff --git a/src/screen/dashboard/pull_request_diff_view.rs b/src/screen/dashboard/pull_request_diff_view.rs index c485896..bf3f3e8 100644 --- a/src/screen/dashboard/pull_request_diff_view.rs +++ b/src/screen/dashboard/pull_request_diff_view.rs @@ -25,7 +25,6 @@ pub(crate) struct PullRequestDiffView { diff_view_state_in_search_mode: Option, diff_view_content: Option, diff_search_result: Option, - diff_search_result_cursor: Option, current_file_path: Option>, @@ -36,6 +35,7 @@ pub(crate) struct PullRequestDiffView { struct DiffSearchHit { diff_line_i: DiffLineIndex, src_byte_range: std::ops::Range, + 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::>(); @@ -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::>(); - 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 = ¤t_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 diff --git a/src/util/syntax_highlight.rs b/src/util/syntax_highlight.rs index 2f8185a..468f251 100644 --- a/src/util/syntax_highlight.rs +++ b/src/util/syntax_highlight.rs @@ -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, + highlights: impl Iterator, ) -> 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]);