| 1 | 2 | ▶ | use 5.10.0; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | |||||||||||||||
| 5 | package Syntax::Feature::Qi; | ||||||||||||||
| 6 | |||||||||||||||
| 7 | # ABSTRACT: Remove the same indendation from all lines in a string | ||||||||||||||
| 8 | 2 | our $AUTHORITY = 'cpan:CSSON'; # AUTHORITY | |||||||||||||
| 9 | 2 | our $VERSION = '1.0000'; | |||||||||||||
| 10 | |||||||||||||||
| 11 | 2 | ▶ | use Devel::Declare(); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 2 | ▶ | use B::Hooks::EndOfScope; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 2 | ▶ | use Sub::Install qw/install_sub/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 2 | ▶ | use Devel::Declare::Context::Simple; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | 2 | ▶ | use namespace::clean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | |||||||||||||||
| 17 | 2 | my %quote_op = qw(qi q qqi qq); | |||||||||||||
| 18 | 2 | my @new_ops = keys %quote_op; | |||||||||||||
| 19 | |||||||||||||||
| 20 | 1 | ▶ | sub install { | ||||||||||||
|
Subroutine
install: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 21 | 1 | my $class = shift; | |||||||||||||
| 22 | 1 | my %args = @_; | |||||||||||||
| 23 | |||||||||||||||
| 24 | 1 | my $target = $args{'into'}; | |||||||||||||
| 25 | |||||||||||||||
| 26 | Devel::Declare->setup_for($target => { | ||||||||||||||
| 27 | map { | ||||||||||||||
| 28 | 1 | my $name = $_; | |||||||||||||
| 29 | ($name => { | ||||||||||||||
| 30 | 17 | ▶ | const => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 31 | 17 | my $context = Devel::Declare::Context::Simple->new; | |||||||||||||
| 32 | 17 | $context->init(@_); | |||||||||||||
| 33 | 17 | return $class->_transform($name, $context); | |||||||||||||
| 34 | }, | ||||||||||||||
| 35 | 2 | }); | |||||||||||||
| 36 | } @new_ops | ||||||||||||||
| 37 | }); | ||||||||||||||
| 38 | 1 | foreach my $name (@new_ops) { | |||||||||||||
| 39 | 2 | install_sub { | |||||||||||||
| 40 | into => $target, | ||||||||||||||
| 41 | as => $name, | ||||||||||||||
| 42 | code => $class->_run_callback, | ||||||||||||||
| 43 | }; | ||||||||||||||
| 44 | } | ||||||||||||||
| 45 | 1 | ▶ | on_scope_end { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 46 | 1 | namespace::clean->clean_subroutines($target, @new_ops); | |||||||||||||
| 47 | 1 | }; | |||||||||||||
| 48 | 1 | return 1; | |||||||||||||
| 49 | } | ||||||||||||||
| 50 | |||||||||||||||
| 51 | 2 | ▶ | sub _run_callback { | ||||||||||||
|
Subroutine
_run_callback: called CC=1 SCAR=0.0
| |||||||||||||||
| 52 | |||||||||||||||
| 53 | 17 | ▶ | return sub ($) { | ||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||
| 54 | 17 | my $string = shift; | |||||||||||||
| 55 | 17 | ▶ | return $string if $string =~ m{\A\s*\Z}ms; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 56 | |||||||||||||||
| 57 | 16 | ▶ | my $remove_indent = $string =~ m{\A(\h*)\S} ? $1 | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 58 | : $string =~ m{\A\s*\n(\h*)\S} ? $1 | ||||||||||||||
| 59 | : '' | ||||||||||||||
| 60 | ; | ||||||||||||||
| 61 | 16 | $string =~ s{^$remove_indent}{}gms; | |||||||||||||
| 62 | 16 | return $string; | |||||||||||||
| 63 | 2 | }; | |||||||||||||
| 64 | } | ||||||||||||||
| 65 | |||||||||||||||
| 66 | 17 | ▶ | sub _transform { | ||||||||||||
|
Subroutine
_transform: called CC=1 SCAR=0.0
| |||||||||||||||
| 67 | 17 | my $class = shift; | |||||||||||||
| 68 | 17 | my $name = shift; | |||||||||||||
| 69 | 17 | my $ctx = shift; | |||||||||||||
| 70 | |||||||||||||||
| 71 | 17 | $ctx->skip_declarator; | |||||||||||||
| 72 | 17 | my $length = Devel::Declare::toke_scan_str($ctx->offset); | |||||||||||||
| 73 | 17 | my $string = Devel::Declare::get_lex_stuff; | |||||||||||||
| 74 | 17 | Devel::Declare::clear_lex_stuff; | |||||||||||||
| 75 | 17 | my $linestr = $ctx->get_linestr; | |||||||||||||
| 76 | 17 | my $quoted = substr $linestr, $ctx->offset, $length; | |||||||||||||
| 77 | 17 | my $spaced = ''; | |||||||||||||
| 78 | 17 | $quoted =~ m{^(\s*)}sm; | |||||||||||||
| 79 | 17 | $spaced = $1; | |||||||||||||
| 80 | my $new = sprintf '(%s)', join '', | ||||||||||||||
| 81 | 17 | $quote_op{$name}, | |||||||||||||
| 82 | $spaced, | ||||||||||||||
| 83 | substr($quoted, length($spaced), 1), | ||||||||||||||
| 84 | $string, | ||||||||||||||
| 85 | substr($quoted, -1, 1); | ||||||||||||||
| 86 | 17 | substr($linestr, $ctx->offset, $length) = $new; | |||||||||||||
| 87 | 17 | $ctx->set_linestr($linestr); | |||||||||||||
| 88 | 17 | return 1; | |||||||||||||
| 89 | } | ||||||||||||||
| 90 | |||||||||||||||
| 91 | 2 | 1; | |||||||||||||
| 92 | |||||||||||||||
| 93 | __END__ | ||||||||||||||