diff --git a/src/buffer.rs b/src/buffer.rs index 881d7eb..29308fe 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -150,6 +150,8 @@ impl Buffer { assert!(self.scroll_position <= self.primary_cursor.head); assert!(self.primary_cursor.head < self.scroll_position + window_size.visible_byte_count()); + debug_assert!(self.cursors.is_sorted_by_key(|cursor| cursor.head)); + app_action } @@ -309,10 +311,20 @@ impl Buffer { pub fn combine_cursors_if_overlapping(&mut self) { let mut index = 0; + // TODO: this can miss some in the WEIRD case that + // [ *] + // [ *] + // [ *] + // where * is the head. + // the first one wont merge with the 2nd, but the 2nd will + // merge with the 3rd, which would then overlap with the 1st, + // but won't be checked + while !self.cursors.is_empty() && index < self.cursors.len() { while index < self.cursors.len() - 1 && self.cursors[index].range().is_overlapping( - &self.cursors[index + 1].range()) + &self.cursors[index + 1].range() + ) { let next_cursor = self.cursors[index + 1]; self.cursors[index].combine_with(next_cursor); @@ -324,9 +336,9 @@ impl Buffer { { self.primary_cursor.combine_with(self.cursors[index]); self.cursors.remove(index); + } else { + index += 1; } - - index += 1; } } } diff --git a/src/buffer/actions.rs b/src/buffer/actions.rs index dc4a5f9..6462d0a 100644 --- a/src/buffer/actions.rs +++ b/src/buffer/actions.rs @@ -425,7 +425,9 @@ impl Buffer { }); self.primary_cursor = new_cursors.next().unwrap(); - self.cursors = new_cursors.collect(); + self.cursors = new_cursors + .sorted_by_key(|cursor| cursor.head) + .collect(); self.clamp_screen_to_primary_cursor(window_size); }