|
1 | 1 | import * as AST from "./ast.js";
|
2 | 2 |
|
3 |
| -function indent(content: string): string { |
| 3 | +function indentExceptFirstLine(content: string): string { |
4 | 4 | return content.split("\n").join("\n ");
|
5 | 5 | }
|
6 | 6 |
|
@@ -136,19 +136,29 @@ function serializeTerm(term: AST.Term): string {
|
136 | 136 |
|
137 | 137 |
|
138 | 138 | function serializeAttribute(attribute: AST.Attribute): string {
|
139 |
| - const value = indent(serializePattern(attribute.value)); |
| 139 | + const value = indentExceptFirstLine(serializePattern(attribute.value)); |
140 | 140 | return `\n .${attribute.id.name} =${value}`;
|
141 | 141 | }
|
142 | 142 |
|
143 | 143 |
|
144 | 144 | function serializePattern(pattern: AST.Pattern): string {
|
145 | 145 | const content = pattern.elements.map(serializeElement).join("");
|
146 |
| - const startOnNewLine = |
| 146 | + const maybeStartOnNewLine = |
147 | 147 | pattern.elements.some(isSelectExpr) ||
|
148 | 148 | pattern.elements.some(includesNewLine);
|
149 | 149 |
|
150 |
| - if (startOnNewLine) { |
151 |
| - return `\n ${indent(content)}`; |
| 150 | + if (maybeStartOnNewLine) { |
| 151 | + const firstElement = pattern.elements[0]; |
| 152 | + if (firstElement instanceof AST.TextElement) { |
| 153 | + const firstChar = firstElement.value[0]; |
| 154 | + // Due to the indentation requirement these text characters may not appear |
| 155 | + // as the first character on a new line. |
| 156 | + if (firstChar === "[" || firstChar === "." || firstChar === "*") { |
| 157 | + return ` ${indentExceptFirstLine(content)}`; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + return `\n ${indentExceptFirstLine(content)}`; |
152 | 162 | }
|
153 | 163 |
|
154 | 164 | return ` ${content}`;
|
@@ -228,7 +238,7 @@ export function serializeExpression(expr: AST.Expression): string {
|
228 | 238 |
|
229 | 239 | function serializeVariant(variant: AST.Variant): string {
|
230 | 240 | const key = serializeVariantKey(variant.key);
|
231 |
| - const value = indent(serializePattern(variant.value)); |
| 241 | + const value = indentExceptFirstLine(serializePattern(variant.value)); |
232 | 242 |
|
233 | 243 | if (variant.default) {
|
234 | 244 | return `\n *[${key}]${value}`;
|
|
0 commit comments