in practice, shoving a space after every ) turned out to suck
[scpubgit/Q-Branch.git] / lib / SQL / Abstract / Formatter.pm
CommitLineData
b3b54441 1package SQL::Abstract::Formatter;
2
3require SQL::Abstract::Parts; # it loads us too, don't cross the streams
4
5use Moo;
6
7has indent_by => (is => 'ro', default => ' ');
8has max_width => (is => 'ro', default => 78);
9
10sub _join {
11 shift;
4ebfbfc1 12 return SQL::Abstract::Parts::stringify(\@_);
b3b54441 13}
14
15sub format {
16 my ($self, $join, @parts) = @_;
07070f1a 17 $self->_fold_sql('', '', @{$self->_simplify($join, @parts)});
18}
19
20sub _simplify {
21 my ($self, $join, @parts) = @_;
22 return '' unless @parts;
23 return $parts[0] if @parts == 1 and !ref($parts[0]);
24 return $self->_simplify(@{$parts[0]}) if @parts == 1;
25 return [ $join, map ref() ? $self->_simplify(@$_) : $_, @parts ];
26}
27
28sub _fold_sql {
07070f1a 29 my ($self, $indent0, $indent, $join, @parts) = @_;
30 my @res;
31 my $w = $self->max_width;
32 my $join_len = 0;
33 (s/, \Z/,\n/ and $join_len = 1)
34 or s/\A /\n/
35 or $_ = "\n"
36 for my $line_join = $join;
37 my ($nl_pre, $nl_post) = split "\n", $line_join;
12fceb64 38 my $line_orig = my $line = $indent0;
07070f1a 39 my $next_indent = $indent.$self->indent_by;
12fceb64 40 my $line_proto = $indent.$nl_post;
07070f1a 41 PART: foreach my $idx (0..$#parts) {
07070f1a 42 my $p = $parts[$idx];
12fceb64 43 my $pre = ($line ne $line_orig ? $join : '');
07070f1a 44 my $j_part = $pre.(my $j = ref($p) ? $self->_join(@$p) : $p);
45 if (length($j_part) + length($line) + $join_len <= $w) {
46 $line .= $j_part;
152035ec 47 next PART;
48 }
49 if (ref($p) and $p->[1] eq '(' and $p->[-1] eq ')') {
50 my $already = !($line eq $indent0 or $line eq $line_orig);
51 push @res, $line.($already ? $join : '').'('."\n";
52 my (undef, undef, $inner) = @$p;
53 my $folded = $self->_fold_sql($next_indent, $next_indent, @$inner);
4a9ad1af 54 $folded =~ s/\n\Z//;
152035ec 55 push @res, $folded."\n";
56 $line_orig = $line
57 = $indent0.')'.(
04fca5d2 58 ($nl_post and $idx < $#parts) ? $nl_post : ''
152035ec 59 );
60 next PART;
61 }
62 push @res, $line.$nl_pre."\n" if $line ne $line_orig;
63 if (length($line = $line_proto.$j) <= $w) {
64 $line_proto = $line;
65 next PART;
07070f1a 66 }
152035ec 67 my $innerdent = @res ? $indent : $next_indent;
68 my $folded = $self->_fold_sql($line_proto, $innerdent, @$p);
69 $folded =~ s/\n\Z//;
70 push @res, $folded.$nl_pre."\n";
71 $line_orig = $line = $idx == $#parts ? '' : $line_proto;
07070f1a 72 }
4ebfbfc1 73 return join '', @res, $line;
b3b54441 74}
75
761;