Use a single Bias enum everywhere

This commit is contained in:
Antonio Scandurra
2021-06-04 13:39:57 +02:00
parent 3b9d760f2b
commit e071d40058
9 changed files with 128 additions and 177 deletions
+18 -21
View File
@@ -13,12 +13,12 @@ use similar::{ChangeTag, TextDiff};
use tree_sitter::{InputEdit, Parser, QueryCursor};
use crate::{
editor::Bias,
language::{Language, Tree},
operation_queue::{self, OperationQueue},
settings::{StyleId, ThemeMap},
sum_tree::{self, FilterCursor, SeekBias, SumTree},
sum_tree::{self, FilterCursor, SumTree},
time::{self, ReplicaId},
util::Bias,
worktree::FileHandle,
};
use anyhow::{anyhow, Result};
@@ -1135,11 +1135,8 @@ impl Buffer {
let mut new_ropes =
RopeBuilder::new(self.visible_text.cursor(0), self.deleted_text.cursor(0));
let mut old_fragments = self.fragments.cursor::<VersionedOffset, VersionedOffset>();
let mut new_fragments = old_fragments.slice(
&VersionedOffset::Offset(ranges[0].start),
SeekBias::Left,
&cx,
);
let mut new_fragments =
old_fragments.slice(&VersionedOffset::Offset(ranges[0].start), Bias::Left, &cx);
new_ropes.push_tree(new_fragments.summary().text);
let mut fragment_start = old_fragments.start().offset();
@@ -1162,7 +1159,7 @@ impl Buffer {
}
let slice =
old_fragments.slice(&VersionedOffset::Offset(range.start), SeekBias::Left, &cx);
old_fragments.slice(&VersionedOffset::Offset(range.start), Bias::Left, &cx);
new_ropes.push_tree(slice.summary().text);
new_fragments.push_tree(slice, &None);
fragment_start = old_fragments.start().offset();
@@ -1342,7 +1339,7 @@ impl Buffer {
let version = Some(edit.version.clone());
let mut old_fragments = self.fragments.cursor::<VersionedOffset, VersionedOffset>();
old_fragments.seek(&VersionedOffset::Offset(0), SeekBias::Left, &version);
old_fragments.seek(&VersionedOffset::Offset(0), Bias::Left, &version);
let mut new_fragments = SumTree::new();
let mut new_ropes =
@@ -1354,7 +1351,7 @@ impl Buffer {
if end_offset < range.start {
let preceding_fragments = old_fragments.slice(
&VersionedOffset::Offset(range.start),
SeekBias::Left,
Bias::Left,
&version,
);
new_ropes.push_tree(preceding_fragments.summary().text);
@@ -1455,7 +1452,7 @@ impl Buffer {
let mut new_ropes =
RopeBuilder::new(self.visible_text.cursor(0), self.deleted_text.cursor(0));
let mut old_fragments = self.fragments.cursor::<usize, FragmentTextSummary>();
let mut new_fragments = old_fragments.slice(&ranges[0].start, SeekBias::Right, &None);
let mut new_fragments = old_fragments.slice(&ranges[0].start, Bias::Right, &None);
new_ropes.push_tree(new_fragments.summary().text);
let mut fragment_start = old_fragments.start().visible;
@@ -1477,7 +1474,7 @@ impl Buffer {
old_fragments.next(&None);
}
let slice = old_fragments.slice(&range.start, SeekBias::Right, &None);
let slice = old_fragments.slice(&range.start, Bias::Right, &None);
new_ropes.push_tree(slice.summary().text);
new_fragments.push_tree(slice, &None);
fragment_start = old_fragments.start().visible;
@@ -1562,25 +1559,25 @@ impl Buffer {
}
pub fn anchor_before<T: ToOffset>(&self, position: T) -> Anchor {
self.anchor_at(position, AnchorBias::Left)
self.anchor_at(position, Bias::Left)
}
pub fn anchor_after<T: ToOffset>(&self, position: T) -> Anchor {
self.anchor_at(position, AnchorBias::Right)
self.anchor_at(position, Bias::Right)
}
pub fn anchor_at<T: ToOffset>(&self, position: T, bias: AnchorBias) -> Anchor {
pub fn anchor_at<T: ToOffset>(&self, position: T, bias: Bias) -> Anchor {
let offset = position.to_offset(self);
let max_offset = self.len();
assert!(offset <= max_offset, "offset is out of range");
if offset == 0 && bias == AnchorBias::Left {
if offset == 0 && bias == Bias::Left {
Anchor::Start
} else if offset == max_offset && bias == AnchorBias::Right {
} else if offset == max_offset && bias == Bias::Right {
Anchor::End
} else {
let mut cursor = self.fragments.cursor::<usize, FragmentTextSummary>();
cursor.seek(&offset, bias.to_seek_bias(), &None);
cursor.seek(&offset, bias, &None);
Anchor::Middle {
offset: offset + cursor.start().deleted,
bias,
@@ -1603,7 +1600,7 @@ impl Buffer {
.cursor::<VersionedOffset, (VersionedOffset, usize)>();
cursor.seek(
&VersionedOffset::Offset(*offset),
bias.to_seek_bias(),
*bias,
&Some(version.clone()),
);
let fragment = cursor.item().unwrap();
@@ -1635,7 +1632,7 @@ impl Buffer {
.cursor::<VersionedOffset, (VersionedOffset, FragmentTextSummary)>();
cursor.seek(
&VersionedOffset::Offset(*offset),
bias.to_seek_bias(),
*bias,
&Some(version.clone()),
);
let overshoot = offset - cursor.start().0.offset();
@@ -2814,7 +2811,7 @@ mod tests {
buffer.add_selection_set(
(0..3)
.map(|row| {
let anchor = buffer.anchor_at(Point::new(row, 0), AnchorBias::Right);
let anchor = buffer.anchor_at(Point::new(row, 0), Bias::Right);
Selection {
id: row as usize,
start: anchor.clone(),
+4 -42
View File
@@ -1,5 +1,5 @@
use super::Buffer;
use crate::{sum_tree, time};
use crate::{time, util::Bias};
use anyhow::Result;
use std::{cmp::Ordering, ops::Range};
@@ -9,47 +9,11 @@ pub enum Anchor {
End,
Middle {
offset: usize,
bias: AnchorBias,
bias: Bias,
version: time::Global,
},
}
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
pub enum AnchorBias {
Left,
Right,
}
impl AnchorBias {
pub fn to_seek_bias(self) -> sum_tree::SeekBias {
match self {
AnchorBias::Left => sum_tree::SeekBias::Left,
AnchorBias::Right => sum_tree::SeekBias::Right,
}
}
}
impl PartialOrd for AnchorBias {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for AnchorBias {
fn cmp(&self, other: &Self) -> Ordering {
use AnchorBias::*;
if self == other {
return Ordering::Equal;
}
match (self, other) {
(Left, _) => Ordering::Less,
(Right, _) => Ordering::Greater,
}
}
}
impl Anchor {
pub fn cmp(&self, other: &Anchor, buffer: &Buffer) -> Result<Ordering> {
if self == other {
@@ -88,8 +52,7 @@ impl Anchor {
match self {
Anchor::Start
| Anchor::Middle {
bias: AnchorBias::Left,
..
bias: Bias::Left, ..
} => self.clone(),
_ => buffer.anchor_before(self),
}
@@ -99,8 +62,7 @@ impl Anchor {
match self {
Anchor::End
| Anchor::Middle {
bias: AnchorBias::Right,
..
bias: Bias::Right, ..
} => self.clone(),
_ => buffer.anchor_after(self),
}
+13 -13
View File
@@ -1,7 +1,7 @@
use super::Point;
use crate::{
editor::Bias,
sum_tree::{self, SeekBias, SumTree},
sum_tree::{self, SumTree},
util::Bias,
};
use arrayvec::ArrayString;
use smallvec::SmallVec;
@@ -129,7 +129,7 @@ impl Rope {
pub fn to_point(&self, offset: usize) -> Point {
assert!(offset <= self.summary().bytes);
let mut cursor = self.chunks.cursor::<usize, TextSummary>();
cursor.seek(&offset, SeekBias::Left, &());
cursor.seek(&offset, Bias::Left, &());
let overshoot = offset - cursor.start().bytes;
cursor.start().lines
+ cursor
@@ -140,14 +140,14 @@ impl Rope {
pub fn to_offset(&self, point: Point) -> usize {
assert!(point <= self.summary().lines);
let mut cursor = self.chunks.cursor::<Point, TextSummary>();
cursor.seek(&point, SeekBias::Left, &());
cursor.seek(&point, Bias::Left, &());
let overshoot = point - cursor.start().lines;
cursor.start().bytes + cursor.item().map_or(0, |chunk| chunk.to_offset(overshoot))
}
pub fn clip_offset(&self, mut offset: usize, bias: Bias) -> usize {
let mut cursor = self.chunks.cursor::<usize, usize>();
cursor.seek(&offset, SeekBias::Left, &());
cursor.seek(&offset, Bias::Left, &());
if let Some(chunk) = cursor.item() {
let mut ix = offset - cursor.start();
while !chunk.0.is_char_boundary(ix) {
@@ -170,7 +170,7 @@ impl Rope {
pub fn clip_point(&self, point: Point, bias: Bias) -> Point {
let mut cursor = self.chunks.cursor::<Point, Point>();
cursor.seek(&point, SeekBias::Right, &());
cursor.seek(&point, Bias::Right, &());
if let Some(chunk) = cursor.item() {
let overshoot = point - cursor.start();
*cursor.start() + chunk.clip_point(overshoot, bias)
@@ -197,7 +197,7 @@ pub struct Cursor<'a> {
impl<'a> Cursor<'a> {
pub fn new(rope: &'a Rope, offset: usize) -> Self {
let mut chunks = rope.chunks.cursor();
chunks.seek(&offset, SeekBias::Right, &());
chunks.seek(&offset, Bias::Right, &());
Self {
rope,
chunks,
@@ -208,7 +208,7 @@ impl<'a> Cursor<'a> {
pub fn seek_forward(&mut self, end_offset: usize) {
debug_assert!(end_offset >= self.offset);
self.chunks.seek_forward(&end_offset, SeekBias::Right, &());
self.chunks.seek_forward(&end_offset, Bias::Right, &());
self.offset = end_offset;
}
@@ -230,7 +230,7 @@ impl<'a> Cursor<'a> {
if end_offset > self.chunks.end(&()) {
self.chunks.next(&());
slice.append(Rope {
chunks: self.chunks.slice(&end_offset, SeekBias::Right, &()),
chunks: self.chunks.slice(&end_offset, Bias::Right, &()),
});
if let Some(end_chunk) = self.chunks.item() {
let end_ix = end_offset - self.chunks.start();
@@ -254,7 +254,7 @@ impl<'a> Cursor<'a> {
if end_offset > self.chunks.end(&()) {
self.chunks.next(&());
summary += &self.chunks.summary(&end_offset, SeekBias::Right, &());
summary += &self.chunks.summary(&end_offset, Bias::Right, &());
if let Some(end_chunk) = self.chunks.item() {
let end_ix = end_offset - self.chunks.start();
summary += TextSummary::from(&end_chunk.0[..end_ix]);
@@ -281,7 +281,7 @@ pub struct Chunks<'a> {
impl<'a> Chunks<'a> {
pub fn new(rope: &'a Rope, range: Range<usize>) -> Self {
let mut chunks = rope.chunks.cursor();
chunks.seek(&range.start, SeekBias::Right, &());
chunks.seek(&range.start, Bias::Right, &());
Self { chunks, range }
}
@@ -291,9 +291,9 @@ impl<'a> Chunks<'a> {
pub fn seek(&mut self, offset: usize) {
if offset >= self.chunks.end(&()) {
self.chunks.seek_forward(&offset, SeekBias::Right, &());
self.chunks.seek_forward(&offset, Bias::Right, &());
} else {
self.chunks.seek(&offset, SeekBias::Right, &());
self.chunks.seek(&offset, Bias::Right, &());
}
self.range.start = offset;
}
+22 -24
View File
@@ -1,12 +1,13 @@
use super::{
buffer::{AnchorRangeExt, TextSummary},
Anchor, Bias, Buffer, DisplayPoint, Edit, Point, ToOffset,
Anchor, Buffer, DisplayPoint, Edit, Point, ToOffset,
};
use crate::{
editor::buffer,
settings::StyleId,
sum_tree::{self, Cursor, FilterCursor, SeekBias, SumTree},
sum_tree::{self, Cursor, FilterCursor, SumTree},
time,
util::Bias,
};
use gpui::{AppContext, ModelHandle};
use parking_lot::{Mutex, MutexGuard};
@@ -125,7 +126,7 @@ impl FoldMap {
let mut new_tree = SumTree::new();
let mut cursor = self.folds.cursor::<_, ()>();
for fold in folds {
new_tree.push_tree(cursor.slice(&fold, SeekBias::Right, buffer), buffer);
new_tree.push_tree(cursor.slice(&fold, Bias::Right, buffer), buffer);
new_tree.push(fold, buffer);
}
new_tree.push_tree(cursor.suffix(buffer), buffer);
@@ -173,7 +174,7 @@ impl FoldMap {
let mut cursor = self.folds.cursor::<_, ()>();
let mut folds = SumTree::new();
for fold_ix in fold_ixs_to_delete {
folds.push_tree(cursor.slice(&fold_ix, SeekBias::Right, buffer), buffer);
folds.push_tree(cursor.slice(&fold_ix, Bias::Right, buffer), buffer);
cursor.next(buffer);
}
folds.push_tree(cursor.suffix(buffer), buffer);
@@ -210,14 +211,14 @@ impl FoldMap {
let offset = offset.to_offset(buffer);
let transforms = self.sync(cx);
let mut cursor = transforms.cursor::<usize, usize>();
cursor.seek(&offset, SeekBias::Right, &());
cursor.seek(&offset, Bias::Right, &());
cursor.item().map_or(false, |t| t.display_text.is_some())
}
pub fn is_line_folded(&self, display_row: u32, cx: &AppContext) -> bool {
let transforms = self.sync(cx);
let mut cursor = transforms.cursor::<DisplayPoint, DisplayPoint>();
cursor.seek(&DisplayPoint::new(display_row, 0), SeekBias::Right, &());
cursor.seek(&DisplayPoint::new(display_row, 0), Bias::Right, &());
while let Some(transform) = cursor.item() {
if transform.display_text.is_some() {
return true;
@@ -242,7 +243,7 @@ impl FoldMap {
pub fn to_buffer_point(&self, display_point: DisplayPoint, cx: &AppContext) -> Point {
let transforms = self.sync(cx);
let mut cursor = transforms.cursor::<DisplayPoint, TransformSummary>();
cursor.seek(&display_point, SeekBias::Right, &());
cursor.seek(&display_point, Bias::Right, &());
let overshoot = display_point.0 - cursor.start().display.lines;
cursor.start().buffer.lines + overshoot
}
@@ -250,7 +251,7 @@ impl FoldMap {
pub fn to_display_point(&self, point: Point, cx: &AppContext) -> DisplayPoint {
let transforms = self.sync(cx);
let mut cursor = transforms.cursor::<Point, TransformSummary>();
cursor.seek(&point, SeekBias::Right, &());
cursor.seek(&point, Bias::Right, &());
let overshoot = point - cursor.start().buffer.lines;
DisplayPoint(cmp::min(
cursor.start().display.lines + overshoot,
@@ -275,17 +276,14 @@ impl FoldMap {
let mut new_transforms = SumTree::new();
let mut transforms = self.transforms.lock();
let mut cursor = transforms.cursor::<usize, usize>();
cursor.seek(&0, SeekBias::Right, &());
cursor.seek(&0, Bias::Right, &());
while let Some(mut edit) = edits.next() {
new_transforms.push_tree(
cursor.slice(&edit.old_range.start, SeekBias::Left, &()),
&(),
);
new_transforms.push_tree(cursor.slice(&edit.old_range.start, Bias::Left, &()), &());
edit.new_range.start -= edit.old_range.start - cursor.start();
edit.old_range.start = *cursor.start();
cursor.seek(&edit.old_range.end, SeekBias::Right, &());
cursor.seek(&edit.old_range.end, Bias::Right, &());
cursor.next(&());
let mut delta = edit.delta();
@@ -302,7 +300,7 @@ impl FoldMap {
if next_edit.old_range.end >= edit.old_range.end {
edit.old_range.end = next_edit.old_range.end;
cursor.seek(&edit.old_range.end, SeekBias::Right, &());
cursor.seek(&edit.old_range.end, Bias::Right, &());
cursor.next(&());
}
} else {
@@ -315,7 +313,7 @@ impl FoldMap {
let anchor = buffer.anchor_before(edit.new_range.start);
let mut folds_cursor = self.folds.cursor::<_, ()>();
folds_cursor.seek(&Fold(anchor..Anchor::End), SeekBias::Left, buffer);
folds_cursor.seek(&Fold(anchor..Anchor::End), Bias::Left, buffer);
let mut folds = iter::from_fn(move || {
let item = folds_cursor
.item()
@@ -432,7 +430,7 @@ impl FoldMapSnapshot {
let display_point = Point::new(start_row, 0);
let mut cursor = self.transforms.cursor();
cursor.seek(&DisplayPoint(display_point), SeekBias::Left, &());
cursor.seek(&DisplayPoint(display_point), Bias::Left, &());
BufferRows {
display_point,
@@ -446,7 +444,7 @@ impl FoldMapSnapshot {
pub fn chunks_at(&self, offset: DisplayOffset) -> Chunks {
let mut transform_cursor = self.transforms.cursor::<DisplayOffset, TransformSummary>();
transform_cursor.seek(&offset, SeekBias::Right, &());
transform_cursor.seek(&offset, Bias::Right, &());
let overshoot = offset.0 - transform_cursor.start().display.bytes;
let buffer_offset = transform_cursor.start().buffer.bytes + overshoot;
Chunks {
@@ -459,11 +457,11 @@ impl FoldMapSnapshot {
pub fn highlighted_chunks(&mut self, range: Range<DisplayOffset>) -> HighlightedChunks {
let mut transform_cursor = self.transforms.cursor::<DisplayOffset, TransformSummary>();
transform_cursor.seek(&range.end, SeekBias::Right, &());
transform_cursor.seek(&range.end, Bias::Right, &());
let overshoot = range.end.0 - transform_cursor.start().display.bytes;
let buffer_end = transform_cursor.start().buffer.bytes + overshoot;
transform_cursor.seek(&range.start, SeekBias::Right, &());
transform_cursor.seek(&range.start, Bias::Right, &());
let overshoot = range.start.0 - transform_cursor.start().display.bytes;
let buffer_start = transform_cursor.start().buffer.bytes + overshoot;
@@ -484,7 +482,7 @@ impl FoldMapSnapshot {
pub fn to_display_offset(&self, point: DisplayPoint) -> DisplayOffset {
let mut cursor = self.transforms.cursor::<DisplayPoint, TransformSummary>();
cursor.seek(&point, SeekBias::Right, &());
cursor.seek(&point, Bias::Right, &());
let overshoot = point.0 - cursor.start().display.lines;
let mut offset = cursor.start().display.bytes;
if !overshoot.is_zero() {
@@ -500,7 +498,7 @@ impl FoldMapSnapshot {
pub fn to_buffer_offset(&self, point: DisplayPoint) -> usize {
let mut cursor = self.transforms.cursor::<DisplayPoint, TransformSummary>();
cursor.seek(&point, SeekBias::Right, &());
cursor.seek(&point, Bias::Right, &());
let overshoot = point.0 - cursor.start().display.lines;
self.buffer
.to_offset(cursor.start().buffer.lines + overshoot)
@@ -509,7 +507,7 @@ impl FoldMapSnapshot {
#[cfg(test)]
pub fn clip_offset(&self, offset: DisplayOffset, bias: Bias) -> DisplayOffset {
let mut cursor = self.transforms.cursor::<DisplayOffset, TransformSummary>();
cursor.seek(&offset, SeekBias::Right, &());
cursor.seek(&offset, Bias::Right, &());
if let Some(transform) = cursor.item() {
let transform_start = cursor.start().display.bytes;
if transform.display_text.is_some() {
@@ -534,7 +532,7 @@ impl FoldMapSnapshot {
pub fn clip_point(&self, point: DisplayPoint, bias: Bias) -> DisplayPoint {
let mut cursor = self.transforms.cursor::<DisplayPoint, TransformSummary>();
cursor.seek(&point, SeekBias::Right, &());
cursor.seek(&point, Bias::Right, &());
if let Some(transform) = cursor.item() {
let transform_start = cursor.start().display.lines;
if transform.display_text.is_some() {