authorgravatar for 178735591+87flowers@users.noreply.github.com87flowers <178735591+87flowers@users.noreply.github.com> 2024-10-17 01:52:53+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-02-22 17:09:20-08:00
logc724e157d6da0236dd8e84e3456ccb13c812adaf
tree5ebfffdec6871b8c6a2cce30bce1d11ff482394d
parenta702dc31ccb3443913b8cc30988d366316085767

std/zig/render: implement fixes for unit tests


2 files changed, 78 insertions(+), 67 deletions(-)

lib/std/zig/parser_test.zig+20-20
......@@ -1776,7 +1776,7 @@ test "zig fmt: if nested" {
17761776 \\ GE_EQUAL
17771777 \\ else
17781778 \\ GE_GREATER
1779 \\ // comment
1779 \\ // comment
17801780 \\ else if (aInt > bInt)
17811781 \\ GE_LESS
17821782 \\ else if (aInt == bInt)
......@@ -4670,7 +4670,7 @@ test "zig fmt: test comments in field access chain" {
46704670 \\ .more() //
46714671 \\ .more().more() //
46724672 \\ .more() //
4673 \\ // .more() //
4673 \\ // .more() //
46744674 \\ .more() //
46754675 \\ .more();
46764676 \\ data: Data,
......@@ -4679,9 +4679,9 @@ test "zig fmt: test comments in field access chain" {
46794679 \\pub const str = struct {
46804680 \\ pub const Thing = more.more //
46814681 \\ .more() //
4682 \\ // .more() //
4683 \\ // .more() //
4684 \\ // .more() //
4682 \\ // .more() //
4683 \\ // .more() //
4684 \\ // .more() //
46854685 \\ .more() //
46864686 \\ .more();
46874687 \\ data: Data,
......@@ -4706,7 +4706,7 @@ test "zig fmt: allow line break before field access" {
47064706 \\ const x = foo
47074707 \\ .bar()
47084708 \\ . // comment
4709 \\ // comment
4709 \\ // comment
47104710 \\ swooop().zippy(zag)
47114711 \\ .iguessthisisok();
47124712 \\
......@@ -4716,7 +4716,7 @@ test "zig fmt: allow line break before field access" {
47164716 \\ .input_manager //
47174717 \\ .default_seat
47184718 \\ . // comment
4719 \\ // another comment
4719 \\ // another comment
47204720 \\ wlr_seat.name;
47214721 \\}
47224722 \\
......@@ -4955,19 +4955,19 @@ test "zig fmt: use of comments and multiline string literals may force the param
49554955 \\
49564956 \\// This looks like garbage don't do this
49574957 \\const rparen = tree.prevToken(
4958 \\// the first token for the annotation expressions is the left
4959 \\// parenthesis, hence the need for two prevToken
4960 \\if (fn_proto.getAlignExpr()) |align_expr|
4961 \\ tree.prevToken(tree.prevToken(align_expr.firstToken()))
4962 \\else if (fn_proto.getSectionExpr()) |section_expr|
4963 \\ tree.prevToken(tree.prevToken(section_expr.firstToken()))
4964 \\else if (fn_proto.getCallconvExpr()) |callconv_expr|
4965 \\ tree.prevToken(tree.prevToken(callconv_expr.firstToken()))
4966 \\else switch (fn_proto.return_type) {
4967 \\ .Explicit => |node| node.firstToken(),
4968 \\ .InferErrorSet => |node| tree.prevToken(node.firstToken()),
4969 \\ .Invalid => unreachable,
4970 \\});
4958 \\ // the first token for the annotation expressions is the left
4959 \\ // parenthesis, hence the need for two prevToken
4960 \\ if (fn_proto.getAlignExpr()) |align_expr|
4961 \\ tree.prevToken(tree.prevToken(align_expr.firstToken()))
4962 \\ else if (fn_proto.getSectionExpr()) |section_expr|
4963 \\ tree.prevToken(tree.prevToken(section_expr.firstToken()))
4964 \\ else if (fn_proto.getCallconvExpr()) |callconv_expr|
4965 \\ tree.prevToken(tree.prevToken(callconv_expr.firstToken()))
4966 \\ else switch (fn_proto.return_type) {
4967 \\ .Explicit => |node| node.firstToken(),
4968 \\ .InferErrorSet => |node| tree.prevToken(node.firstToken()),
4969 \\ .Invalid => unreachable,
4970 \\ });
49714971 \\
49724972 );
49734973}
lib/std/zig/render.zig+58-47
......@@ -369,23 +369,19 @@ fn renderExpression(r: *Render, node: Ast.Node.Index, space: Space) Error!void {
369369 while (i <= datas[node].rhs) : (i += 1) try renderToken(r, i, .newline);
370370
371371 // dedent the next thing that comes after a multiline string literal
372 if (!ais.indentStackEmpty()) {
372 if (!ais.indentStackEmpty() and
373 token_tags[i] != .colon and
374 ((token_tags[i] != .semicolon and token_tags[i] != .comma) or
375 ais.lastSpaceModeIndent() < ais.currentIndent()))
376 {
373377 ais.popIndent();
374378 try ais.pushIndent(.normal);
375379 }
376380
377381 switch (space) {
378382 .none, .space, .newline, .skip => {},
379 .semicolon => if (token_tags[i] == .semicolon) {
380 ais.enableSpaceMode(.semicolon);
381 try renderToken(r, i, .newline);
382 ais.disableSpaceMode();
383 },
384 .comma => if (token_tags[i] == .comma) {
385 ais.enableSpaceMode(.comma);
386 try renderToken(r, i, .newline);
387 ais.disableSpaceMode();
388 },
383 .semicolon => if (token_tags[i] == .semicolon) try renderTokenOverrideSpaceMode(r, i, .newline, .semicolon),
384 .comma => if (token_tags[i] == .comma) try renderTokenOverrideSpaceMode(r, i, .newline, .comma),
389385 .comma_space => if (token_tags[i] == .comma) try renderToken(r, i, .space),
390386 }
391387 },
......@@ -476,7 +472,7 @@ fn renderExpression(r: *Render, node: Ast.Node.Index, space: Space) Error!void {
476472 const main_token = main_tokens[node];
477473 const field_access = datas[node];
478474
479 try ais.pushIndent(.normal);
475 try ais.pushIndent(.field_access);
480476 try renderExpression(r, field_access.lhs, .none);
481477
482478 // Allow a line break between the lhs and the dot if the lhs and rhs
......@@ -740,8 +736,8 @@ fn renderExpression(r: *Render, node: Ast.Node.Index, space: Space) Error!void {
740736 },
741737
742738 .grouped_expression => {
743 try renderToken(r, main_tokens[node], .none); // lparen
744739 try ais.pushIndent(.normal);
740 try renderToken(r, main_tokens[node], .none); // lparen
745741 try renderExpression(r, datas[node].lhs, .none);
746742 ais.popIndent();
747743 return renderToken(r, datas[node].rhs, space); // rparen
......@@ -2444,7 +2440,7 @@ fn renderAsm(
24442440 }
24452441
24462442 if (asm_node.ast.items.len == 0) {
2447 try ais.pushIndent(.normal);
2443 try ais.forcePushIndent(.normal);
24482444 if (asm_node.first_clobber) |first_clobber| {
24492445 // asm ("foo" ::: "a", "b")
24502446 // asm ("foo" ::: "a", "b",)
......@@ -2482,7 +2478,7 @@ fn renderAsm(
24822478 }
24832479 }
24842480
2485 try ais.pushIndent(.normal);
2481 try ais.forcePushIndent(.normal);
24862482 try renderExpression(r, asm_node.ast.template, .newline);
24872483 ais.setIndentDelta(asm_indent_delta);
24882484 const colon1 = tree.lastToken(asm_node.ast.template) + 1;
......@@ -2493,7 +2489,7 @@ fn renderAsm(
24932489 } else colon2: {
24942490 try renderToken(r, colon1, .space); // :
24952491
2496 try ais.pushIndent(.normal);
2492 try ais.forcePushIndent(.normal);
24972493 for (asm_node.outputs, 0..) |asm_output, i| {
24982494 if (i + 1 < asm_node.outputs.len) {
24992495 const next_asm_output = asm_node.outputs[i + 1];
......@@ -2529,7 +2525,7 @@ fn renderAsm(
25292525 break :colon3 colon2 + 1;
25302526 } else colon3: {
25312527 try renderToken(r, colon2, .space); // :
2532 try ais.pushIndent(.normal);
2528 try ais.forcePushIndent(.normal);
25332529 for (asm_node.inputs, 0..) |asm_input, i| {
25342530 if (i + 1 < asm_node.inputs.len) {
25352531 const next_asm_input = asm_node.inputs[i + 1];
......@@ -2568,16 +2564,16 @@ fn renderAsm(
25682564 switch (token_tags[tok_i + 1]) {
25692565 .r_paren => {
25702566 ais.setIndentDelta(indent_delta);
2571 ais.popIndent();
25722567 try renderToken(r, tok_i, .newline);
2568 ais.popIndent();
25732569 return renderToken(r, tok_i + 1, space);
25742570 },
25752571 .comma => {
25762572 switch (token_tags[tok_i + 2]) {
25772573 .r_paren => {
25782574 ais.setIndentDelta(indent_delta);
2579 ais.popIndent();
25802575 try renderToken(r, tok_i, .newline);
2576 ais.popIndent();
25812577 return renderToken(r, tok_i + 2, space);
25822578 },
25832579 else => {
......@@ -2644,19 +2640,10 @@ fn renderParamList(
26442640 return renderToken(r, after_last_param_tok + 1, space); // )
26452641 }
26462642
2643 try ais.pushIndent(.normal);
26472644 try renderToken(r, lparen, .none); // (
2648
26492645 for (params, 0..) |param_node, i| {
2650 const first_param_token = tree.firstToken(param_node);
2651 if (token_tags[first_param_token] == .multiline_string_literal_line or
2652 hasSameLineComment(tree, first_param_token - 1))
2653 {
2654 try ais.pushIndent(.normal);
2655 try renderExpression(r, param_node, .none);
2656 ais.popIndent();
2657 } else {
2658 try renderExpression(r, param_node, .none);
2659 }
2646 try renderExpression(r, param_node, .none);
26602647
26612648 if (i + 1 < params.len) {
26622649 const comma = tree.lastToken(param_node) + 1;
......@@ -2666,7 +2653,7 @@ fn renderParamList(
26662653 try renderToken(r, comma, comma_space);
26672654 }
26682655 }
2669
2656 ais.popIndent();
26702657 return renderToken(r, after_last_param_tok, space); // )
26712658}
26722659
......@@ -2741,6 +2728,16 @@ fn renderToken(r: *Render, token_index: Ast.TokenIndex, space: Space) Error!void
27412728 try renderSpace(r, token_index, lexeme.len, space);
27422729}
27432730
2731fn renderTokenOverrideSpaceMode(r: *Render, token_index: Ast.TokenIndex, space: Space, override_space: Space) Error!void {
2732 const tree = r.tree;
2733 const ais = r.ais;
2734 const lexeme = tokenSliceForRender(tree, token_index);
2735 try ais.writer().writeAll(lexeme);
2736 ais.enableSpaceMode(override_space);
2737 defer ais.disableSpaceMode();
2738 try renderSpace(r, token_index, lexeme.len, space);
2739}
2740
27442741fn renderSpace(r: *Render, token_index: Ast.TokenIndex, lexeme_len: usize, space: Space) Error!void {
27452742 const tree = r.tree;
27462743 const ais = r.ais;
......@@ -3315,6 +3312,7 @@ fn AutoIndentingStream(comptime UnderlyingWriter: type) type {
33153312 normal,
33163313 after_equals,
33173314 binop,
3315 field_access,
33183316 };
33193317 const StackElem = struct {
33203318 indent_type: IndentType,
......@@ -3404,20 +3402,26 @@ fn AutoIndentingStream(comptime UnderlyingWriter: type) type {
34043402 self.current_line_empty = true;
34053403 if (self.disable_indent_committing > 0) return;
34063404 if (self.indent_stack.items.len > 0) {
3407 // Only realize last pushed indent
3408 if (!self.indent_stack.items[self.indent_stack.items.len - 1].realized) {
3409 if (self.indent_stack.items.len >= 2 and
3410 self.indent_stack.items[self.indent_stack.items.len - 2].indent_type == .after_equals and
3411 self.indent_stack.items[self.indent_stack.items.len - 2].realized and
3412 self.indent_stack.items[self.indent_stack.items.len - 1].indent_type == .binop)
3413 {
3414 // collapse one level of indentation in binop after equals sign
3415 return;
3416 }
3405 var to_realize = self.indent_stack.items.len - 1;
3406
3407 if (self.indent_stack.items.len >= 2 and
3408 self.indent_stack.items[to_realize - 1].indent_type == .after_equals and
3409 self.indent_stack.items[to_realize - 1].realized and
3410 self.indent_stack.items[to_realize].indent_type == .binop)
3411 {
3412 // collapse one level of indentation in binop after equals sign
3413 return;
3414 }
34173415
3418 self.indent_stack.items[self.indent_stack.items.len - 1].realized = true;
3419 self.indent_count += 1;
3416 if (self.indent_stack.items[to_realize].indent_type == .field_access) {
3417 // only realize topmost field_access in a chain
3418 while (to_realize > 0 and self.indent_stack.items[to_realize - 1].indent_type == .field_access)
3419 to_realize -= 1;
34203420 }
3421
3422 if (self.indent_stack.items[to_realize].realized) return;
3423 self.indent_stack.items[to_realize].realized = true;
3424 self.indent_count += 1;
34213425 }
34223426 }
34233427
......@@ -3441,10 +3445,7 @@ fn AutoIndentingStream(comptime UnderlyingWriter: type) type {
34413445 pub fn enableSpaceMode(self: *Self, space: Space) void {
34423446 if (self.space_stack.items.len == 0) return;
34433447 const curr = self.space_stack.getLast();
3444 if (curr.space != space) {
3445 return;
3446 }
3447 assert(curr.space == space);
3448 if (curr.space != space) return;
34483449 self.space_mode = curr.indent_count;
34493450 }
34503451
......@@ -3452,6 +3453,11 @@ fn AutoIndentingStream(comptime UnderlyingWriter: type) type {
34523453 self.space_mode = null;
34533454 }
34543455
3456 pub fn lastSpaceModeIndent(self: *Self) usize {
3457 if (self.space_stack.items.len == 0) return 0;
3458 return self.space_stack.getLast().indent_count * self.indent_delta;
3459 }
3460
34553461 /// Insert a newline unless the current line is blank
34563462 pub fn maybeInsertNewline(self: *Self) WriteError!void {
34573463 if (!self.current_line_empty)
......@@ -3465,6 +3471,11 @@ fn AutoIndentingStream(comptime UnderlyingWriter: type) type {
34653471 try self.indent_stack.append(.{ .indent_type = indent_type, .realized = false });
34663472 }
34673473
3474 pub fn forcePushIndent(self: *Self, indent_type: IndentType) !void {
3475 try self.indent_stack.append(.{ .indent_type = indent_type, .realized = true });
3476 self.indent_count += 1;
3477 }
3478
34683479 pub fn popIndent(self: *Self) void {
34693480 if (self.indent_stack.pop().realized) {
34703481 assert(self.indent_count > 0);