mirror of
https://github.com/ziglang/zig.git
synced 2026-01-05 13:03:25 +00:00
zig fmt: improve var decl initializer formatting
This commit is contained in:
parent
3599fb9bfc
commit
be71994fb1
@ -274,6 +274,51 @@ test "recovery: missing block after for/while loops" {
|
||||
});
|
||||
}
|
||||
|
||||
test "zig fmt: respect line breaks after var declarations" {
|
||||
try testCanonical(
|
||||
\\const crc =
|
||||
\\ lookup_tables[0][p[7]] ^
|
||||
\\ lookup_tables[1][p[6]] ^
|
||||
\\ lookup_tables[2][p[5]] ^
|
||||
\\ lookup_tables[3][p[4]] ^
|
||||
\\ lookup_tables[4][@truncate(u8, self.crc >> 24)] ^
|
||||
\\ lookup_tables[5][@truncate(u8, self.crc >> 16)] ^
|
||||
\\ lookup_tables[6][@truncate(u8, self.crc >> 8)] ^
|
||||
\\ lookup_tables[7][@truncate(u8, self.crc >> 0)];
|
||||
\\
|
||||
);
|
||||
}
|
||||
|
||||
test "zig fmt: multiline string mixed with comments" {
|
||||
try testCanonical(
|
||||
\\const s1 =
|
||||
\\ //\\one
|
||||
\\ \\two)
|
||||
\\ \\three
|
||||
\\;
|
||||
\\const s2 =
|
||||
\\ \\one
|
||||
\\ \\two)
|
||||
\\ //\\three
|
||||
\\;
|
||||
\\const s3 =
|
||||
\\ \\one
|
||||
\\ //\\two)
|
||||
\\ \\three
|
||||
\\;
|
||||
\\const s4 =
|
||||
\\ \\one
|
||||
\\ //\\two
|
||||
\\ \\three
|
||||
\\ //\\four
|
||||
\\ \\five
|
||||
\\;
|
||||
\\const a =
|
||||
\\ 1;
|
||||
\\
|
||||
);
|
||||
}
|
||||
|
||||
test "zig fmt: empty file" {
|
||||
try testCanonical(
|
||||
\\
|
||||
@ -3224,7 +3269,8 @@ test "zig fmt: integer literals with underscore separators" {
|
||||
\\ 1_234_567
|
||||
\\ +(0b0_1-0o7_0+0xff_FF ) + 0_0;
|
||||
,
|
||||
\\const x = 1_234_567 + (0b0_1 - 0o7_0 + 0xff_FF) + 0_0;
|
||||
\\const x =
|
||||
\\ 1_234_567 + (0b0_1 - 0o7_0 + 0xff_FF) + 0_0;
|
||||
\\
|
||||
);
|
||||
}
|
||||
|
||||
@ -2209,10 +2209,10 @@ fn renderAsmOutput(
|
||||
try ais.writer().writeAll(" (");
|
||||
|
||||
switch (asm_output.kind) {
|
||||
ast.Node.Asm.Output.Kind.Variable => |variable_name| {
|
||||
.Variable => |variable_name| {
|
||||
try renderExpression(allocator, ais, tree, &variable_name.base, Space.None);
|
||||
},
|
||||
ast.Node.Asm.Output.Kind.Return => |return_type| {
|
||||
.Return => |return_type| {
|
||||
try ais.writer().writeAll("-> ");
|
||||
try renderExpression(allocator, ais, tree, return_type, Space.None);
|
||||
},
|
||||
@ -2304,8 +2304,17 @@ fn renderVarDecl(
|
||||
}
|
||||
|
||||
if (var_decl.getInitNode()) |init_node| {
|
||||
const s = if (init_node.tag == .MultilineStringLiteral) Space.None else Space.Space;
|
||||
try renderToken(tree, ais, var_decl.getEqToken().?, s); // =
|
||||
const eq_token = var_decl.getEqToken().?;
|
||||
const eq_space = blk: {
|
||||
const loc = tree.tokenLocation(tree.token_locs[eq_token].end, tree.nextToken(eq_token));
|
||||
break :blk if (loc.line == 0) Space.Space else Space.Newline;
|
||||
};
|
||||
|
||||
{
|
||||
ais.pushIndent();
|
||||
defer ais.popIndent();
|
||||
try renderToken(tree, ais, eq_token, eq_space); // =
|
||||
}
|
||||
ais.pushIndentOneShot();
|
||||
try renderExpression(allocator, ais, tree, init_node, Space.None);
|
||||
}
|
||||
@ -2470,20 +2479,20 @@ fn renderTokenOffset(
|
||||
|
||||
var loc = tree.tokenLocationLoc(token_loc.end, next_token_loc);
|
||||
if (loc.line == 0) {
|
||||
try ais.writer().print(" {}", .{mem.trimRight(u8, tree.tokenSliceLoc(next_token_loc), " ")});
|
||||
if (tree.token_ids[token_index] != .MultilineStringLiteralLine) {
|
||||
try ais.writer().writeByte(' ');
|
||||
}
|
||||
try ais.writer().writeAll(mem.trimRight(u8, tree.tokenSliceLoc(next_token_loc), " "));
|
||||
offset = 2;
|
||||
token_loc = next_token_loc;
|
||||
next_token_loc = tree.token_locs[token_index + offset];
|
||||
next_token_id = tree.token_ids[token_index + offset];
|
||||
if (next_token_id != .LineComment) {
|
||||
switch (space) {
|
||||
Space.None, Space.Space => {
|
||||
.None, .Space, .SpaceOrOutdent => {
|
||||
try ais.insertNewline();
|
||||
},
|
||||
Space.SpaceOrOutdent => {
|
||||
try ais.insertNewline();
|
||||
},
|
||||
Space.Newline => {
|
||||
.Newline => {
|
||||
if (next_token_id == .MultilineStringLiteralLine) {
|
||||
return;
|
||||
} else {
|
||||
@ -2491,8 +2500,8 @@ fn renderTokenOffset(
|
||||
return;
|
||||
}
|
||||
},
|
||||
Space.NoNewline => {},
|
||||
Space.NoComment, Space.Comma, Space.BlockStart => unreachable,
|
||||
.NoNewline => {},
|
||||
.NoComment, .Comma, .BlockStart => unreachable,
|
||||
}
|
||||
return;
|
||||
}
|
||||
@ -2513,7 +2522,7 @@ fn renderTokenOffset(
|
||||
next_token_id = tree.token_ids[token_index + offset];
|
||||
if (next_token_id != .LineComment) {
|
||||
switch (space) {
|
||||
Space.Newline => {
|
||||
.Newline => {
|
||||
if (next_token_id == .MultilineStringLiteralLine) {
|
||||
return;
|
||||
} else {
|
||||
@ -2521,14 +2530,11 @@ fn renderTokenOffset(
|
||||
return;
|
||||
}
|
||||
},
|
||||
Space.None, Space.Space => {
|
||||
.None, .Space, .SpaceOrOutdent => {
|
||||
try ais.insertNewline();
|
||||
},
|
||||
Space.SpaceOrOutdent => {
|
||||
try ais.insertNewline();
|
||||
},
|
||||
Space.NoNewline => {},
|
||||
Space.NoComment, Space.Comma, Space.BlockStart => unreachable,
|
||||
.NoNewline => {},
|
||||
.NoComment, .Comma, .BlockStart => unreachable,
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user