File Coverage

blib/lib/Perl/Tags/Tag.pm
Criterion Covered Total %
statement 60 62 96.7
branch 12 18 66.6
condition 2 5 40.0
subroutine 18 20 90.0
pod 5 5 100.0
total 97 110 88.1


line stmt bran cond sub pod time code
1             package Perl::Tags::Tag;
2 9     9   56 use strict; use warnings;
  9     9   18  
  9         275  
  9         44  
  9         17  
  9         400  
3              
4 9     9   1848 use overload q("") => \&to_string;
  9         1043  
  9         87  
5              
6             our $VERSION = '0.32';
7              
8             =head2 C<new>
9            
10             Returns a new tag object
11            
12             =cut
13              
14             sub new {
15 81     81 1 234     my $class = shift;
16 81         346     my %options = @_;
17              
18 81         237     $options{type} = $class->type;
19              
20             # chomp and escape line
21 81         158     chomp (my $line = $options{line});
22              
23 81         127     $line =~ s{\\}{\\\\}g;
24 81         94     $line =~ s{/}{\\/}g;
25             # $line =~ s{\$}{\\\$}g;
26              
27 81         675     my $self = bless {
28                     name => $options{name},
29                     file => $options{file},
30                     type => $options{type},
31                     is_static => $options{is_static},
32                     line => $line,
33                     linenum => $options{linenum},
34                     exts => $options{exts}, # exuberant?
35                     pkg => $options{pkg}, # package name
36                 }, $class;
37              
38 81         295     $self->modify_options();
39 81         391     return $self;
40             }
41              
42             =head2 C<type>, C<modify_options>
43            
44             Abstract methods
45            
46             =cut
47              
48             sub type {
49 0     0 1 0     die "Tried to call 'type' on virtual superclass";
50             }
51              
52 68     68 1 128 sub modify_options { return } # no change
53              
54             =head2 C<to_string>
55            
56             A tag stringifies to an appropriate line in a ctags file.
57            
58             =cut
59              
60             sub to_string {
61 157     157 1 189     my $self = shift;
62              
63 157 50       429     my $name = $self->{name} or die;
64 157 50       384     my $file = $self->{file} or die;
65 157 50       317     my $line = $self->{line} or die;
66 157         461     my $linenum = $self->{linenum};
67 157   50     334     my $pkg = $self->{pkg} || '';
68              
69 157         323     my $tagline = "$name\t$file\t/$line/";
70              
71             # Exuberant extensions
72 157 100       316     if ($self->{exts}) {
73 16         28         $tagline .= qq(;"\t$self->{type});
74 16         17         $tagline .= "\tline:$linenum";
75 16 100       35         $tagline .= ($self->{is_static} ? "\tfile:" : '');
76 16 50       32         $tagline .= ($self->{pkg} ? "\tclass:$self->{pkg}" : '');
77                 }
78 157         1183     return $tagline;
79             }
80              
81             =head2 C<on_register>
82            
83             Allows tag to meddle with process when registered with the main tagger object.
84             Return false if want to prevent registration (e.g. for control tags such as
85             C<Perl::Tags::Tag::Recurse>.)
86            
87             =cut
88              
89             sub on_register {
90             # my $self = shift;
91             # my $tags = shift;
92             # .... do stuff in subclasses
93              
94 2     2 1 7     return 1; # or undef to prevent registration
95             }
96              
97             =head1 C<Perl::Tags::Tag::Package>
98            
99             =head2 C<type>: p
100            
101             =head2 C<modify_options>
102            
103             Sets static=0
104            
105             =head2 C<on_register>
106            
107             Sets the package name
108            
109             =cut
110              
111             package Perl::Tags::Tag::Package;
112             our @ISA = qw/Perl::Tags::Tag/;
113              
114             # QUOTE:
115             # Make a tag for this package unless we're told not to. A
116             # package is never static.
117              
118 13     13   36 sub type { 'p' }
119              
120             sub modify_options {
121 13     13   19     my $self = shift;
122 13         479     $self->{is_static} = 0;
123             }
124              
125             sub on_register {
126 13     13   22     my ($self, $tags) = @_;
127 13         76     $tags->{current}{package_name} = $self->{name};
128             }
129              
130             =head1 C<Perl::Tags::Tag::Var>
131            
132             =head2 C<type>: v
133            
134             =head2 C<on_register>
135            
136             Make a tag for this variable unless we're told not to. We
137             assume that a variable is always static, unless it appears
138             in a package before any sub. (Not necessarily true, but
139             it's ok for most purposes and Vim works fine even if it is
140             incorrect)
141             - pltags.pl comments
142            
143             =cut
144              
145             package Perl::Tags::Tag::Var;
146             our @ISA = qw/Perl::Tags::Tag/;
147              
148 16     16   40 sub type { 'v' }
149              
150             # QUOTE:
151              
152             sub on_register {
153 16     16   28     my ($self, $tags) = @_;
154 16 50 33     455     $self->{is_static} = ( $tags->{current}{package_name} || $tags->{current}{has_subs} ) ? 1 : 0;
155              
156 16         59     return 1;
157             }
158             =head1 C<Perl::Tags::Tag::Sub>
159            
160             =head2 C<type>: s
161            
162             =head2 C<on_register>
163            
164             Make a tag for this sub unless we're told not to. We assume
165             that a sub is static, unless it appears in a package. (Not
166             necessarily true, but it's ok for most purposes and Vim works
167             fine even if it is incorrect)
168             - pltags comments
169            
170             =cut
171              
172             package Perl::Tags::Tag::Sub;
173             our @ISA = qw/Perl::Tags::Tag/;
174              
175 8     8   31 sub type { 's' }
176              
177             sub on_register {
178 10     10   32     my ($self, $tags) = @_;
179 10         65     $tags->{current}{has_subs}++ ;
180 10 50       44     $self->{is_static}++ unless $tags->{current}{package_name};
181              
182 10         34     return 1;
183             } 
184              
185             =head1 C<Perl::Tags::Tag::Constant>
186            
187             =head2 C<type>: c
188            
189             =cut
190              
191             package Perl::Tags::Tag::Constant;
192             our @ISA = qw/Perl::Tags::Tag/;
193              
194 0     0   0 sub type { 'c' }
195              
196             =head1 C<Perl::Tags::Tag::Label>
197            
198             =head2 C<type>: l
199            
200             =cut
201              
202             package Perl::Tags::Tag::Label;
203             our @ISA = qw/Perl::Tags::Tag/;
204              
205 1     1   3 sub type { 'l' }
206              
207             =head1 C<Perl::Tags::Tag::Recurse>
208            
209             =head2 C<type>: dummy
210            
211             This is a pseudo-tag, see L<Perl::Tags/register>.
212            
213             =head2 C<on_register>
214            
215             Recurse adding this new module to the queue.
216            
217             =cut
218              
219             package Perl::Tags::Tag::Recurse;
220             our @ISA = qw/Perl::Tags::Tag/;
221              
222 9     9   15944 use Module::Locate qw/locate/;
  9         156420  
  9         66  
223              
224 40     40   99 sub type { 'dummy' }
225              
226             sub on_register {
227 40     40   70     my ($self, $tags) = @_;
228              
229 40         1521     my $name = $self->{name};
230 40         424     my $path;
231 40         224     eval {
232 40         293         $path = locate( $name ); # or warn "Couldn't find path for $name";
233                 };
234             # return if $@;
235 40 100       6907     return unless $path;
236 28         360     $tags->queue( { file=>$path, level=>$tags->{current}{level}+1 , refresh=>0} );
237 28         297     return; # don't get added
238             }
239              
240             1;
241