File Coverage

blib/lib/PLS/Parser/Pod/Subroutine.pm
Criterion Covered Total %
statement 27 108 25.0
branch 0 42 0.0
condition 0 12 0.0
subroutine 9 12 75.0
pod 0 3 0.0
total 36 177 20.3


line stmt bran cond sub pod time code
1             package PLS::Parser::Pod::Subroutine;
2              
3 11     11   810 use strict;
  11         23  
  11         285  
4 11     11   55 use warnings;
  11         21  
  11         256  
5              
6 11     11   55 use parent 'PLS::Parser::Pod';
  11         21  
  11         57  
7              
8 11     11   562 use Pod::Simple::Search;
  11         21  
  11         190  
9 11     11   47 use Pod::Markdown;
  11         12  
  11         264  
10              
11 11     11   4673 use PLS::Parser::Document;
  11         31  
  11         708  
12 11     11   4918 use PLS::Parser::PackageSymbols;
  11         31  
  11         405  
13 11     11   3769 use PLS::Parser::Pod::Builtin;
  11         24  
  11         323  
14 11     11   65 use PLS::Server::State;
  11         21  
  11         7983  
15              
16             =head1 NAME
17              
18             PLS::Parser::Pod::Subroutine
19              
20             =head1 DESCRIPTION
21              
22             This is a subclass of L<PLS::Parser::Pod>, for finding POD for a subroutine.
23             This class also might find built-in Perl functions if found and C<include_builtins>
24             is set to true.
25              
26             =cut
27              
28             sub new
29             {
30 0     0 0   my ($class, @args) = @_;
31              
32 0           my %args = @args;
33 0           my $self = $class->SUPER::new(%args);
34 0           $self->{subroutine} = $args{subroutine};
35 0           $self->{packages} = $args{packages};
36 0           $self->{include_builtins} = $args{include_builtins};
37 0           $self->{uri} = $args{uri};
38              
39 0           return $self;
40             } ## end sub new
41              
42             sub find
43             {
44 0     0 0   my ($self) = @_;
45              
46 0           my $definitions;
47              
48             # If there is no package name in the subroutine call, check to see if the
49             # function is imported.
50 0 0 0       if (length $self->{uri} and (ref $self->{packages} ne 'ARRAY' or not scalar @{$self->{packages}}))
      0        
51             {
52 0           my $full_text = PLS::Parser::Document->text_from_uri($self->{uri});
53 0           my $imports = PLS::Parser::Document->get_imports($full_text);
54 0           my $imported_functions = PLS::Parser::PackageSymbols::get_imported_package_symbols($PLS::Server::State::CONFIG, @{$imports})->get();
  0            
55              
56 0           PACKAGE: foreach my $package (keys %{$imported_functions})
  0            
57             {
58 0           foreach my $subroutine (@{$imported_functions->{$package}})
  0            
59             {
60 0 0         if ($self->{subroutine} eq $subroutine)
61             {
62 0           $self->{packages} = [$package];
63 0           last PACKAGE;
64             }
65             } ## end foreach my $subroutine (@{$imported_functions...})
66             } ## end foreach my $package (keys %...)
67             } ## end if (length $self->{uri...})
68              
69 0           my @markdown;
70             my @definitions;
71              
72 0 0 0       if (ref $self->{packages} eq 'ARRAY' and scalar @{$self->{packages}})
  0 0          
73             {
74 0           my $include = $self->get_clean_inc();
75 0           foreach my $package (@{$self->{packages}})
  0            
76             {
77 0           my $search = Pod::Simple::Search->new();
78 0           $search->inc(0);
79 0           my $path = $search->find($package, @{$include});
  0            
80 0           my $ok;
81              
82 0 0         if (length $path)
83             {
84 0           my $markdown;
85 0           ($ok, $markdown) = $self->find_pod_in_file($path, $self->{subroutine});
86 0 0         push @markdown, $$markdown if $ok;
87             } ## end if (length $path)
88              
89 0 0         unless ($ok)
90             {
91 0 0         push @definitions, @{$self->{index}->find_package_subroutine($package, $self->{subroutine})} if (ref $self->{index} eq 'PLS::Parser::Index');
  0            
92             }
93             } ## end foreach my $package (@{$self...})
94             } ## end if (ref $self->{packages...})
95             elsif (ref $self->{index} eq 'PLS::Parser::Index')
96             {
97 0           push @definitions, @{$self->{index}->find_subroutine($self->{subroutine})};
  0            
98             }
99              
100 0 0         if (scalar @definitions)
101             {
102 0           my ($ok, $markdown) = $self->find_pod_in_definitions(\@definitions);
103 0 0         push @markdown, $$markdown if $ok;
104             }
105              
106 0 0         if ($self->{include_builtins})
107             {
108 0           my $builtin = PLS::Parser::Pod::Builtin->new(function => $self->{subroutine});
109 0           my $ok = $builtin->find();
110 0 0         unshift @markdown, ${$builtin->{markdown}} if $ok;
  0            
111             } ## end if ($self->{include_builtins...})
112              
113 0 0         if (scalar @markdown)
114             {
115 0           $self->{markdown} = \($self->combine_markdown(@markdown));
116 0           return 1;
117             }
118              
119             # if all else fails, show documentation for the entire package
120 0 0 0       if (ref $self->{packages} and scalar @{$self->{packages}})
  0            
121             {
122 0           foreach my $package (@{$self->{packages}})
  0            
123             {
124 0           my ($ok, $markdown) = $self->get_markdown_for_package($package);
125              
126 0 0         unless ($ok)
127             {
128 0           $package = join '::', $package, $self->{subroutine};
129 0           ($ok, $markdown) = $self->get_markdown_for_package($package);
130             }
131              
132 0 0         push @markdown, $$markdown if $ok;
133             } ## end foreach my $package (@{$self...})
134             } ## end if (ref $self->{packages...})
135              
136 0 0         if (scalar @markdown)
137             {
138 0           $self->{markdown} = \($self->combine_markdown(@markdown));
139 0           return 1;
140             }
141              
142 0           return 0;
143             } ## end sub find
144              
145             sub find_pod_in_definitions
146             {
147 0     0 0   my ($self, $definitions) = @_;
148              
149 0 0 0       return 0 unless (ref $definitions eq 'ARRAY' and scalar @$definitions);
150              
151 0           my $ok;
152             my @markdown_parts;
153              
154 0           foreach my $definition (@{$definitions})
  0            
155             {
156 0           my $path = URI->new($definition->{uri})->file;
157 0           my ($found, $markdown_part) = $self->find_pod_in_file($path, $self->{subroutine});
158 0 0         next unless $found;
159              
160 0 0         if (length $$markdown_part)
161             {
162 0           $$markdown_part = "*(From $path)*\n" . $$markdown_part;
163 0           push @markdown_parts, $$markdown_part;
164             }
165              
166 0           $ok = 1;
167             } ## end foreach my $definition (@{$definitions...})
168              
169 0 0         return 0 unless $ok;
170 0           my $markdown = $self->combine_markdown(@markdown_parts);
171 0           return 1, \$markdown;
172             } ## end sub find_pod_in_definitions
173              
174             1;