better indentation and interpolation
[scpubgit/Q-Branch.git] / lib / SQL / Abstract / Formatter.pm
index d9eb350..6fedeb5 100644 (file)
@@ -45,20 +45,21 @@ sub _fold_sql {
       $line .= $j_part;
     } else {
       if (ref($p) and $p->[1] eq '(' and $p->[-1] eq ')') {
-        push @res, $line.'('."\n";
+        push @res, $line.($line =~ /S/ ? $join : '').'('."\n";
         my (undef, undef, $inner) = @$p;
         my $folded = $self->_fold_sql($indent, $indent, @$inner);
-        push @res, $nl_post.$folded."\n";
-        $line = $indent0.')';
+        push @res, $folded."\n";
+        $line = $indent0.')'.($nl_post and $idx < $#parts ? ' '.$nl_post : '');
         next PART;
       }
-      push @res, $line.$nl_pre."\n";
+      push @res, $line.$nl_pre."\n" if $line =~ /\S/;
       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;
+      my $folded = $self->_fold_sql($indent.$nl_post, $next_indent, @$p);
+      $folded =~ s/\n\Z//;
+      push @res, $folded.$nl_pre."\n";
+      $line = $idx == $#parts ? '' : $indent.$nl_post;
     }
   }
   return join '', @res, $line;