fix: search result cursor advancing
This commit is contained in:
@@ -14,7 +14,7 @@ use crate::{
|
||||
text_input::{self, TextInput, text_input},
|
||||
},
|
||||
query::{self, QueryStatus, read_query, use_query, watch_query},
|
||||
util,
|
||||
util::{self, diff::DiffLineIndex},
|
||||
};
|
||||
|
||||
pub(crate) struct PullRequestDiffView {
|
||||
@@ -33,15 +33,19 @@ pub(crate) struct PullRequestDiffView {
|
||||
search_input: gpui::Entity<TextInput>,
|
||||
}
|
||||
|
||||
struct DiffSearchHit {
|
||||
diff_line_i: DiffLineIndex,
|
||||
src_byte_range: std::ops::Range<usize>,
|
||||
}
|
||||
|
||||
struct DiffSearchResult {
|
||||
cursor: DiffSearchResultCursor,
|
||||
old_side: Vec<(usize, std::ops::Range<usize>)>,
|
||||
new_side: Vec<(usize, std::ops::Range<usize>)>,
|
||||
old_side: Vec<DiffSearchHit>,
|
||||
new_side: Vec<DiffSearchHit>,
|
||||
}
|
||||
|
||||
struct DiffSearchResultCursor {
|
||||
is_old: bool,
|
||||
line_index: usize,
|
||||
side: util::diff::DiffSide,
|
||||
index: usize,
|
||||
}
|
||||
|
||||
@@ -204,6 +208,10 @@ impl PullRequestDiffView {
|
||||
}
|
||||
|
||||
fn search_in_diff(&mut self, search_str: &str, cx: &mut gpui::Context<Self>) {
|
||||
let Some(diff_view_content) = &self.diff_view_content else {
|
||||
return;
|
||||
};
|
||||
|
||||
let diff_view_state_in_search_mode = self
|
||||
.diff_view_state_in_search_mode
|
||||
.get_or_insert(DiffViewState::fork_from(&self.diff_view_state));
|
||||
@@ -226,7 +234,13 @@ impl PullRequestDiffView {
|
||||
.diff_view_state
|
||||
.old_side_highlights()?
|
||||
.line_index_of_range(&range)?;
|
||||
Some((line_idx, range))
|
||||
let diff_line_i = diff_view_content
|
||||
.diff
|
||||
.diff_line_index_for_line(util::diff::DiffSide::Old, line_idx);
|
||||
Some(DiffSearchHit {
|
||||
diff_line_i,
|
||||
src_byte_range: range,
|
||||
})
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
let new_search_result = memchr::memmem::find_iter(&diff.new_content, search_str.as_bytes())
|
||||
@@ -236,21 +250,27 @@ impl PullRequestDiffView {
|
||||
.diff_view_state
|
||||
.new_side_highlights()?
|
||||
.line_index_of_range(&range)?;
|
||||
Some((line_idx, range))
|
||||
let diff_line_i = diff_view_content
|
||||
.diff
|
||||
.diff_line_index_for_line(util::diff::DiffSide::New, line_idx);
|
||||
Some(DiffSearchHit {
|
||||
diff_line_i,
|
||||
src_byte_range: range,
|
||||
})
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let old_side_highlights =
|
||||
old_search_result
|
||||
.iter()
|
||||
.map(|(_, r)| -> util::syntax_highlight::HighlightedRange {
|
||||
(r.clone(), symbol_highlight_style(theme))
|
||||
.map(|hit| -> util::syntax_highlight::HighlightedRange {
|
||||
(hit.src_byte_range.clone(), symbol_highlight_style(theme))
|
||||
});
|
||||
let new_side_highlights =
|
||||
new_search_result
|
||||
.iter()
|
||||
.map(|(_, r)| -> util::syntax_highlight::HighlightedRange {
|
||||
(r.clone(), symbol_highlight_style(theme))
|
||||
.map(|hit| -> util::syntax_highlight::HighlightedRange {
|
||||
(hit.src_byte_range.clone(), symbol_highlight_style(theme))
|
||||
});
|
||||
|
||||
if let Some(h) = self
|
||||
@@ -276,14 +296,12 @@ impl PullRequestDiffView {
|
||||
} else {
|
||||
let cursor = if !old_search_result.is_empty() {
|
||||
DiffSearchResultCursor {
|
||||
is_old: true,
|
||||
line_index: old_search_result[0].0,
|
||||
side: util::diff::DiffSide::Old,
|
||||
index: 0,
|
||||
}
|
||||
} else {
|
||||
DiffSearchResultCursor {
|
||||
is_old: false,
|
||||
line_index: new_search_result[0].0,
|
||||
side: util::diff::DiffSide::New,
|
||||
index: 0,
|
||||
}
|
||||
};
|
||||
@@ -305,21 +323,23 @@ impl PullRequestDiffView {
|
||||
return;
|
||||
};
|
||||
|
||||
let (current_side, other_side) = if search_result.cursor.is_old {
|
||||
(&search_result.old_side, &search_result.new_side)
|
||||
} else {
|
||||
(&search_result.new_side, &search_result.old_side)
|
||||
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_line_index = search_result.cursor.line_index;
|
||||
let current_search_hit = ¤t_side[search_result.cursor.index];
|
||||
let highlight_range = match current_side.get(search_result.cursor.index + 1) {
|
||||
| Some((next_highlight_line, next_range)) if *next_highlight_line == current_line_index => {
|
||||
| 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;
|
||||
Some((
|
||||
next_range.clone(),
|
||||
src_byte_range.clone(),
|
||||
gpui::HighlightStyle {
|
||||
background_color: Some(gpui::red()),
|
||||
..symbol_highlight_style(theme)
|
||||
@@ -327,33 +347,42 @@ impl PullRequestDiffView {
|
||||
))
|
||||
}
|
||||
| next => {
|
||||
let next_highlight_line = next.map(|(line, _)| *line).unwrap_or(usize::MAX);
|
||||
let next_highlight_line = next
|
||||
.map(|hit| hit.diff_line_i)
|
||||
.unwrap_or(DiffLineIndex::MAX);
|
||||
|
||||
let mut i = 0;
|
||||
let mut other_side_result: Option<(usize, usize, &std::ops::Range<usize>)> = None;
|
||||
while let Some((other_side_line_i, r)) = &other_side.get(i) {
|
||||
if *other_side_line_i == current_line_index {
|
||||
let mut other_side_result: Option<(DiffLineIndex, usize, &std::ops::Range<usize>)> =
|
||||
None;
|
||||
while let Some(DiffSearchHit {
|
||||
diff_line_i,
|
||||
src_byte_range,
|
||||
}) = &other_side.get(i)
|
||||
{
|
||||
if *diff_line_i == current_search_hit.diff_line_i
|
||||
&& matches!(search_result.cursor.side, util::diff::DiffSide::Old)
|
||||
{
|
||||
// found other side highlight on the current line
|
||||
other_side_result = Some((*other_side_line_i, i, r));
|
||||
// we are on old side, so jump to new side on same line
|
||||
other_side_result = Some((*diff_line_i, i, src_byte_range));
|
||||
break;
|
||||
}
|
||||
if *other_side_line_i > current_line_index
|
||||
&& *other_side_line_i < next_highlight_line
|
||||
if *diff_line_i > current_search_hit.diff_line_i
|
||||
&& *diff_line_i <= next_highlight_line
|
||||
{
|
||||
// found other side highlight in between current side highlight and next side highlight
|
||||
other_side_result = Some((*other_side_line_i, i, r));
|
||||
other_side_result = Some((*diff_line_i, i, src_byte_range));
|
||||
break;
|
||||
}
|
||||
if *other_side_line_i > next_highlight_line {
|
||||
if *diff_line_i > next_highlight_line {
|
||||
break;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
|
||||
if let Some((other_side_line_i, other_side_i, r)) = other_side_result {
|
||||
if let Some((_, other_side_i, r)) = other_side_result {
|
||||
// next cursor should be on other side with the found index
|
||||
search_result.cursor.is_old = !search_result.cursor.is_old;
|
||||
search_result.cursor.line_index = other_side_line_i;
|
||||
search_result.cursor.side = search_result.cursor.side.flipped();
|
||||
search_result.cursor.index = other_side_i;
|
||||
Some((
|
||||
r.clone(),
|
||||
@@ -364,10 +393,9 @@ impl PullRequestDiffView {
|
||||
))
|
||||
} else if let Some(next) = next {
|
||||
// stay on old side, point to next old side highlight
|
||||
search_result.cursor.line_index = next_highlight_line;
|
||||
search_result.cursor.index = search_result.cursor.index + 1;
|
||||
Some((
|
||||
next.1.clone(),
|
||||
next.src_byte_range.clone(),
|
||||
gpui::HighlightStyle {
|
||||
background_color: Some(gpui::red()),
|
||||
..symbol_highlight_style(theme)
|
||||
@@ -380,13 +408,11 @@ impl PullRequestDiffView {
|
||||
};
|
||||
|
||||
if let Some(highlight_range) = highlight_range {
|
||||
let content = if search_result.cursor.is_old {
|
||||
diff_view_state.old_side_highlights_mut()
|
||||
} else {
|
||||
diff_view_state.new_side_highlights_mut()
|
||||
let content = match search_result.cursor.side {
|
||||
| util::diff::DiffSide::Old => diff_view_state.old_side_highlights_mut(),
|
||||
| util::diff::DiffSide::New => diff_view_state.new_side_highlights_mut(),
|
||||
};
|
||||
if let Some(mut content) = content {
|
||||
println!("replacing highlight range {:?}", highlight_range);
|
||||
content.replace_highlight_range(&highlight_range);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user