Fix crash and wrong columnar selection when mousing over block lines
This commit is contained in:
parent
e1431ede36
commit
096f4693d3
3 changed files with 46 additions and 40 deletions
|
@ -337,7 +337,7 @@ pub enum SelectPhase {
|
|||
},
|
||||
BeginColumnar {
|
||||
position: DisplayPoint,
|
||||
overshoot: u32,
|
||||
goal_column: u32,
|
||||
},
|
||||
Extend {
|
||||
position: DisplayPoint,
|
||||
|
@ -345,7 +345,7 @@ pub enum SelectPhase {
|
|||
},
|
||||
Update {
|
||||
position: DisplayPoint,
|
||||
overshoot: u32,
|
||||
goal_column: u32,
|
||||
scroll_position: Vector2F,
|
||||
},
|
||||
End,
|
||||
|
@ -1498,17 +1498,17 @@ impl Editor {
|
|||
} => self.begin_selection(*position, *add, *click_count, cx),
|
||||
SelectPhase::BeginColumnar {
|
||||
position,
|
||||
overshoot,
|
||||
} => self.begin_columnar_selection(*position, *overshoot, cx),
|
||||
goal_column,
|
||||
} => self.begin_columnar_selection(*position, *goal_column, cx),
|
||||
SelectPhase::Extend {
|
||||
position,
|
||||
click_count,
|
||||
} => self.extend_selection(*position, *click_count, cx),
|
||||
SelectPhase::Update {
|
||||
position,
|
||||
overshoot,
|
||||
goal_column,
|
||||
scroll_position,
|
||||
} => self.update_selection(*position, *overshoot, *scroll_position, cx),
|
||||
} => self.update_selection(*position, *goal_column, *scroll_position, cx),
|
||||
SelectPhase::End => self.end_selection(cx),
|
||||
}
|
||||
}
|
||||
|
@ -1616,7 +1616,7 @@ impl Editor {
|
|||
fn begin_columnar_selection(
|
||||
&mut self,
|
||||
position: DisplayPoint,
|
||||
overshoot: u32,
|
||||
goal_column: u32,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) {
|
||||
if !self.focused {
|
||||
|
@ -1631,7 +1631,7 @@ impl Editor {
|
|||
self.select_columns(
|
||||
tail.to_display_point(&display_map),
|
||||
position,
|
||||
overshoot,
|
||||
goal_column,
|
||||
&display_map,
|
||||
cx,
|
||||
);
|
||||
|
@ -1640,7 +1640,7 @@ impl Editor {
|
|||
fn update_selection(
|
||||
&mut self,
|
||||
position: DisplayPoint,
|
||||
overshoot: u32,
|
||||
goal_column: u32,
|
||||
scroll_position: Vector2F,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) {
|
||||
|
@ -1648,7 +1648,7 @@ impl Editor {
|
|||
|
||||
if let Some(tail) = self.columnar_selection_tail.as_ref() {
|
||||
let tail = tail.to_display_point(&display_map);
|
||||
self.select_columns(tail, position, overshoot, &display_map, cx);
|
||||
self.select_columns(tail, position, goal_column, &display_map, cx);
|
||||
} else if let Some(mut pending) = self.selections.pending_anchor().clone() {
|
||||
let buffer = self.buffer.read(cx).snapshot(cx);
|
||||
let head;
|
||||
|
@ -1749,14 +1749,14 @@ impl Editor {
|
|||
&mut self,
|
||||
tail: DisplayPoint,
|
||||
head: DisplayPoint,
|
||||
overshoot: u32,
|
||||
goal_column: u32,
|
||||
display_map: &DisplaySnapshot,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) {
|
||||
let start_row = cmp::min(tail.row(), head.row());
|
||||
let end_row = cmp::max(tail.row(), head.row());
|
||||
let start_column = cmp::min(tail.column(), head.column() + overshoot);
|
||||
let end_column = cmp::max(tail.column(), head.column() + overshoot);
|
||||
let start_column = cmp::min(tail.column(), goal_column);
|
||||
let end_column = cmp::max(tail.column(), goal_column);
|
||||
let reversed = start_column < tail.column();
|
||||
|
||||
let selection_ranges = (start_row..=end_row)
|
||||
|
|
|
@ -122,8 +122,9 @@ impl EditorElement {
|
|||
cx: &mut EventContext,
|
||||
) -> bool {
|
||||
if cmd && paint.text_bounds.contains_point(position) {
|
||||
let (point, overshoot) = paint.point_for_position(&self.snapshot(cx), layout, position);
|
||||
if overshoot.is_zero() {
|
||||
let (point, target_point) =
|
||||
paint.point_for_position(&self.snapshot(cx), layout, position);
|
||||
if point == target_point {
|
||||
if shift {
|
||||
cx.dispatch_action(GoToFetchedTypeDefinition { point });
|
||||
} else {
|
||||
|
@ -141,12 +142,12 @@ impl EditorElement {
|
|||
}
|
||||
|
||||
let snapshot = self.snapshot(cx.app);
|
||||
let (position, overshoot) = paint.point_for_position(&snapshot, layout, position);
|
||||
let (position, target_position) = paint.point_for_position(&snapshot, layout, position);
|
||||
|
||||
if shift && alt {
|
||||
cx.dispatch_action(Select(SelectPhase::BeginColumnar {
|
||||
position,
|
||||
overshoot: overshoot.column(),
|
||||
goal_column: target_position.column(),
|
||||
}));
|
||||
} else if shift {
|
||||
cx.dispatch_action(Select(SelectPhase::Extend {
|
||||
|
@ -229,11 +230,11 @@ impl EditorElement {
|
|||
}
|
||||
|
||||
let snapshot = self.snapshot(cx.app);
|
||||
let (position, overshoot) = paint.point_for_position(&snapshot, layout, position);
|
||||
let (position, target_position) = paint.point_for_position(&snapshot, layout, position);
|
||||
|
||||
cx.dispatch_action(Select(SelectPhase::Update {
|
||||
position,
|
||||
overshoot: overshoot.column(),
|
||||
goal_column: target_position.column(),
|
||||
scroll_position: (snapshot.scroll_position() + scroll_delta)
|
||||
.clamp(Vector2F::zero(), layout.scroll_max),
|
||||
}));
|
||||
|
@ -258,8 +259,9 @@ impl EditorElement {
|
|||
// This will be handled more correctly once https://github.com/zed-industries/zed/issues/1218 is completed
|
||||
// Don't trigger hover popover if mouse is hovering over context menu
|
||||
let point = if paint.text_bounds.contains_point(position) {
|
||||
let (point, overshoot) = paint.point_for_position(&self.snapshot(cx), layout, position);
|
||||
if overshoot.is_zero() {
|
||||
let (point, target_point) =
|
||||
paint.point_for_position(&self.snapshot(cx), layout, position);
|
||||
if point == target_point {
|
||||
Some(point)
|
||||
} else {
|
||||
None
|
||||
|
@ -1702,9 +1704,10 @@ pub struct PaintState {
|
|||
}
|
||||
|
||||
impl PaintState {
|
||||
/// Returns two display points. The first is the nearest valid
|
||||
/// position in the current buffer and the second is the distance to the
|
||||
/// nearest valid position if there was overshoot.
|
||||
/// Returns two display points:
|
||||
/// 1. The nearest *valid* position in the editor
|
||||
/// 2. An unclipped, potentially *invalid* position that maps directly to
|
||||
/// the given pixel position.
|
||||
fn point_for_position(
|
||||
&self,
|
||||
snapshot: &EditorSnapshot,
|
||||
|
@ -1714,25 +1717,26 @@ impl PaintState {
|
|||
let scroll_position = snapshot.scroll_position();
|
||||
let position = position - self.text_bounds.origin();
|
||||
let y = position.y().max(0.0).min(layout.size.y());
|
||||
let row = ((y / layout.line_height) + scroll_position.y()) as u32;
|
||||
let row_overshoot = row.saturating_sub(snapshot.max_point().row());
|
||||
let row = cmp::min(row, snapshot.max_point().row());
|
||||
let line = &layout.line_layouts[(row - scroll_position.y() as u32) as usize];
|
||||
let x = position.x() + (scroll_position.x() * layout.em_width);
|
||||
|
||||
let column = if x >= 0.0 {
|
||||
line.index_for_x(x)
|
||||
.map(|ix| ix as u32)
|
||||
.unwrap_or_else(|| snapshot.line_len(row))
|
||||
let row = (y / layout.line_height + scroll_position.y()) as u32;
|
||||
let (column, x_overshoot) = if let Some(line) = layout
|
||||
.line_layouts
|
||||
.get(row as usize - scroll_position.y() as usize)
|
||||
{
|
||||
if let Some(ix) = line.index_for_x(x) {
|
||||
(ix as u32, 0.0)
|
||||
} else {
|
||||
0
|
||||
(line.len() as u32, 0f32.max(x - line.width()))
|
||||
}
|
||||
} else {
|
||||
(0, x)
|
||||
};
|
||||
|
||||
let point = snapshot.clip_point(DisplayPoint::new(row, column), Bias::Left);
|
||||
let mut column_overshoot = (0f32.max(x - line.width()) / layout.em_advance) as u32;
|
||||
column_overshoot = column_overshoot + column - point.column();
|
||||
let mut target_point = DisplayPoint::new(row, column);
|
||||
let point = snapshot.clip_point(target_point, Bias::Left);
|
||||
*target_point.column_mut() += (x_overshoot / layout.em_advance) as u32;
|
||||
|
||||
(point, DisplayPoint::new(row_overshoot, column_overshoot))
|
||||
(point, target_point)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -238,8 +238,10 @@ impl Line {
|
|||
None
|
||||
}
|
||||
|
||||
// If round_to_closest, find the closest index to the given x position
|
||||
// If !round_to_closest, find the largest index before the given x position
|
||||
pub fn len(&self) -> usize {
|
||||
self.layout.len
|
||||
}
|
||||
|
||||
pub fn index_for_x(&self, x: f32) -> Option<usize> {
|
||||
if x >= self.layout.width {
|
||||
None
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue