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.

join_lines: Skip over leading indentation

Conrad Irwin91bd8e305e77
diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs
index c8daff3fd5f..3c6410cef90 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 ab950fd5a56..6fcb6f778ff 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
});
}