Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

editor: Fix hit testing when the editor is clipped (#56861)

Tim Vermeulencc409210c151
diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs
index fe3ec5cb462..bca778b79be 100644
--- a/crates/editor/src/element.rs
+++ b/crates/editor/src/element.rs
@@ -12193,9 +12193,9 @@ impl PositionMap {
let x = position.x + (scroll_position.x as f32 * self.em_layout_width);
let row = ((y / self.line_height) as f64 + scroll_position.y) as u32;
- let (column, x_overshoot_after_line_end) = if let Some(line) = self
- .line_layouts
- .get(row as usize - scroll_position.y as usize)
+ let (column, x_overshoot_after_line_end) = if let Some(line_index) =
+ row.checked_sub(self.visible_row_range.start.0)
+ && let Some(line) = self.line_layouts.get(line_index as usize)
{
let alignment_offset = line.alignment_offset(self.text_align, self.content_width);
let x_relative_to_text = x - alignment_offset;
@@ -12927,6 +12927,47 @@ mod tests {
}
}
+ #[gpui::test]
+ async fn test_point_for_position_clipped_rows(cx: &mut TestAppContext) {
+ init_test(cx, |_| {});
+
+ let text = "aaa\nbbb";
+ let window = cx.add_window(|window, cx| {
+ let buffer = MultiBuffer::build_simple(text, cx);
+ Editor::new(EditorMode::full(), buffer, None, window, cx)
+ });
+
+ let cx = &mut VisualTestContext::from_window(*window, cx);
+ let editor = window.root(cx).unwrap();
+ let style = editor.update(cx, |editor, cx| editor.style(cx).clone());
+ let line_height = window
+ .update(cx, |_, window, _| {
+ style.text.line_height_in_pixels(window.rem_size())
+ })
+ .unwrap();
+
+ // the first line is clipped
+ let (_, state) = cx.draw(
+ point(Pixels::ZERO, Pixels::ZERO - line_height * 1.5),
+ size(px(500.), px(500.)),
+ |_, _| EditorElement::new(&editor, style),
+ );
+
+ // click at the end of the second line
+ let target_point = DisplayPoint::new(DisplayRow(1), 3);
+ let click_x = state.content_origin.x
+ + editor.update_in(cx, |editor, window, cx| {
+ editor
+ .snapshot(window, cx)
+ .x_for_display_point(target_point, &editor.text_layout_details(window, cx))
+ });
+
+ let point = state
+ .position_map
+ .point_for_position(point(click_x, px(0.)));
+ assert_eq!(point.nearest_valid, target_point);
+ }
+
#[gpui::test]
fn test_navigation_overlay_covered_text_highlights_are_replaced(cx: &mut TestAppContext) {
init_test(cx, |_| {});