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