WIP: start on fixing point translation in BlockMap
SEED=437554 SIMPLE_TEXT=1 OPERATIONS=1 cargo test --package=editor random_blocks Co-Authored-By: Richard <richard@zed.dev>
This commit is contained in:
@@ -1306,26 +1306,40 @@ impl BlockSnapshot {
|
||||
|
||||
loop {
|
||||
if let Some(transform) = cursor.item() {
|
||||
if transform.block.is_none() {
|
||||
let (output_start_row, input_start_row) = cursor.start();
|
||||
let (output_end_row, input_end_row) = cursor.end(&());
|
||||
let output_start = Point::new(output_start_row.0, 0);
|
||||
let input_start = Point::new(input_start_row.0, 0);
|
||||
let input_end = Point::new(input_end_row.0, 0);
|
||||
let input_point = if point.row >= output_end_row.0 {
|
||||
let line_len = self.wrap_snapshot.line_len(input_end_row.0 - 1);
|
||||
self.wrap_snapshot
|
||||
.clip_point(WrapPoint::new(input_end_row.0 - 1, line_len), bias)
|
||||
} else {
|
||||
let output_overshoot = point.0.saturating_sub(output_start);
|
||||
self.wrap_snapshot
|
||||
.clip_point(WrapPoint(input_start + output_overshoot), bias)
|
||||
};
|
||||
let (output_start_row, input_start_row) = cursor.start();
|
||||
let (output_end_row, input_end_row) = cursor.end(&());
|
||||
let output_start = Point::new(output_start_row.0, 0);
|
||||
let output_end = Point::new(output_end_row.0, 0);
|
||||
let input_start = Point::new(input_start_row.0, 0);
|
||||
let input_end = Point::new(input_end_row.0, 0);
|
||||
|
||||
if (input_start..input_end).contains(&input_point.0) {
|
||||
let input_overshoot = input_point.0.saturating_sub(input_start);
|
||||
return BlockPoint(output_start + input_overshoot);
|
||||
match transform.block.as_ref() {
|
||||
Some(Block::Custom(block))
|
||||
if matches!(block.placement, BlockPlacement::Replace(_)) =>
|
||||
{
|
||||
if bias == Bias::Left {
|
||||
return BlockPoint(output_start);
|
||||
} else {
|
||||
return BlockPoint(Point::new(output_end.row - 1, 0));
|
||||
}
|
||||
}
|
||||
None => {
|
||||
let input_point = if point.row >= output_end_row.0 {
|
||||
let line_len = self.wrap_snapshot.line_len(input_end_row.0 - 1);
|
||||
self.wrap_snapshot
|
||||
.clip_point(WrapPoint::new(input_end_row.0 - 1, line_len), bias)
|
||||
} else {
|
||||
let output_overshoot = point.0.saturating_sub(output_start);
|
||||
self.wrap_snapshot
|
||||
.clip_point(WrapPoint(input_start + output_overshoot), bias)
|
||||
};
|
||||
|
||||
if (input_start..input_end).contains(&input_point.0) {
|
||||
let input_overshoot = input_point.0.saturating_sub(input_start);
|
||||
return BlockPoint(output_start + input_overshoot);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
if search_left {
|
||||
@@ -1347,17 +1361,18 @@ impl BlockSnapshot {
|
||||
let mut cursor = self.transforms.cursor::<(WrapRow, BlockRow)>(&());
|
||||
cursor.seek(&WrapRow(wrap_point.row()), Bias::Right, &());
|
||||
if let Some(transform) = cursor.item() {
|
||||
let todo = (); // introduce a new bias parameter.
|
||||
// debug_assert!(transform.block.is_none());
|
||||
if transform.block.is_some() {
|
||||
BlockPoint::new(cursor.start().1 .0, 0)
|
||||
} else {
|
||||
let (input_start_row, output_start_row) = cursor.start();
|
||||
let input_start = Point::new(input_start_row.0, 0);
|
||||
let output_start = Point::new(output_start_row.0, 0);
|
||||
let input_overshoot = wrap_point.0 - input_start;
|
||||
BlockPoint(output_start + input_overshoot)
|
||||
}
|
||||
} else {
|
||||
return self.max_point();
|
||||
self.max_point()
|
||||
}
|
||||
|
||||
let (input_start_row, output_start_row) = cursor.start();
|
||||
let input_start = Point::new(input_start_row.0, 0);
|
||||
let output_start = Point::new(output_start_row.0, 0);
|
||||
let input_overshoot = wrap_point.0 - input_start;
|
||||
BlockPoint(output_start + input_overshoot)
|
||||
}
|
||||
|
||||
pub fn to_wrap_point(&self, block_point: BlockPoint) -> WrapPoint {
|
||||
@@ -1366,13 +1381,11 @@ impl BlockSnapshot {
|
||||
if let Some(transform) = cursor.item() {
|
||||
match transform.block.as_ref() {
|
||||
Some(block) => {
|
||||
if block.place_above() {
|
||||
WrapPoint::new(cursor.start().1 .0, 0)
|
||||
} else if block.place_below() {
|
||||
if block.place_below() {
|
||||
let wrap_row = cursor.start().1 .0 - 1;
|
||||
WrapPoint::new(wrap_row, self.wrap_snapshot.line_len(wrap_row))
|
||||
} else {
|
||||
todo!()
|
||||
WrapPoint::new(cursor.start().1 .0, 0)
|
||||
}
|
||||
}
|
||||
None => {
|
||||
@@ -2210,7 +2223,8 @@ mod tests {
|
||||
log::info!("Wrap width: {:?}", wrap_width);
|
||||
log::info!("Excerpt Header Height: {:?}", excerpt_header_height);
|
||||
log::info!("Excerpt Footer Height: {:?}", excerpt_footer_height);
|
||||
let is_singleton = rng.gen();
|
||||
let mut is_singleton = rng.gen();
|
||||
is_singleton = true;
|
||||
let buffer = if is_singleton {
|
||||
let len = rng.gen_range(0..10);
|
||||
let text = RandomCharIter::new(&mut rng).take(len).collect::<String>();
|
||||
@@ -2576,12 +2590,12 @@ mod tests {
|
||||
longest_line_len,
|
||||
);
|
||||
|
||||
continue;
|
||||
|
||||
// Ensure that conversion between block points and wrap points is stable.
|
||||
for row in 0..=blocks_snapshot.wrap_snapshot.max_point().row() {
|
||||
let wrap_point = WrapPoint::new(row, 0);
|
||||
let block_point = blocks_snapshot.to_block_point(wrap_point);
|
||||
assert_eq!(blocks_snapshot.to_wrap_point(block_point), wrap_point);
|
||||
let wrap_point = blocks_snapshot.to_wrap_point(block_point);
|
||||
assert_eq!(blocks_snapshot.to_block_point(wrap_point), block_point);
|
||||
}
|
||||
|
||||
let mut block_point = BlockPoint::new(0, 0);
|
||||
@@ -2590,7 +2604,9 @@ mod tests {
|
||||
let left_buffer_point = blocks_snapshot.to_point(left_point, Bias::Left);
|
||||
assert_eq!(
|
||||
blocks_snapshot.to_block_point(blocks_snapshot.to_wrap_point(left_point)),
|
||||
left_point
|
||||
left_point,
|
||||
"wrap point: {:?}",
|
||||
blocks_snapshot.to_wrap_point(left_point)
|
||||
);
|
||||
assert_eq!(
|
||||
left_buffer_point,
|
||||
@@ -2599,18 +2615,19 @@ mod tests {
|
||||
left_point
|
||||
);
|
||||
|
||||
let right_point = blocks_snapshot.clip_point(block_point, Bias::Right);
|
||||
let right_buffer_point = blocks_snapshot.to_point(right_point, Bias::Right);
|
||||
assert_eq!(
|
||||
blocks_snapshot.to_block_point(blocks_snapshot.to_wrap_point(right_point)),
|
||||
right_point
|
||||
);
|
||||
assert_eq!(
|
||||
right_buffer_point,
|
||||
buffer_snapshot.clip_point(right_buffer_point, Bias::Left),
|
||||
"{:?} is not valid in buffer coordinates",
|
||||
right_point
|
||||
);
|
||||
// let right_point = blocks_snapshot.clip_point(block_point, Bias::Right);
|
||||
// let right_buffer_point = blocks_snapshot.to_point(right_point, Bias::Right);
|
||||
// assert_eq!(
|
||||
// blocks_snapshot
|
||||
// .to_block_point(dbg!(blocks_snapshot.to_wrap_point(right_point))),
|
||||
// right_point
|
||||
// );
|
||||
// assert_eq!(
|
||||
// right_buffer_point,
|
||||
// buffer_snapshot.clip_point(right_buffer_point, Bias::Left),
|
||||
// "{:?} is not valid in buffer coordinates",
|
||||
// right_point
|
||||
// );
|
||||
|
||||
if c == '\n' {
|
||||
block_point.0 += Point::new(1, 0);
|
||||
|
||||
Reference in New Issue
Block a user