File Coverage

blib/lib/PPIx/EditorTools/FindVariableDeclaration.pm
Criterion Covered Total %
statement 30 30 100.0
branch 4 8 50.0
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 42 46 91.3


line stmt bran cond sub pod time code
1             package PPIx::EditorTools::FindVariableDeclaration;
2              
3             # ABSTRACT: Finds where a variable was declared using PPI
4              
5 2     2   245175 use 5.008;
  2         8  
  2         88  
6 2     2   12 use strict;
  2         6  
  2         60  
7 2     2   13 use warnings;
  2         4  
  2         63  
8 2     2   11 use Carp;
  2         29  
  2         148  
9              
10 2     2   12 use base 'PPIx::EditorTools';
  2         4  
  2         729  
11 2     2   13 use Class::XSAccessor accessors => { 'location' => 'location' };
  2         4  
  2         28  
12              
13             our $VERSION = '0.18';
14              
15             =pod
16              
17             =head1 NAME
18              
19             PPIx::EditorTools::FindVariableDeclaration - inds where a variable was declared using PPI
20              
21             =head1 SYNOPSIS
22              
23             # finds declaration of variable at cursor
24             my $declaration = PPIx::EditorTools::FindVariableDeclaration->new->find(
25             code =>
26             "package TestPackage;\nuse strict;\nBEGIN {
27             $^W = 1;
28             }\nmy \$x=1;\n\$x++;"
29             line => 5,
30             column => 2,
31             );
32             my $location = $declaration->element->location;
33              
34             =head1 DESCRIPTION
35              
36             Finds the location of a variable declaration.
37              
38             =head1 METHODS
39              
40             =over 4
41              
42             =item new()
43              
44             Constructor. Generally shouldn't be called with any arguments.
45              
46             =item find( ppi => PPI::Document $ppi, line => $line, column => $column )
47             =item find( code => Str $code, line => $line, column => $column )
48              
49             Accepts either a C to process or a string containing
50             the code (which will be converted into a C) to process.
51             Searches for the variable declaration and returns a
52             C with the declaration
53             (C) available via the C accessor.
54              
55             Croaks with a "no token" exception if no token is found at the location.
56             Croaks with a "no declaration" exception if unable to find the declaration.
57              
58             =back
59              
60             =cut
61              
62             sub find {
63 2     2 1 737 my ( $self, %args ) = @_;
64 2         47 $self->process_doc(%args);
65 2 50       10 my $column = $args{column} or croak "column required";
66 2 50       9 my $line = $args{line} or croak "line required";
67 2         6 my $location = [ $line, $column ];
68              
69 2         7 my $ppi = $self->ppi;
70 2         9 $ppi->flush_locations;
71              
72 2         372 my $token = PPIx::EditorTools::find_token_at_location( $ppi, $location );
73 2 50       102 croak "no token" unless $token;
74              
75 2         9 my $declaration = PPIx::EditorTools::find_variable_declaration($token);
76 2 50       13 croak "no declaration" unless $declaration;
77              
78 2         16 return PPIx::EditorTools::ReturnObject->new(
79             ppi => $ppi,
80             element => $declaration,
81             );
82             }
83              
84             1;
85              
86             __END__