join_lines: Skip over leading indentation

Conrad Irwin created

Change summary

crates/editor/src/editor.rs       |  5 +++--
crates/editor/src/editor_tests.rs | 28 ++++++++++++++++++++++++++++
2 files changed, 31 insertions(+), 2 deletions(-)

Detailed changes

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 {
                         ""

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
     });
 }