@@ -366,6 +366,7 @@ static const char *get_simple_binary_op_name(OpExpr *expr);
366
366
static bool isSimpleNode (Node * node , Node * parentNode , int prettyFlags );
367
367
static void appendContextKeyword (deparse_context * context , const char * str ,
368
368
int indentBefore , int indentAfter , int indentPlus );
369
+ static void removeStringInfoSpaces (StringInfo str );
369
370
static void get_rule_expr (Node * node , deparse_context * context ,
370
371
bool showimplicit );
371
372
static void get_oper_expr (OpExpr * expr , deparse_context * context );
@@ -4494,42 +4495,42 @@ get_target_list(List *targetList, deparse_context *context,
4494
4495
/* Consider line-wrapping if enabled */
4495
4496
if (PRETTY_INDENT (context ) && context -> wrapColumn >= 0 )
4496
4497
{
4497
- int leading_nl_pos = -1 ;
4498
- char * trailing_nl ;
4499
- int pos ;
4498
+ int leading_nl_pos ;
4500
4499
4501
- /* Does the new field start with whitespace plus a new line? */
4502
- for (pos = 0 ; pos < targetbuf .len ; pos ++ )
4500
+ /* Does the new field start with a new line? */
4501
+ if (targetbuf .len > 0 && targetbuf .data [0 ] == '\n' )
4502
+ leading_nl_pos = 0 ;
4503
+ else
4504
+ leading_nl_pos = -1 ;
4505
+
4506
+ /* If so, we shouldn't add anything */
4507
+ if (leading_nl_pos >= 0 )
4503
4508
{
4504
- if (targetbuf .data [pos ] == '\n' )
4505
- {
4506
- leading_nl_pos = pos ;
4507
- break ;
4508
- }
4509
- if (targetbuf .data [pos ] != ' ' )
4510
- break ;
4509
+ /* instead, remove any trailing spaces currently in buf */
4510
+ removeStringInfoSpaces (buf );
4511
4511
}
4512
-
4513
- /* Locate the start of the current line in the output buffer */
4514
- trailing_nl = strrchr (buf -> data , '\n' );
4515
- if (trailing_nl == NULL )
4516
- trailing_nl = buf -> data ;
4517
4512
else
4518
- trailing_nl ++ ;
4513
+ {
4514
+ char * trailing_nl ;
4519
4515
4520
- /*
4521
- * If the field we're adding is the first in the list, or it
4522
- * already has a leading newline, don't add anything. Otherwise,
4523
- * add a newline, plus some indentation, if either the new field
4524
- * would cause an overflow or the last field used more than one
4525
- * line.
4526
- */
4527
- if (colno > 1 &&
4528
- leading_nl_pos == -1 &&
4529
- ((strlen (trailing_nl ) + strlen (targetbuf .data ) > context -> wrapColumn ) ||
4530
- last_was_multiline ))
4531
- appendContextKeyword (context , "" , - PRETTYINDENT_STD ,
4532
- PRETTYINDENT_STD , PRETTYINDENT_VAR );
4516
+ /* Locate the start of the current line in the output buffer */
4517
+ trailing_nl = strrchr (buf -> data , '\n' );
4518
+ if (trailing_nl == NULL )
4519
+ trailing_nl = buf -> data ;
4520
+ else
4521
+ trailing_nl ++ ;
4522
+
4523
+ /*
4524
+ * Add a newline, plus some indentation, if the new field is
4525
+ * not the first and either the new field would cause an
4526
+ * overflow or the last field used more than one line.
4527
+ */
4528
+ if (colno > 1 &&
4529
+ ((strlen (trailing_nl ) + targetbuf .len > context -> wrapColumn ) ||
4530
+ last_was_multiline ))
4531
+ appendContextKeyword (context , "" , - PRETTYINDENT_STD ,
4532
+ PRETTYINDENT_STD , PRETTYINDENT_VAR );
4533
+ }
4533
4534
4534
4535
/* Remember this field's multiline status for next iteration */
4535
4536
last_was_multiline =
@@ -6251,23 +6252,42 @@ static void
6251
6252
appendContextKeyword (deparse_context * context , const char * str ,
6252
6253
int indentBefore , int indentAfter , int indentPlus )
6253
6254
{
6255
+ StringInfo buf = context -> buf ;
6256
+
6254
6257
if (PRETTY_INDENT (context ))
6255
6258
{
6256
6259
context -> indentLevel += indentBefore ;
6257
6260
6258
- appendStringInfoChar (context -> buf , '\n' );
6259
- appendStringInfoSpaces (context -> buf ,
6261
+ /* remove any trailing spaces currently in the buffer ... */
6262
+ removeStringInfoSpaces (buf );
6263
+ /* ... then add a newline and some spaces */
6264
+ appendStringInfoChar (buf , '\n' );
6265
+ appendStringInfoSpaces (buf ,
6260
6266
Max (context -> indentLevel , 0 ) + indentPlus );
6261
- appendStringInfoString (context -> buf , str );
6267
+
6268
+ appendStringInfoString (buf , str );
6262
6269
6263
6270
context -> indentLevel += indentAfter ;
6264
6271
if (context -> indentLevel < 0 )
6265
6272
context -> indentLevel = 0 ;
6266
6273
}
6267
6274
else
6268
- appendStringInfoString (context -> buf , str );
6275
+ appendStringInfoString (buf , str );
6269
6276
}
6270
6277
6278
+ /*
6279
+ * removeStringInfoSpaces - delete trailing spaces from a buffer.
6280
+ *
6281
+ * Possibly this should move to stringinfo.c at some point.
6282
+ */
6283
+ static void
6284
+ removeStringInfoSpaces (StringInfo str )
6285
+ {
6286
+ while (str -> len > 0 && str -> data [str -> len - 1 ] == ' ' )
6287
+ str -> data [-- (str -> len )] = '\0' ;
6288
+ }
6289
+
6290
+
6271
6291
/*
6272
6292
* get_rule_expr_paren - deparse expr using get_rule_expr,
6273
6293
* embracing the string with parentheses if necessary for prettyPrint.
@@ -7929,22 +7949,33 @@ get_from_clause(Query *query, const char *prefix, deparse_context *context)
7929
7949
/* Consider line-wrapping if enabled */
7930
7950
if (PRETTY_INDENT (context ) && context -> wrapColumn >= 0 )
7931
7951
{
7932
- char * trailing_nl ;
7933
-
7934
- /* Locate the start of the current line in the buffer */
7935
- trailing_nl = strrchr (buf -> data , '\n' );
7936
- if (trailing_nl == NULL )
7937
- trailing_nl = buf -> data ;
7952
+ /* Does the new item start with a new line? */
7953
+ if (itembuf .len > 0 && itembuf .data [0 ] == '\n' )
7954
+ {
7955
+ /* If so, we shouldn't add anything */
7956
+ /* instead, remove any trailing spaces currently in buf */
7957
+ removeStringInfoSpaces (buf );
7958
+ }
7938
7959
else
7939
- trailing_nl ++ ;
7960
+ {
7961
+ char * trailing_nl ;
7940
7962
7941
- /*
7942
- * Add a newline, plus some indentation, if the new item would
7943
- * cause an overflow.
7944
- */
7945
- if (strlen (trailing_nl ) + strlen (itembuf .data ) > context -> wrapColumn )
7946
- appendContextKeyword (context , "" , - PRETTYINDENT_STD ,
7947
- PRETTYINDENT_STD , PRETTYINDENT_VAR );
7963
+ /* Locate the start of the current line in the buffer */
7964
+ trailing_nl = strrchr (buf -> data , '\n' );
7965
+ if (trailing_nl == NULL )
7966
+ trailing_nl = buf -> data ;
7967
+ else
7968
+ trailing_nl ++ ;
7969
+
7970
+ /*
7971
+ * Add a newline, plus some indentation, if the new item
7972
+ * would cause an overflow.
7973
+ */
7974
+ if (strlen (trailing_nl ) + itembuf .len > context -> wrapColumn )
7975
+ appendContextKeyword (context , "" , - PRETTYINDENT_STD ,
7976
+ PRETTYINDENT_STD ,
7977
+ PRETTYINDENT_VAR );
7978
+ }
7948
7979
}
7949
7980
7950
7981
/* Add the new item */
0 commit comments