on master help sign in
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.

commit7cf448eb5bfd604e1342a1d0f5685e3231f6c1a9 authorPascal Kuthe <pascal.kuthe@semimod.de> dateWed 22 Mar 2023, 18:38 (4 years ago) parent72b9311
1 file changed+1−4
helix-core/src/text_annotations.rs+1−4
helix-core/src/text_annotations.rs+1−4
@@ -1,5 +1,4 @@
11use std::cell::Cell;
2−use std::convert::identity;
32use std::ops::Range;
43use std::rc::Rc;
54
@@ -113,9 +112,7 @@ impl<A, M> Layer<A, M> {
113112 pub fn reset_pos(&self, char_idx: usize, get_char_idx: impl Fn(&A) -> usize) {
114113 let new_index = self
115114 .annotations
116− .binary_search_by_key(&char_idx, get_char_idx)
117− .unwrap_or_else(identity);
118−
115+ .partition_point(|annot| get_char_idx(annot) < char_idx);
119116 self.current_index.set(new_index);
120117 }
121118