lib/Neovim/RPC/Plugin/FlipOperator.pm

filter: Statementstmt 41.1% 21 / 51 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 50.0% 6 / 12 Podpod 0.0% 0 / 3 totaltotal 37.5% 27 / 72 CC 4 SCAR 14.0
CC
4
Cov
37%
flip_snippet
12.0
block_to_postifx
6.0
__ANON__
2.0
CRAP 4.1
« lib/Neovim/RPC/API/Command.pm ↑ Summary lib/Neovim/RPC.pm »
1 package Neovim::RPC::Plugin::FlipOperator;
2 1 our $AUTHORITY = 'cpan:YANICK';
3 1 $Neovim::RPC::Plugin::FlipOperator::VERSION = '1.0.1';
4 1 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 1 ▶use Neovim::RPC::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Promises qw/ deferred collect_hash /;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use experimental 'postderef', 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 subscribe flip_operator => #rpcrequest
17 0 ▶ sub( $self, $event ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
18 0 my( $from, $to, $code ) = $event->all_params;
19  
20 collect_hash(
21 from => $from,
22 to => $to,
23 code => [ split "\n", $self->flip_snippet(join "\n", @$code ) ],
24 0 ▶ buffer_id => $self->rpc->api->vim_get_current_buffer->then( sub { ord $_[0]->data } )
Subroutine __ANON__: not called CC=1 SCAR=6.9
25 0 );
26 },
27 0 ▶ sub ($self,%c) {
Subroutine __ANON__: not called CC=1 SCAR=1.2
28 $self->rpc->api->buffer_set_lines(
29 $c{buffer_id}, $c{from}-1, $c{to}, 1, $c{code}
30 )
31 0 };
32  
33 0 ▶sub flip_snippet($self,$snippet) {
Subroutine flip_snippet: not called CC=3 SCAR=24.8
Pod undocumented
34  
35  
36 0 $snippet =~ s/^\s*\n//gm;
37 0 my ($indent) = $snippet =~ /^(\s*)/;
38 0 $snippet =~ s/^$indent//gm;
39  
40 0 my $operators = join '|', qw/ if unless while for until /;
41  
42 0 my $block_re = qr/
43 ^
44 (?<operator>$operators)
45 \s* (?:my \s+ (?<variable>\$\w+) \s* )?
46 \( \s* (?<array>[^)]+) \) \s* {
47 (?<inner>.*)
48 }
49 \s* $
50 /xs;
51  
52 0 my $postfix_re = qr/
53 ^
54 (?<inner>[^;]+?)
55 \s+ (?<operator>$operators)
56 \s+ (?<array>[^;]+?)
57 \s* ;
58 $
59 /xs;
60  
61 0 ▶ if ( $snippet =~ $block_re ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($snippet =~ /$block_re/u) { } 0 0 0
elsif ($snippet =~ /$postfix_re/u) { } 0 0 0
62 0 $snippet = block_to_postifx( $snippet, %+ );
63 }
64 elsif( $snippet =~ $postfix_re ) {
65 0 $snippet = postfix_to_block( $snippet, %+ );
66 }
67  
68 0 warn $snippet;
69 0 $snippet =~ s/^\s*?\n//;
70  
71 0 $snippet =~ s/^/$indent/gm;
72  
73 0 return $snippet;
74 }
75  
76 0 ▶sub postfix_to_block {
Subroutine postfix_to_block: not called CC=1 SCAR=6.9
Pod undocumented
77 0 my( $snippet, %capture ) = @_;
78  
79 0 $snippet = $capture{inner};
80 0 chomp $capture{array};
81 0 $snippet = "$capture{operator} ( $capture{array} ) {\n $snippet\n}";
82  
83 }
84  
85 0 ▶sub block_to_postifx {
Subroutine block_to_postifx: not called CC=2 SCAR=17.9
Pod undocumented
86 0 my( $snippet, %capture ) = @_;
87  
88 # more than one statement? Don't touch it
89 0 ▶ return $snippet if $capture{inner} =~ /(;)/ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $capture{"inner"} =~ /(;)/u > 1 0 0 0
90  
91 0 $snippet = $capture{inner};
92 0 $snippet =~ s/;\s*$//;
93  
94 0 $snippet =~ s/\Q$capture{variable}/\$_/g;
95 0 $snippet =~ s/\$_\s*=~\s*//g;
96  
97 0 $capture{array} =~ s/\s*$//;
98  
99 0 $snippet .= " $capture{operator} $capture{array};";
100  
101 0 return $snippet;
102 }
103  
104 1 1;
105  
106 __END__
« lib/Neovim/RPC/API/Command.pm ↑ Summary lib/Neovim/RPC.pm »