public inbox for [email protected]  
help / color / mirror / Atom feed
Re: [PATCH] pg_bsd_indent: improve formatting of multiline comments
4+ messages / 3 participants
[nested] [flat]

* Re: [PATCH] pg_bsd_indent: improve formatting of multiline comments
@ 2026-02-09 11:19 Aleksander Alekseev <[email protected]>
  2026-05-04 16:40 ` Re: [PATCH] pg_bsd_indent: improve formatting of multiline comments Tom Lane <[email protected]>
  0 siblings, 1 reply; 4+ messages in thread

From: Aleksander Alekseev @ 2026-02-09 11:19 UTC (permalink / raw)
  To: PostgreSQL Hackers <[email protected]>; +Cc: Nathan Bossart <[email protected]>

Hi,

> > Here's a patch for this.  I also changed the #undefs to #defines.
>
> I went ahead and committed this.

Thanks, Nathan. Re-submitting v6 to make it visible by cfbot again and
in order to simplify the work of reviewers a bit.

-- 
Best regards,
Aleksander Alekseev


Attachments:

  [text/x-patch] v6-0001-pgindent-improve-formatting-of-multiline-comments.patch (2.3K, 2-v6-0001-pgindent-improve-formatting-of-multiline-comments.patch)
  download | inline diff:
From ad91fda8a7406f9fd5dbbfe5a895a811230e2eec Mon Sep 17 00:00:00 2001
From: Aleksander Alekseev <[email protected]>
Date: Fri, 20 Jun 2025 16:31:36 +0300
Subject: [PATCH v6] pgindent: improve formatting of multiline comments

Format multiline comments like this:

/* line 1
 * line 2
 */

... into:

/*
 * line 1
 * line 2
 */

This is more consistent with what we currently have in the tree.

Author: Aleksander Alekseev <[email protected]>
Reported-by: Michael Paquier <[email protected]>
Reviewed-by: Arseniy Mukhin <[email protected]>
Reviewed-by: Nathan Bossart <[email protected]>
Discussion: https://postgr.es/m/CAJ7c6TPQ0kkHQG-AqeAJ3PV_YtmDzcc7s%2B_V4%3Dt%2BxgSnZm1cFw%40mail.gmail.com
---
 src/tools/pgindent/pgindent | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/src/tools/pgindent/pgindent b/src/tools/pgindent/pgindent
index b7d71808924..4db12cb1d92 100755
--- a/src/tools/pgindent/pgindent
+++ b/src/tools/pgindent/pgindent
@@ -281,6 +281,9 @@ sub post_indent
 	# Fix run-together comments to have a tab between them
 	$source =~ s!\*/(/\*.*\*/)$!*/\t$1!gm;
 
+	# Postprocess multiline comments except for /**... and /*-... ones
+	$source =~ s!^(/\*[^\*\-].*?\*/)!postprocess_multiline_comment($1)!mgse;
+
 	## Functions
 
 	# Use a single space before '*' in function return types
@@ -289,6 +292,39 @@ sub post_indent
 	return $source;
 }
 
+sub postprocess_multiline_comment
+{
+    my $source = shift;
+    my @lines = split "\n", $source;
+
+    # Only format comments that match the expected format,
+    # or at least that could have been the author's intent.
+    if (($lines[0] ne "/*" && $lines[-1] ne " */") or ($lines[1] !~ m!^ \*!))
+    {
+        return $source;
+    }
+
+    # Check each line except for the first and the last one
+    for my $i ( 1 .. scalar @lines - 2 )
+    {
+        $lines[$i] = " *".$lines[$i] if $lines[$i] !~ /^ \*/;
+    }
+
+    # Keep /* === and /* --- lines as is
+    if ($lines[0] !~ m!^/\* [=-]+!) {
+        $lines[0] =~ s!/\*(.+)!/\*\n *$1!;
+    }
+
+    # Keep === */ and --- */ lines as is
+    if ($lines[-1] !~ m![=-]+ \*/$!) {
+        $lines[-1] =~ s!(.+) \*/!$1\n \*/!;
+    }
+
+    $source = join "\n", @lines;
+
+    return $source;
+}
+
 sub run_indent
 {
 	my $source = shift;
-- 
2.43.0



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: [PATCH] pg_bsd_indent: improve formatting of multiline comments
  2026-02-09 11:19 Re: [PATCH] pg_bsd_indent: improve formatting of multiline comments Aleksander Alekseev <[email protected]>
@ 2026-05-04 16:40 ` Tom Lane <[email protected]>
  2026-05-04 17:11   ` Re: [PATCH] pg_bsd_indent: improve formatting of multiline comments Nathan Bossart <[email protected]>
  2026-05-05 13:06   ` Re: [PATCH] pg_bsd_indent: improve formatting of multiline comments Aleksander Alekseev <[email protected]>
  0 siblings, 2 replies; 4+ messages in thread

From: Tom Lane @ 2026-05-04 16:40 UTC (permalink / raw)
  To: Aleksander Alekseev <[email protected]>; +Cc: PostgreSQL Hackers <[email protected]>; Nathan Bossart <[email protected]>

Aleksander Alekseev <[email protected]> writes:
> Thanks, Nathan. Re-submitting v6 to make it visible by cfbot again and
> in order to simplify the work of reviewers a bit.

I tested the v6 patch by running it on current HEAD.  Pretty nearly all
the changes it makes are improvements, but it seems like there's still
some work to be done.  In particular, in src/port/inet_aton.c it
produces this diff:

--- a/src/port/inet_aton.c
+++ b/src/port/inet_aton.c
@@ -36,7 +37,8 @@
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.  */
+ * SUCH DAMAGE. 
+ */
 
 #include "c.h"

in which not all the trailing whitespace has been removed from the
first line, causing "git diff --check" to complain:

$ git diff --check
src/port/inet_aton.c:40: trailing whitespace.
+ * SUCH DAMAGE. 

In this case the problem is that the cleanup pattern only accounts for
one trailing space.  But a variant of this, which I think affects many
of the steps in the patch, is that there could be tab(s) there.  You
should fix the patterns to allow any number of spaces/tabs at the
spots where they currently expect just one space.  This might result
in finding cleanups they miss now.

Another amusing diff I noticed:

--- a/src/backend/partitioning/partbounds.c
+++ b/src/backend/partitioning/partbounds.c
@@ -5713,7 +5713,7 @@ check_parent_values_in_new_partitions(Relation parent,
  * 3. In case new partitions don't contain the DEFAULT partition and the
  *	  partitioned table does not have the DEFAULT partition, the following
  *	  should be true: the sum of the bounds of new partitions should be equal
- &	  to the bound of the split partition.
+ * &	  to the bound of the split partition.
  *
  * parent:			partitioned table
  * splitPartOid:	split partition Oid

Clearly, this is somebody's off-by-one-key typo, and the correct
fix is s/&/*/.  I suspect that fixing that manually is the most
expedient answer, rather than trying to make pg_bsd_indent smart
enough to DTRT.

One other nitpick is that the patch itself needs to be run through
pgperltidy (which has different opinions than your editor about
tabs vs spaces, apparently).

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: [PATCH] pg_bsd_indent: improve formatting of multiline comments
  2026-02-09 11:19 Re: [PATCH] pg_bsd_indent: improve formatting of multiline comments Aleksander Alekseev <[email protected]>
  2026-05-04 16:40 ` Re: [PATCH] pg_bsd_indent: improve formatting of multiline comments Tom Lane <[email protected]>
@ 2026-05-04 17:11   ` Nathan Bossart <[email protected]>
  1 sibling, 0 replies; 4+ messages in thread

From: Nathan Bossart @ 2026-05-04 17:11 UTC (permalink / raw)
  To: Tom Lane <[email protected]>; +Cc: Aleksander Alekseev <[email protected]>; PostgreSQL Hackers <[email protected]>

On Mon, May 04, 2026 at 12:40:19PM -0400, Tom Lane wrote:
> Another amusing diff I noticed:
> 
> --- a/src/backend/partitioning/partbounds.c
> +++ b/src/backend/partitioning/partbounds.c
> @@ -5713,7 +5713,7 @@ check_parent_values_in_new_partitions(Relation parent,
>   * 3. In case new partitions don't contain the DEFAULT partition and the
>   *	  partitioned table does not have the DEFAULT partition, the following
>   *	  should be true: the sum of the bounds of new partitions should be equal
> - &	  to the bound of the split partition.
> + * &	  to the bound of the split partition.
>   *
>   * parent:			partitioned table
>   * splitPartOid:	split partition Oid
> 
> Clearly, this is somebody's off-by-one-key typo, and the correct
> fix is s/&/*/.  I suspect that fixing that manually is the most
> expedient answer, rather than trying to make pg_bsd_indent smart
> enough to DTRT.

+1.  This patch has been rather good at finding small mistakes like this,
which I've been manually fixing along the way.

-- 
nathan





^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: [PATCH] pg_bsd_indent: improve formatting of multiline comments
  2026-02-09 11:19 Re: [PATCH] pg_bsd_indent: improve formatting of multiline comments Aleksander Alekseev <[email protected]>
  2026-05-04 16:40 ` Re: [PATCH] pg_bsd_indent: improve formatting of multiline comments Tom Lane <[email protected]>
@ 2026-05-05 13:06   ` Aleksander Alekseev <[email protected]>
  1 sibling, 0 replies; 4+ messages in thread

From: Aleksander Alekseev @ 2026-05-05 13:06 UTC (permalink / raw)
  To: Tom Lane <[email protected]>; +Cc: PostgreSQL Hackers <[email protected]>; Nathan Bossart <[email protected]>

Hi Tom,

Thanks for your feedback.

> In this case the problem is that the cleanup pattern only accounts for
> one trailing space.  But a variant of this, which I think affects many
> of the steps in the patch, is that there could be tab(s) there.  You
> should fix the patterns to allow any number of spaces/tabs at the
> spots where they currently expect just one space.  This might result
> in finding cleanups they miss now.

Fair point. Fixed.

> Another amusing diff I noticed:
>
> [...]
>
> Clearly, this is somebody's off-by-one-key typo, and the correct
> fix is s/&/*/.  I suspect that fixing that manually is the most
> expedient answer, rather than trying to make pg_bsd_indent smart
> enough to DTRT.

Agree.

> One other nitpick is that the patch itself needs to be run through
> pgperltidy (which has different opinions than your editor about
> tabs vs spaces, apparently).

Fixed in v7.

-- 
Best regards,
Aleksander Alekseev


Attachments:

  [text/x-patch] v7-0001-pgindent-improve-formatting-of-multiline-comments.patch (2.3K, 2-v7-0001-pgindent-improve-formatting-of-multiline-comments.patch)
  download | inline diff:
From 7c3abf671bafaf3a5425994e9cfedf5dafaf9895 Mon Sep 17 00:00:00 2001
From: Aleksander Alekseev <[email protected]>
Date: Fri, 20 Jun 2025 16:31:36 +0300
Subject: [PATCH v7] pgindent: improve formatting of multiline comments

Format multiline comments like this:

/* line 1
 * line 2
 */

... into:

/*
 * line 1
 * line 2
 */

This is more consistent with what we currently have in the tree.

Author: Aleksander Alekseev <[email protected]>
Reported-by: Michael Paquier <[email protected]>
Reviewed-by: Arseniy Mukhin <[email protected]>
Reviewed-by: Nathan Bossart <[email protected]>
Reviewed-by: Tom Lane <[email protected]>
Discussion: https://postgr.es/m/CAJ7c6TPQ0kkHQG-AqeAJ3PV_YtmDzcc7s%2B_V4%3Dt%2BxgSnZm1cFw%40mail.gmail.com
---
 src/tools/pgindent/pgindent | 40 +++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/src/tools/pgindent/pgindent b/src/tools/pgindent/pgindent
index b2ec5e2914b..f4be74fc3a4 100755
--- a/src/tools/pgindent/pgindent
+++ b/src/tools/pgindent/pgindent
@@ -285,6 +285,9 @@ sub post_indent
 	# Fix run-together comments to have a tab between them
 	$source =~ s!\*/(/\*.*\*/)$!*/\t$1!gm;
 
+	# Postprocess multiline comments except for /**... and /*-... ones
+	$source =~ s!^(/\*[^\*\-].*?\*/)!postprocess_multiline_comment($1)!mgse;
+
 	## Functions
 
 	# Use a single space before '*' in function return types
@@ -293,6 +296,43 @@ sub post_indent
 	return $source;
 }
 
+sub postprocess_multiline_comment
+{
+	my $source = shift;
+	my @lines = split "\n", $source;
+
+	# Only format comments that match the expected format,
+	# or at least that could have been the author's intent.
+	if (   ($lines[0] ne "/*" && $lines[-1] ne " */")
+		or ($lines[1] !~ m!^\s+\*!))
+	{
+		return $source;
+	}
+
+	# Check each line except for the first and the last one
+	for my $i (1 .. scalar @lines - 2)
+	{
+		$lines[$i] = " *" . $lines[$i] if $lines[$i] !~ /^\s+\*/;
+	}
+
+	# Keep /* === and /* --- lines as is
+	if ($lines[0] !~ m!^/\*\s+[=-]+!)
+	{
+		$lines[0] =~ s!/\*(.+)!/\*\n *$1!;
+	}
+
+	# Keep === */ and --- */ lines as is
+	if ($lines[-1] !~ m![=-]+\s+\*/$!)
+	{
+		# also remove trailing whitespaces
+		$lines[-1] =~ s!(.+?)\s+\*/!$1\n \*/!;
+	}
+
+	$source = join "\n", @lines;
+
+	return $source;
+}
+
 sub run_indent
 {
 	my $source = shift;
-- 
2.43.0



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2026-05-05 13:06 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-02-09 11:19 Re: [PATCH] pg_bsd_indent: improve formatting of multiline comments Aleksander Alekseev <[email protected]>
2026-05-04 16:40 ` Tom Lane <[email protected]>
2026-05-04 17:11   ` Nathan Bossart <[email protected]>
2026-05-05 13:06   ` Aleksander Alekseev <[email protected]>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox