@@ -365,6 +365,7 @@ static const char *get_simple_binary_op_name(OpExpr *expr);
365365static bool isSimpleNode (Node * node , Node * parentNode , int prettyFlags );
366366static void appendContextKeyword (deparse_context * context , const char * str ,
367367 int indentBefore , int indentAfter , int indentPlus );
368+ static void removeStringInfoSpaces (StringInfo str );
368369static void get_rule_expr (Node * node , deparse_context * context ,
369370 bool showimplicit );
370371static void get_oper_expr (OpExpr * expr , deparse_context * context );
@@ -4479,42 +4480,42 @@ get_target_list(List *targetList, deparse_context *context,
44794480 /* Consider line-wrapping if enabled */
44804481 if (PRETTY_INDENT (context ) && context -> wrapColumn >= 0 )
44814482 {
4482- int leading_nl_pos = -1 ;
4483- char * trailing_nl ;
4484- int pos ;
4483+ int leading_nl_pos ;
44854484
4486- /* Does the new field start with whitespace plus a new line? */
4487- for (pos = 0 ; pos < targetbuf .len ; pos ++ )
4485+ /* Does the new field start with a new line? */
4486+ if (targetbuf .len > 0 && targetbuf .data [0 ] == '\n' )
4487+ leading_nl_pos = 0 ;
4488+ else
4489+ leading_nl_pos = -1 ;
4490+
4491+ /* If so, we shouldn't add anything */
4492+ if (leading_nl_pos >= 0 )
44884493 {
4489- if (targetbuf .data [pos ] == '\n' )
4490- {
4491- leading_nl_pos = pos ;
4492- break ;
4493- }
4494- if (targetbuf .data [pos ] != ' ' )
4495- break ;
4494+ /* instead, remove any trailing spaces currently in buf */
4495+ removeStringInfoSpaces (buf );
44964496 }
4497-
4498- /* Locate the start of the current line in the output buffer */
4499- trailing_nl = strrchr (buf -> data , '\n' );
4500- if (trailing_nl == NULL )
4501- trailing_nl = buf -> data ;
45024497 else
4503- trailing_nl ++ ;
4498+ {
4499+ char * trailing_nl ;
45044500
4505- /*
4506- * If the field we're adding is the first in the list, or it
4507- * already has a leading newline, don't add anything. Otherwise,
4508- * add a newline, plus some indentation, if either the new field
4509- * would cause an overflow or the last field used more than one
4510- * line.
4511- */
4512- if (colno > 1 &&
4513- leading_nl_pos == -1 &&
4514- ((strlen (trailing_nl ) + strlen (targetbuf .data ) > context -> wrapColumn ) ||
4515- last_was_multiline ))
4516- appendContextKeyword (context , "" , - PRETTYINDENT_STD ,
4517- PRETTYINDENT_STD , PRETTYINDENT_VAR );
4501+ /* Locate the start of the current line in the output buffer */
4502+ trailing_nl = strrchr (buf -> data , '\n' );
4503+ if (trailing_nl == NULL )
4504+ trailing_nl = buf -> data ;
4505+ else
4506+ trailing_nl ++ ;
4507+
4508+ /*
4509+ * Add a newline, plus some indentation, if the new field is
4510+ * not the first and either the new field would cause an
4511+ * overflow or the last field used more than one line.
4512+ */
4513+ if (colno > 1 &&
4514+ ((strlen (trailing_nl ) + targetbuf .len > context -> wrapColumn ) ||
4515+ last_was_multiline ))
4516+ appendContextKeyword (context , "" , - PRETTYINDENT_STD ,
4517+ PRETTYINDENT_STD , PRETTYINDENT_VAR );
4518+ }
45184519
45194520 /* Remember this field's multiline status for next iteration */
45204521 last_was_multiline =
@@ -6236,23 +6237,42 @@ static void
62366237appendContextKeyword (deparse_context * context , const char * str ,
62376238 int indentBefore , int indentAfter , int indentPlus )
62386239{
6240+ StringInfo buf = context -> buf ;
6241+
62396242 if (PRETTY_INDENT (context ))
62406243 {
62416244 context -> indentLevel += indentBefore ;
62426245
6243- appendStringInfoChar (context -> buf , '\n' );
6244- appendStringInfoSpaces (context -> buf ,
6246+ /* remove any trailing spaces currently in the buffer ... */
6247+ removeStringInfoSpaces (buf );
6248+ /* ... then add a newline and some spaces */
6249+ appendStringInfoChar (buf , '\n' );
6250+ appendStringInfoSpaces (buf ,
62456251 Max (context -> indentLevel , 0 ) + indentPlus );
6246- appendStringInfoString (context -> buf , str );
6252+
6253+ appendStringInfoString (buf , str );
62476254
62486255 context -> indentLevel += indentAfter ;
62496256 if (context -> indentLevel < 0 )
62506257 context -> indentLevel = 0 ;
62516258 }
62526259 else
6253- appendStringInfoString (context -> buf , str );
6260+ appendStringInfoString (buf , str );
62546261}
62556262
6263+ /*
6264+ * removeStringInfoSpaces - delete trailing spaces from a buffer.
6265+ *
6266+ * Possibly this should move to stringinfo.c at some point.
6267+ */
6268+ static void
6269+ removeStringInfoSpaces (StringInfo str )
6270+ {
6271+ while (str -> len > 0 && str -> data [str -> len - 1 ] == ' ' )
6272+ str -> data [-- (str -> len )] = '\0' ;
6273+ }
6274+
6275+
62566276/*
62576277 * get_rule_expr_paren - deparse expr using get_rule_expr,
62586278 * embracing the string with parentheses if necessary for prettyPrint.
@@ -7942,22 +7962,33 @@ get_from_clause(Query *query, const char *prefix, deparse_context *context)
79427962 /* Consider line-wrapping if enabled */
79437963 if (PRETTY_INDENT (context ) && context -> wrapColumn >= 0 )
79447964 {
7945- char * trailing_nl ;
7946-
7947- /* Locate the start of the current line in the buffer */
7948- trailing_nl = strrchr (buf -> data , '\n' );
7949- if (trailing_nl == NULL )
7950- trailing_nl = buf -> data ;
7965+ /* Does the new item start with a new line? */
7966+ if (itembuf .len > 0 && itembuf .data [0 ] == '\n' )
7967+ {
7968+ /* If so, we shouldn't add anything */
7969+ /* instead, remove any trailing spaces currently in buf */
7970+ removeStringInfoSpaces (buf );
7971+ }
79517972 else
7952- trailing_nl ++ ;
7973+ {
7974+ char * trailing_nl ;
79537975
7954- /*
7955- * Add a newline, plus some indentation, if the new item would
7956- * cause an overflow.
7957- */
7958- if (strlen (trailing_nl ) + strlen (itembuf .data ) > context -> wrapColumn )
7959- appendContextKeyword (context , "" , - PRETTYINDENT_STD ,
7960- PRETTYINDENT_STD , PRETTYINDENT_VAR );
7976+ /* Locate the start of the current line in the buffer */
7977+ trailing_nl = strrchr (buf -> data , '\n' );
7978+ if (trailing_nl == NULL )
7979+ trailing_nl = buf -> data ;
7980+ else
7981+ trailing_nl ++ ;
7982+
7983+ /*
7984+ * Add a newline, plus some indentation, if the new item
7985+ * would cause an overflow.
7986+ */
7987+ if (strlen (trailing_nl ) + itembuf .len > context -> wrapColumn )
7988+ appendContextKeyword (context , "" , - PRETTYINDENT_STD ,
7989+ PRETTYINDENT_STD ,
7990+ PRETTYINDENT_VAR );
7991+ }
79617992 }
79627993
79637994 /* Add the new item */
0 commit comments