editor: Fix multicursor indent edge case where few lines would indent incorrectly (#30461)

This should have been part of [editor: Fix inconsistent relative indent
when using tab with multi
cursors](https://github.com/zed-industries/zed/pull/29519)

Before / After:


https://github.com/user-attachments/assets/b7ab0eef-2764-44dc-b51f-b96dccd5ecb3

Release Notes:

- N/A

---------

Co-authored-by: Ben Kunkle <ben.kunkle@gmail.com>
This commit is contained in:
Smit Barmase 2025-05-10 06:13:15 -07:00 committed by GitHub
parent 471e02d48f
commit 172a475515
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 123 additions and 10 deletions

View file

@ -8779,15 +8779,13 @@ impl Editor {
continue;
}
// If the selection is empty and the cursor is in the leading whitespace before the
// suggested indentation, then auto-indent the line.
let cursor = selection.head();
let current_indent = snapshot.indent_size_for_line(MultiBufferRow(cursor.row));
if let Some(suggested_indent) =
suggested_indents.get(&MultiBufferRow(cursor.row)).copied()
{
// If there exist any empty selection in the leading whitespace, then skip
// indent for selections at the boundary.
// Don't do anything if already at suggested indent
// and there is any other cursor which is not
if has_some_cursor_in_whitespace
&& cursor.column == current_indent.len
&& current_indent.len == suggested_indent.len
@ -8795,6 +8793,8 @@ impl Editor {
continue;
}
// Adjust line and move cursor to suggested indent
// if cursor is not at suggested indent
if cursor.column < suggested_indent.len
&& cursor.column <= current_indent.len
&& current_indent.len <= suggested_indent.len
@ -8811,6 +8811,14 @@ impl Editor {
}
continue;
}
// If current indent is more than suggested indent
// only move cursor to current indent and skip indent
if cursor.column < current_indent.len && current_indent.len > suggested_indent.len {
selection.start = Point::new(cursor.row, current_indent.len);
selection.end = selection.start;
continue;
}
}
// Otherwise, insert a hard or soft tab.

View file

@ -2871,7 +2871,8 @@ async fn test_tab_in_leading_whitespace_auto_indents_lines(cx: &mut TestAppConte
);
cx.update_buffer(|buffer, cx| buffer.set_language(Some(language), cx));
// when all cursors are to the left of the suggested indent, then auto-indent all.
// test when all cursors are not at suggested indent
// then simply move to their suggested indent location
cx.set_state(indoc! {"
const a: B = (
c(
@ -2888,9 +2889,8 @@ async fn test_tab_in_leading_whitespace_auto_indents_lines(cx: &mut TestAppConte
);
"});
// cursors that are already at the suggested indent level do not move
// until other cursors that are to the left of the suggested indent
// auto-indent.
// test cursor already at suggested indent not moving when
// other cursors are yet to reach their suggested indents
cx.set_state(indoc! {"
ˇ
const a: B = (
@ -2914,8 +2914,7 @@ async fn test_tab_in_leading_whitespace_auto_indents_lines(cx: &mut TestAppConte
ˇ)
);
"});
// once all multi-cursors are at the suggested
// indent level, they all insert a soft tab together.
// test when all cursors are at suggested indent then tab is inserted
cx.update_editor(|e, window, cx| e.tab(&Tab, window, cx));
cx.assert_editor_state(indoc! {"
ˇ
@ -2929,6 +2928,112 @@ async fn test_tab_in_leading_whitespace_auto_indents_lines(cx: &mut TestAppConte
);
"});
// test when current indent is less than suggested indent,
// we adjust line to match suggested indent and move cursor to it
//
// when no other cursor is at word boundary, all of them should move
cx.set_state(indoc! {"
const a: B = (
c(
d(
ˇ
ˇ )
ˇ )
);
"});
cx.update_editor(|e, window, cx| e.tab(&Tab, window, cx));
cx.assert_editor_state(indoc! {"
const a: B = (
c(
d(
ˇ
ˇ)
ˇ)
);
"});
// test when current indent is less than suggested indent,
// we adjust line to match suggested indent and move cursor to it
//
// when some other cursor is at word boundary, it should not move
cx.set_state(indoc! {"
const a: B = (
c(
d(
ˇ
ˇ )
ˇ)
);
"});
cx.update_editor(|e, window, cx| e.tab(&Tab, window, cx));
cx.assert_editor_state(indoc! {"
const a: B = (
c(
d(
ˇ
ˇ)
ˇ)
);
"});
// test when current indent is more than suggested indent,
// we just move cursor to current indent instead of suggested indent
//
// when no other cursor is at word boundary, all of them should move
cx.set_state(indoc! {"
const a: B = (
c(
d(
ˇ
ˇ )
ˇ )
);
"});
cx.update_editor(|e, window, cx| e.tab(&Tab, window, cx));
cx.assert_editor_state(indoc! {"
const a: B = (
c(
d(
ˇ
ˇ)
ˇ)
);
"});
cx.update_editor(|e, window, cx| e.tab(&Tab, window, cx));
cx.assert_editor_state(indoc! {"
const a: B = (
c(
d(
ˇ
ˇ)
ˇ)
);
"});
// test when current indent is more than suggested indent,
// we just move cursor to current indent instead of suggested indent
//
// when some other cursor is at word boundary, it doesn't move
cx.set_state(indoc! {"
const a: B = (
c(
d(
ˇ
ˇ )
ˇ)
);
"});
cx.update_editor(|e, window, cx| e.tab(&Tab, window, cx));
cx.assert_editor_state(indoc! {"
const a: B = (
c(
d(
ˇ
ˇ)
ˇ)
);
"});
// handle auto-indent when there are multiple cursors on the same line
cx.set_state(indoc! {"
const a: B = (