cli: disambiguate block statement boundaries
This commit is contained in:
1 parent
462ddd2c5c
commit
911982dd1c
33 files changed
+465
-7
No files matched your search
+18
-3
@@ -11,7 +11,7 @@ const Error = error{ OutOfMemory, Invariant };
|
||||
pub const Result = union(enum) { ok: []const u8, err: parse.Failure };
|
||||
|
||||
const Curly = enum { block, list, literal };
|
||||
const Mark = struct { angle: bool = false, prefix: bool = false, curly: Curly = .block, top: bool = false, module_end: bool = false, mate: usize = 0, multiline: bool = false, layout_scope: usize = 0 };
|
||||
const Mark = struct { angle: bool = false, prefix: bool = false, curly: Curly = .block, top: bool = false, module_end: bool = false, statement_end: bool = false, mate: usize = 0, multiline: bool = false, layout_scope: usize = 0 };
|
||||
const Node = union(enum) { block: *const ast.Block, stmt: *const ast.Stmt, expr: *const ast.Expr };
|
||||
|
||||
pub fn format(arena: Allocator, source: []const u8) Error!Result {
|
||||
@@ -107,7 +107,11 @@ const Formatter = struct {
|
||||
// Iterative traversal also handles long left-associated expressions.
|
||||
while (queue.pop()) |node| switch (node) {
|
||||
.block => |b| {
|
||||
for (b.stmts) |*s| try queue.append(f.arena, .{ .stmt = s });
|
||||
for (b.stmts) |*s| {
|
||||
const end = f.index(s.span.end);
|
||||
if (end != 0 and f.tokens[end - 1].tag == .r_brace) f.marks[end - 1].statement_end = true;
|
||||
try queue.append(f.arena, .{ .stmt = s });
|
||||
}
|
||||
if (b.tail) |e| try queue.append(f.arena, .{ .expr = e });
|
||||
},
|
||||
.stmt => |s| switch (s.kind) {
|
||||
@@ -346,7 +350,7 @@ const Formatter = struct {
|
||||
const frame = f.frames.items[f.frames.items.len - 1];
|
||||
f.pending_line = frame.tag == .l_brace and frame.curly == .list;
|
||||
},
|
||||
.r_brace => f.pending_line = closing.?.curly != .literal and afterBlock(next),
|
||||
.r_brace => f.pending_line = f.marks[i].statement_end or (closing.?.curly != .literal and afterBlock(next)),
|
||||
else => {},
|
||||
}
|
||||
if (f.marks[i].module_end) f.pending_line = true;
|
||||
@@ -407,6 +411,17 @@ test "libraries and syntax failures are independent of semantics" {
|
||||
try std.testing.expect((try format(a.allocator(), "module bad fn main()->u64 effects [] {return 1}")) == .err);
|
||||
}
|
||||
|
||||
test "AST statement boundaries separate unary starts without splitting expression contexts" {
|
||||
var a = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer a.deinit();
|
||||
const input = "module m fn f()->u64 effects [] {if true {} else {} *p=2u64; match 0u64 {0=>{} _=>{}} -3i64; {} (1u64); {} [1u64;2]; let x={2u64}*3u64; ({2u64})*3u64}";
|
||||
const output = (try format(a.allocator(), input)).ok;
|
||||
for ([_][]const u8{ "else {}\n *p =", "}\n -3i64;", "{}\n (1u64);", "{}\n [1u64; 2];", "} * 3u64;", "}) * 3u64" }) |part| {
|
||||
try std.testing.expect(std.mem.indexOf(u8, output, part) != null);
|
||||
}
|
||||
try std.testing.expectEqualStrings(output, (try format(a.allocator(), output)).ok);
|
||||
}
|
||||
|
||||
test "angles, shifts, literals, grouping and standalone comments" {
|
||||
var a = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer a.deinit();
|
||||
|
||||
Reference in new issue
Block a user