From eb537214ed4c4ce0c0eed9e96c3eea8a01e32a22 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Mon, 31 Jan 2022 11:04:53 +0100 Subject: [PATCH] Fix panic when moving to next/prev result but there are no matches --- crates/find/src/find.rs | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/crates/find/src/find.rs b/crates/find/src/find.rs index ea2b29a277..d393cbd9c8 100644 --- a/crates/find/src/find.rs +++ b/crates/find/src/find.rs @@ -512,17 +512,21 @@ impl FindBar { let editor = editor.read(cx); let position = editor.newest_anchor_selection()?.head(); let ranges = editor.highlighted_ranges_for_type::()?.1; - let buffer = editor.buffer().read(cx).read(cx); - match ranges.binary_search_by(|probe| { - if probe.end.cmp(&position, &*buffer).unwrap().is_lt() { - Ordering::Less - } else if probe.start.cmp(&position, &*buffer).unwrap().is_gt() { - Ordering::Greater - } else { - Ordering::Equal + if ranges.is_empty() { + None + } else { + let buffer = editor.buffer().read(cx).read(cx); + match ranges.binary_search_by(|probe| { + if probe.end.cmp(&position, &*buffer).unwrap().is_lt() { + Ordering::Less + } else if probe.start.cmp(&position, &*buffer).unwrap().is_gt() { + Ordering::Greater + } else { + Ordering::Equal + } + }) { + Ok(i) | Err(i) => Some(cmp::min(i, ranges.len() - 1)), } - }) { - Ok(i) | Err(i) => Some(cmp::min(i, ranges.len().saturating_sub(1))), } } }