File Coverage

blib/lib/Vim/Complete.pm
Criterion Covered Total %
statement 53 60 88.3
branch 9 22 40.9
condition 3 5 60.0
subroutine 11 12 91.6
pod 4 4 100.0
total 80 103 77.6


line stmt bran cond sub pod time code
1 1     1   946 use 5.008;
  1         4  
  1         45  
2 1     1   7 use strict;
  1         2  
  1         28  
3 1     1   5 use warnings;
  1         2  
  1         60  
4              
5             package Vim::Complete;
6             our $VERSION = '1.100880';
7             # ABSTRACT: Generate auto completion information for vim
8 1     1   1039 use PPI;
  1         207713  
  1         49  
9 1     1   12 use File::Find;
  1         2  
  1         92  
10 1     1   6 use File::Spec;
  1         1  
  1         27  
11 1     1   1269 use parent qw(Class::Accessor::Complex);
  1         359  
  1         6  
12             __PACKAGE__->mk_new->mk_scalar_accessors(qw(min_length))
13             ->mk_array_accessors(qw(dirs))->mk_hash_accessors(qw(result))
14             ->mk_boolean_accessors(qw(verbose));
15              
16             sub gather {
17 1     1 1 3 my ($self, $filename) = @_;
18              
19             # does PPI not like relative filenames?
20 1         47 $filename = File::Spec->rel2abs($filename);
21 1         15 my $document = PPI::Document->new($filename);
22 1 50       113583 unless (UNIVERSAL::isa($document, 'PPI::Document')) {
23 0         0 warn "couldn't parse $filename\n";
24 0         0 return;
25             }
26              
27             # get a hash reference so we can change the hash in place
28 1         10 my $result = $self->result;
29 1   50     64 my $min_length = $self->min_length || 3;
30 1         68 $result->{$_} = 1
31 1         11 for grep { /\w{$min_length,}/ }
  1         20184  
32 1 50       28 map { $_->namespace }
33             @{ $document->find('PPI::Statement::Package') || [] };
34 13         87 $result->{$_} = 1
35 1         3 for grep { /\w{$min_length,}/ }
  13         46  
36 13         17 map { substr($_, 0, 1, ''); $_ }
  11         21102  
37 1 50       7 map { $_->variables }
38             @{ $document->find('PPI::Statement::Variable') || [] };
39 4         70 $result->{$_} = 1
40 1         7 for grep { /\w{$min_length,}/ }
  4         25673  
41 1 50       6 map { $_->name } @{ $document->find('PPI::Statement::Sub') || [] };
42             }
43              
44             sub report {
45 1     1 1 9 my $self = shift;
46 1         9 my @result = sort $self->result_keys;
47 1 50       31 wantarray ? @result : \@result;
48             }
49              
50             sub report_to_file {
51 0     0 1 0 my ($self, $filename) = @_;
52 0 0       0 die "no filename\n" unless defined $filename;
53 0 0       0 open my $fh, '>', $filename or die "can't open $filename for writing: $!\n";
54 0         0 print $fh "$_\n" for $self->report;
55 0 0       0 close $fh or die "can't close $fh: $!\n";
56             }
57              
58             sub parse {
59 1     1 1 159 my $self = shift;
60 1         6 my $verbose = $self->verbose;
61             find(
62             sub {
63 3 100 66 3   428 return unless -f && /\.pm$/;
64              
65             # We can see a file more than once if we have nested paths in @INC, so
66             # check
67 1         2 our %seen;
68 1 50       10 return if $seen{$File::Find::name}++;
69 1 50       5 warn "processing $File::Find::name\n" if $verbose;
70 1         6 $self->gather($_);
71             },
72 1         14 $self->dirs
73             );
74 1         6024 $self; # for chaining
75             }
76             1;
77              
78              
79             __END__