diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index c806acaa5d..c975bc82df 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -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, ) { 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, ) { @@ -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, ) { 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) diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index fa4c126671..95d760846f 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -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 { + (line.len() as u32, 0f32.max(x - line.width())) + } } else { - 0 + (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) } } diff --git a/crates/gpui/src/text_layout.rs b/crates/gpui/src/text_layout.rs index 5a256d271a..571f932e52 100644 --- a/crates/gpui/src/text_layout.rs +++ b/crates/gpui/src/text_layout.rs @@ -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 { if x >= self.layout.width { None