authorgravatar for goon.pri.low@gmail.comKendall Condon <goon.pri.low@gmail.com> 2025-07-28 13:32:29-04:00
committergravatar for goon.pri.low@gmail.comKendall Condon <goon.pri.low@gmail.com> 2026-03-12 17:23:07-04:00
logd5d3e40b6c262875d766946d0edc02b8cd14ee98
treecbcc620ed0ea1ead41bf25defe4c022a9e2871de
parentfcc54ca981d30af0a187c9d187b2a63ce0195f3c

zig fmt: preserve space for field accesses of number literals


2 files changed, 18 insertions(+), 4 deletions(-)

lib/std/zig/Ast/Render.zig+9-4
...@@ -441,13 +441,18 @@ fn renderExpression(r: *Render, node: Ast.Node.Index, space: Space) Error!void {...@@ -441,13 +441,18 @@ fn renderExpression(r: *Render, node: Ast.Node.Index, space: Space) Error!void {
441 const dot_token = name_token - 1;441 const dot_token = name_token - 1;
442442
443 try ais.pushIndent(.field_access);443 try ais.pushIndent(.field_access);
444 try renderExpression(r, lhs, .none);
445444
446 // Allow a line break between the lhs and the dot if the lhs and rhs
447 // are on different lines.
448 const lhs_last_token = tree.lastToken(lhs);445 const lhs_last_token = tree.lastToken(lhs);
449 const same_line = tree.tokensOnSameLine(lhs_last_token, name_token);446 const same_line = tree.tokensOnSameLine(lhs_last_token, name_token);
450 if (!same_line and !hasComment(tree, lhs_last_token, dot_token)) try ais.insertNewline();447 // Keeping a space after the number ensures it will not turn into a decimal number
448 // (e.g. "0xF .A").
449 const number_space: Space = if (tree.tokenTag(lhs_last_token) == .number_literal and same_line) .space else .none;
450 try renderExpression(r, lhs, number_space);
451
452 // Allow a line break between the lhs and the dot if the lhs and rhs
453 // are on different lines.
454 if (!same_line and !hasComment(tree, lhs_last_token, dot_token))
455 try ais.insertNewline();
451456
452 try renderToken(r, dot_token, .none);457 try renderToken(r, dot_token, .none);
453458
lib/std/zig/parser_test.zig+9
...@@ -6054,6 +6054,15 @@ test "zig fmt: extern addrspace in struct" {...@@ -6054,6 +6054,15 @@ test "zig fmt: extern addrspace in struct" {
6054 );6054 );
6055}6055}
60566056
6057test "zig fmt: field accesses on number literals" {
6058 try testCanonical(
6059 \\const a = 0xF .A;
6060 \\const a = 0xF
6061 \\ .A;
6062 \\
6063 );
6064}
6065
6057test "zig fmt: whitespace with multiline strings" {6066test "zig fmt: whitespace with multiline strings" {
6058 try testCanonical(6067 try testCanonical(
6059 \\const a = .{6068 \\const a = .{