File Coverage

blib/lib/PPIx/EditorTools/FindVariableDeclaration.pm
Criterion Covered Total %
statement 29 29 100.0
branch 4 8 50.0
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 41 45 91.1


line stmt bran cond sub pod time code
1             package PPIx::EditorTools::FindVariableDeclaration;
2             our $AUTHORITY = 'cpan:YANICK';
3             # ABSTRACT: Finds where a variable was declared using PPI
4             $PPIx::EditorTools::FindVariableDeclaration::VERSION = '0.20';
5 2     2   162014 use 5.008;
  2         14  
6 2     2   10 use strict;
  2         2  
  2         38  
7 2     2   10 use warnings;
  2         4  
  2         66  
8 2     2   13 use Carp;
  2         5  
  2         112  
9              
10 2     2   10 use base 'PPIx::EditorTools';
  2         4  
  2         391  
11 2     2   12 use Class::XSAccessor accessors => { 'location' => 'location' };
  2         5  
  2         41  
12              
13              
14             sub find {
15 2     2 1 790 my ( $self, %args ) = @_;
16 2         10 $self->process_doc(%args);
17 2 50       7 my $column = $args{column} or croak "column required";
18 2 50       6 my $line = $args{line} or croak "line required";
19 2         4 my $location = [ $line, $column ];
20              
21 2         3 my $ppi = $self->ppi;
22 2         7 $ppi->flush_locations;
23              
24 2         291 my $token = PPIx::EditorTools::find_token_at_location( $ppi, $location );
25 2 50       71 croak "no token" unless $token;
26              
27 2         6 my $declaration = PPIx::EditorTools::find_variable_declaration($token);
28 2 50       8 croak "no declaration" unless $declaration;
29              
30 2         13 return PPIx::EditorTools::ReturnObject->new(
31             ppi => $ppi,
32             element => $declaration,
33             );
34             }
35              
36             1;
37              
38             =pod
39              
40             =encoding UTF-8
41              
42             =head1 NAME
43              
44             PPIx::EditorTools::FindVariableDeclaration - Finds where a variable was declared using PPI
45              
46             =head1 VERSION
47              
48             version 0.20
49              
50             =head1 SYNOPSIS
51              
52             # finds declaration of variable at cursor
53             my $declaration = PPIx::EditorTools::FindVariableDeclaration->new->find(
54             code =>
55             "package TestPackage;\nuse strict;\nBEGIN {
56             \$^W = 1;
57             }\nmy \$x=1;\n\$x++;"
58             line => 5,
59             column => 2,
60             );
61             my $location = $declaration->element->location;
62              
63             =head1 DESCRIPTION
64              
65             Finds the location of a variable declaration.
66              
67             =head1 METHODS
68              
69             =over 4
70              
71             =item new()
72              
73             Constructor. Generally shouldn't be called with any arguments.
74              
75             =item find( ppi => PPI::Document $ppi, line => $line, column => $column )
76              
77             =item find( code => Str $code, line => $line, column => $column )
78              
79             Accepts either a C<PPI::Document> to process or a string containing
80             the code (which will be converted into a C<PPI::Document>) to process.
81             Searches for the variable declaration and returns a
82             C<PPIx::EditorTools::ReturnObject> with the declaration
83             (C<PPI::Statement::Variable>) available via the C<element> accessor.
84              
85             Croaks with a "no token" exception if no token is found at the location.
86             Croaks with a "no declaration" exception if unable to find the declaration.
87              
88             =back
89              
90             =head1 SEE ALSO
91              
92             This class inherits from C<PPIx::EditorTools>.
93             Also see L<App::EditorTools>, L<Padre>, and L<PPI>.
94              
95             =head1 AUTHORS
96              
97             =over 4
98              
99             =item *
100              
101             Steffen Mueller C<smueller@cpan.org>
102              
103             =item *
104              
105             Mark Grimes C<mgrimes@cpan.org>
106              
107             =item *
108              
109             Ahmad M. Zawawi <ahmad.zawawi@gmail.com>
110              
111             =item *
112              
113             Gabor Szabo <gabor@szabgab.com>
114              
115             =item *
116              
117             Yanick Champoux <yanick@cpan.org>
118              
119             =back
120              
121             =head1 COPYRIGHT AND LICENSE
122              
123             This software is copyright (c) 2017, 2014, 2012 by The Padre development team as listed in Padre.pm..
124              
125             This is free software; you can redistribute it and/or modify it under
126             the same terms as the Perl 5 programming language system itself.
127              
128             =cut
129              
130             __END__
131              
132              
133             # Copyright 2008-2009 The Padre development team as listed in Padre.pm.
134             # LICENSE
135             # This program is free software; you can redistribute it and/or
136             # modify it under the same terms as Perl 5 itself.