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 {...@@ -26,6 +26,8 @@ pub const Fixups = struct {
26 omit_nodes: std.AutoHashMapUnmanaged(Ast.Node.Index, void) = .{},26 omit_nodes: std.AutoHashMapUnmanaged(Ast.Node.Index, void) = .{},
27 /// These expressions will be replaced with the string value.27 /// These expressions will be replaced with the string value.
28 replace_nodes_with_string: std.AutoHashMapUnmanaged(Ast.Node.Index, []const u8) = .{},28 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) = .{},
29 /// These nodes will be replaced with a different node.31 /// These nodes will be replaced with a different node.
30 replace_nodes_with_node: std.AutoHashMapUnmanaged(Ast.Node.Index, Ast.Node.Index) = .{},32 replace_nodes_with_node: std.AutoHashMapUnmanaged(Ast.Node.Index, Ast.Node.Index) = .{},
31 /// Change all identifier names matching the key to be value instead.33 /// Change all identifier names matching the key to be value instead.
...@@ -40,6 +42,7 @@ pub const Fixups = struct {...@@ -40,6 +42,7 @@ pub const Fixups = struct {
40 f.gut_functions.count() +42 f.gut_functions.count() +
41 f.omit_nodes.count() +43 f.omit_nodes.count() +
42 f.replace_nodes_with_string.count() +44 f.replace_nodes_with_string.count() +
45 f.append_string_after_node.count() +
43 f.replace_nodes_with_node.count() +46 f.replace_nodes_with_node.count() +
44 f.rename_identifiers.count() +47 f.rename_identifiers.count() +
45 @intFromBool(f.rebase_imported_paths != null);48 @intFromBool(f.rebase_imported_paths != null);
...@@ -50,6 +53,7 @@ pub const Fixups = struct {...@@ -50,6 +53,7 @@ pub const Fixups = struct {
50 f.gut_functions.clearRetainingCapacity();53 f.gut_functions.clearRetainingCapacity();
51 f.omit_nodes.clearRetainingCapacity();54 f.omit_nodes.clearRetainingCapacity();
52 f.replace_nodes_with_string.clearRetainingCapacity();55 f.replace_nodes_with_string.clearRetainingCapacity();
56 f.append_string_after_node.clearRetainingCapacity();
53 f.replace_nodes_with_node.clearRetainingCapacity();57 f.replace_nodes_with_node.clearRetainingCapacity();
54 f.rename_identifiers.clearRetainingCapacity();58 f.rename_identifiers.clearRetainingCapacity();
5559
...@@ -61,6 +65,7 @@ pub const Fixups = struct {...@@ -61,6 +65,7 @@ pub const Fixups = struct {
61 f.gut_functions.deinit(gpa);65 f.gut_functions.deinit(gpa);
62 f.omit_nodes.deinit(gpa);66 f.omit_nodes.deinit(gpa);
63 f.replace_nodes_with_string.deinit(gpa);67 f.replace_nodes_with_string.deinit(gpa);
68 f.append_string_after_node.deinit(gpa);
64 f.replace_nodes_with_node.deinit(gpa);69 f.replace_nodes_with_node.deinit(gpa);
65 f.rename_identifiers.deinit(gpa);70 f.rename_identifiers.deinit(gpa);
66 f.* = undefined;71 f.* = undefined;
...@@ -912,6 +917,16 @@ fn renderExpression(r: *Render, node: Ast.Node.Index, space: Space) Error!void {...@@ -912,6 +917,16 @@ fn renderExpression(r: *Render, node: Ast.Node.Index, space: Space) Error!void {
912 }917 }
913}918}
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
915fn renderArrayType(930fn renderArrayType(
916 r: *Render,931 r: *Render,
917 array_type: Ast.full.ArrayType,932 array_type: Ast.full.ArrayType,
...@@ -2093,10 +2108,11 @@ fn renderStructInit(...@@ -2093,10 +2108,11 @@ fn renderStructInit(
2093 // Don't output a space after the = if expression is a multiline string,2108 // Don't output a space after the = if expression is a multiline string,
2094 // since then it will start on the next line.2109 // since then it will start on the next line.
2095 const nodes = tree.nodes.items(.tag);2110 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];
2097 var space_after_equal: Space = if (expr == .multiline_string_literal) .none else .space;2113 var space_after_equal: Space = if (expr == .multiline_string_literal) .none else .space;
2098 try renderToken(r, struct_init.ast.lbrace + 3, space_after_equal); // =2114 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
2101 for (struct_init.ast.fields[1..]) |field_init| {2117 for (struct_init.ast.fields[1..]) |field_init| {
2102 const init_token = tree.firstToken(field_init);2118 const init_token = tree.firstToken(field_init);
...@@ -2105,7 +2121,7 @@ fn renderStructInit(...@@ -2105,7 +2121,7 @@ fn renderStructInit(
2105 try renderIdentifier(r, init_token - 2, .space, .eagerly_unquote); // name2121 try renderIdentifier(r, init_token - 2, .space, .eagerly_unquote); // name
2106 space_after_equal = if (nodes[field_init] == .multiline_string_literal) .none else .space;2122 space_after_equal = if (nodes[field_init] == .multiline_string_literal) .none else .space;
2107 try renderToken(r, init_token - 1, space_after_equal); // =2123 try renderToken(r, init_token - 1, space_after_equal); // =
2108 try renderExpression(r, field_init, .comma);2124 try renderExpressionFixup(r, field_init, .comma);
2109 }2125 }
21102126
2111 ais.popIndent();2127 ais.popIndent();
...@@ -2118,7 +2134,7 @@ fn renderStructInit(...@@ -2118,7 +2134,7 @@ fn renderStructInit(
2118 try renderToken(r, init_token - 3, .none); // .2134 try renderToken(r, init_token - 3, .none); // .
2119 try renderIdentifier(r, init_token - 2, .space, .eagerly_unquote); // name2135 try renderIdentifier(r, init_token - 2, .space, .eagerly_unquote); // name
2120 try renderToken(r, init_token - 1, .space); // =2136 try renderToken(r, init_token - 1, .space); // =
2121 try renderExpression(r, field_init, .comma_space);2137 try renderExpressionFixup(r, field_init, .comma_space);
2122 }2138 }
2123 }2139 }
21242140