From: Matt S Trout Date: Sat, 14 Sep 2019 01:56:42 +0000 (+0000) Subject: cleanup and improve formatting logic X-Git-Tag: v1.90_01~130 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=12fceb64ae12d5938c54d76234846ce6c229ac95;p=dbsrgits%2FSQL-Abstract.git cleanup and improve formatting logic --- diff --git a/lib/SQL/Abstract/Formatter.pm b/lib/SQL/Abstract/Formatter.pm index 6fedeb5..f337a25 100644 --- a/lib/SQL/Abstract/Formatter.pm +++ b/lib/SQL/Abstract/Formatter.pm @@ -35,31 +35,36 @@ sub _fold_sql { or $_ = "\n" for my $line_join = $join; my ($nl_pre, $nl_post) = split "\n", $line_join; - my $line = $indent0; + my $line_orig = my $line = $indent0; my $next_indent = $indent.$self->indent_by; + my $line_proto = $indent.$nl_post; PART: foreach my $idx (0..$#parts) { my $p = $parts[$idx]; - my $pre = $idx ? $join : ''; + my $pre = ($line ne $line_orig ? $join : ''); my $j_part = $pre.(my $j = ref($p) ? $self->_join(@$p) : $p); if (length($j_part) + length($line) + $join_len <= $w) { $line .= $j_part; } else { if (ref($p) and $p->[1] eq '(' and $p->[-1] eq ')') { - push @res, $line.($line =~ /S/ ? $join : '').'('."\n"; + my $already = !($line eq $indent0 or $line eq $line_orig); + push @res, $line.($already ? $join : '').'('."\n"; my (undef, undef, $inner) = @$p; - my $folded = $self->_fold_sql($indent, $indent, @$inner); + my $fold_indent = $already ? $indent : $next_indent; + my $folded = $self->_fold_sql($fold_indent, $fold_indent, @$inner); push @res, $folded."\n"; - $line = $indent0.')'.($nl_post and $idx < $#parts ? ' '.$nl_post : ''); + $line_orig = $line + = $indent0.')'.($nl_post and $idx < $#parts ? ' '.$nl_post : ''); next PART; } - push @res, $line.$nl_pre."\n" if $line =~ /\S/; - if (length($line = $indent.$nl_post.$j) <= $w) { + push @res, $line.$nl_pre."\n" if $line ne $line_orig; + if (length($line = $line_proto.$j) <= $w) { + $line_proto = $line; next PART; } - my $folded = $self->_fold_sql($indent.$nl_post, $next_indent, @$p); + my $folded = $self->_fold_sql($line_proto, $next_indent, @$p); $folded =~ s/\n\Z//; push @res, $folded.$nl_pre."\n"; - $line = $idx == $#parts ? '' : $indent.$nl_post; + $line_orig = $line = $idx == $#parts ? '' : $line_proto; } } return join '', @res, $line;