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.21';
5 2     2   170489 use 5.008;
  2         12  
6 2     2   8 use strict;
  2         3  
  2         33  
7 2     2   7 use warnings;
  2         4  
  2         48  
8 2     2   7 use Carp;
  2         4  
  2         94  
9              
10 2     2   9 use base 'PPIx::EditorTools';
  2         4  
  2         512  
11 2     2   11 use Class::XSAccessor accessors => { 'location' => 'location' };
  2         2  
  2         19  
12              
13              
14             sub find {
15 2     2 1 696 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       5 my $line = $args{line} or croak "line required";
19 2         5 my $location = [ $line, $column ];
20              
21 2         4 my $ppi = $self->ppi;
22 2         14 $ppi->flush_locations;
23              
24 2         274 my $token = PPIx::EditorTools::find_token_at_location( $ppi, $location );
25 2 50       76 croak "no token" unless $token;
26              
27 2         6 my $declaration = PPIx::EditorTools::find_variable_declaration($token);
28 2 50       7 croak "no declaration" unless $declaration;
29              
30 2         10 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.21
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 to process or a string containing
80             the code (which will be converted into a C) to process.
81             Searches for the variable declaration and returns a
82             C with the declaration
83             (C) available via the C 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.
93             Also see L, L, and L.
94              
95             =head1 AUTHORS
96              
97             =over 4
98              
99             =item *
100              
101             Steffen Mueller C
102              
103             =item *
104              
105             Mark Grimes C
106              
107             =item *
108              
109             Ahmad M. Zawawi
110              
111             =item *
112              
113             Gabor Szabo
114              
115             =item *
116              
117             Yanick Champoux
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__