authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-11-26 17:03:12-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-11-26 19:41:00-07:00
log70d51be3a25bcd889bba3c1f67009801b4c32c70
tree9e1fb4f4f453e278dae553741e17e227d64bc88a
parent19af8aac82510d9adc35fa11db9c57676d4abd23

std.zig.render: add ability to append strings after nodes


1 files changed, 20 insertions(+), 4 deletions(-)

lib/std/zig/render.zig+20-4
......@@ -26,6 +26,8 @@ pub const Fixups = struct {
2626 omit_nodes: std.AutoHashMapUnmanaged(Ast.Node.Index, void) = .{},
2727 /// These expressions will be replaced with the string value.
2828 replace_nodes_with_string: std.AutoHashMapUnmanaged(Ast.Node.Index, []const u8) = .{},
29 /// The string value will be inserted directly after the node.
30 append_string_after_node: std.AutoHashMapUnmanaged(Ast.Node.Index, []const u8) = .{},
2931 /// These nodes will be replaced with a different node.
3032 replace_nodes_with_node: std.AutoHashMapUnmanaged(Ast.Node.Index, Ast.Node.Index) = .{},
3133 /// Change all identifier names matching the key to be value instead.
......@@ -40,6 +42,7 @@ pub const Fixups = struct {
4042 f.gut_functions.count() +
4143 f.omit_nodes.count() +
4244 f.replace_nodes_with_string.count() +
45 f.append_string_after_node.count() +
4346 f.replace_nodes_with_node.count() +
4447 f.rename_identifiers.count() +
4548 @intFromBool(f.rebase_imported_paths != null);
......@@ -50,6 +53,7 @@ pub const Fixups = struct {
5053 f.gut_functions.clearRetainingCapacity();
5154 f.omit_nodes.clearRetainingCapacity();
5255 f.replace_nodes_with_string.clearRetainingCapacity();
56 f.append_string_after_node.clearRetainingCapacity();
5357 f.replace_nodes_with_node.clearRetainingCapacity();
5458 f.rename_identifiers.clearRetainingCapacity();
5559
......@@ -61,6 +65,7 @@ pub const Fixups = struct {
6165 f.gut_functions.deinit(gpa);
6266 f.omit_nodes.deinit(gpa);
6367 f.replace_nodes_with_string.deinit(gpa);
68 f.append_string_after_node.deinit(gpa);
6469 f.replace_nodes_with_node.deinit(gpa);
6570 f.rename_identifiers.deinit(gpa);
6671 f.* = undefined;
......@@ -912,6 +917,16 @@ fn renderExpression(r: *Render, node: Ast.Node.Index, space: Space) Error!void {
912917 }
913918}
914919
920/// Same as `renderExpression`, but afterwards looks for any
921/// append_string_after_node fixups to apply
922fn renderExpressionFixup(r: *Render, node: Ast.Node.Index, space: Space) Error!void {
923 const ais = r.ais;
924 try renderExpression(r, node, space);
925 if (r.fixups.append_string_after_node.get(node)) |bytes| {
926 try ais.writer().writeAll(bytes);
927 }
928}
929
915930fn renderArrayType(
916931 r: *Render,
917932 array_type: Ast.full.ArrayType,
......@@ -2093,10 +2108,11 @@ fn renderStructInit(
20932108 // Don't output a space after the = if expression is a multiline string,
20942109 // since then it will start on the next line.
20952110 const nodes = tree.nodes.items(.tag);
2096 const expr = nodes[struct_init.ast.fields[0]];
2111 const field_node = struct_init.ast.fields[0];
2112 const expr = nodes[field_node];
20972113 var space_after_equal: Space = if (expr == .multiline_string_literal) .none else .space;
20982114 try renderToken(r, struct_init.ast.lbrace + 3, space_after_equal); // =
2099 try renderExpression(r, struct_init.ast.fields[0], .comma);
2115 try renderExpressionFixup(r, field_node, .comma);
21002116
21012117 for (struct_init.ast.fields[1..]) |field_init| {
21022118 const init_token = tree.firstToken(field_init);
......@@ -2105,7 +2121,7 @@ fn renderStructInit(
21052121 try renderIdentifier(r, init_token - 2, .space, .eagerly_unquote); // name
21062122 space_after_equal = if (nodes[field_init] == .multiline_string_literal) .none else .space;
21072123 try renderToken(r, init_token - 1, space_after_equal); // =
2108 try renderExpression(r, field_init, .comma);
2124 try renderExpressionFixup(r, field_init, .comma);
21092125 }
21102126
21112127 ais.popIndent();
......@@ -2118,7 +2134,7 @@ fn renderStructInit(
21182134 try renderToken(r, init_token - 3, .none); // .
21192135 try renderIdentifier(r, init_token - 2, .space, .eagerly_unquote); // name
21202136 try renderToken(r, init_token - 1, .space); // =
2121 try renderExpression(r, field_init, .comma_space);
2137 try renderExpressionFixup(r, field_init, .comma_space);
21222138 }
21232139 }
21242140