| 1 | package Neovim::RPC::API; | ||
| 2 | 4 | our $AUTHORITY = 'cpan:YANICK'; | |
| 3 | 4 | $Neovim::RPC::API::VERSION = '0.2.0'; | |
| 4 | 4 | ▶ | use 5.10.0; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 4 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 4 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 4 | ▶ | use Moose::Role; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | 4 | ▶ | use Neovim::RPC::API::Command; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | |||
| 13 | 4 | ▶ | use List::AllUtils qw/ any /; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | |||
| 15 | 4 | with 'MooseX::Role::Loggable'; | |
| 16 | |||
| 17 | 4 | has "rpc" => ( | |
| 18 | isa => 'Neovim::RPC', | ||
| 19 | is => 'ro', | ||
| 20 | required => 1, | ||
| 21 | ); | ||
| 22 | |||
| 23 | 4 | has channel_id => ( | |
| 24 | is => 'rw', | ||
| 25 | isa => 'Int', | ||
| 26 | ); | ||
| 27 | |||
| 28 | has commands => ( | ||
| 29 | traits => [ 'Array' ], | ||
| 30 | is => 'ro', | ||
| 31 | isa => 'ArrayRef', | ||
| 32 | 0 | ▶ | default => sub { [] }, |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 33 | 4 | handles => { | |
| 34 | _push_command => 'push', | ||
| 35 | all_commands => 'elements', | ||
| 36 | }, | ||
| 37 | ); | ||
| 38 | |||
| 39 | has types => ( | ||
| 40 | is => 'ro', | ||
| 41 | 0 | ▶ | default => sub{ {} }, |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 42 | 4 | ); | |
| 43 | |||
| 44 | sub print_commands { | ||
| 45 | 0 | ▶ | my( $self ) = @_; |
|
Subroutine
print_commands: not called CC=2 SCAR=17.9
Pod
undocumented
| |||
| 46 | |||
| 47 | 0 | for my $c ( sort { $a->name cmp $b->name } $self->all_commands ) { | |
| 48 | 0 | say $c->name, ' ( ', join( ', ', map { join ' ', @$_ } $c->all_parameters ) , ' ) -> ', | |
| 49 | $c->return_type; | ||
| 50 | } | ||
| 51 | |||
| 52 | } | ||
| 53 | |||
| 54 | sub has_command { | ||
| 55 | 0 | ▶ | my( $self, $command ) = @_; |
|
Subroutine
has_command: not called CC=1 SCAR=6.9
Pod
undocumented
| |||
| 56 | 0 | ▶ | return any { $_->{name} eq $command } $self->all_commands; |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 57 | } | ||
| 58 | |||
| 59 | sub add_command { | ||
| 60 | 0 | ▶ | my( $self, $command ) = @_; |
|
Subroutine
add_command: not called CC=1 SCAR=6.9
Pod
undocumented
| |||
| 61 | |||
| 62 | 0 | $command->{api} = $self; | |
| 63 | |||
| 64 | 0 | my $c = Neovim::RPC::API::Command->new($command); | |
| 65 | 0 | $self->_push_command($c); | |
| 66 | |||
| 67 | $self->meta->add_method( $c->name => sub { | ||
| 68 | 0 | ▶ | shift; |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 69 | 0 | my @args = @_; | |
| 70 | |||
| 71 | 0 | my $struct = $c->args_to_struct(@args); | |
| 72 | |||
| 73 | 0 | $self->rpc->request( $c->name => $struct); | |
| 74 | }) | ||
| 75 | |||
| 76 | 0 | } | |
| 77 | |||
| 78 | |||
| 79 | sub export_dsl { | ||
| 80 | 0 | ▶ | my $self = shift; |
|
Subroutine
export_dsl: not called CC=2 SCAR=17.9
Pod
documented
| |||
| 81 | 0 | my $interactive = shift; | |
| 82 | |||
| 83 | 0 | my $class = caller; | |
| 84 | |||
| 85 | 0 | for my $command ( $self->all_commands ) { | |
| 86 | 0 | my $name = $command->name; | |
| 87 | |||
| 88 | 0 | eval qq! | |
| 89 | sub ${class}::$name { | ||
| 90 | my \$p =\$self->$name(\@_); | ||
| 91 | return unless \$interactive; | ||
| 92 | \$self->rpc->loop(\$p); | ||
| 93 | \$p->is_done ? \$p->get : warn \$p->failure; | ||
| 94 | } | ||
| 95 | !; | ||
| 96 | } | ||
| 97 | |||
| 98 | } | ||
| 99 | |||
| 100 | |||
| 101 | 4 | 1; | |
| 102 | |||
| 103 | __END__ | ||
| 104 | |||
| 105 | =pod | ||
| 106 | |||
| 107 | =encoding UTF-8 | ||
| 108 | |||
| 109 | =head1 NAME | ||
| 110 | |||
| 111 | Neovim::RPC::API | ||
| 112 | |||
| 113 | =head1 VERSION | ||
| 114 | |||
| 115 | version 0.2.0 | ||
| 116 | |||
| 117 | =head1 METHODS | ||
| 118 | |||
| 119 | =head2 export_dsl( $interactive ) | ||
| 120 | |||
| 121 | Exports all the api commands as functions in the current namespace. | ||
| 122 | |||
| 123 | $rpc->api->export_dsl; | ||
| 124 | |||
| 125 | vim_set_current_line( "hello there!" ); | ||
| 126 | |||
| 127 | If C<$interactive> is set to C<true>, the function will also | ||
| 128 | C<$rpc->loop()> until the response is received from the editor. | ||
| 129 | The result of the response promise will then be returned (or, if there was a problem, the error message will be I<warn>ed). | ||
| 130 | |||
| 131 | $rpc->api->export_dsl(1); | ||
| 132 | |||
| 133 | vim_get_current_line->on_done( | ||
| 134 | vim_set_current_line( scalar reverse shift ); | ||
| 135 | ); | ||
| 136 | |||
| 137 | =head1 AUTHOR | ||
| 138 | |||
| 139 | Yanick Champoux <yanick@cpan.org> | ||
| 140 | |||
| 141 | =head1 COPYRIGHT AND LICENSE | ||
| 142 | |||
| 143 | This software is copyright (c) 2015 by Yanick Champoux. | ||
| 144 | |||
| 145 | This is free software; you can redistribute it and/or modify it under | ||
| 146 | the same terms as the Perl 5 programming language system itself. | ||
| 147 | |||
| 148 | =cut | ||