From b1629a59fd1d81211cb42f6829f7f2381fda7c44 Mon Sep 17 00:00:00 2001 From: "A.Shakhmatov" Date: Wed, 7 Oct 2026 20:36:27 +0300 Subject: [PATCH] cli: preserve keyword spacing in unpack and grouped ranges --- src/frontend/format.zig | 15 +++++++++++++-- tests/hosted/source_format.py | 15 +++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/frontend/format.zig b/src/frontend/format.zig index fb5a6b2..09a661f 100644 --- a/src/frontend/format.zig +++ b/src/frontend/format.zig @@ -377,14 +377,14 @@ fn needsSpace(a: Token, b: Token, am: Mark, bm: Mark) bool { if (bm.angle) return false; if (am.angle and a.tag == .lt) return false; if (am.prefix) return word(a, "not"); + if (a.tag == .dot_dot or b.tag == .dot_dot) return false; if (b.tag == .l_paren) { - if (a.tag == .ident) return word(a, "if") or word(a, "while") or word(a, "return") or word(a, "match") or word(a, "not") or word(a, "and") or word(a, "or"); + if (a.tag == .ident) return word(a, "if") or word(a, "while") or word(a, "return") or word(a, "match") or word(a, "not") or word(a, "and") or word(a, "or") or word(a, "let") or word(a, "in"); if (am.angle and (a.tag == .gt or a.tag == .shr)) return false; return a.tag != .r_paren and a.tag != .r_bracket and a.tag != .int and a.tag != .bytes; } if (b.tag == .l_bracket and (a.tag == .ident or a.tag == .r_bracket or a.tag == .r_paren)) return word(a, "effects") or word(a, "return") or word(a, "in") or word(a, "and") or word(a, "or"); if (a.tag == .l_brace and b.tag == .r_brace) return false; - if (a.tag == .dot_dot or b.tag == .dot_dot) return false; return true; } fn merges(a: []const u8, b: []const u8) bool { @@ -434,6 +434,17 @@ test "angles, shifts, literals, grouping and standalone comments" { try std.testing.expectEqualStrings(output, (try format(a.allocator(), output)).ok); } +test "grouped range bounds and unpack bindings retain keyword spaces" { + const source = "module m fn main()->u64 effects [] {let p=[1u64,2u64];let(a,b)=array.unpack(p);var n:u64=0;for i in(0usize)..(2usize){n=n+a+b;}n}"; + var arena = std.heap.ArenaAllocator.init(std.testing.allocator); + defer arena.deinit(); + const result = try format(arena.allocator(), source); + const output = result.ok; + try std.testing.expect(std.mem.indexOf(u8, output, "let (a, b) = array.unpack(p);") != null); + try std.testing.expect(std.mem.indexOf(u8, output, "for i in (0usize)..(2usize)") != null); + try std.testing.expectEqualStrings(output, (try format(arena.allocator(), output)).ok); +} + test "long left-associated expressions use an iterative layout traversal" { var a = std.heap.ArenaAllocator.init(std.testing.allocator); defer a.deinit(); diff --git a/tests/hosted/source_format.py b/tests/hosted/source_format.py index f22b3ca..e74c474 100644 --- a/tests/hosted/source_format.py +++ b/tests/hosted/source_format.py @@ -112,6 +112,21 @@ with tempfile.TemporaryDirectory(prefix="aji-format-") as directory: assert b"// lit \t\n b: 2u64" in text assert b"not (true and false)" in text + # Grouped range bounds and unpack bindings have keyword spacing, + # while calls and the range operator stay compact. AIR stays identical. + sample = root / "keyword-spaces.aji" + sample.write_bytes(b"module spaces fn main()->u64 effects [] {let p=[1u64,2u64];" + b"let(a,b)=array.unpack(p);var n:u64=0;" + b"for i in(0usize)..(2usize){n=n+a+b;}n}") + original_air = cli("lower", sample) + text = cli("fmt", sample) + assert b"let (a, b) = array.unpack(p);" in text + assert b"for i in (0usize)..(2usize)" in text + sample.write_bytes(text) + assert cli("fmt", sample) == text + assert cli("lower", sample) == original_air + assert json.loads(cli("run", "--fuel", "10000", sample))["value"] == 6 + # Q-040: statement boundaries are lexical, not dependent on the original # line breaks. Formatting exposes them without inserting separators. for construct in ("{*p=2u64;}", "if true {*p=2u64;} else {}",