Avoid optional on select_match

This commit is contained in:
Conrad Irwin 2023-07-17 12:02:10 -06:00
parent f887a17ffe
commit c9bf407431
6 changed files with 80 additions and 114 deletions

View file

@ -954,15 +954,19 @@ impl SearchableItem for Editor {
fn select_matches(&mut self, matches: Vec<Self::Match>, cx: &mut ViewContext<Self>) {
self.unfold_ranges(matches.clone(), false, false, cx);
self.change_selections(None, cx, |s| s.select_ranges(matches));
let mut ranges = Vec::new();
for m in &matches {
ranges.push(self.range_for_match(&m))
}
self.change_selections(None, cx, |s| s.select_ranges(ranges));
}
fn match_index_for_direction(
&mut self,
matches: &Vec<Range<Anchor>>,
mut current_index: usize,
current_index: usize,
direction: Direction,
count: Option<usize>,
count: usize,
cx: &mut ViewContext<Self>,
) -> usize {
let buffer = self.buffer().read(cx).snapshot(cx);
@ -971,39 +975,39 @@ impl SearchableItem for Editor {
} else {
matches[current_index].start
};
if count.is_none()
&& matches[current_index]
.start
.cmp(&current_index_position, &buffer)
.is_gt()
{
if direction == Direction::Prev {
if current_index == 0 {
current_index = matches.len() - 1;
let mut count = count % matches.len();
if count == 0 {
return current_index;
}
match direction {
Direction::Next => {
if matches[current_index]
.start
.cmp(&current_index_position, &buffer)
.is_gt()
{
count = count - 1
}
(current_index + count) % matches.len()
}
Direction::Prev => {
if matches[current_index]
.end
.cmp(&current_index_position, &buffer)
.is_lt()
{
count = count - 1;
}
if current_index >= count {
current_index - count
} else {
current_index -= 1;
matches.len() - (count - current_index)
}
}
} else if count.is_none()
&& matches[current_index]
.end
.cmp(&current_index_position, &buffer)
.is_lt()
{
if direction == Direction::Next {
current_index = 0;
}
} else if direction == Direction::Prev {
let count = count.unwrap_or(1) % matches.len();
if current_index >= count {
current_index = current_index - count;
} else {
current_index = matches.len() - (count - current_index);
}
} else if direction == Direction::Next {
current_index = (current_index + count.unwrap_or(1)) % matches.len()
};
current_index
}
}
fn find_matches(