cli: preserve keyword spacing in unpack and grouped ranges

This commit is contained in:
dev committed 2026-10-07 20:36:27 +03:00
1 parent 9001e56325
commit b1629a59fd
2 files changed
+28 -2

No files matched your search

+13 -2
View File
@@ -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();