From c477df98bbddb35e3abb7d88fbe7e250660458b8 Mon Sep 17 00:00:00 2001 From: Kendall Condon Date: Mon, 28 Jul 2025 14:18:14 -0400 Subject: [PATCH] zig fmt: fix tracking of applied indentation --- lib/std/zig/Ast/Render.zig | 4 ++-- lib/std/zig/parser_test.zig | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/std/zig/Ast/Render.zig b/lib/std/zig/Ast/Render.zig index 85ff1993a1a4458b108f020a029b7a7ff43d8426..28d7b684581e687793e05ed476bff9efdb3579c9 100644 --- a/lib/std/zig/Ast/Render.zig +++ b/lib/std/zig/Ast/Render.zig @@ -3883,8 +3883,8 @@ const AutoIndentingStream = struct { /// Writes ' ' bytes if the current line is empty fn applyIndent(ais: *AutoIndentingStream) Error!void { const current_indent = ais.currentIndent(); - if (ais.current_line_empty and current_indent > 0) { - if (ais.disabled_offset == null) { + if (ais.current_line_empty) { + if (current_indent > 0 and ais.disabled_offset == null) { try ais.underlying_writer.splatByteAll(' ', current_indent); } ais.applied_indent = current_indent; diff --git a/lib/std/zig/parser_test.zig b/lib/std/zig/parser_test.zig index ac3a52dfd41c97a12c1bf544d271de899a3c117b..429f2791259ea553caf305d4cde86949ddc2bc2a 100644 --- a/lib/std/zig/parser_test.zig +++ b/lib/std/zig/parser_test.zig @@ -6676,6 +6676,23 @@ test "zig fmt: doc comments on fn parameters" { ); } +test "zig fmt: proper tracking of indentation" { + try testCanonical( + \\const a = { + \\ {} + \\}; + \\const b = if (x) {}; + \\const c = .{ + \\ { + \\ {} + \\ } // + \\ , + \\ if (x) {}, + \\}; + \\ + ); +} + test "zig fmt: canonicalize stray backslashes in identifiers" { try testTransform( \\const @"\x" = undefined; -- 2.54.0