This commit is contained in:
Antonio Scandurra 2024-10-31 15:53:24 +01:00
parent b338475855
commit 02701b49f2
4 changed files with 199 additions and 3 deletions

View File

@ -942,6 +942,14 @@ impl DisplaySnapshot {
DisplayPoint(clipped)
}
pub fn clip_point_2(&self, point: DisplayPoint, bias: Bias, skip_blocks: bool) -> DisplayPoint {
let mut clipped = self.block_snapshot.clip_point_2(point.0, bias, skip_blocks);
if self.clip_at_line_ends {
clipped = self.clip_at_line_end(DisplayPoint(clipped)).0
}
DisplayPoint(clipped)
}
pub fn clip_ignoring_line_ends(&self, point: DisplayPoint, bias: Bias) -> DisplayPoint {
DisplayPoint(self.block_snapshot.clip_point(point.0, bias))
}

View File

@ -1298,6 +1298,68 @@ impl BlockSnapshot {
cursor.item().map_or(false, |t| t.block.is_some())
}
pub fn clip_point_2(&self, point: BlockPoint, bias: Bias, skip_blocks: bool) -> BlockPoint {
let mut cursor = self.transforms.cursor::<(BlockRow, WrapRow)>(&());
cursor.seek(&BlockRow(point.row), Bias::Right, &());
let max_input_row = WrapRow(self.transforms.summary().input_rows);
let mut search_left =
(bias == Bias::Left && cursor.start().1 .0 > 0) || cursor.end(&()).1 == max_input_row;
let mut reversed = false;
loop {
if let Some(transform) = cursor.item() {
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);
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 {
cursor.prev(&());
} else {
cursor.next(&());
}
} else if reversed {
return self.max_point();
} else {
reversed = true;
search_left = !search_left;
cursor.seek(&BlockRow(point.row), Bias::Right, &());
}
}
}
pub fn clip_point(&self, point: BlockPoint, bias: Bias) -> BlockPoint {
let mut cursor = self.transforms.cursor::<(BlockRow, WrapRow)>(&());
cursor.seek(&BlockRow(point.row), Bias::Right, &());

View File

@ -7463,11 +7463,12 @@ impl Editor {
if !selection.is_empty() && !line_mode {
selection.goal = SelectionGoal::None;
}
let (cursor, goal) = movement::up(
let (cursor, goal) = movement::up2(
map,
selection.start,
selection.goal,
false,
false,
text_layout_details,
);
selection.collapse_to(cursor, goal);
@ -7628,8 +7629,16 @@ impl Editor {
pub fn select_up(&mut self, _: &SelectUp, cx: &mut ViewContext<Self>) {
let text_layout_details = &self.text_layout_details(cx);
self.change_selections(Some(Autoscroll::fit()), cx, |s| {
s.move_heads_with(|map, head, goal| {
movement::up(map, head, goal, false, text_layout_details)
s.move_with(|map, selection| {
let (head, goal) = movement::up2(
map,
selection.head(),
selection.goal,
false,
!selection.reversed,
text_layout_details,
);
selection.set_head(head, goal);
})
})
}

View File

@ -76,6 +76,26 @@ pub fn saturating_right(map: &DisplaySnapshot, mut point: DisplayPoint) -> Displ
map.clip_point(point, Bias::Right)
}
/// Returns a display point for the preceding displayed line (which might be a soft-wrapped line).
pub fn up2(
map: &DisplaySnapshot,
start: DisplayPoint,
goal: SelectionGoal,
preserve_column_at_start: bool,
skip_replace_blocks: bool,
text_layout_details: &TextLayoutDetails,
) -> (DisplayPoint, SelectionGoal) {
up_by_rows2(
map,
start,
1,
goal,
preserve_column_at_start,
skip_replace_blocks,
text_layout_details,
)
}
/// Returns a display point for the preceding displayed line (which might be a soft-wrapped line).
pub fn up(
map: &DisplaySnapshot,
@ -112,6 +132,26 @@ pub fn down(
)
}
/// Returns a display point for the next displayed line (which might be a soft-wrapped line).
pub fn down2(
map: &DisplaySnapshot,
start: DisplayPoint,
goal: SelectionGoal,
preserve_column_at_end: bool,
skip_replace_blocks: bool,
text_layout_details: &TextLayoutDetails,
) -> (DisplayPoint, SelectionGoal) {
down_by_rows2(
map,
start,
1,
goal,
preserve_column_at_end,
skip_replace_blocks,
text_layout_details,
)
}
pub(crate) fn up_by_rows(
map: &DisplaySnapshot,
start: DisplayPoint,
@ -151,6 +191,46 @@ pub(crate) fn up_by_rows(
)
}
pub(crate) fn up_by_rows2(
map: &DisplaySnapshot,
start: DisplayPoint,
row_count: u32,
goal: SelectionGoal,
preserve_column_at_start: bool,
skip_replace_blocks: bool,
text_layout_details: &TextLayoutDetails,
) -> (DisplayPoint, SelectionGoal) {
let mut goal_x = match goal {
SelectionGoal::HorizontalPosition(x) => x.into(),
SelectionGoal::WrappedHorizontalPosition((_, x)) => x.into(),
SelectionGoal::HorizontalRange { end, .. } => end.into(),
_ => map.x_for_display_point(start, text_layout_details),
};
let prev_row = DisplayRow(start.row().0.saturating_sub(row_count));
let mut point = map.clip_point(
DisplayPoint::new(prev_row, map.line_len(prev_row)),
Bias::Left,
);
if point.row() < start.row() {
*point.column_mut() = map.display_column_for_x(point.row(), goal_x, text_layout_details)
} else if preserve_column_at_start {
return (start, goal);
} else {
point = DisplayPoint::new(DisplayRow(0), 0);
goal_x = px(0.);
}
let mut clipped_point = map.clip_point_2(point, Bias::Left, skip_replace_blocks);
if clipped_point.row() < point.row() {
clipped_point = map.clip_point_2(point, Bias::Right, skip_replace_blocks);
}
(
clipped_point,
SelectionGoal::HorizontalPosition(goal_x.into()),
)
}
pub(crate) fn down_by_rows(
map: &DisplaySnapshot,
start: DisplayPoint,
@ -187,6 +267,43 @@ pub(crate) fn down_by_rows(
)
}
pub(crate) fn down_by_rows2(
map: &DisplaySnapshot,
start: DisplayPoint,
row_count: u32,
goal: SelectionGoal,
preserve_column_at_end: bool,
skip_replace_blocks: bool,
text_layout_details: &TextLayoutDetails,
) -> (DisplayPoint, SelectionGoal) {
let mut goal_x = match goal {
SelectionGoal::HorizontalPosition(x) => x.into(),
SelectionGoal::WrappedHorizontalPosition((_, x)) => x.into(),
SelectionGoal::HorizontalRange { end, .. } => end.into(),
_ => map.x_for_display_point(start, text_layout_details),
};
let new_row = DisplayRow(start.row().0 + row_count);
let mut point = map.clip_point(DisplayPoint::new(new_row, 0), Bias::Right);
if point.row() > start.row() {
*point.column_mut() = map.display_column_for_x(point.row(), goal_x, text_layout_details)
} else if preserve_column_at_end {
return (start, goal);
} else {
point = map.max_point();
goal_x = map.x_for_display_point(point, text_layout_details)
}
let mut clipped_point = map.clip_point_2(point, Bias::Right, skip_replace_blocks);
if clipped_point.row() > point.row() {
clipped_point = map.clip_point_2(point, Bias::Left, skip_replace_blocks);
}
(
clipped_point,
SelectionGoal::HorizontalPosition(goal_x.into()),
)
}
/// Returns a position of the start of line.
/// If `stop_at_soft_boundaries` is true, the returned position is that of the
/// displayed line (e.g. it could actually be in the middle of a text line if that line is soft-wrapped).