« blib/lib/PPIx/EditorTools/RenamePackage.pm
↑ Summary
blib/lib/PPIx/EditorTools/FindUnmatchedBrace.pm »
| 1 | package PPIx::EditorTools::FindVariableDeclaration; | ||||||||||
| 2 | 3 | our $AUTHORITY = 'cpan:YANICK'; | |||||||||
| 3 | # ABSTRACT: Finds where a variable was declared using PPI | ||||||||||
| 4 | 3 | $PPIx::EditorTools::FindVariableDeclaration::VERSION = '0.20'; | |||||||||
| 5 | 3 | ▶ | use 5.008; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 3 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 3 | ▶ | use base 'PPIx::EditorTools'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 3 | ▶ | use Class::XSAccessor accessors => { 'location' => 'location' }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | |||||||||||
| 14 | sub find { | ||||||||||
| 15 | 2 | ▶ | my ( $self, %args ) = @_; | ||||||||
|
Subroutine
find: called CC=5 SCAR=16.5
Pod
documented
| |||||||||||
| 16 | 2 | $self->process_doc(%args); | |||||||||
| 17 | 2 | ▶ | my $column = $args{column} or croak "column required"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 18 | 2 | ▶ | my $line = $args{line} or croak "line required"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 19 | 2 | my $location = [ $line, $column ]; | |||||||||
| 20 | |||||||||||
| 21 | 2 | my $ppi = $self->ppi; | |||||||||
| 22 | 2 | $ppi->flush_locations; | |||||||||
| 23 | |||||||||||
| 24 | 2 | my $token = PPIx::EditorTools::find_token_at_location( $ppi, $location ); | |||||||||
| 25 | 2 | ▶ | croak "no token" unless $token; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | |||||||||||
| 27 | 2 | my $declaration = PPIx::EditorTools::find_variable_declaration($token); | |||||||||
| 28 | 2 | ▶ | croak "no declaration" unless $declaration; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | |||||||||||
| 30 | 2 | return PPIx::EditorTools::ReturnObject->new( | |||||||||
| 31 | ppi => $ppi, | ||||||||||
| 32 | element => $declaration, | ||||||||||
| 33 | ); | ||||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 3 | 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. | ||||||||||