From 7cf448eb5bfd604e1342a1d0f5685e3231f6c1a9 Mon Sep 17 00:00:00 2001
From: Pascal Kuthe
Date: Wed, 22 Mar 2023 18:38:32 +0100
Subject: use partition_point instead of binary_search_by
Using `partition_point` ensures we always find the first entry.
With binary search it is "random" (deterministic but implementation
specific) which index is retruned if there are multiple equal elements.
`partition_point` was added to the standard library to cover extactly
the usecase here.
---
helix-core/src/text_annotations.rs | 5 +----
1 file changed, 1 insertion(+), 4 deletions(-)
(limited to 'helix-core')
diff --git a/helix-core/src/text_annotations.rs b/helix-core/src/text_annotations.rs
index 3e48de4d..e6093184 100644
--- a/helix-core/src/text_annotations.rs
+++ b/helix-core/src/text_annotations.rs
@@ -1,5 +1,4 @@
use std::cell::Cell;
-use std::convert::identity;
use std::ops::Range;
use std::rc::Rc;
@@ -113,9 +112,7 @@ impl Layer {
pub fn reset_pos(&self, char_idx: usize, get_char_idx: impl Fn(&A) -> usize) {
let new_index = self
.annotations
- .binary_search_by_key(&char_idx, get_char_idx)
- .unwrap_or_else(identity);
-
+ .partition_point(|annot| get_char_idx(annot) < char_idx);
self.current_index.set(new_index);
}
--
cgit v1.2.3-70-g09d2