Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Fix invalid number of space characters inserted for tab (#27336)
Rodrigo Freire2e56935997ae
diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rsindex 6290a0606a2..9fe0e81ab0a 100644--- a/crates/editor/src/editor.rs+++ b/crates/editor/src/editor.rs@@ -8209,12 +8209,18 @@ impl Editor {IndentSize::tab()} else {let tab_size = settings.tab_size.get();- let char_column = snapshot+ let indent_remainder = snapshot.text_for_range(Point::new(cursor.row, 0)..cursor).flat_map(str::chars)- .count()- + row_delta as usize;- let chars_to_next_tab_stop = tab_size - (char_column as u32 % tab_size);+ .fold(row_delta % tab_size, |counter: u32, c| {+ if c == '\t' {+ 0+ } else {+ (counter + 1) % tab_size+ }+ });++ let chars_to_next_tab_stop = tab_size - indent_remainder;IndentSize::spaces(chars_to_next_tab_stop)};selection.start = Point::new(cursor.row, cursor.column + row_delta + tab_size.len);diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rsindex 761add2715d..68eff95163b 100644--- a/crates/editor/src/editor_tests.rs+++ b/crates/editor/src/editor_tests.rs@@ -2918,7 +2918,32 @@ async fn test_tab_in_leading_whitespace_auto_indents_lines(cx: &mut TestAppConte}#[gpui::test]-async fn test_tab_with_mixed_whitespace(cx: &mut TestAppContext) {+async fn test_tab_with_mixed_whitespace_txt(cx: &mut TestAppContext) {+ init_test(cx, |settings| {+ settings.defaults.tab_size = NonZeroU32::new(3)+ });++ let mut cx = EditorTestContext::new(cx).await;+ cx.set_state(indoc! {"+ ˇ+ \t ˇ+ \t ˇ+ \t ˇ+ \t \t\t \t \t\t \t\t \t \t ˇ+ "});++ cx.update_editor(|e, window, cx| e.tab(&Tab, window, cx));+ cx.assert_editor_state(indoc! {"+ ˇ+ \t ˇ+ \t ˇ+ \t ˇ+ \t \t\t \t \t\t \t\t \t \t ˇ+ "});+}++#[gpui::test]+async fn test_tab_with_mixed_whitespace_rust(cx: &mut TestAppContext) {init_test(cx, |settings| {settings.defaults.tab_size = NonZeroU32::new(4)});