Fix invalid number of space characters inserted for tab (#27336)

Closes #25941 

Release Notes:

- Corrected SoftTab indentation handling for lines with mixed spaces and
tabs across .go files and other file types.
- Renamed the editor test `test_tab_with_mixed_whitespace` to
`test_tab_with_mixed_whitespace_rust` as it only tested this behavior
for Rust buffers, which have auto-indentation support. This change
clarifies that the test does not cover default files without
language-specific features.
- Added a new editor test `test_tab_with_mixed_whitespace_txt` to ensure
proper coverage for files with no associated language.

While investigating the issue — initially thought to be Go-related — I
discovered that the underlying problem was how soft tabs were calculated
in `Editor::tab`, given that the problem could also be observed on
`.txt` files

The correct soft tab indentation is now determined by treating all `\t`
characters before the cursor (on the same row) as new indentation
levels, resetting the remainder counter accordingly.


https://github.com/user-attachments/assets/78192e98-2b81-43cb-ae6f-7c48cd17d168
This commit is contained in:
Rodrigo Freire 2025-04-09 17:22:14 +01:00 committed by GitHub
parent e43a397f1d
commit 2e56935997
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 36 additions and 5 deletions

View file

@ -8209,12 +8209,18 @@ impl Editor {
IndentSize::tab() IndentSize::tab()
} else { } else {
let tab_size = settings.tab_size.get(); let tab_size = settings.tab_size.get();
let char_column = snapshot let indent_remainder = snapshot
.text_for_range(Point::new(cursor.row, 0)..cursor) .text_for_range(Point::new(cursor.row, 0)..cursor)
.flat_map(str::chars) .flat_map(str::chars)
.count() .fold(row_delta % tab_size, |counter: u32, c| {
+ row_delta as usize; if c == '\t' {
let chars_to_next_tab_stop = tab_size - (char_column as u32 % tab_size); 0
} else {
(counter + 1) % tab_size
}
});
let chars_to_next_tab_stop = tab_size - indent_remainder;
IndentSize::spaces(chars_to_next_tab_stop) IndentSize::spaces(chars_to_next_tab_stop)
}; };
selection.start = Point::new(cursor.row, cursor.column + row_delta + tab_size.len); selection.start = Point::new(cursor.row, cursor.column + row_delta + tab_size.len);

View file

@ -2918,7 +2918,32 @@ async fn test_tab_in_leading_whitespace_auto_indents_lines(cx: &mut TestAppConte
} }
#[gpui::test] #[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| { init_test(cx, |settings| {
settings.defaults.tab_size = NonZeroU32::new(4) settings.defaults.tab_size = NonZeroU32::new(4)
}); });