File Coverage

blib/lib/PPIx/EditorTools/FindUnmatchedBrace.pm
Criterion Covered Total %
statement 28 30 93.3
branch 1 4 25.0
condition 0 3 0.0
subroutine 8 8 100.0
pod 1 1 100.0
total 38 46 82.6


line stmt bran cond sub pod time code
1             package PPIx::EditorTools::FindUnmatchedBrace;
2              
3             # ABSTRACT: PPI-based unmatched-brace-finder
4              
5 2     2   299294 use 5.008;
  2         8  
  2         78  
6 2     2   11 use strict;
  2         5  
  2         62  
7 2     2   11 use warnings;
  2         4  
  2         54  
8 2     2   10 use Carp;
  2         4  
  2         143  
9              
10 2     2   10 use base 'PPIx::EditorTools';
  2         4  
  2         835  
11 2     2   21 use Class::XSAccessor accessors => {};
  2         4  
  2         19  
12              
13 2     2   250 use PPI;
  2         5  
  2         454  
14              
15             our $VERSION = '0.18';
16              
17             =pod
18              
19             =head1 NAME
20              
21             PPIx::EditorTools::FindUnmatchedBrace - PPI-based unmatched-brace-finder
22              
23             =head1 SYNOPSIS
24              
25             my $brace = PPIx::EditorTools::FindUnmatchedBrace->new->find(
26             code => "package TestPackage;\nsub x { 1;\n"
27             );
28             my $location = $brace->element->location;
29              
30             =head1 DESCRIPTION
31              
32             Finds the location of unmatched braces in a C.
33              
34             =head1 METHODS
35              
36             =over 4
37              
38             =item new()
39              
40             Constructor. Generally shouldn't be called with any arguments.
41              
42             =item find( ppi => PPI::Document $ppi )
43             =item find( code => Str $code )
44              
45             Accepts either a C to process or a string containing
46             the code (which will be converted into a C) to process.
47             Finds the location of unmatched braces. Returns a
48             C with the unmatched brace (a
49             C) available via the C accessor.
50             If there is no unmatched brace, returns undef.
51              
52             =back
53              
54             =cut
55              
56             sub find {
57 2     2 1 684 my ( $self, %args ) = @_;
58 2         18 $self->process_doc(%args);
59              
60 2         10 my $ppi = $self->ppi;
61              
62 2         17 my $where = $ppi->find( \&PPIx::EditorTools::find_unmatched_brace );
63 2 50       31 if ($where) {
64 0 0 0     0 @$where = sort {
65 2         7 PPIx::EditorTools::element_depth($b) <=> PPIx::EditorTools::element_depth($a)
66             or $a->location->[0] <=> $b->location->[0]
67             or $a->location->[1] <=> $b->location->[1]
68             } @$where;
69              
70 2         19 return PPIx::EditorTools::ReturnObject->new(
71             ppi => $ppi,
72             element => $where->[0]
73             );
74             }
75 0           return;
76             }
77              
78             1;
79              
80             __END__