From bbe956f750c37cbbe58baa1a04b77ae02ecb5d71 Mon Sep 17 00:00:00 2001 From: "gcp-cherry-pick-bot[bot]" <98988430+gcp-cherry-pick-bot[bot]@users.noreply.github.com> Date: Thu, 17 Apr 2025 21:50:04 -0600 Subject: [PATCH] Make Copy and Trim ignore empty lines, and fix vim line selections (cherry-pick #29019) (#29023) Cherry-picked Make Copy and Trim ignore empty lines, and fix vim line selections (#29019) Close #28519 Release Notes: Update `editor: copy and trim` command: 1. Ignore empty lines in the middle: ``` Line 1 Line 2 ``` Will copy text to clipboard: ``` Line 1 Line 2 ``` Before this commit trim not performed 1. Fix select use vim line selections, trim not works Co-authored-by: redforks --- crates/editor/src/editor.rs | 12 ++++++++++-- crates/editor/src/editor_tests.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index fde2bd0b6094e8257d4f32809d61ea4e20abe231..6440dd860c7d5d644b7dd4dfdb277f3f79cff518 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -10122,11 +10122,19 @@ impl Editor { ..Point::new(row.0, buffer.line_len(row)), ); for row in start.row + 1..=end.row { + let mut line_len = buffer.line_len(MultiBufferRow(row)); + if row == end.row { + line_len = end.column; + } + if line_len == 0 { + trimmed_selections + .push(Point::new(row, 0)..Point::new(row, line_len)); + continue; + } let row_indent_size = buffer.indent_size_for_line(MultiBufferRow(row)); if row_indent_size.len >= first_indent.len { trimmed_selections.push( - Point::new(row, first_indent.len) - ..Point::new(row, buffer.line_len(MultiBufferRow(row))), + Point::new(row, first_indent.len)..Point::new(row, line_len), ); } else { trimmed_selections.clear(); diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs index 655438e639dcd2879cf4e991b05f1ceee8583b44..2f8b62037fe0e7613085899ec05f78f111497a56 100644 --- a/crates/editor/src/editor_tests.rs +++ b/crates/editor/src/editor_tests.rs @@ -5121,6 +5121,36 @@ if is_entire_line { ), "When selecting past the indent, nothing is trimmed" ); + + cx.set_state( + r#" «for selection in selections.iter() { + let mut start = selection.start; + + let mut end = selection.end; + let is_entire_line = selection.is_empty(); + if is_entire_line { + start = Point::new(start.row, 0); +ˇ» end = cmp::min(max_point, Point::new(end.row + 1, 0)); + } + "#, + ); + cx.update_editor(|e, window, cx| e.copy_and_trim(&CopyAndTrim, window, cx)); + assert_eq!( + cx.read_from_clipboard() + .and_then(|item| item.text().as_deref().map(str::to_string)), + Some( + "for selection in selections.iter() { +let mut start = selection.start; + +let mut end = selection.end; +let is_entire_line = selection.is_empty(); +if is_entire_line { + start = Point::new(start.row, 0); +" + .to_string() + ), + "Copying with stripping should ignore empty lines" + ); } #[gpui::test]