cli: preserve keyword spacing in unpack and grouped ranges
This commit is contained in:
1 parent
9001e56325
commit
b1629a59fd
2 files changed
+28
-2
No files matched your search
+13
-2
@@ -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();
|
||||
|
||||
@@ -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 {}",
|
||||
|
||||
Reference in new issue
Block a user