blib/lib/Vim/X/Line.pm

filter: Statementstmt 20.7% 11 / 53 Branchbran 0.0% 0 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 15.0% 3 / 20 Podpod 87.5% 7 / 8 totaltotal 21.6% 21 / 97 CC 9 SCAR 20.8
CC
9
Cov
16%
_search
30.0
__ANON__
6.0
content
6.0
CRAP 8.0
« blib/lib/Vim/X/Plugin/ResolveConflict.pm ↑ Summary blib/lib/Vim/X/Range.pm »
1 package Vim::X::Line;
2 BEGIN {
3 4 ▶ $Vim::X::Line::AUTHORITY = 'cpan:YANICK';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 }
5 # ABSTRACT: A line in a Vim buffer
6 4 $Vim::X::Line::VERSION = '1.0.1';
7 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 use overload
11 0 ▶ '""' => sub { $_[0]->content },
Subroutine __ANON__: not called CC=1 SCAR=6.9
12 0 ▶ 'eq' => sub { $_[0]->content eq $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
13 0 ▶ '<<=' => sub { $_[0]->content( $_[1] ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
14 0 ▶ '0+' => sub { return $_[0]->index },
Subroutine __ANON__: not called CC=1 SCAR=6.9
15 0 ▶ '+' => sub { return $_[0]->index + $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
16 '<=>' => sub {
17 0 ▶ return $_[0]->index <=> (ref $_[1] ? $_[1]->index : $_[1])
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
18 },
19 4 ▶ ;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21  
22  
23 4 has buffer => (
24 is => 'ro',
25 required => 1,
26 );
27  
28  
29 4 has 'index' => (
30 is => 'rw',
31 required => 1,
32 );
33  
34  
35 sub clone {
36 0 ▶ my $self = shift;
Subroutine clone: not called CC=1 SCAR=6.9
Pod documented
37 0 return Vim::X::Line->new( buffer => $self->buffer, index => $self->index );
38 }
39  
40  
41 sub content {
42 0 ▶ my $self = shift;
Subroutine content: not called CC=2 SCAR=17.9
Pod documented
43 0 ▶ $self->buffer->set_line( $self->index, map { split "\n" } @_ ) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
44 0 return $self->buffer->lines_content( $self->index );
45 }
46  
47  
48 sub append {
49 0 ▶ my( $self, @lines ) = @_;
Subroutine append: not called CC=1 SCAR=6.9
Pod documented
50 0 $self->buffer->append( $self->index, @lines );
51 }
52  
53  
54  
55 sub dec {
56 0 ▶ my $self = shift;
Subroutine dec: not called CC=2 SCAR=17.9
Pod documented
57  
58 0 ▶ return if $self->index == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->index == 1 0 0 0
59  
60 0 $self->index( $self->index - 1 );
61 0 return $self;
62 }
63  
64  
65 sub inc {
66 0 ▶ my $self = shift;
Subroutine inc: not called CC=2 SCAR=17.9
Pod documented
67  
68 0 ▶ return if $self->index == $self->buffer->size;
Branch: 1 branch
BranchTrueFalseTotal
if $self->index == $self->buffer->size 0 0 0
69  
70 0 $self->index( $self->index + 1 );
71 0 return $self;
72 }
73  
74 sub _search {
75 0 ▶ my( $self, $direction, $condition ) = @_;
Subroutine _search: not called CC=5 SCAR=34.0
76  
77 0 my $target = $self->clone;
78  
79 0 ▶ my $it = $direction > 0 ? sub { $target->inc } : sub { $target->dec };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$direction > 0 ? : 0 0 0
80  
81 0 my $cond = $condition;
82 0 ▶ $cond = sub { $_[0]->content =~ $condition } if ref $condition eq 'Regexp';
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if ref $condition eq "Regexp" 0 0 0
83  
84 0 $_ = $target;
85 0 ▶ $it->() or return until $cond->($target);
Branch: 2 branches
BranchTrueFalseTotal
&$cond($target) or do { &$it() or return } 0 0 0
&$it() or return 0 0 0
86  
87 0 $self->index( $target->index );
88 0 return $self;
89 }
90  
91  
92 sub rewind {
93 0 ▶ my( $self, $condition ) = @_;
Subroutine rewind: not called CC=1 SCAR=6.9
Pod documented
94  
95 0 return $self->_search(-1,$condition);
96 }
97  
98  
99  
100 sub ff {
101 0 ▶ my( $self, $condition ) = @_;
Subroutine ff: not called CC=1 SCAR=6.9
Pod documented
102  
103 0 return $self->_search(1,$condition);
104 }
105  
106 sub map {
107 0 ▶ my( $self, $code ) = @_;
Subroutine map: not called CC=1 SCAR=6.9
Pod undocumented
108  
109 0 $_ = "$self";
110 0 my @values = $code->();
111 0 $self &= $values[0];
112
113 }
114  
115 4 1;
116  
117 __END__
118  
119 =pod
120  
121 =encoding UTF-8
122  
123 =head1 NAME
124  
125 Vim::X::Line - A line in a Vim buffer
126  
127 =head1 VERSION
128  
129 version 1.0.1
130  
131 =head1 METHODS
132  
133 =head2 new( buffer => $buffer, index => $i )
134  
135 Creates a new L<Vim::X::Buffer> object. Both I<buffer> and I<index>
136 arguments are required.
137  
138 =head2 clone
139  
140 Makes a copy of the object.
141  
142 =head2 content( $new_content )
143  
144 Sets the content of the line to ne I<$new_content> or, if I<$new_content> is not given, returns
145 the current content.
146  
147 =head2 append( @lines )
148  
149 Append the I<@lines> after the current line.
150  
151 Carriage returns in lines will cause them to be splitted.
152  
153 =head2 dec()
154  
155 Makes the object point to the previous line. Returns the object or
156 C<undef>
157 if at the beginning of the buffer and can't backtrack anymore.
158  
159 =head2 inc()
160  
161 Makes the object point to the next line. Returns the object or,
162 C<undef>
163 if at the end of the buffer and can't advance anymore.
164  
165 =head2 rewind( $condition )
166  
167 Move back in the buffer until I<$condition> is met.
168  
169 The condition can be a regular expression, or a coderef that will
170 return C<true> on success. If the condition is not met, the method returns C<false>
171 and the line index of the object is not modified.
172  
173 =head2 ff( $condition )
174  
175 Move forward in the buffer until I<$condition> is met.
176  
177 The condition can be a regular expression, or a coderef that will
178 return C<true> on success. If the condition is not met, the method returns C<false>
179 and the line index of the object is not modified.
180  
181 =description
182  
183 =head2 BIG HUGE WARNING
184  
185 When created, the line objects store the buffer and index of the line they
186 correspond to, so they are more like cursors than representations of the actual lines.
187 If lines are added or deleted in the buffer, the object
188 B<will not be updated> in consequence. For example, this won't do what you think:
189  
190 vim_append( '1 meh', '2 meh', '3 meh' );
191  
192 for my $line ( vim_lines ) {
193 $line->delete if "$line" =~ /meh/;
194 }
195  
196 vim_msg join ' ', vim_lines; # prints '2 meh'
197  
198 That's because the original line #2 become line #1 after the first
199 delete. For things to work correctly, you can process the lines in
200 reverse order:
201  
202 vim_append( '1 meh', '2 meh', '3 meh' );
203  
204 for my $line ( reverse vim_lines ) {
205 $line->delete if "$line" =~ /meh/;
206 }
207  
208 vim_msg join ' ', vim_lines; # prints nothing!
209  
210 =head1 OVERLOADING
211  
212 The line object, when used as a string, will yield its content. And if used
213 in a numerical context, it'll returns its line number.
214  
215 As an additional piece of sugar is the overloading of '<<=', which sets the content of the
216 line.
217  
218 $line <<= "$line" =~ s/foo/bar/rg;
219  
220 # equivalent to
221  
222 $line->content( $line->content =~ s/foo/bar/rg );
223  
224 =attribute buffer
225  
226 The L<Vim::X::Buffer> the line belongs to. Read-only.
227  
228 =attribute index($i)
229  
230 Sets or gets the line number of the object.
231  
232 =head1 AUTHOR
233  
234 Yanick Champoux <yanick@cpan.org>
235  
236 =head1 COPYRIGHT AND LICENSE
237  
238 This software is copyright (c) 2014 by Yanick Champoux.
239  
240 This is free software; you can redistribute it and/or modify it under
241 the same terms as the Perl 5 programming language system itself.
242  
243 =cut
« blib/lib/Vim/X/Plugin/ResolveConflict.pm ↑ Summary blib/lib/Vim/X/Range.pm »