From 92f01b784cd6ce042bb09dc27134814e2affdb2f Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 13 Oct 2025 14:40:20 +0200 Subject: [PATCH] editor: Fix `delete line` moving the cursor too far --- crates/editor/src/editor.rs | 26 +++++++++++++++----------- crates/editor/src/editor_tests.rs | 20 +++++++++++++++++++- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 8f30d79c51caec7b2428c015e42c6ff9fd93ce86..cf14e3309f8f3f347546c910907a5d3ae2637d55 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -10463,29 +10463,33 @@ impl Editor { let buffer = display_map.buffer_snapshot(); let mut edit_start = ToOffset::to_offset(&Point::new(rows.start.0, 0), buffer); - let edit_end = if buffer.max_point().row >= rows.end.0 { + let (edit_end, target_row) = if buffer.max_point().row >= rows.end.0 { // If there's a line after the range, delete the \n from the end of the row range - ToOffset::to_offset(&Point::new(rows.end.0, 0), buffer) + ( + ToOffset::to_offset(&Point::new(rows.end.0, 0), buffer), + rows.end, + ) } else { // If there isn't a line after the range, delete the \n from the line before the // start of the row range edit_start = edit_start.saturating_sub(1); - buffer.len() + (buffer.len(), rows.start.previous_row()) }; - let (cursor, goal) = movement::down_by_rows( - &display_map, + let text_layout_details = self.text_layout_details(window); + let x = display_map.x_for_display_point( selection.head().to_display_point(&display_map), - rows.len() as u32, - selection.goal, - false, - &self.text_layout_details(window), + &text_layout_details, ); + let row = Point::new(target_row.0, 0) + .to_display_point(&display_map) + .row(); + let column = display_map.display_column_for_x(row, x, &text_layout_details); new_cursors.push(( selection.id, - buffer.anchor_after(cursor.to_point(&display_map)), - goal, + buffer.anchor_after(DisplayPoint::new(row, column).to_point(&display_map)), + SelectionGoal::None, )); edit_ranges.push(edit_start..edit_end); } diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs index 69fa137e723cf7cc26cc70120ac2f816fd1fe85a..2bd33cd63f441fd31a4656286dc0d39710ae031f 100644 --- a/crates/editor/src/editor_tests.rs +++ b/crates/editor/src/editor_tests.rs @@ -4300,8 +4300,8 @@ fn test_delete_line(cx: &mut TestAppContext) { assert_eq!( editor.selections.display_ranges(cx), vec![ + DisplayPoint::new(DisplayRow(0), 0)..DisplayPoint::new(DisplayRow(0), 0), DisplayPoint::new(DisplayRow(0), 1)..DisplayPoint::new(DisplayRow(0), 1), - DisplayPoint::new(DisplayRow(0), 3)..DisplayPoint::new(DisplayRow(0), 3), ] ); }); @@ -4323,6 +4323,24 @@ fn test_delete_line(cx: &mut TestAppContext) { vec![DisplayPoint::new(DisplayRow(0), 1)..DisplayPoint::new(DisplayRow(0), 1)] ); }); + + let editor = cx.add_window(|window, cx| { + let buffer = MultiBuffer::build_simple("abc\ndef\nghi\n\njkl\nmno", cx); + build_editor(buffer, window, cx) + }); + _ = editor.update(cx, |editor, window, cx| { + editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| { + s.select_display_ranges([ + DisplayPoint::new(DisplayRow(0), 1)..DisplayPoint::new(DisplayRow(2), 1) + ]) + }); + editor.delete_line(&DeleteLine, window, cx); + assert_eq!(editor.display_text(cx), "\njkl\nmno"); + assert_eq!( + editor.selections.display_ranges(cx), + vec![DisplayPoint::new(DisplayRow(0), 0)..DisplayPoint::new(DisplayRow(0), 0)] + ); + }); } #[gpui::test]