From 91bd8e305e773b6300577568ab35c72b9352ccf2 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Wed, 21 Jun 2023 13:14:21 -0600 Subject: [PATCH] join_lines: Skip over leading indentation --- crates/editor/src/editor.rs | 5 +++-- crates/editor/src/editor_tests.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index c8daff3fd5ff2b5609c65763b141cd30e18ffd23..3c6410cef903269df099477c962082bb070ca475 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -3987,9 +3987,10 @@ impl Editor { for row_range in row_ranges.into_iter().rev() { for row in row_range.rev() { let end_of_line = Point::new(row, snapshot.line_len(row)); - let start_of_next_line = end_of_line + Point::new(1, 0); + let indent = snapshot.indent_size_for_line(row + 1); + let start_of_next_line = Point::new(row + 1, indent.len); - let replace = if snapshot.line_len(row + 1) > 0 { + let replace = if snapshot.line_len(row + 1) > indent.len { " " } else { "" diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs index ab950fd5a56cf307f2aaf13379230a62431cbac1..6fcb6f778ff08ffa18a481272439f23eb1f5532f 100644 --- a/crates/editor/src/editor_tests.rs +++ b/crates/editor/src/editor_tests.rs @@ -2396,6 +2396,34 @@ fn test_join_lines_with_single_selection(cx: &mut TestAppContext) { [Point::new(2, 3)..Point::new(2, 3)] ); + // reset to test indentation + editor.buffer.update(cx, |buffer, cx| { + buffer.edit( + [ + (Point::new(1, 0)..Point::new(1, 2), " "), + (Point::new(2, 0)..Point::new(2, 3), " \n\td"), + ], + None, + cx, + ) + }); + + // We remove any leading spaces + assert_eq!(buffer.read(cx).text(), "aaa bbb\n c\n \n\td"); + editor.change_selections(None, cx, |s| { + s.select_ranges([Point::new(0, 1)..Point::new(0, 1)]) + }); + editor.join_lines(&JoinLines, cx); + assert_eq!(buffer.read(cx).text(), "aaa bbb c\n \n\td"); + + // We don't insert a space for a line containing only spaces + editor.join_lines(&JoinLines, cx); + assert_eq!(buffer.read(cx).text(), "aaa bbb c\n\td"); + + // We ignore any leading tabs + editor.join_lines(&JoinLines, cx); + assert_eq!(buffer.read(cx).text(), "aaa bbb c d"); + editor }); }