| 1 | package Text::Snippet; | ||||||||||||
| 2 | 9 | ▶ | BEGIN { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 9 | $Text::Snippet::VERSION = '0.04'; | |||||||||||
| 4 | } | ||||||||||||
| 5 | |||||||||||||
| 6 | # ABSTRACT: TextMate-like snippet functionality | ||||||||||||
| 7 | |||||||||||||
| 8 | 9 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 9 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 9 | ▶ | use Text::Balanced qw(extract_bracketed extract_multiple); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 9 | ▶ | use Text::Snippet::TabStop::Parser; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 9 | ▶ | use Text::Snippet::TabStop::Cursor; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 9 | ▶ | use Scalar::Util qw(blessed); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 9 | ▶ | use Carp qw(croak); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | |||||||||||||
| 16 | |||||||||||||
| 17 | 24 | ▶ | sub _new { | ||||||||||
|
Subroutine
_new: called CC=3 SCAR=11.3
| |||||||||||||
| 18 | 24 | my $class = shift; | |||||||||||
| 19 | 24 | ▶ | my $args = ref($_[0]) ? shift : {@_}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 20 | 24 | my $self = { | |||||||||||
| 21 | chunks => [], | ||||||||||||
| 22 | tab_stops => [], | ||||||||||||
| 23 | %$args | ||||||||||||
| 24 | }; | ||||||||||||
| 25 | 24 | ▶ | croak "no src attribute specified" unless defined($self->{src}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 26 | 24 | return bless $self, $class; | |||||||||||
| 27 | } | ||||||||||||
| 28 | 24 | ▶ | sub parse { | ||||||||||
|
Subroutine
parse: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||
| 29 | 24 | my $class = shift; | |||||||||||
| 30 | 24 | my $source = shift; | |||||||||||
| 31 | my @raw = extract_multiple( $source, [ { Simple => qr/\$\d+/ }, | ||||||||||||
| 32 | 73 | ▶ | { Curly => sub { extract_bracketed( $_[0], '{}', '\$(?=\{\d)' ) } }, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 33 | 24 | { Plain => qr/[^\$]+/ }, | |||||||||||
| 34 | ], undef, 1); | ||||||||||||
| 35 | |||||||||||||
| 36 | 24 | my %tab_stop_cache; | |||||||||||
| 37 | my @chunks; | ||||||||||||
| 38 | 24 | foreach my $c (@raw) { | |||||||||||
| 39 | 100 | ▶ | if ( ref($c) eq 'Plain' ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 40 | 54 | push( @chunks, $$c ); | |||||||||||
| 41 | } else { | ||||||||||||
| 42 | |||||||||||||
| 43 | # the leading $ gets stripped on these by extract_bracketed... | ||||||||||||
| 44 | 46 | ▶ | $$c = '$' . $$c if(ref($c) eq 'Curly'); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 45 | |||||||||||||
| 46 | 46 | my $t = Text::Snippet::TabStop::Parser->parse( $$c ); | |||||||||||
| 47 | |||||||||||||
| 48 | 46 | ▶ | if ( exists( $tab_stop_cache{ $t->index } ) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 49 | 11 | $t->parent($tab_stop_cache{ $t->index }); | |||||||||||
| 50 | } else { | ||||||||||||
| 51 | 35 | $tab_stop_cache{ $t->index } = $t; | |||||||||||
| 52 | } | ||||||||||||
| 53 | 46 | push( @chunks, $t ); | |||||||||||
| 54 | } | ||||||||||||
| 55 | } | ||||||||||||
| 56 | |||||||||||||
| 57 | 24 | my @tab_stops = map { $tab_stop_cache{$_} } sort { $a <=> $b } keys %tab_stop_cache; | |||||||||||
| 58 | |||||||||||||
| 59 | 24 | ▶ | if ( exists( $tab_stop_cache{'0'} ) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 60 | # put the zero-th tab stop on the end of the array | ||||||||||||
| 61 | 0 | push( @tab_stops, shift(@tab_stops) ); | |||||||||||
| 62 | } else { | ||||||||||||
| 63 | # append the implicit zero-th tab stop on the end of the array | ||||||||||||
| 64 | 24 | my $implicit = Text::Snippet::TabStop::Parser->parse( '$0' ); | |||||||||||
| 65 | 24 | push( @tab_stops, $implicit ); | |||||||||||
| 66 | 24 | push( @chunks, $implicit ); | |||||||||||
| 67 | } | ||||||||||||
| 68 | |||||||||||||
| 69 | 24 | my %params = ( | |||||||||||
| 70 | src => $source, | ||||||||||||
| 71 | chunks => \@chunks, | ||||||||||||
| 72 | tab_stops => \@tab_stops, | ||||||||||||
| 73 | ); | ||||||||||||
| 74 | 24 | return $class->_new(%params); | |||||||||||
| 75 | } | ||||||||||||
| 76 | |||||||||||||
| 77 | |||||||||||||
| 78 | |||||||||||||
| 79 | 9 | ▶ | use overload '""' => sub { shift->to_string }, fallback => 1; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 80 | |||||||||||||
| 81 | 24 | ▶ | sub to_string { | ||||||||||
|
Subroutine
to_string: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||
| 82 | 24 | my $self = shift; | |||||||||||
| 83 | 24 | ▶ | return join( '', @{ $self->chunks } ) || ''; | ||||||||||
|
Condition50%1/2
join('', @{$self->chunks;}) || ''
MC/DC0%0/1
join('', @{$self->chunks;}) || ''
join('', @{$self->chunks;})
Truth table50%1/2
A
A= join('', @{$self->chunks;})
| |||||||||||||
| 84 | } | ||||||||||||
| 85 | |||||||||||||
| 86 | 9 | ▶ | use Class::XSAccessor getters => { src => 'src', tab_stops => 'tab_stops', chunks => 'chunks' }; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 87 | |||||||||||||
| 88 | |||||||||||||
| 89 | 3 | ▶ | sub cursor { | ||||||||||
|
Subroutine
cursor: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 90 | 3 | my $self = shift; | |||||||||||
| 91 | 3 | return Text::Snippet::TabStop::Cursor->new( snippet => $self ); | |||||||||||
| 92 | } | ||||||||||||
| 93 | |||||||||||||
| 94 | |||||||||||||
| 95 | 9 | 1; | |||||||||||
| 96 | |||||||||||||
| 97 | __END__ | ||||||||||||