X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FAbstract%2FFormatter.pm;h=0f0daf31640debd50265845dad089f7cfda2945c;hb=116bc0b69bc3d55647a4e195cf212a6af38ba5b8;hp=59f5f200d7a0bdf0da1d0f83ac8c7a294ab5663a;hpb=4ebfbfc14c43fde301c9e3ee25df9f078dff0a3c;p=scpubgit%2FQ-Branch.git diff --git a/lib/SQL/Abstract/Formatter.pm b/lib/SQL/Abstract/Formatter.pm index 59f5f20..0f0daf3 100644 --- a/lib/SQL/Abstract/Formatter.pm +++ b/lib/SQL/Abstract/Formatter.pm @@ -35,31 +35,44 @@ 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 : ''; +#::DwarnT STARTPART => $p, \@res, $line, $line_orig; + 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 ($p->[1] eq '(' and $p->[-1] eq ')') { - push @res, $line.$pre.'('."\n"; - my (undef, undef, $inner) = @$p; - my $folded = $self->_fold_sql($indent, $indent, @$inner); - push @res, $nl_post.$folded."\n"; - $line = $indent0.')'; - next PART; - } - push @res, $line.$nl_pre."\n"; - if (length($line = $indent.$nl_post.$j) <= $w) { - next PART; - } - my $folded = $self->_fold_sql($indent, $next_indent, @$p); - push @res, $folded.$pre."\n"; - $line = $indent.$nl_post; + next PART; } + my $innerdent = @res + ? $next_indent + : $indent0.$self->indent_by; + if (ref($p) and $p->[1] eq '(' and $p->[-1] eq ')') { + 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($innerdent, $innerdent, @$inner); + $folded =~ s/\n\Z//; + push @res, $folded."\n"; + $line_orig = $line + = $indent0.')'.($idx == $#parts ? '' : $join); + next PART; + } + if ($line ne $line_orig) { + push @res, $line.($idx == $#parts ? '' : $nl_pre)."\n"; + } + if (length($line = $line_proto.$j) <= $w) { + next PART; + } + my $folded = $self->_fold_sql($line_proto, $innerdent, @$p); + $folded =~ s/\n\Z//; + push @res, $folded.($idx == $#parts ? '' : $nl_pre)."\n"; + $line_orig = $line = $idx == $#parts ? '' : $line_proto; + } continue { +#::DwarnT ENDPART => $parts[$idx], \@res, $line, $line_orig; } return join '', @res, $line; }