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.

Fix hard-tab block autoindent skipping unindented lines (#60406)

Apoorva Vermad9ada8487bca
diff --git a/crates/language/src/buffer.rs b/crates/language/src/buffer.rs
index d41143d12f5..c37065146f2 100644
--- a/crates/language/src/buffer.rs
+++ b/crates/language/src/buffer.rs
@@ -2163,6 +2163,11 @@ impl Buffer {
for row in row_range.skip(1) {
indent_sizes.entry(row).or_insert_with(|| {
let mut size = snapshot.indent_size_for_line(row);
+ // A line with no indentation has an arbitrary
+ // indent kind, so it can adopt the new kind.
+ if size.len == 0 {
+ size.kind = new_indent.kind;
+ }
if size.kind == new_indent.kind {
match delta.cmp(&0) {
Ordering::Greater => size.len += delta as u32,
diff --git a/crates/language/src/buffer_tests.rs b/crates/language/src/buffer_tests.rs
index cdcd90314cd..29afbe84b72 100644
--- a/crates/language/src/buffer_tests.rs
+++ b/crates/language/src/buffer_tests.rs
@@ -2180,6 +2180,38 @@ fn test_autoindent_block_mode_without_original_indent_columns(cx: &mut App) {
});
}
+#[gpui::test]
+fn test_autoindent_block_mode_with_hard_tabs(cx: &mut App) {
+ init_settings(cx, |settings| {
+ settings.defaults.hard_tabs = Some(true);
+ });
+
+ cx.new(|cx| {
+ let text = "fn a() {\n\tb();\n}";
+ let mut buffer = Buffer::local(text, cx).with_language(rust_lang(), cx);
+
+ // Insert a block whose indentation mixes tab-indented lines with
+ // lines that have no leading whitespace, like a snippet body.
+ let inserted_text = "if c {\n\td();\n}\n";
+ buffer.edit(
+ [(Point::new(2, 0)..Point::new(2, 0), inserted_text)],
+ Some(AutoindentMode::Block {
+ original_indent_columns: Vec::new(),
+ }),
+ cx,
+ );
+
+ // All of the block's lines are indented, including the ones that
+ // originally had no indentation.
+ assert_eq!(
+ buffer.text(),
+ "fn a() {\n\tb();\n\tif c {\n\t\td();\n\t}\n}"
+ );
+
+ buffer
+ });
+}
+
#[gpui::test]
fn test_autoindent_block_mode_multiple_adjacent_ranges(cx: &mut App) {
init_settings(cx, |_| {});