|
69 | 69 | #define PRETTYINDENT_JOIN 4
|
70 | 70 | #define PRETTYINDENT_VAR 4
|
71 | 71 |
|
| 72 | +#define PRETTYINDENT_LIMIT 40 /* wrap limit */ |
| 73 | + |
72 | 74 | /* Pretty flags */
|
73 | 75 | #define PRETTYFLAG_PAREN 1
|
74 | 76 | #define PRETTYFLAG_INDENT 2
|
@@ -6258,14 +6260,36 @@ appendContextKeyword(deparse_context *context, const char *str,
|
6258 | 6260 |
|
6259 | 6261 | if (PRETTY_INDENT(context))
|
6260 | 6262 | {
|
| 6263 | + int indentAmount; |
| 6264 | + |
6261 | 6265 | context->indentLevel += indentBefore;
|
6262 | 6266 |
|
6263 | 6267 | /* remove any trailing spaces currently in the buffer ... */
|
6264 | 6268 | removeStringInfoSpaces(buf);
|
6265 | 6269 | /* ... then add a newline and some spaces */
|
6266 | 6270 | appendStringInfoChar(buf, '\n');
|
6267 |
| - appendStringInfoSpaces(buf, |
6268 |
| - Max(context->indentLevel, 0) + indentPlus); |
| 6271 | + |
| 6272 | + if (context->indentLevel < PRETTYINDENT_LIMIT) |
| 6273 | + indentAmount = Max(context->indentLevel, 0) + indentPlus; |
| 6274 | + else |
| 6275 | + { |
| 6276 | + /* |
| 6277 | + * If we're indented more than PRETTYINDENT_LIMIT characters, try |
| 6278 | + * to conserve horizontal space by reducing the per-level |
| 6279 | + * indentation. For best results the scale factor here should |
| 6280 | + * divide all the indent amounts that get added to indentLevel |
| 6281 | + * (PRETTYINDENT_STD, etc). It's important that the indentation |
| 6282 | + * not grow unboundedly, else deeply-nested trees use O(N^2) |
| 6283 | + * whitespace; so we also wrap modulo PRETTYINDENT_LIMIT. |
| 6284 | + */ |
| 6285 | + indentAmount = PRETTYINDENT_LIMIT + |
| 6286 | + (context->indentLevel - PRETTYINDENT_LIMIT) / |
| 6287 | + (PRETTYINDENT_STD / 2); |
| 6288 | + indentAmount %= PRETTYINDENT_LIMIT; |
| 6289 | + /* scale/wrap logic affects indentLevel, but not indentPlus */ |
| 6290 | + indentAmount += indentPlus; |
| 6291 | + } |
| 6292 | + appendStringInfoSpaces(buf, indentAmount); |
6269 | 6293 |
|
6270 | 6294 | appendStringInfoString(buf, str);
|
6271 | 6295 |
|
|
0 commit comments