authorgravatar for Lewis@scphillips.comLewis Gaul <Lewis@scphillips.com> 2021-03-16 08:26:28+00:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-03-16 09:26:28+01:00
log6787f163eb6db2b8b89c2ea6cb51d63606487e12
treea59105f31e28cf58d60b9a21243f69e9d56596d6
parent5ecf8bddaeebf26ac6cd174f5f79e02ca243e501
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

zig fmt: don't add trailing whitespace on switch case


2 files changed, 58 insertions(+), 3 deletions(-)

lib/std/zig/parser_test.zig+48
......@@ -3063,6 +3063,54 @@ test "zig fmt: switch" {
30633063 \\}
30643064 \\
30653065 );
3066
3067 try testTransform(
3068 \\test {
3069 \\ switch (x) {
3070 \\ foo =>
3071 \\ "bar",
3072 \\ }
3073 \\}
3074 \\
3075 ,
3076 \\test {
3077 \\ switch (x) {
3078 \\ foo => "bar",
3079 \\ }
3080 \\}
3081 \\
3082 );
3083}
3084
3085test "zig fmt: switch multiline string" {
3086 try testCanonical(
3087 \\test "switch multiline string" {
3088 \\ const x: u32 = 0;
3089 \\ const str = switch (x) {
3090 \\ 1 => "one",
3091 \\ 2 =>
3092 \\ \\ Comma after the multiline string
3093 \\ \\ is needed
3094 \\ ,
3095 \\ 3 => "three",
3096 \\ else => "else",
3097 \\ };
3098 \\
3099 \\ const Union = union(enum) {
3100 \\ Int: i64,
3101 \\ Float: f64,
3102 \\ };
3103 \\
3104 \\ const str = switch (u) {
3105 \\ Union.Int => |int|
3106 \\ \\ Comma after the multiline string
3107 \\ \\ is needed
3108 \\ ,
3109 \\ Union.Float => |*float| unreachable,
3110 \\ };
3111 \\}
3112 \\
3113 );
30663114}
30673115
30683116test "zig fmt: while" {
lib/std/zig/render.zig+10-3
......@@ -1444,6 +1444,7 @@ fn renderSwitchCase(
14441444 switch_case: ast.full.SwitchCase,
14451445 space: Space,
14461446) Error!void {
1447 const node_tags = tree.nodes.items(.tag);
14471448 const token_tags = tree.tokens.items(.tag);
14481449 const trailing_comma = token_tags[switch_case.ast.arrow_token - 1] == .comma;
14491450
......@@ -1466,17 +1467,23 @@ fn renderSwitchCase(
14661467 }
14671468
14681469 // Render the arrow and everything after it
1469 try renderToken(ais, tree, switch_case.ast.arrow_token, .space);
1470 const pre_target_space = if (node_tags[switch_case.ast.target_expr] == .multiline_string_literal)
1471 // Newline gets inserted when rendering the target expr.
1472 Space.none
1473 else
1474 Space.space;
1475 const after_arrow_space: Space = if (switch_case.payload_token == null) pre_target_space else .space;
1476 try renderToken(ais, tree, switch_case.ast.arrow_token, after_arrow_space);
14701477
14711478 if (switch_case.payload_token) |payload_token| {
14721479 try renderToken(ais, tree, payload_token - 1, .none); // pipe
14731480 if (token_tags[payload_token] == .asterisk) {
14741481 try renderToken(ais, tree, payload_token, .none); // asterisk
14751482 try renderToken(ais, tree, payload_token + 1, .none); // identifier
1476 try renderToken(ais, tree, payload_token + 2, .space); // pipe
1483 try renderToken(ais, tree, payload_token + 2, pre_target_space); // pipe
14771484 } else {
14781485 try renderToken(ais, tree, payload_token, .none); // identifier
1479 try renderToken(ais, tree, payload_token + 1, .space); // pipe
1486 try renderToken(ais, tree, payload_token + 1, pre_target_space); // pipe
14801487 }
14811488 }
14821489