authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-12-09 12:58:04+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-12-11 00:39:52-07:00
logc9bc8b9d0cf1ca7d21ddb60d08a4cdd0730a253d
tree66f2177fb1a886b135ace7ff0dfbf15976c021bb
parentfd4c98cbb7af337895f003d83d6cd2fb3447d6c2

zig fmt: improve var decl initializer formatting


2 files changed, 72 insertions(+), 20 deletions(-)

lib/std/zig/parser_test.zig+47-1
...@@ -274,6 +274,51 @@ test "recovery: missing block after for/while loops" {...@@ -274,6 +274,51 @@ test "recovery: missing block after for/while loops" {
274 });274 });
275}275}
276276
277test "zig fmt: respect line breaks after var declarations" {
278 try testCanonical(
279 \\const crc =
280 \\ lookup_tables[0][p[7]] ^
281 \\ lookup_tables[1][p[6]] ^
282 \\ lookup_tables[2][p[5]] ^
283 \\ lookup_tables[3][p[4]] ^
284 \\ lookup_tables[4][@truncate(u8, self.crc >> 24)] ^
285 \\ lookup_tables[5][@truncate(u8, self.crc >> 16)] ^
286 \\ lookup_tables[6][@truncate(u8, self.crc >> 8)] ^
287 \\ lookup_tables[7][@truncate(u8, self.crc >> 0)];
288 \\
289 );
290}
291
292test "zig fmt: multiline string mixed with comments" {
293 try testCanonical(
294 \\const s1 =
295 \\ //\\one
296 \\ \\two)
297 \\ \\three
298 \\;
299 \\const s2 =
300 \\ \\one
301 \\ \\two)
302 \\ //\\three
303 \\;
304 \\const s3 =
305 \\ \\one
306 \\ //\\two)
307 \\ \\three
308 \\;
309 \\const s4 =
310 \\ \\one
311 \\ //\\two
312 \\ \\three
313 \\ //\\four
314 \\ \\five
315 \\;
316 \\const a =
317 \\ 1;
318 \\
319 );
320}
321
277test "zig fmt: empty file" {322test "zig fmt: empty file" {
278 try testCanonical(323 try testCanonical(
279 \\324 \\
...@@ -3224,7 +3269,8 @@ test "zig fmt: integer literals with underscore separators" {...@@ -3224,7 +3269,8 @@ test "zig fmt: integer literals with underscore separators" {
3224 \\ 1_234_5673269 \\ 1_234_567
3225 \\ +(0b0_1-0o7_0+0xff_FF ) + 0_0;3270 \\ +(0b0_1-0o7_0+0xff_FF ) + 0_0;
3226 ,3271 ,
3227 \\const x = 1_234_567 + (0b0_1 - 0o7_0 + 0xff_FF) + 0_0;3272 \\const x =
3273 \\ 1_234_567 + (0b0_1 - 0o7_0 + 0xff_FF) + 0_0;
3228 \\3274 \\
3229 );3275 );
3230}3276}
lib/std/zig/render.zig+25-19
...@@ -2209,10 +2209,10 @@ fn renderAsmOutput(...@@ -2209,10 +2209,10 @@ fn renderAsmOutput(
2209 try ais.writer().writeAll(" (");2209 try ais.writer().writeAll(" (");
22102210
2211 switch (asm_output.kind) {2211 switch (asm_output.kind) {
2212 ast.Node.Asm.Output.Kind.Variable => |variable_name| {2212 .Variable => |variable_name| {
2213 try renderExpression(allocator, ais, tree, &variable_name.base, Space.None);2213 try renderExpression(allocator, ais, tree, &variable_name.base, Space.None);
2214 },2214 },
2215 ast.Node.Asm.Output.Kind.Return => |return_type| {2215 .Return => |return_type| {
2216 try ais.writer().writeAll("-> ");2216 try ais.writer().writeAll("-> ");
2217 try renderExpression(allocator, ais, tree, return_type, Space.None);2217 try renderExpression(allocator, ais, tree, return_type, Space.None);
2218 },2218 },
...@@ -2304,8 +2304,17 @@ fn renderVarDecl(...@@ -2304,8 +2304,17 @@ fn renderVarDecl(
2304 }2304 }
23052305
2306 if (var_decl.getInitNode()) |init_node| {2306 if (var_decl.getInitNode()) |init_node| {
2307 const s = if (init_node.tag == .MultilineStringLiteral) Space.None else Space.Space;2307 const eq_token = var_decl.getEqToken().?;
2308 try renderToken(tree, ais, var_decl.getEqToken().?, s); // =2308 const eq_space = blk: {
2309 const loc = tree.tokenLocation(tree.token_locs[eq_token].end, tree.nextToken(eq_token));
2310 break :blk if (loc.line == 0) Space.Space else Space.Newline;
2311 };
2312
2313 {
2314 ais.pushIndent();
2315 defer ais.popIndent();
2316 try renderToken(tree, ais, eq_token, eq_space); // =
2317 }
2309 ais.pushIndentOneShot();2318 ais.pushIndentOneShot();
2310 try renderExpression(allocator, ais, tree, init_node, Space.None);2319 try renderExpression(allocator, ais, tree, init_node, Space.None);
2311 }2320 }
...@@ -2470,20 +2479,20 @@ fn renderTokenOffset(...@@ -2470,20 +2479,20 @@ fn renderTokenOffset(
24702479
2471 var loc = tree.tokenLocationLoc(token_loc.end, next_token_loc);2480 var loc = tree.tokenLocationLoc(token_loc.end, next_token_loc);
2472 if (loc.line == 0) {2481 if (loc.line == 0) {
2473 try ais.writer().print(" {}", .{mem.trimRight(u8, tree.tokenSliceLoc(next_token_loc), " ")});2482 if (tree.token_ids[token_index] != .MultilineStringLiteralLine) {
2483 try ais.writer().writeByte(' ');
2484 }
2485 try ais.writer().writeAll(mem.trimRight(u8, tree.tokenSliceLoc(next_token_loc), " "));
2474 offset = 2;2486 offset = 2;
2475 token_loc = next_token_loc;2487 token_loc = next_token_loc;
2476 next_token_loc = tree.token_locs[token_index + offset];2488 next_token_loc = tree.token_locs[token_index + offset];
2477 next_token_id = tree.token_ids[token_index + offset];2489 next_token_id = tree.token_ids[token_index + offset];
2478 if (next_token_id != .LineComment) {2490 if (next_token_id != .LineComment) {
2479 switch (space) {2491 switch (space) {
2480 Space.None, Space.Space => {2492 .None, .Space, .SpaceOrOutdent => {
2481 try ais.insertNewline();
2482 },
2483 Space.SpaceOrOutdent => {
2484 try ais.insertNewline();2493 try ais.insertNewline();
2485 },2494 },
2486 Space.Newline => {2495 .Newline => {
2487 if (next_token_id == .MultilineStringLiteralLine) {2496 if (next_token_id == .MultilineStringLiteralLine) {
2488 return;2497 return;
2489 } else {2498 } else {
...@@ -2491,8 +2500,8 @@ fn renderTokenOffset(...@@ -2491,8 +2500,8 @@ fn renderTokenOffset(
2491 return;2500 return;
2492 }2501 }
2493 },2502 },
2494 Space.NoNewline => {},2503 .NoNewline => {},
2495 Space.NoComment, Space.Comma, Space.BlockStart => unreachable,2504 .NoComment, .Comma, .BlockStart => unreachable,
2496 }2505 }
2497 return;2506 return;
2498 }2507 }
...@@ -2513,7 +2522,7 @@ fn renderTokenOffset(...@@ -2513,7 +2522,7 @@ fn renderTokenOffset(
2513 next_token_id = tree.token_ids[token_index + offset];2522 next_token_id = tree.token_ids[token_index + offset];
2514 if (next_token_id != .LineComment) {2523 if (next_token_id != .LineComment) {
2515 switch (space) {2524 switch (space) {
2516 Space.Newline => {2525 .Newline => {
2517 if (next_token_id == .MultilineStringLiteralLine) {2526 if (next_token_id == .MultilineStringLiteralLine) {
2518 return;2527 return;
2519 } else {2528 } else {
...@@ -2521,14 +2530,11 @@ fn renderTokenOffset(...@@ -2521,14 +2530,11 @@ fn renderTokenOffset(
2521 return;2530 return;
2522 }2531 }
2523 },2532 },
2524 Space.None, Space.Space => {2533 .None, .Space, .SpaceOrOutdent => {
2525 try ais.insertNewline();
2526 },
2527 Space.SpaceOrOutdent => {
2528 try ais.insertNewline();2534 try ais.insertNewline();
2529 },2535 },
2530 Space.NoNewline => {},2536 .NoNewline => {},
2531 Space.NoComment, Space.Comma, Space.BlockStart => unreachable,2537 .NoComment, .Comma, .BlockStart => unreachable,
2532 }2538 }
2533 return;2539 return;
2534 }2540 }