Remove result from anchor cmp functions

Co-authored-by: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Keith Simmons 2022-03-24 11:48:31 -07:00
parent ab631cf6c3
commit 92c7b5d6ef
11 changed files with 68 additions and 141 deletions

View file

@ -367,8 +367,7 @@ impl ProjectDiagnosticsEditor {
range_a range_a
.start .start
.cmp(&range_b.start, &snapshot) .cmp(&range_b.start, &snapshot)
.unwrap() .then_with(|| range_a.end.cmp(&range_b.end, &snapshot))
.then_with(|| range_a.end.cmp(&range_b.end, &snapshot).unwrap())
}); });
if path_state.diagnostic_groups.is_empty() { if path_state.diagnostic_groups.is_empty() {

View file

@ -499,7 +499,7 @@ impl<'a> BlockMapWriter<'a> {
let block_ix = match self let block_ix = match self
.0 .0
.blocks .blocks
.binary_search_by(|probe| probe.position.cmp(&position, &buffer).unwrap()) .binary_search_by(|probe| probe.position.cmp(&position, &buffer))
{ {
Ok(ix) | Err(ix) => ix, Ok(ix) | Err(ix) => ix,
}; };

View file

@ -256,7 +256,7 @@ impl FoldMap {
let mut folds = self.folds.iter().peekable(); let mut folds = self.folds.iter().peekable();
while let Some(fold) = folds.next() { while let Some(fold) = folds.next() {
if let Some(next_fold) = folds.peek() { if let Some(next_fold) = folds.peek() {
let comparison = fold.0.cmp(&next_fold.0, &self.buffer.lock()).unwrap(); let comparison = fold.0.cmp(&next_fold.0, &self.buffer.lock());
assert!(comparison.is_le()); assert!(comparison.is_le());
} }
} }
@ -699,10 +699,7 @@ impl FoldSnapshot {
let ranges = &highlights.1; let ranges = &highlights.1;
let start_ix = match ranges.binary_search_by(|probe| { let start_ix = match ranges.binary_search_by(|probe| {
let cmp = probe let cmp = probe.end.cmp(&transform_start, &self.buffer_snapshot());
.end
.cmp(&transform_start, &self.buffer_snapshot())
.unwrap();
if cmp.is_gt() { if cmp.is_gt() {
Ordering::Greater Ordering::Greater
} else { } else {
@ -715,7 +712,6 @@ impl FoldSnapshot {
if range if range
.start .start
.cmp(&transform_end, &self.buffer_snapshot) .cmp(&transform_end, &self.buffer_snapshot)
.unwrap()
.is_ge() .is_ge()
{ {
break; break;
@ -820,8 +816,8 @@ where
let start = buffer.anchor_before(range.start.to_offset(buffer)); let start = buffer.anchor_before(range.start.to_offset(buffer));
let end = buffer.anchor_after(range.end.to_offset(buffer)); let end = buffer.anchor_after(range.end.to_offset(buffer));
let mut cursor = folds.filter::<_, usize>(move |summary| { let mut cursor = folds.filter::<_, usize>(move |summary| {
let start_cmp = start.cmp(&summary.max_end, buffer).unwrap(); let start_cmp = start.cmp(&summary.max_end, buffer);
let end_cmp = end.cmp(&summary.min_start, buffer).unwrap(); let end_cmp = end.cmp(&summary.min_start, buffer);
if inclusive { if inclusive {
start_cmp <= Ordering::Equal && end_cmp >= Ordering::Equal start_cmp <= Ordering::Equal && end_cmp >= Ordering::Equal
@ -962,19 +958,19 @@ impl sum_tree::Summary for FoldSummary {
type Context = MultiBufferSnapshot; type Context = MultiBufferSnapshot;
fn add_summary(&mut self, other: &Self, buffer: &MultiBufferSnapshot) { fn add_summary(&mut self, other: &Self, buffer: &MultiBufferSnapshot) {
if other.min_start.cmp(&self.min_start, buffer).unwrap() == Ordering::Less { if other.min_start.cmp(&self.min_start, buffer) == Ordering::Less {
self.min_start = other.min_start.clone(); self.min_start = other.min_start.clone();
} }
if other.max_end.cmp(&self.max_end, buffer).unwrap() == Ordering::Greater { if other.max_end.cmp(&self.max_end, buffer) == Ordering::Greater {
self.max_end = other.max_end.clone(); self.max_end = other.max_end.clone();
} }
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
{ {
let start_comparison = self.start.cmp(&other.start, buffer).unwrap(); let start_comparison = self.start.cmp(&other.start, buffer);
assert!(start_comparison <= Ordering::Equal); assert!(start_comparison <= Ordering::Equal);
if start_comparison == Ordering::Equal { if start_comparison == Ordering::Equal {
assert!(self.end.cmp(&other.end, buffer).unwrap() >= Ordering::Equal); assert!(self.end.cmp(&other.end, buffer) >= Ordering::Equal);
} }
} }
@ -993,7 +989,7 @@ impl<'a> sum_tree::Dimension<'a, FoldSummary> for Fold {
impl<'a> sum_tree::SeekTarget<'a, FoldSummary, Fold> for Fold { impl<'a> sum_tree::SeekTarget<'a, FoldSummary, Fold> for Fold {
fn cmp(&self, other: &Self, buffer: &MultiBufferSnapshot) -> Ordering { fn cmp(&self, other: &Self, buffer: &MultiBufferSnapshot) -> Ordering {
self.0.cmp(&other.0, buffer).unwrap() self.0.cmp(&other.0, buffer)
} }
} }
@ -1600,9 +1596,8 @@ mod tests {
.filter(|fold| { .filter(|fold| {
let start = buffer_snapshot.anchor_before(start); let start = buffer_snapshot.anchor_before(start);
let end = buffer_snapshot.anchor_after(end); let end = buffer_snapshot.anchor_after(end);
start.cmp(&fold.0.end, &buffer_snapshot).unwrap() == Ordering::Less start.cmp(&fold.0.end, &buffer_snapshot) == Ordering::Less
&& end.cmp(&fold.0.start, &buffer_snapshot).unwrap() && end.cmp(&fold.0.start, &buffer_snapshot) == Ordering::Greater
== Ordering::Greater
}) })
.map(|fold| fold.0) .map(|fold| fold.0)
.collect::<Vec<_>>(); .collect::<Vec<_>>();
@ -1680,7 +1675,7 @@ mod tests {
let buffer = self.buffer.lock().clone(); let buffer = self.buffer.lock().clone();
let mut folds = self.folds.items(&buffer); let mut folds = self.folds.items(&buffer);
// Ensure sorting doesn't change how folds get merged and displayed. // Ensure sorting doesn't change how folds get merged and displayed.
folds.sort_by(|a, b| a.0.cmp(&b.0, &buffer).unwrap()); folds.sort_by(|a, b| a.0.cmp(&b.0, &buffer));
let mut fold_ranges = folds let mut fold_ranges = folds
.iter() .iter()
.map(|fold| fold.0.start.to_offset(&buffer)..fold.0.end.to_offset(&buffer)) .map(|fold| fold.0.start.to_offset(&buffer)..fold.0.end.to_offset(&buffer))

View file

@ -2545,7 +2545,7 @@ impl Editor {
.range .range
.end .end
.min(&excerpt_range.end, cursor_buffer_snapshot); .min(&excerpt_range.end, cursor_buffer_snapshot);
if start.cmp(&end, cursor_buffer_snapshot).unwrap().is_ge() { if start.cmp(&end, cursor_buffer_snapshot).is_ge() {
continue; continue;
} }
@ -2672,8 +2672,7 @@ impl Editor {
}) })
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
tabstop_ranges tabstop_ranges.sort_unstable_by(|a, b| a.start.cmp(&b.start, snapshot));
.sort_unstable_by(|a, b| a.start.cmp(&b.start, snapshot).unwrap());
tabstop_ranges tabstop_ranges
}) })
.collect::<Vec<_>>() .collect::<Vec<_>>()
@ -4700,13 +4699,13 @@ impl Editor {
let start_ix = match self let start_ix = match self
.selections .selections
.binary_search_by(|probe| probe.end.cmp(&range.start, &buffer).unwrap()) .binary_search_by(|probe| probe.end.cmp(&range.start, &buffer))
{ {
Ok(ix) | Err(ix) => ix, Ok(ix) | Err(ix) => ix,
}; };
let end_ix = match self let end_ix = match self
.selections .selections
.binary_search_by(|probe| probe.start.cmp(&range.end, &buffer).unwrap()) .binary_search_by(|probe| probe.start.cmp(&range.end, &buffer))
{ {
Ok(ix) => ix + 1, Ok(ix) => ix + 1,
Err(ix) => ix, Err(ix) => ix,
@ -4928,8 +4927,7 @@ impl Editor {
selections.sort_by(|a, b| { selections.sort_by(|a, b| {
a.start a.start
.cmp(&b.start, &*buffer) .cmp(&b.start, &*buffer)
.unwrap() .then_with(|| b.end.cmp(&a.end, &*buffer))
.then_with(|| b.end.cmp(&a.end, &*buffer).unwrap())
}); });
// Merge overlapping selections // Merge overlapping selections
@ -4938,24 +4936,17 @@ impl Editor {
if selections[i - 1] if selections[i - 1]
.end .end
.cmp(&selections[i].start, &*buffer) .cmp(&selections[i].start, &*buffer)
.unwrap()
.is_ge() .is_ge()
{ {
let removed = selections.remove(i); let removed = selections.remove(i);
if removed if removed
.start .start
.cmp(&selections[i - 1].start, &*buffer) .cmp(&selections[i - 1].start, &*buffer)
.unwrap()
.is_lt() .is_lt()
{ {
selections[i - 1].start = removed.start; selections[i - 1].start = removed.start;
} }
if removed if removed.end.cmp(&selections[i - 1].end, &*buffer).is_gt() {
.end
.cmp(&selections[i - 1].end, &*buffer)
.unwrap()
.is_gt()
{
selections[i - 1].end = removed.end; selections[i - 1].end = removed.end;
} }
} else { } else {
@ -5429,7 +5420,7 @@ impl Editor {
let buffer = &display_snapshot.buffer_snapshot; let buffer = &display_snapshot.buffer_snapshot;
for (color, ranges) in self.background_highlights.values() { for (color, ranges) in self.background_highlights.values() {
let start_ix = match ranges.binary_search_by(|probe| { let start_ix = match ranges.binary_search_by(|probe| {
let cmp = probe.end.cmp(&search_range.start, &buffer).unwrap(); let cmp = probe.end.cmp(&search_range.start, &buffer);
if cmp.is_gt() { if cmp.is_gt() {
Ordering::Greater Ordering::Greater
} else { } else {
@ -5439,7 +5430,7 @@ impl Editor {
Ok(i) | Err(i) => i, Ok(i) | Err(i) => i,
}; };
for range in &ranges[start_ix..] { for range in &ranges[start_ix..] {
if range.start.cmp(&search_range.end, &buffer).unwrap().is_ge() { if range.start.cmp(&search_range.end, &buffer).is_ge() {
break; break;
} }
let start = range let start = range

View file

@ -522,24 +522,14 @@ impl MultiBuffer {
self.buffers.borrow()[&buffer_id] self.buffers.borrow()[&buffer_id]
.buffer .buffer
.update(cx, |buffer, cx| { .update(cx, |buffer, cx| {
selections.sort_unstable_by(|a, b| a.start.cmp(&b.start, buffer).unwrap()); selections.sort_unstable_by(|a, b| a.start.cmp(&b.start, buffer));
let mut selections = selections.into_iter().peekable(); let mut selections = selections.into_iter().peekable();
let merged_selections = Arc::from_iter(iter::from_fn(|| { let merged_selections = Arc::from_iter(iter::from_fn(|| {
let mut selection = selections.next()?; let mut selection = selections.next()?;
while let Some(next_selection) = selections.peek() { while let Some(next_selection) = selections.peek() {
if selection if selection.end.cmp(&next_selection.start, buffer).is_ge() {
.end
.cmp(&next_selection.start, buffer)
.unwrap()
.is_ge()
{
let next_selection = selections.next().unwrap(); let next_selection = selections.next().unwrap();
if next_selection if next_selection.end.cmp(&selection.end, buffer).is_ge() {
.end
.cmp(&selection.end, buffer)
.unwrap()
.is_ge()
{
selection.end = next_selection.end; selection.end = next_selection.end;
} }
} else { } else {
@ -1936,11 +1926,7 @@ impl MultiBufferSnapshot {
.range .range
.start .start
.bias(anchor.text_anchor.bias, &excerpt.buffer); .bias(anchor.text_anchor.bias, &excerpt.buffer);
if text_anchor if text_anchor.cmp(&excerpt.range.end, &excerpt.buffer).is_gt() {
.cmp(&excerpt.range.end, &excerpt.buffer)
.unwrap()
.is_gt()
{
text_anchor = excerpt.range.end.clone(); text_anchor = excerpt.range.end.clone();
} }
Anchor { Anchor {
@ -1955,7 +1941,6 @@ impl MultiBufferSnapshot {
.bias(anchor.text_anchor.bias, &excerpt.buffer); .bias(anchor.text_anchor.bias, &excerpt.buffer);
if text_anchor if text_anchor
.cmp(&excerpt.range.start, &excerpt.buffer) .cmp(&excerpt.range.start, &excerpt.buffer)
.unwrap()
.is_lt() .is_lt()
{ {
text_anchor = excerpt.range.start.clone(); text_anchor = excerpt.range.start.clone();
@ -1975,7 +1960,7 @@ impl MultiBufferSnapshot {
result.push((anchor_ix, anchor, kept_position)); result.push((anchor_ix, anchor, kept_position));
} }
} }
result.sort_unstable_by(|a, b| a.1.cmp(&b.1, self).unwrap()); result.sort_unstable_by(|a, b| a.1.cmp(&b.1, self));
result result
} }
@ -2322,10 +2307,10 @@ impl MultiBufferSnapshot {
excerpt_id: excerpt.id.clone(), excerpt_id: excerpt.id.clone(),
text_anchor: selection.end.clone(), text_anchor: selection.end.clone(),
}; };
if range.start.cmp(&start, self).unwrap().is_gt() { if range.start.cmp(&start, self).is_gt() {
start = range.start.clone(); start = range.start.clone();
} }
if range.end.cmp(&end, self).unwrap().is_lt() { if range.end.cmp(&end, self).is_lt() {
end = range.end.clone(); end = range.end.clone();
} }
@ -2549,17 +2534,9 @@ impl Excerpt {
} }
fn clip_anchor(&self, text_anchor: text::Anchor) -> text::Anchor { fn clip_anchor(&self, text_anchor: text::Anchor) -> text::Anchor {
if text_anchor if text_anchor.cmp(&self.range.start, &self.buffer).is_lt() {
.cmp(&self.range.start, &self.buffer)
.unwrap()
.is_lt()
{
self.range.start.clone() self.range.start.clone()
} else if text_anchor } else if text_anchor.cmp(&self.range.end, &self.buffer).is_gt() {
.cmp(&self.range.end, &self.buffer)
.unwrap()
.is_gt()
{
self.range.end.clone() self.range.end.clone()
} else { } else {
text_anchor text_anchor
@ -2572,13 +2549,11 @@ impl Excerpt {
.range .range
.start .start
.cmp(&anchor.text_anchor, &self.buffer) .cmp(&anchor.text_anchor, &self.buffer)
.unwrap()
.is_le() .is_le()
&& self && self
.range .range
.end .end
.cmp(&anchor.text_anchor, &self.buffer) .cmp(&anchor.text_anchor, &self.buffer)
.unwrap()
.is_ge() .is_ge()
} }
} }
@ -3385,7 +3360,7 @@ mod tests {
let bias = if rng.gen() { Bias::Left } else { Bias::Right }; let bias = if rng.gen() { Bias::Left } else { Bias::Right };
log::info!("Creating anchor at {} with bias {:?}", offset, bias); log::info!("Creating anchor at {} with bias {:?}", offset, bias);
anchors.push(multibuffer.anchor_at(offset, bias)); anchors.push(multibuffer.anchor_at(offset, bias));
anchors.sort_by(|a, b| a.cmp(&b, &multibuffer).unwrap()); anchors.sort_by(|a, b| a.cmp(&b, &multibuffer));
} }
40..=44 if !anchors.is_empty() => { 40..=44 if !anchors.is_empty() => {
let multibuffer = multibuffer.read(cx).read(cx); let multibuffer = multibuffer.read(cx).read(cx);

View file

@ -1,5 +1,4 @@
use super::{ExcerptId, MultiBufferSnapshot, ToOffset, ToPoint}; use super::{ExcerptId, MultiBufferSnapshot, ToOffset, ToPoint};
use anyhow::Result;
use std::{ use std::{
cmp::Ordering, cmp::Ordering,
ops::{Range, Sub}, ops::{Range, Sub},
@ -35,18 +34,18 @@ impl Anchor {
&self.excerpt_id &self.excerpt_id
} }
pub fn cmp<'a>(&self, other: &Anchor, snapshot: &MultiBufferSnapshot) -> Result<Ordering> { pub fn cmp<'a>(&self, other: &Anchor, snapshot: &MultiBufferSnapshot) -> Ordering {
let excerpt_id_cmp = self.excerpt_id.cmp(&other.excerpt_id); let excerpt_id_cmp = self.excerpt_id.cmp(&other.excerpt_id);
if excerpt_id_cmp.is_eq() { if excerpt_id_cmp.is_eq() {
if self.excerpt_id == ExcerptId::min() || self.excerpt_id == ExcerptId::max() { if self.excerpt_id == ExcerptId::min() || self.excerpt_id == ExcerptId::max() {
Ok(Ordering::Equal) Ordering::Equal
} else if let Some(excerpt) = snapshot.excerpt(&self.excerpt_id) { } else if let Some(excerpt) = snapshot.excerpt(&self.excerpt_id) {
self.text_anchor.cmp(&other.text_anchor, &excerpt.buffer) self.text_anchor.cmp(&other.text_anchor, &excerpt.buffer)
} else { } else {
Ok(Ordering::Equal) Ordering::Equal
} }
} else { } else {
Ok(excerpt_id_cmp) excerpt_id_cmp
} }
} }
@ -97,17 +96,17 @@ impl ToPoint for Anchor {
} }
pub trait AnchorRangeExt { pub trait AnchorRangeExt {
fn cmp(&self, b: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> Result<Ordering>; fn cmp(&self, b: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> Ordering;
fn to_offset(&self, content: &MultiBufferSnapshot) -> Range<usize>; fn to_offset(&self, content: &MultiBufferSnapshot) -> Range<usize>;
fn to_point(&self, content: &MultiBufferSnapshot) -> Range<Point>; fn to_point(&self, content: &MultiBufferSnapshot) -> Range<Point>;
} }
impl AnchorRangeExt for Range<Anchor> { impl AnchorRangeExt for Range<Anchor> {
fn cmp(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> Result<Ordering> { fn cmp(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> Ordering {
Ok(match self.start.cmp(&other.start, buffer)? { match self.start.cmp(&other.start, buffer) {
Ordering::Equal => other.end.cmp(&self.end, buffer)?, Ordering::Equal => other.end.cmp(&self.end, buffer),
ord @ _ => ord, ord @ _ => ord,
}) }
} }
fn to_offset(&self, content: &MultiBufferSnapshot) -> Range<usize> { fn to_offset(&self, content: &MultiBufferSnapshot) -> Range<usize> {

View file

@ -1813,20 +1813,12 @@ impl BufferSnapshot {
}) })
.map(move |(replica_id, set)| { .map(move |(replica_id, set)| {
let start_ix = match set.selections.binary_search_by(|probe| { let start_ix = match set.selections.binary_search_by(|probe| {
probe probe.end.cmp(&range.start, self).then(Ordering::Greater)
.end
.cmp(&range.start, self)
.unwrap()
.then(Ordering::Greater)
}) { }) {
Ok(ix) | Err(ix) => ix, Ok(ix) | Err(ix) => ix,
}; };
let end_ix = match set.selections.binary_search_by(|probe| { let end_ix = match set.selections.binary_search_by(|probe| {
probe probe.start.cmp(&range.end, self).then(Ordering::Less)
.start
.cmp(&range.end, self)
.unwrap()
.then(Ordering::Less)
}) { }) {
Ok(ix) | Err(ix) => ix, Ok(ix) | Err(ix) => ix,
}; };

View file

@ -81,8 +81,8 @@ impl DiagnosticSet {
let range = buffer.anchor_before(range.start)..buffer.anchor_at(range.end, end_bias); let range = buffer.anchor_before(range.start)..buffer.anchor_at(range.end, end_bias);
let mut cursor = self.diagnostics.filter::<_, ()>({ let mut cursor = self.diagnostics.filter::<_, ()>({
move |summary: &Summary| { move |summary: &Summary| {
let start_cmp = range.start.cmp(&summary.max_end, buffer).unwrap(); let start_cmp = range.start.cmp(&summary.max_end, buffer);
let end_cmp = range.end.cmp(&summary.min_start, buffer).unwrap(); let end_cmp = range.end.cmp(&summary.min_start, buffer);
if inclusive { if inclusive {
start_cmp <= Ordering::Equal && end_cmp >= Ordering::Equal start_cmp <= Ordering::Equal && end_cmp >= Ordering::Equal
} else { } else {
@ -123,7 +123,7 @@ impl DiagnosticSet {
let start_ix = output.len(); let start_ix = output.len();
output.extend(groups.into_values().filter_map(|mut entries| { output.extend(groups.into_values().filter_map(|mut entries| {
entries.sort_unstable_by(|a, b| a.range.start.cmp(&b.range.start, buffer).unwrap()); entries.sort_unstable_by(|a, b| a.range.start.cmp(&b.range.start, buffer));
entries entries
.iter() .iter()
.position(|entry| entry.diagnostic.is_primary) .position(|entry| entry.diagnostic.is_primary)
@ -137,7 +137,6 @@ impl DiagnosticSet {
.range .range
.start .start
.cmp(&b.entries[b.primary_ix].range.start, buffer) .cmp(&b.entries[b.primary_ix].range.start, buffer)
.unwrap()
}); });
} }
@ -200,15 +199,10 @@ impl sum_tree::Summary for Summary {
type Context = text::BufferSnapshot; type Context = text::BufferSnapshot;
fn add_summary(&mut self, other: &Self, buffer: &Self::Context) { fn add_summary(&mut self, other: &Self, buffer: &Self::Context) {
if other if other.min_start.cmp(&self.min_start, buffer).is_lt() {
.min_start
.cmp(&self.min_start, buffer)
.unwrap()
.is_lt()
{
self.min_start = other.min_start.clone(); self.min_start = other.min_start.clone();
} }
if other.max_end.cmp(&self.max_end, buffer).unwrap().is_gt() { if other.max_end.cmp(&self.max_end, buffer).is_gt() {
self.max_end = other.max_end.clone(); self.max_end = other.max_end.clone();
} }
self.start = other.start.clone(); self.start = other.start.clone();

View file

@ -39,9 +39,9 @@ pub(crate) fn active_match_index(
None None
} else { } else {
match ranges.binary_search_by(|probe| { match ranges.binary_search_by(|probe| {
if probe.end.cmp(&cursor, &*buffer).unwrap().is_lt() { if probe.end.cmp(&cursor, &*buffer).is_lt() {
Ordering::Less Ordering::Less
} else if probe.start.cmp(&cursor, &*buffer).unwrap().is_gt() { } else if probe.start.cmp(&cursor, &*buffer).is_gt() {
Ordering::Greater Ordering::Greater
} else { } else {
Ordering::Equal Ordering::Equal
@ -59,7 +59,7 @@ pub(crate) fn match_index_for_direction(
direction: Direction, direction: Direction,
buffer: &MultiBufferSnapshot, buffer: &MultiBufferSnapshot,
) -> usize { ) -> usize {
if ranges[index].start.cmp(&cursor, &buffer).unwrap().is_gt() { if ranges[index].start.cmp(&cursor, &buffer).is_gt() {
if direction == Direction::Prev { if direction == Direction::Prev {
if index == 0 { if index == 0 {
index = ranges.len() - 1; index = ranges.len() - 1;
@ -67,7 +67,7 @@ pub(crate) fn match_index_for_direction(
index -= 1; index -= 1;
} }
} }
} else if ranges[index].end.cmp(&cursor, &buffer).unwrap().is_lt() { } else if ranges[index].end.cmp(&cursor, &buffer).is_lt() {
if direction == Direction::Next { if direction == Direction::Next {
index = 0; index = 0;
} }

View file

@ -24,7 +24,7 @@ impl Anchor {
bias: Bias::Right, bias: Bias::Right,
}; };
pub fn cmp(&self, other: &Anchor, buffer: &BufferSnapshot) -> Result<Ordering> { pub fn cmp(&self, other: &Anchor, buffer: &BufferSnapshot) -> Ordering {
let fragment_id_comparison = if self.timestamp == other.timestamp { let fragment_id_comparison = if self.timestamp == other.timestamp {
Ordering::Equal Ordering::Equal
} else { } else {
@ -33,13 +33,13 @@ impl Anchor {
.cmp(&buffer.fragment_id_for_anchor(other)) .cmp(&buffer.fragment_id_for_anchor(other))
}; };
Ok(fragment_id_comparison fragment_id_comparison
.then_with(|| self.offset.cmp(&other.offset)) .then_with(|| self.offset.cmp(&other.offset))
.then_with(|| self.bias.cmp(&other.bias))) .then_with(|| self.bias.cmp(&other.bias))
} }
pub fn min(&self, other: &Self, buffer: &BufferSnapshot) -> Self { pub fn min(&self, other: &Self, buffer: &BufferSnapshot) -> Self {
if self.cmp(other, buffer).unwrap().is_le() { if self.cmp(other, buffer).is_le() {
self.clone() self.clone()
} else { } else {
other.clone() other.clone()
@ -47,7 +47,7 @@ impl Anchor {
} }
pub fn max(&self, other: &Self, buffer: &BufferSnapshot) -> Self { pub fn max(&self, other: &Self, buffer: &BufferSnapshot) -> Self {
if self.cmp(other, buffer).unwrap().is_ge() { if self.cmp(other, buffer).is_ge() {
self.clone() self.clone()
} else { } else {
other.clone() other.clone()
@ -117,8 +117,8 @@ pub trait AnchorRangeExt {
impl AnchorRangeExt for Range<Anchor> { impl AnchorRangeExt for Range<Anchor> {
fn cmp(&self, other: &Range<Anchor>, buffer: &BufferSnapshot) -> Result<Ordering> { fn cmp(&self, other: &Range<Anchor>, buffer: &BufferSnapshot) -> Result<Ordering> {
Ok(match self.start.cmp(&other.start, buffer)? { Ok(match self.start.cmp(&other.start, buffer) {
Ordering::Equal => other.end.cmp(&self.end, buffer)?, Ordering::Equal => other.end.cmp(&self.end, buffer),
ord @ _ => ord, ord @ _ => ord,
}) })
} }

View file

@ -340,59 +340,41 @@ fn test_anchors() {
let anchor_at_offset_2 = buffer.anchor_before(2); let anchor_at_offset_2 = buffer.anchor_before(2);
assert_eq!( assert_eq!(
anchor_at_offset_0 anchor_at_offset_0.cmp(&anchor_at_offset_0, &buffer),
.cmp(&anchor_at_offset_0, &buffer)
.unwrap(),
Ordering::Equal Ordering::Equal
); );
assert_eq!( assert_eq!(
anchor_at_offset_1 anchor_at_offset_1.cmp(&anchor_at_offset_1, &buffer),
.cmp(&anchor_at_offset_1, &buffer)
.unwrap(),
Ordering::Equal Ordering::Equal
); );
assert_eq!( assert_eq!(
anchor_at_offset_2 anchor_at_offset_2.cmp(&anchor_at_offset_2, &buffer),
.cmp(&anchor_at_offset_2, &buffer)
.unwrap(),
Ordering::Equal Ordering::Equal
); );
assert_eq!( assert_eq!(
anchor_at_offset_0 anchor_at_offset_0.cmp(&anchor_at_offset_1, &buffer),
.cmp(&anchor_at_offset_1, &buffer)
.unwrap(),
Ordering::Less Ordering::Less
); );
assert_eq!( assert_eq!(
anchor_at_offset_1 anchor_at_offset_1.cmp(&anchor_at_offset_2, &buffer),
.cmp(&anchor_at_offset_2, &buffer)
.unwrap(),
Ordering::Less Ordering::Less
); );
assert_eq!( assert_eq!(
anchor_at_offset_0 anchor_at_offset_0.cmp(&anchor_at_offset_2, &buffer),
.cmp(&anchor_at_offset_2, &buffer)
.unwrap(),
Ordering::Less Ordering::Less
); );
assert_eq!( assert_eq!(
anchor_at_offset_1 anchor_at_offset_1.cmp(&anchor_at_offset_0, &buffer),
.cmp(&anchor_at_offset_0, &buffer)
.unwrap(),
Ordering::Greater Ordering::Greater
); );
assert_eq!( assert_eq!(
anchor_at_offset_2 anchor_at_offset_2.cmp(&anchor_at_offset_1, &buffer),
.cmp(&anchor_at_offset_1, &buffer)
.unwrap(),
Ordering::Greater Ordering::Greater
); );
assert_eq!( assert_eq!(
anchor_at_offset_2 anchor_at_offset_2.cmp(&anchor_at_offset_0, &buffer),
.cmp(&anchor_at_offset_0, &buffer)
.unwrap(),
Ordering::Greater Ordering::Greater
); );
} }