| ... | @@ -1206,7 +1206,7 @@ fn renderContainerField( | ... | @@ -1206,7 +1206,7 @@ fn renderContainerField( |
| 1206 | ais.pushIndent(); | 1206 | ais.pushIndent(); |
| 1207 | try renderExpression(gpa, ais, tree, field.ast.value_expr, .none); // value | 1207 | try renderExpression(gpa, ais, tree, field.ast.value_expr, .none); // value |
| 1208 | ais.popIndent(); | 1208 | ais.popIndent(); |
| 1209 | try renderToken(ais, tree, maybe_comma, space); | 1209 | try renderToken(ais, tree, maybe_comma, .newline); |
| 1210 | } else { | 1210 | } else { |
| 1211 | ais.pushIndent(); | 1211 | ais.pushIndent(); |
| 1212 | try renderExpression(gpa, ais, tree, field.ast.value_expr, space); // value | 1212 | try renderExpression(gpa, ais, tree, field.ast.value_expr, space); // value |
| ... | @@ -1894,7 +1894,11 @@ fn renderContainerDecl( | ... | @@ -1894,7 +1894,11 @@ fn renderContainerDecl( |
| 1894 | | 1894 | |
| 1895 | const src_has_trailing_comma = token_tags[rbrace - 1] == .comma; | 1895 | const src_has_trailing_comma = token_tags[rbrace - 1] == .comma; |
| 1896 | if (!src_has_trailing_comma) one_line: { | 1896 | if (!src_has_trailing_comma) one_line: { |
| 1897 | // We can only print all the members in-line if all the members are fields. | 1897 | // We can only print all the members in-line if there are no comments or multiline strings, |
| | 1898 | // and all the members are fields. |
| | 1899 | if (hasComment(tree, lbrace, rbrace) or hasMultilineString(tree, lbrace, rbrace)) { |
| | 1900 | break :one_line; |
| | 1901 | } |
| 1898 | for (container_decl.ast.members) |member| { | 1902 | for (container_decl.ast.members) |member| { |
| 1899 | if (!node_tags[member].isContainerField()) break :one_line; | 1903 | if (!node_tags[member].isContainerField()) break :one_line; |
| 1900 | } | 1904 | } |
| ... | @@ -1912,7 +1916,18 @@ fn renderContainerDecl( | ... | @@ -1912,7 +1916,18 @@ fn renderContainerDecl( |
| 1912 | if (token_tags[lbrace + 1] == .container_doc_comment) { | 1916 | if (token_tags[lbrace + 1] == .container_doc_comment) { |
| 1913 | try renderContainerDocComments(ais, tree, lbrace + 1); | 1917 | try renderContainerDocComments(ais, tree, lbrace + 1); |
| 1914 | } | 1918 | } |
| 1915 | try renderMembers(gpa, ais, tree, container_decl.ast.members); | 1919 | for (container_decl.ast.members) |member, i| { |
| | 1920 | if (i != 0) try renderExtraNewline(ais, tree, member); |
| | 1921 | switch (tree.nodes.items(.tag)[member]) { |
| | 1922 | // For container fields, ensure a trailing comma is added if necessary. |
| | 1923 | .container_field_init, |
| | 1924 | .container_field_align, |
| | 1925 | .container_field, |
| | 1926 | => try renderMember(gpa, ais, tree, member, .comma), |
| | 1927 | |
| | 1928 | else => try renderMember(gpa, ais, tree, member, .newline), |
| | 1929 | } |
| | 1930 | } |
| 1916 | ais.popIndent(); | 1931 | ais.popIndent(); |
| 1917 | | 1932 | |
| 1918 | return renderToken(ais, tree, rbrace, space); // rbrace | 1933 | return renderToken(ais, tree, rbrace, space); // rbrace |
| ... | @@ -2200,10 +2215,11 @@ fn renderExpressionIndented(gpa: *Allocator, ais: *Ais, tree: Ast, node: Ast.Nod | ... | @@ -2200,10 +2215,11 @@ fn renderExpressionIndented(gpa: *Allocator, ais: *Ais, tree: Ast, node: Ast.Nod |
| 2200 | } | 2215 | } |
| 2201 | | 2216 | |
| 2202 | /// Render an expression, and the comma that follows it, if it is present in the source. | 2217 | /// Render an expression, and the comma that follows it, if it is present in the source. |
| | 2218 | /// If a comma is present, and `space` is `Space.comma`, render only a single comma. |
| 2203 | fn renderExpressionComma(gpa: *Allocator, ais: *Ais, tree: Ast, node: Ast.Node.Index, space: Space) Error!void { | 2219 | fn renderExpressionComma(gpa: *Allocator, ais: *Ais, tree: Ast, node: Ast.Node.Index, space: Space) Error!void { |
| 2204 | const token_tags = tree.tokens.items(.tag); | 2220 | const token_tags = tree.tokens.items(.tag); |
| 2205 | const maybe_comma = tree.lastToken(node) + 1; | 2221 | const maybe_comma = tree.lastToken(node) + 1; |
| 2206 | if (token_tags[maybe_comma] == .comma) { | 2222 | if (token_tags[maybe_comma] == .comma and space != .comma) { |
| 2207 | try renderExpression(gpa, ais, tree, node, .none); | 2223 | try renderExpression(gpa, ais, tree, node, .none); |
| 2208 | return renderToken(ais, tree, maybe_comma, space); | 2224 | return renderToken(ais, tree, maybe_comma, space); |
| 2209 | } else { | 2225 | } else { |
| ... | @@ -2211,10 +2227,12 @@ fn renderExpressionComma(gpa: *Allocator, ais: *Ais, tree: Ast, node: Ast.Node.I | ... | @@ -2211,10 +2227,12 @@ fn renderExpressionComma(gpa: *Allocator, ais: *Ais, tree: Ast, node: Ast.Node.I |
| 2211 | } | 2227 | } |
| 2212 | } | 2228 | } |
| 2213 | | 2229 | |
| | 2230 | /// Render a token, and the comma that follows it, if it is present in the source. |
| | 2231 | /// If a comma is present, and `space` is `Space.comma`, render only a single comma. |
| 2214 | fn renderTokenComma(ais: *Ais, tree: Ast, token: Ast.TokenIndex, space: Space) Error!void { | 2232 | fn renderTokenComma(ais: *Ais, tree: Ast, token: Ast.TokenIndex, space: Space) Error!void { |
| 2215 | const token_tags = tree.tokens.items(.tag); | 2233 | const token_tags = tree.tokens.items(.tag); |
| 2216 | const maybe_comma = token + 1; | 2234 | const maybe_comma = token + 1; |
| 2217 | if (token_tags[maybe_comma] == .comma) { | 2235 | if (token_tags[maybe_comma] == .comma and space != .comma) { |
| 2218 | try renderToken(ais, tree, token, .none); | 2236 | try renderToken(ais, tree, token, .none); |
| 2219 | return renderToken(ais, tree, maybe_comma, space); | 2237 | return renderToken(ais, tree, maybe_comma, space); |
| 2220 | } else { | 2238 | } else { |