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