authorgravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-02-23 19:56:56+01:00
committergravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-02-23 19:56:56+01:00
logb028a92a6033b5a8de6607b3f544f9c8f376f0fd
treece2a4dbf48603088c12ce1c43b443904f5a10baf
parentabfe21383035522945e52ffb3954c03398767a39
signaturelock-open Commit is signed but in an unrecognized format.

zig fmt: handle comments in array type/init/access


2 files changed, 53 insertions(+), 39 deletions(-)

lib/std/zig/parser_test.zig+35-31
...@@ -3521,37 +3521,41 @@ test "zig fmt: comment after params" {...@@ -3521,37 +3521,41 @@ test "zig fmt: comment after params" {
3521 );3521 );
3522}3522}
35233523
3524//test "zig fmt: comment in array initializer/access" {3524test "zig fmt: comment in array initializer/access" {
3525// try testCanonical(3525 try testCanonical(
3526// \\test "a" {3526 \\test "a" {
3527// \\ var a = x{ //aa3527 \\ var a = x{ //aa
3528// \\ //bb3528 \\ //bb
3529// \\ };3529 \\ };
3530// \\ var a = []x{ //aa3530 \\ var a = []x{ //aa
3531// \\ //bb3531 \\ //bb
3532// \\ };3532 \\ };
3533// \\ var b = [ //aa3533 \\ var b = [ //aa
3534// \\ _3534 \\ _
3535// \\ ]x{ //aa3535 \\ ]x{ //aa
3536// \\ //bb3536 \\ //bb
3537// \\ 9,3537 \\ 9,
3538// \\ };3538 \\ };
3539// \\ var c = b[ //aa3539 \\ var c = b[ //aa
3540// \\ 03540 \\ 0
3541// \\ ];3541 \\ ];
3542// \\ var d = [_3542 \\ var d = [
3543// \\ //aa3543 \\ _
3544// \\ ]x{ //aa3544 \\ //aa
3545// \\ //bb3545 \\ :
3546// \\ 9,3546 \\ 0
3547// \\ };3547 \\ ]x{ //aa
3548// \\ var e = d[03548 \\ //bb
3549// \\ //aa3549 \\ 9,
3550// \\ ];3550 \\ };
3551// \\}3551 \\ var e = d[
3552// \\3552 \\ 0
3553// );3553 \\ //aa
3554//}3554 \\ ];
3555 \\}
3556 \\
3557 );
3558}
35553559
3556test "zig fmt: comments at several places in struct init" {3560test "zig fmt: comments at several places in struct init" {
3557 try testTransform(3561 try testTransform(
lib/std/zig/render.zig+18-8
...@@ -439,9 +439,13 @@ fn renderExpression(gpa: *Allocator, ais: *Ais, tree: ast.Tree, node: ast.Node.I...@@ -439,9 +439,13 @@ fn renderExpression(gpa: *Allocator, ais: *Ais, tree: ast.Tree, node: ast.Node.I
439 const suffix = datas[node];439 const suffix = datas[node];
440 const lbracket = tree.firstToken(suffix.rhs) - 1;440 const lbracket = tree.firstToken(suffix.rhs) - 1;
441 const rbracket = tree.lastToken(suffix.rhs) + 1;441 const rbracket = tree.lastToken(suffix.rhs) + 1;
442 const one_line = tree.tokensOnSameLine(lbracket, rbracket);
443 const inner_space = if (one_line) Space.none else Space.newline;
442 try renderExpression(gpa, ais, tree, suffix.lhs, .none);444 try renderExpression(gpa, ais, tree, suffix.lhs, .none);
443 try renderToken(ais, tree, lbracket, .none); // [445 ais.pushIndentNextLine();
444 try renderExpression(gpa, ais, tree, suffix.rhs, .none);446 try renderToken(ais, tree, lbracket, inner_space); // [
447 try renderExpression(gpa, ais, tree, suffix.rhs, inner_space);
448 ais.popIndent();
445 return renderToken(ais, tree, rbracket, space); // ]449 return renderToken(ais, tree, rbracket, space); // ]
446 },450 },
447451
...@@ -679,7 +683,6 @@ fn renderExpression(gpa: *Allocator, ais: *Ais, tree: ast.Tree, node: ast.Node.I...@@ -679,7 +683,6 @@ fn renderExpression(gpa: *Allocator, ais: *Ais, tree: ast.Tree, node: ast.Node.I
679 }683 }
680}684}
681685
682// TODO: handle comments inside the brackets
683fn renderArrayType(686fn renderArrayType(
684 gpa: *Allocator,687 gpa: *Allocator,
685 ais: *Ais,688 ais: *Ais,
...@@ -687,13 +690,18 @@ fn renderArrayType(...@@ -687,13 +690,18 @@ fn renderArrayType(
687 array_type: ast.full.ArrayType,690 array_type: ast.full.ArrayType,
688 space: Space,691 space: Space,
689) Error!void {692) Error!void {
690 try renderToken(ais, tree, array_type.ast.lbracket, .none); // lbracket693 const rbracket = tree.firstToken(array_type.ast.elem_type) - 1;
691 try renderExpression(gpa, ais, tree, array_type.ast.elem_count, .none);694 const one_line = tree.tokensOnSameLine(array_type.ast.lbracket, rbracket);
695 const inner_space = if (one_line) Space.none else Space.newline;
696 ais.pushIndentNextLine();
697 try renderToken(ais, tree, array_type.ast.lbracket, inner_space); // lbracket
698 try renderExpression(gpa, ais, tree, array_type.ast.elem_count, inner_space);
692 if (array_type.ast.sentinel) |sentinel| {699 if (array_type.ast.sentinel) |sentinel| {
693 try renderToken(ais, tree, tree.firstToken(sentinel) - 1, .none); // colon700 try renderToken(ais, tree, tree.firstToken(sentinel) - 1, inner_space); // colon
694 try renderExpression(gpa, ais, tree, sentinel, .none);701 try renderExpression(gpa, ais, tree, sentinel, inner_space);
695 }702 }
696 try renderToken(ais, tree, tree.firstToken(array_type.ast.elem_type) - 1, .none); // rbracket703 ais.popIndent();
704 try renderToken(ais, tree, rbracket, .none); // rbracket
697 return renderExpression(gpa, ais, tree, array_type.ast.elem_type, space);705 return renderExpression(gpa, ais, tree, array_type.ast.elem_type, space);
698}706}
699707
...@@ -1577,7 +1585,9 @@ fn renderStructInit(...@@ -1577,7 +1585,9 @@ fn renderStructInit(
1577 try renderExpression(gpa, ais, tree, struct_init.ast.type_expr, .none); // T1585 try renderExpression(gpa, ais, tree, struct_init.ast.type_expr, .none); // T
1578 }1586 }
1579 if (struct_init.ast.fields.len == 0) {1587 if (struct_init.ast.fields.len == 0) {
1588 ais.pushIndentNextLine();
1580 try renderToken(ais, tree, struct_init.ast.lbrace, .none); // lbrace1589 try renderToken(ais, tree, struct_init.ast.lbrace, .none); // lbrace
1590 ais.popIndent();
1581 return renderToken(ais, tree, struct_init.ast.lbrace + 1, space); // rbrace1591 return renderToken(ais, tree, struct_init.ast.lbrace + 1, space); // rbrace
1582 }1592 }
15831593