From 9cb9a350251be18aa2123f2fa67d53a835891d32 Mon Sep 17 00:00:00 2001 From: edjubert Date: Sat, 12 Sep 2026 04:32:15 +0200 Subject: [PATCH] Fix non-converging indentation of adjacent string constants Adjacent string constants separated by a newline are tokenized as a single token whose inner whitespace is kept verbatim, then _add_token prefixes $sp to every constant it holds. Since $sp is a single space when the token is not at the start of a line, the whitespace already produced by a previous run was never normalized and one space was appended on every run, so the output never converged. Rebuild the separators instead: a single space when the constants are on the same line, the current indentation after a newline. Before, formatting three times in a row: SELECT concat('a' , x , ' AU ' 'b') AS lib SELECT concat('a' , x , ' AU ' 'b') AS lib SELECT concat('a' , x , ' AU ' 'b') AS lib After: SELECT concat('a', x, ' AU ' 'b') AS lib --- lib/pgFormatter/Beautify.pm | 7 ++++++- t/02_regress.t | 2 +- t/test-files/ex85.sql | 17 +++++++++++++++++ t/test-files/expected/ex85.sql | 27 +++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 t/test-files/ex85.sql create mode 100644 t/test-files/expected/ex85.sql diff --git a/lib/pgFormatter/Beautify.pm b/lib/pgFormatter/Beautify.pm index 8e0bc254..f56d78ac 100755 --- a/lib/pgFormatter/Beautify.pm +++ b/lib/pgFormatter/Beautify.pm @@ -4705,7 +4705,12 @@ sub _add_token { { if ( $token !~ /^['"].*['"]$/ or $last_token ne ':' ) { if ( $token =~ /AAKEYWCONST\d+AA\s+AAKEYWCONST\d+AA/ ) { - $token =~ s/(AAKEYWCONST\d+AA)/$sp$1/gs; + # Rebuild the separators, the tokenizer kept them verbatim + my $nl_sp = $self->{'space'} x + ( $self->{'spaces'} * ( $self->{'_level'} // 0 ) ); + $token =~ s/[ \t]+(AAKEYWCONST\d+AA)/ $1/gs; + $token =~ s/ ?\n ?(AAKEYWCONST\d+AA)/\n$nl_sp$1/gs; + $token =~ s/^(AAKEYWCONST\d+AA)/$sp$1/s; } else { $self->{'content'} .= $sp diff --git a/t/02_regress.t b/t/02_regress.t index 29bc6092..840a2023 100755 --- a/t/02_regress.t +++ b/t/02_regress.t @@ -1,4 +1,4 @@ -use Test::Simple tests => 86; +use Test::Simple tests => 87; use File::Temp qw/ tempfile /; my $pg_format = $ENV{PG_FORMAT} // './pg_format'; # set to the full path to 'pg_format' to test installed binary in /usr/bin diff --git a/t/test-files/ex85.sql b/t/test-files/ex85.sql new file mode 100644 index 00000000..26417641 --- /dev/null +++ b/t/test-files/ex85.sql @@ -0,0 +1,17 @@ +SELECT 'a' +'b' AS lit +FROM t; + +SELECT concat('CLOTURE AVGLI PROP LOCATAIRE ' , l.imme_no , ' AU ' +'2024-12-31') AS libelle +FROM locataires l; + +SELECT 'one' +'two' +'three' AS parts +FROM t; + +SELECT * +FROM t +WHERE t.code = 'pre' +'fix'; diff --git a/t/test-files/expected/ex85.sql b/t/test-files/expected/ex85.sql new file mode 100644 index 00000000..26d0849f --- /dev/null +++ b/t/test-files/expected/ex85.sql @@ -0,0 +1,27 @@ +SELECT + 'a' + 'b' AS lit +FROM + t; + +SELECT + concat('CLOTURE AVGLI PROP LOCATAIRE ', l.imme_no, ' AU ' + '2024-12-31') AS libelle +FROM + locataires l; + +SELECT + 'one' + 'two' + 'three' AS parts +FROM + t; + +SELECT + * +FROM + t +WHERE + t.code = 'pre' + 'fix'; +