| 1 | package Neovim::RPC::Plugin; | ||||||||||||||||||||||||||||
| 2 | 5 | our $AUTHORITY = 'cpan:YANICK'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: base role for Neovim::RPC plugins | ||||||||||||||||||||||||||||
| 4 | 5 | $Neovim::RPC::Plugin::VERSION = '1.0.1'; | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 5 | ▶ | use MooseX::ClassAttribute; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 5 | ▶ | use Moose::Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 5 | ▶ | use Moose::Util qw/ apply_all_roles /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 5 | ▶ | use Promises qw/ deferred /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 5 | Moose::Exporter->setup_import_methods( | |||||||||||||||||||||||||||
| 21 | also => [ 'Moose' ], | ||||||||||||||||||||||||||||
| 22 | with_meta => [ 'subscribe' ], | ||||||||||||||||||||||||||||
| 23 | as_is => [ 'rpcrequest', 'accumulate_responses' ], | ||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 5 | ▶ | use experimental 'signatures', 'postderef'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | class_has subscriptions => ( | ||||||||||||||||||||||||||||
| 29 | is => 'ro', | ||||||||||||||||||||||||||||
| 30 | 4 | ▶ | default => sub { +{} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 5 | ); | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | has rpc => ( | ||||||||||||||||||||||||||||
| 34 | is => 'ro', | ||||||||||||||||||||||||||||
| 35 | required => 1, | ||||||||||||||||||||||||||||
| 36 | handles => [ 'api' ], | ||||||||||||||||||||||||||||
| 37 | 0 | ▶ | trigger => sub { $_[0]->register }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 38 | 5 | ); | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | sub register($self) { | ||||||||||||||||||||||||||
|
Subroutine
register: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 41 | 0 | ▶ | while( my( $event, $chain ) = each $self->subscriptions->%* ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 0 | $self->rpc->subscribe( $event => $self->expand_subscription_chain(@$chain) ); | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | sub expand_subscription_chain($self,@chain) { | ||||||||||||||||||||||||||
|
Subroutine
expand_subscription_chain: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | return sub($event) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 48 | 0 | my $d = deferred; | |||||||||||||||||||||||||||
| 49 | 0 | $d->resolve($event); | |||||||||||||||||||||||||||
| 50 | 0 | my $current = $d; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | for my $next ( @chain ) { | |||||||||||||||||||||||||||
| 53 | 0 | ▶ | if( ref $next eq 'CODE' ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | $current = $current->then(sub{ | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 55 | 0 | $next->($self,@_); | |||||||||||||||||||||||||||
| 56 | }) | ||||||||||||||||||||||||||||
| 57 | 0 | } | |||||||||||||||||||||||||||
| 58 | elsif( ref $next eq 'ARRAY' ) { | ||||||||||||||||||||||||||||
| 59 | $current = $current->then( | ||||||||||||||||||||||||||||
| 60 | 0 | ▶ | sub{ $next->[0]($self,@_) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | sub{ $next->[1]($self,@_) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 62 | ) | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | } | |||||||||||||||||||||||||||
| 65 | elsif( ref $next eq 'HASH' ) { | ||||||||||||||||||||||||||||
| 66 | 0 | ▶ | die "hash can only have exactly one key\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | unless 1 == keys %$next; | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | my( $method, $sub ) = %$next; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | $current = $current->$method( | ||||||||||||||||||||||||||||
| 72 | 0 | ▶ | sub{ $sub->($self,@_) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 73 | ) | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | } | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | return $current; | |||||||||||||||||||||||||||
| 79 | 0 | }; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 5 | ▶ | sub subscribe($self, $name, @chain) { | ||||||||||||||||||||||||||
|
Subroutine
subscribe: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | 5 | $self->name->subscriptions->{$name} = \@chain; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 4 | ▶ | sub init_meta { | ||||||||||||||||||||||||||
|
Subroutine
init_meta: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 87 | 4 | shift; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 4 | my $meta = Moose->init_meta(@_); | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 4 | apply_all_roles( $meta->name, __PACKAGE__ ); | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 4 | return $meta; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 2 | ▶ | sub rpcrequest { | ||||||||||||||||||||||||||
|
Subroutine
rpcrequest: called CC=1 SCAR=2.0
Pod
documented
| |||||||||||||||||||||||||||||
| 98 | 2 | my @chain = @_; | |||||||||||||||||||||||||||
| 99 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 100 | 0 | my( $self, $event ) = @_; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | $self->expand_subscription_chain( | ||||||||||||||||||||||||||||
| 103 | @chain, | ||||||||||||||||||||||||||||
| 104 | 0 | ▶ | { finally => sub { $event->resp('ok') } } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 105 | 0 | )->($event); | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | 2 | } | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | ▶ | sub accumulate_responses( @subs ) { | ||||||||||||||||||||||||||
|
Subroutine
accumulate_responses: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | sub($self,@args) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 112 | 0 | my $promise = deferred; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | my @return =$self->expand_subscription_chain(@subs)->(@args); | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | if( @return == 1 and eval { $return[0]->isa('Promises::Promise') } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@return == 1 and eval {
do {
$return[0]->isa("Promises::Promise")
}
}
MC/DC0%0/2
@return == 1 and eval {
do {
$return[0]->isa("Promises::Promise")
}
}
@return == 1
eval {
do {
$return[0]->isa("Promises::Promise")
}
}
Truth table0%0/3
A and B
A= @return == 1B= eval {
do {
$return[0]->isa("Promises::Promise")
}
}
| |||||||||||||||||||||||||||||
| 117 | $return[0]->then( | ||||||||||||||||||||||||||||
| 118 | 0 | ▶ | sub{ $promise->resolve(@args, [ @_ ]) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 119 | 0 | ▶ | sub{ $promise->reject(@_) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 120 | 0 | ); | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | else { | ||||||||||||||||||||||||||||
| 123 | 0 | $promise->resolve(@args, \@return); | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 0 | return $promise; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | 0 | } | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 5 | 1; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | __END__ | ||||||||||||||||||||||||||||