Add a bias parameter when converting buffer points to fold points
This commit is contained in:
@@ -80,8 +80,8 @@ impl Selection {
|
||||
}
|
||||
|
||||
pub fn display_range(&self, map: &DisplayMapSnapshot) -> Range<DisplayPoint> {
|
||||
let start = self.start.to_display_point(map);
|
||||
let end = self.end.to_display_point(map);
|
||||
let start = self.start.to_display_point(map, Bias::Left);
|
||||
let end = self.end.to_display_point(map, Bias::Left);
|
||||
if self.reversed {
|
||||
end..start
|
||||
} else {
|
||||
@@ -94,11 +94,11 @@ impl Selection {
|
||||
include_end_if_at_line_start: bool,
|
||||
map: &DisplayMapSnapshot,
|
||||
) -> (Range<u32>, Range<u32>) {
|
||||
let display_start = self.start.to_display_point(map);
|
||||
let display_start = self.start.to_display_point(map, Bias::Left);
|
||||
let buffer_start =
|
||||
DisplayPoint::new(display_start.row(), 0).to_buffer_point(map, Bias::Left);
|
||||
|
||||
let mut display_end = self.end.to_display_point(map);
|
||||
let mut display_end = self.end.to_display_point(map, Bias::Right);
|
||||
if !include_end_if_at_line_start
|
||||
&& display_end.row() != map.max_point().row()
|
||||
&& display_start.row() != display_end.row()
|
||||
|
||||
@@ -117,24 +117,25 @@ impl DisplayMapSnapshot {
|
||||
let mut point = display_point.to_buffer_point(self, Bias::Left);
|
||||
while point.column != 0 {
|
||||
point.column = 0;
|
||||
display_point = point.to_display_point(self);
|
||||
display_point = point.to_display_point(self, Bias::Left);
|
||||
if display_point.column() != 0 {
|
||||
*display_point.column_mut() = 0;
|
||||
point = display_point.to_buffer_point(self, Bias::Left);
|
||||
}
|
||||
point = display_point.to_buffer_point(self, Bias::Left);
|
||||
}
|
||||
|
||||
(display_point, point)
|
||||
}
|
||||
|
||||
pub fn next_row_boundary(&self, mut display_point: DisplayPoint) -> (DisplayPoint, Point) {
|
||||
let max_point = self.max_point();
|
||||
*display_point.row_mut() += 1;
|
||||
*display_point.column_mut() = 0;
|
||||
let mut point = display_point.to_buffer_point(self, Bias::Right);
|
||||
while point.column != 0 {
|
||||
while point.column != 0 && display_point <= max_point {
|
||||
point.column = 0;
|
||||
point.row += 1;
|
||||
display_point = point.to_display_point(self);
|
||||
display_point = point.to_display_point(self, Bias::Right);
|
||||
if display_point.column() != 0 {
|
||||
*display_point.row_mut() += 1;
|
||||
*display_point.column_mut() = 0;
|
||||
@@ -142,7 +143,7 @@ impl DisplayMapSnapshot {
|
||||
}
|
||||
}
|
||||
|
||||
(display_point, point)
|
||||
(display_point.min(max_point), point)
|
||||
}
|
||||
|
||||
pub fn max_point(&self) -> DisplayPoint {
|
||||
@@ -307,8 +308,8 @@ impl DisplayPoint {
|
||||
}
|
||||
|
||||
impl Point {
|
||||
pub fn to_display_point(self, map: &DisplayMapSnapshot) -> DisplayPoint {
|
||||
let fold_point = self.to_fold_point(&map.folds_snapshot);
|
||||
pub fn to_display_point(self, map: &DisplayMapSnapshot, bias: Bias) -> DisplayPoint {
|
||||
let fold_point = self.to_fold_point(&map.folds_snapshot, bias);
|
||||
let tab_point = map.tabs_snapshot.to_tab_point(fold_point);
|
||||
let wrap_point = map.wraps_snapshot.to_wrap_point(tab_point);
|
||||
DisplayPoint(wrap_point)
|
||||
@@ -316,8 +317,9 @@ impl Point {
|
||||
}
|
||||
|
||||
impl Anchor {
|
||||
pub fn to_display_point(&self, map: &DisplayMapSnapshot) -> DisplayPoint {
|
||||
self.to_point(&map.buffer_snapshot).to_display_point(map)
|
||||
pub fn to_display_point(&self, map: &DisplayMapSnapshot, bias: Bias) -> DisplayPoint {
|
||||
self.to_point(&map.buffer_snapshot)
|
||||
.to_display_point(map, bias)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -435,13 +437,13 @@ mod tests {
|
||||
}
|
||||
|
||||
assert_eq!(
|
||||
prev_buffer_bound.to_display_point(&snapshot),
|
||||
prev_buffer_bound.to_display_point(&snapshot, Left),
|
||||
prev_display_bound,
|
||||
"{:?} to display point",
|
||||
prev_buffer_bound
|
||||
);
|
||||
assert_eq!(
|
||||
next_buffer_bound.to_display_point(&snapshot),
|
||||
next_buffer_bound.to_display_point(&snapshot, Left),
|
||||
next_display_bound,
|
||||
"{:?} to display point",
|
||||
next_buffer_bound
|
||||
@@ -787,40 +789,40 @@ mod tests {
|
||||
|
||||
let point = Point::new(0, "✅\t\t".len() as u32);
|
||||
let display_point = DisplayPoint::new(0, "✅ ".len() as u32);
|
||||
assert_eq!(point.to_display_point(&map), display_point);
|
||||
assert_eq!(display_point.to_buffer_point(&map, Bias::Left), point,);
|
||||
assert_eq!(point.to_display_point(&map, Left), display_point);
|
||||
assert_eq!(display_point.to_buffer_point(&map, Left), point,);
|
||||
|
||||
let point = Point::new(1, "β\t".len() as u32);
|
||||
let display_point = DisplayPoint::new(1, "β ".len() as u32);
|
||||
assert_eq!(point.to_display_point(&map), display_point);
|
||||
assert_eq!(display_point.to_buffer_point(&map, Bias::Left), point,);
|
||||
assert_eq!(point.to_display_point(&map, Left), display_point);
|
||||
assert_eq!(display_point.to_buffer_point(&map, Left), point,);
|
||||
|
||||
let point = Point::new(2, "🏀β\t\t".len() as u32);
|
||||
let display_point = DisplayPoint::new(2, "🏀β ".len() as u32);
|
||||
assert_eq!(point.to_display_point(&map), display_point);
|
||||
assert_eq!(display_point.to_buffer_point(&map, Bias::Left), point,);
|
||||
assert_eq!(point.to_display_point(&map, Left), display_point);
|
||||
assert_eq!(display_point.to_buffer_point(&map, Left), point,);
|
||||
|
||||
// Display points inside of expanded tabs
|
||||
assert_eq!(
|
||||
DisplayPoint::new(0, "✅ ".len() as u32).to_buffer_point(&map, Bias::Right),
|
||||
DisplayPoint::new(0, "✅ ".len() as u32).to_buffer_point(&map, Right),
|
||||
Point::new(0, "✅\t\t".len() as u32),
|
||||
);
|
||||
assert_eq!(
|
||||
DisplayPoint::new(0, "✅ ".len() as u32).to_buffer_point(&map, Bias::Left),
|
||||
DisplayPoint::new(0, "✅ ".len() as u32).to_buffer_point(&map, Left),
|
||||
Point::new(0, "✅\t".len() as u32),
|
||||
);
|
||||
assert_eq!(
|
||||
DisplayPoint::new(0, "✅ ".len() as u32).to_buffer_point(&map, Bias::Right),
|
||||
DisplayPoint::new(0, "✅ ".len() as u32).to_buffer_point(&map, Right),
|
||||
Point::new(0, "✅\t".len() as u32),
|
||||
);
|
||||
assert_eq!(
|
||||
DisplayPoint::new(0, "✅ ".len() as u32).to_buffer_point(&map, Bias::Left),
|
||||
DisplayPoint::new(0, "✅ ".len() as u32).to_buffer_point(&map, Left),
|
||||
Point::new(0, "✅".len() as u32),
|
||||
);
|
||||
|
||||
// Clipping display points inside of multi-byte characters
|
||||
assert_eq!(
|
||||
map.clip_point(DisplayPoint::new(0, "✅".len() as u32 - 1), Bias::Left),
|
||||
map.clip_point(DisplayPoint::new(0, "✅".len() as u32 - 1), Left),
|
||||
DisplayPoint::new(0, 0)
|
||||
);
|
||||
assert_eq!(
|
||||
|
||||
@@ -77,14 +77,21 @@ impl FoldPoint {
|
||||
}
|
||||
|
||||
impl Point {
|
||||
pub fn to_fold_point(&self, snapshot: &Snapshot) -> FoldPoint {
|
||||
pub fn to_fold_point(&self, snapshot: &Snapshot, bias: Bias) -> FoldPoint {
|
||||
let mut cursor = snapshot.transforms.cursor::<Point, FoldPoint>();
|
||||
cursor.seek(self, Bias::Right, &());
|
||||
let overshoot = *self - cursor.seek_start();
|
||||
FoldPoint(cmp::min(
|
||||
cursor.sum_start().0 + overshoot,
|
||||
cursor.sum_end(&()).0,
|
||||
))
|
||||
if cursor.item().map_or(false, |t| t.is_fold()) {
|
||||
match bias {
|
||||
Bias::Left => *cursor.sum_start(),
|
||||
Bias::Right => cursor.sum_end(&()),
|
||||
}
|
||||
} else {
|
||||
let overshoot = *self - cursor.seek_start();
|
||||
FoldPoint(cmp::min(
|
||||
cursor.sum_start().0 + overshoot,
|
||||
cursor.sum_end(&()).0,
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1365,7 +1372,7 @@ mod tests {
|
||||
let buffer_point = fold_point.to_buffer_point(&snapshot);
|
||||
let buffer_offset = buffer_point.to_offset(&buffer);
|
||||
assert_eq!(
|
||||
buffer_point.to_fold_point(&snapshot),
|
||||
buffer_point.to_fold_point(&snapshot, Right),
|
||||
fold_point,
|
||||
"buffer_Point.to_fold_point({:?})",
|
||||
buffer_point,
|
||||
@@ -1413,7 +1420,9 @@ mod tests {
|
||||
}
|
||||
|
||||
for (idx, buffer_row) in expected_buffer_rows.iter().enumerate() {
|
||||
let fold_row = Point::new(*buffer_row, 0).to_fold_point(&snapshot).row();
|
||||
let fold_row = Point::new(*buffer_row, 0)
|
||||
.to_fold_point(&snapshot, Right)
|
||||
.row();
|
||||
assert_eq!(
|
||||
snapshot.buffer_rows(fold_row).collect::<Vec<_>>(),
|
||||
expected_buffer_rows[idx..],
|
||||
@@ -1421,7 +1430,10 @@ mod tests {
|
||||
}
|
||||
|
||||
for fold_range in map.merged_fold_ranges(cx.as_ref()) {
|
||||
let fold_point = fold_range.start.to_point(&buffer).to_fold_point(&snapshot);
|
||||
let fold_point = fold_range
|
||||
.start
|
||||
.to_point(&buffer)
|
||||
.to_fold_point(&snapshot, Right);
|
||||
assert!(snapshot.is_line_folded(fold_point.row()));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user