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" {
274274 });
275275}
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
277322test "zig fmt: empty file" {
278323 try testCanonical(
279324 \\
......@@ -3224,7 +3269,8 @@ test "zig fmt: integer literals with underscore separators" {
32243269 \\ 1_234_567
32253270 \\ +(0b0_1-0o7_0+0xff_FF ) + 0_0;
32263271 ,
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;
32283274 \\
32293275 );
32303276}
lib/std/zig/render.zig+25-19
......@@ -2209,10 +2209,10 @@ fn renderAsmOutput(
22092209 try ais.writer().writeAll(" (");
22102210
22112211 switch (asm_output.kind) {
2212 ast.Node.Asm.Output.Kind.Variable => |variable_name| {
2212 .Variable => |variable_name| {
22132213 try renderExpression(allocator, ais, tree, &variable_name.base, Space.None);
22142214 },
2215 ast.Node.Asm.Output.Kind.Return => |return_type| {
2215 .Return => |return_type| {
22162216 try ais.writer().writeAll("-> ");
22172217 try renderExpression(allocator, ais, tree, return_type, Space.None);
22182218 },
......@@ -2304,8 +2304,17 @@ fn renderVarDecl(
23042304 }
23052305
23062306 if (var_decl.getInitNode()) |init_node| {
2307 const s = if (init_node.tag == .MultilineStringLiteral) Space.None else Space.Space;
2308 try renderToken(tree, ais, var_decl.getEqToken().?, s); // =
2307 const eq_token = var_decl.getEqToken().?;
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 }
23092318 ais.pushIndentOneShot();
23102319 try renderExpression(allocator, ais, tree, init_node, Space.None);
23112320 }
......@@ -2470,20 +2479,20 @@ fn renderTokenOffset(
24702479
24712480 var loc = tree.tokenLocationLoc(token_loc.end, next_token_loc);
24722481 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), " "));
24742486 offset = 2;
24752487 token_loc = next_token_loc;
24762488 next_token_loc = tree.token_locs[token_index + offset];
24772489 next_token_id = tree.token_ids[token_index + offset];
24782490 if (next_token_id != .LineComment) {
24792491 switch (space) {
2480 Space.None, Space.Space => {
2481 try ais.insertNewline();
2482 },
2483 Space.SpaceOrOutdent => {
2492 .None, .Space, .SpaceOrOutdent => {
24842493 try ais.insertNewline();
24852494 },
2486 Space.Newline => {
2495 .Newline => {
24872496 if (next_token_id == .MultilineStringLiteralLine) {
24882497 return;
24892498 } else {
......@@ -2491,8 +2500,8 @@ fn renderTokenOffset(
24912500 return;
24922501 }
24932502 },
2494 Space.NoNewline => {},
2495 Space.NoComment, Space.Comma, Space.BlockStart => unreachable,
2503 .NoNewline => {},
2504 .NoComment, .Comma, .BlockStart => unreachable,
24962505 }
24972506 return;
24982507 }
......@@ -2513,7 +2522,7 @@ fn renderTokenOffset(
25132522 next_token_id = tree.token_ids[token_index + offset];
25142523 if (next_token_id != .LineComment) {
25152524 switch (space) {
2516 Space.Newline => {
2525 .Newline => {
25172526 if (next_token_id == .MultilineStringLiteralLine) {
25182527 return;
25192528 } else {
......@@ -2521,14 +2530,11 @@ fn renderTokenOffset(
25212530 return;
25222531 }
25232532 },
2524 Space.None, Space.Space => {
2525 try ais.insertNewline();
2526 },
2527 Space.SpaceOrOutdent => {
2533 .None, .Space, .SpaceOrOutdent => {
25282534 try ais.insertNewline();
25292535 },
2530 Space.NoNewline => {},
2531 Space.NoComment, Space.Comma, Space.BlockStart => unreachable,
2536 .NoNewline => {},
2537 .NoComment, .Comma, .BlockStart => unreachable,
25322538 }
25332539 return;
25342540 }