File Coverage

blib/lib/YAML/Tidy/Node.pm
Criterion Covered Total %
statement 290 292 99.3
branch 49 56 87.5
condition 30 32 93.7
subroutine 52 52 100.0
pod 0 2 0.0
total 421 434 97.0


line stmt bran cond sub pod time code
1             # ABSTRACT: yamltidy parse tree element
2 13     13   215782 use strict;
  13         26  
  13         508  
3 13     13   61 use warnings;
  13         20  
  13         721  
4 13     13   162 use v5.20;
  13         89  
5 13     13   714 use experimental qw/ signatures /;
  13         2190  
  13         79  
6              
7             package YAML::Tidy::Node;
8              
9             our $VERSION = 'v0.11.0'; # VERSION
10 13     13   6177 use overload '""' => \&_stringify;
  13         10263  
  13         152  
11              
12 30334     30334 0 49353 sub new($class, %args) {
  30334         48749  
  30334         127228  
  30334         46514  
13 30334         154168 my $self = {
14             %args,
15             };
16 30334         133001 return bless $self, $class;
17             }
18              
19 44294     44294   68791 sub _stringify($self, @args) {
  44294         66516  
  44294         104951  
  44294         59334  
20 44294   100     127538 my $type = $self->{type} // '';
21 44294         83927 my $str = "($type)";
22 44294 100       95175 if ($self->is_collection) {
23 42840         85566 my $open = $self->open;
24 42840         87270 my $close = $self->close;
25             $str .= sprintf " - ",
26             $open->{start}->{line}, $open->{start}->{column},
27             $open->{end}->{line}, $open->{end}->{column},
28             $close->{start}->{line}, $close->{start}->{column},
29             $close->{end}->{line}, $close->{end}->{column},
30 42840         303107 }
31             else {
32 1454         4119 my $val = substr($self->{value}, 0, 20);
33 1454         2772 local $Data::Dumper::Useqq = 1;
34 1454         10923 $val = Data::Dumper->Dump([$val], ['val']);
35 1454         89218 chomp $val;
36             $str .= sprintf " - | %s",
37             $self->start->{line}, $self->start->{column},
38 1454         4055 $self->end->{line}, $self->end->{column}, $val
39             }
40 44294         170268 return $str;
41             }
42              
43 2272     2272 0 5240 sub is_mapping_value($node, $parent) {
  2272         3454  
  2272         3557  
  2272         3590  
44 2272   100     14765 return ($parent->{type} eq 'MAP' and not $node->{index} % 2);
45             }
46              
47             package YAML::Tidy::Node::Collection;
48 13 50   13   7991 use constant DEBUG => $ENV{YAML_TIDY_DEBUG} ? 1 : 0;
  13         28  
  13         1312  
49              
50 13     13   93 use base 'YAML::Tidy::Node';
  13         71  
  13         33245  
51              
52 64713     64713   93208 sub is_collection($self) { 1 }
  64713         100249  
  64713         95392  
  64713         149014  
53              
54 1352     1352   2525 sub is_alias($self) { 0 }
  1352         2424  
  1352         2043  
  1352         4743  
55              
56 34213     34213   48978 sub indent($self) {
  34213         48924  
  34213         50766  
57 34213         69670 my $firstevent = $self->open;
58 34213 100       100278 if ($firstevent->{name} eq 'document_start_event') {
59 7059         21042 return 0;
60             }
61 27154         69327 my $startcol = $firstevent->{end}->{column};
62 27154         71412 return $startcol;
63             }
64              
65 126776     126776   172360 sub open($self) { $self->{start} }
  126776         175950  
  126776         179314  
  126776         307445  
66 73675     73675   112948 sub close($self) { $self->{end} }
  73675         119166  
  73675         106284  
  73675         182916  
67              
68 727     727   1321 sub sibling($self, $node, $diff) {
  727         1211  
  727         1159  
  727         1202  
  727         1431  
69 727 50       1932 return unless $diff;
70 727         1750 my $children = $self->{children};
71             # double check if $node is child of $parent
72 727 50       3016 unless ("$children->[ $node->{index} - 1 ]" eq "$node") {
73 0         0 die ">$node< is not a child of >$self<";
74             }
75 727         2389 my $new_index = $node->{index} - 1 + $diff;
76 727 50 33     4304 if ($new_index < 0 or $new_index > $#$children) {
77 0         0 die "$new_index out of range for '$self'";
78             }
79 727         2653 return $children->[ $new_index ];
80             }
81              
82              
83 13770     13770   25651 sub end($self) {
  13770         24768  
  13770         20813  
84 13770         31510 return $self->close->{end};
85             }
86              
87 544     544   1161 sub realendline($self) {
  544         1008  
  544         1066  
88 544         1560 $self->close->{end}->{line} - 1;
89             }
90              
91 18393     18393   27339 sub start($self) {
  18393         26773  
  18393         26399  
92 18393         41743 return $self->open->{start};
93             }
94              
95 10365     10365   15912 sub line($self) {
  10365         16488  
  10365         15942  
96              
97 10365         22306 my $contentstart = $self->contentstart;
98 10365         26520 return $contentstart->{line};
99             }
100              
101 10365     10365   18668 sub contentstart($self) {
  10365         18651  
  10365         16053  
102 10365         19158 my $firstevent = $self->open;
103 10365         23839 return $firstevent->{end};
104             }
105              
106 730     730   1405 sub fix_node_indent($self, $fix) {
  730         1248  
  730         1419  
  730         1104  
107 730         1974 for my $e ($self->open, $self->close) {
108 1460         3666 for my $pos (@$e{qw/ start end /}) {
109 2920         6085 $pos->{column} += $fix;
110             }
111             }
112 730         1363 for my $c (@{ $self->{children} }) {
  730         2252  
113 1796         4577 $c->fix_node_indent($fix);
114             }
115             }
116              
117 6844     6844   10079 sub _move_columns($self, $line, $offset, $fix) {
  6844         9993  
  6844         9789  
  6844         11013  
  6844         9760  
  6844         9693  
118             # warn __PACKAGE__.':'.__LINE__.": _move_columns $self $line $offset $fix\n";
119 6844 100       14402 return if $self->end->{line} < $line;
120 6074 100       13714 return if $self->start->{line} > $line;
121 5133         10067 for my $e ($self->open, $self->close) {
122 10266         23957 for my $pos (@$e{qw/ start end /}) {
123 20532 100 100     67550 if ($pos->{line} == $line and $pos->{column} >= $offset) {
124 804         1787 $pos->{column} += $fix;
125             }
126             }
127             }
128 5133         7771 for my $c (@{ $self->{children} }) {
  5133         12132  
129 14023         32220 $c->_move_columns($line, $offset, $fix);
130             }
131             }
132              
133              
134 3498     3498   5400 sub _fix_flow_indent($self, %args) {
  3498         5164  
  3498         10131  
  3498         4833  
135             # warn __PACKAGE__.':'.__LINE__.": ========== _fix_flow_indent l=$line diff=$diff\n";
136 3498         7004 my $line = $args{line};
137 3498         5699 my $diff = $args{diff};
138 3498         6930 my $start = $self->open;
139 3498         7152 my $end = $self->close;
140 3498         10073 for my $pos ($start->{start}, $start->{end}, $end->{start}, $end->{end}) {
141 13992 100       38907 if ($pos->{line} == $line) {
142 3039         5484 $pos->{column} += $diff;
143             }
144             }
145 3498         5124 for my $c (@{ $self->{children} }) {
  3498         8141  
146 7431         20430 $c->_fix_flow_indent(%args);
147             }
148             }
149              
150 3989     3989   5999 sub fix_lines($self, $startline, $diff) {
  3989         6098  
  3989         6969  
  3989         6231  
  3989         5943  
151 3989         7960 my $start = $self->open;
152 3989         5799 DEBUG and warn __PACKAGE__.':'.__LINE__.": ======== fix_lines $startline $diff ($self)\n";
153 3989         9266 my $end = $self->close;
154 3989         12053 for my $pos ($start->{start}, $start->{end}) {
155 7978 100       23159 if ($pos->{line} >= $startline) {
156 3400         7088 $pos->{line} += $diff;
157             }
158             }
159 3989         10292 for my $pos ($end->{start}, $end->{end}) {
160 7978 100 100     33528 if ($pos->{column} == 0 and $pos->{line} == $startline) {
    100          
161             }
162             elsif ($pos->{line} >= $startline) {
163 3910         7219 $pos->{line} += $diff;
164             }
165             }
166 3989         5826 for my $c (@{ $self->{children} }) {
  3989         10974  
167 7556         18097 $c->fix_lines($startline, $diff);
168             }
169             }
170              
171             package YAML::Tidy::Node::Leaf;
172 13     13   184 use base 'YAML::Tidy::Node';
  13         23  
  13         1751  
173 13 50   13   87 use constant DEBUG => $ENV{YAML_TIDY_DEBUG} ? 1 : 0;
  13         24  
  13         17101  
174              
175 27914     27914   88655 sub is_collection { 0 }
176              
177 17372     17372   27894 sub indent($self) {
  17372         33906  
  17372         29004  
178 17372         31489 return $self->open->{column};
179             }
180              
181 40851     40851   56194 sub start($self) {
  40851         63741  
  40851         57465  
182 40851         87738 return $self->open;
183             }
184              
185 21363     21363   36999 sub end($self) {
  21363         29879  
  21363         34469  
186 21363         39924 return $self->close;
187             }
188              
189 129135     129135   191581 sub open($self) { $self->{start} }
  129135         185339  
  129135         171325  
  129135         344541  
190 103001     103001   156785 sub close($self) { $self->{end} }
  103001         150769  
  103001         146673  
  103001         268673  
191              
192 4555     4555   6922 sub fix_lines($self, $startline, $diff) {
  4555         6418  
  4555         9972  
  4555         6166  
  4555         6622  
193 4555         6087 DEBUG and warn __PACKAGE__.':'.__LINE__.": ======== fix_lines $startline $diff ($self)\n";
194 4555         9670 for my $pos ($self->open) {
195 4555 100 100     12615 if ($self->empty_leaf and $pos->{column} == 0 and $pos->{line} == $startline) {
    100 100        
196             }
197             elsif ($pos->{line} >= $startline) {
198 2233         4665 $pos->{line} += $diff;
199             }
200             }
201 4555         8666 for my $pos ($self->close) {
202 4555 100 100     23510 if ($pos->{column} == 0 and $pos->{line} == $startline) {
    100          
203             }
204             elsif ($pos->{line} >= $startline) {
205 2233         6911 $pos->{line} += $diff;
206             }
207             }
208             }
209              
210 22759     22759   33186 sub empty_leaf($self) {
  22759         33514  
  22759         30977  
211 22759         50409 my ($start, $end) = ($self->open, $self->close);
212 22759 100 100     116125 if ($start->{line} == $end->{line} and $start->{column} == $end->{column}) {
213 528         2554 return 1;
214             }
215 22231         75733 return 0;
216             }
217              
218 1610     1610   2363 sub fix_node_indent($self, $fix) {
  1610         2296  
  1610         2375  
  1610         2272  
219 1610         3409 for my $pos ($self->open, $self->close) {
220 3220         7504 $pos->{column} += $fix;
221             }
222             }
223              
224 4992     4992   7287 sub _fix_flow_indent($self, %args) {
  4992         8641  
  4992         9743  
  4992         6744  
225 4992         7898 my $line = $args{line};
226 4992         7779 my $diff = $args{diff};
227             # warn __PACKAGE__.':'.__LINE__.": ========== _fix_flow_indent l=$line diff=$diff\n";
228 4992         10186 for my $pos ($self->open, $self->close) {
229 9984 100       29233 if ($pos->{line} == $line) {
230 2850         8293 $pos->{column} += $diff;
231             }
232             }
233             }
234              
235 8538     8538   11787 sub _move_columns($self, $line, $offset, $fix) {
  8538         11901  
  8538         13783  
  8538         11707  
  8538         11552  
  8538         13051  
236             # warn __PACKAGE__.':'.__LINE__.": _move_columns $self $line $offset $fix\n";
237 8538 100       17072 return if $self->end->{line} < $line;
238 5462 100       10650 return if $self->start->{line} > $line;
239 2808         5936 for my $pos ($self->open, $self->close) {
240 5616 100 100     29817 if ($pos->{line} == $line and $pos->{column} >= $offset) {
241 1522         4403 $pos->{column} += $fix;
242             }
243             }
244             }
245              
246              
247             package YAML::Tidy::Node::Scalar;
248 13     13   121 use base 'YAML::Tidy::Node::Leaf';
  13         36  
  13         5186  
249 13 50   13   107 use constant DEBUG => $ENV{YAML_TIDY_DEBUG} ? 1 : 0;
  13         28  
  13         1293  
250 13         7424 use YAML::PP::Common qw/
251             YAML_PLAIN_SCALAR_STYLE YAML_SINGLE_QUOTED_SCALAR_STYLE
252             YAML_DOUBLE_QUOTED_SCALAR_STYLE YAML_LITERAL_SCALAR_STYLE
253             YAML_FOLDED_SCALAR_STYLE
254             YAML_FLOW_SEQUENCE_STYLE YAML_FLOW_MAPPING_STYLE
255 13     13   9338 /;
  13         27316  
256              
257 12593     12593   47770 sub is_alias($self) { 0 }
  12593         19983  
  12593         20096  
  12593         37254  
258              
259 687     687   1244 sub is_quoted($self) {
  687         1282  
  687         1078  
260 687         4147 return $self->{style} ne YAML_PLAIN_SCALAR_STYLE
261             }
262              
263 12476     12476   21499 sub realendline($self) {
  12476         19121  
  12476         16986  
264 12476         27292 my $end = $self->close;
265 12476 100 100     80007 if ($self->{style} == YAML_LITERAL_SCALAR_STYLE
266             or $self->{style} == YAML_FOLDED_SCALAR_STYLE) {
267 2341 100       9561 if ($end->{column} == 0) {
268 2265         7288 return $end->{line} - 1;
269             }
270             }
271 10211         33559 $end->{line};
272             }
273              
274 19459     19459   30870 sub line($self) {
  19459         30453  
  19459         28990  
275 19459         47835 my $contentstart = $self->contentstart;
276 19459         52783 return $contentstart->{line};
277             }
278              
279 19459     19459   31377 sub contentstart($self) {
  19459         32722  
  19459         28438  
280 19459         48093 return $self->start;
281             }
282              
283 21882     21882   38947 sub multiline($self) {
  21882         31033  
  21882         32125  
284 21882 100       51410 if ($self->open->{line} < $self->close->{line}) {
285 2762         14843 return 1;
286             }
287 19120         67049 return 0;
288             }
289              
290             package YAML::Tidy::Node::Alias;
291 13 50   13   108 use constant DEBUG => $ENV{YAML_TIDY_DEBUG} ? 1 : 0;
  13         50  
  13         987  
292 13     13   79 use base 'YAML::Tidy::Node::Leaf';
  13         25  
  13         7512  
293              
294 283     283   539 sub line($self) {
  283         601  
  283         438  
295 283         709 return $self->open->{line};
296             }
297              
298 12     12   24 sub multiline($self) { 0 }
  12         19  
  12         19  
  12         35  
299 288     288   509 sub is_alias($self) { 1 }
  288         488  
  288         490  
  288         834  
300              
301             1;