Ext/Plugin/Host.pm

filter: Statementstmt 15.8% 16 / 101 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 16.6% 4 / 24 Podpod 100% 4 / 4 totaltotal 16.0% 24 / 150 CC 15 SCAR 28.0
CC
15
Cov
13%
_load
56.0
_unload
30.0
new
6.0
CRAP 16.4
↑ Summary Ext/LuaFuncs.pm »
1 package Neovim::Ext::Plugin::Host;
2 56 $Neovim::Ext::Plugin::Host::VERSION = '0.01';
3 56 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 56 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 56 ▶use base qw/Class::Accessor/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 56 ▶use Scalar::Util qw/weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 56 __PACKAGE__->mk_accessors (qw/nvim _loaded _load_errors _specs notification_handlers request_handlers/);
8  
9  
10 56 our $lastPackage;
11  
12 sub register
13 {
14 0 ▶ my ($this, $package) = @_;
Subroutine register: not called CC=1 SCAR=6.9
Pod documented
15 0 $lastPackage = $package;
16 }
17  
18  
19  
20 sub new
21 {
22 0 ▶ my ($this, $nvim) = @_;
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
23  
24 0 ▶ my $class = ref ($this) || $this;
Condition0%0/3
ref $this || $this
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table0%0/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
25 0 my $self =
26 {
27 nvim => $nvim,
28 _loaded => {},
29 _load_errors => {},
30 _specs => {},
31 };
32  
33 0 my $obj = bless $self, $class;
34  
35 0 my $host = $obj;
36 0 weaken ($host);
37  
38 $obj->notification_handlers
39 ({
40 nvim_error_event => sub
41 {
42 0 ▶ $host->_on_error_event (@_);
Subroutine __ANON__: not called CC=1 SCAR=6.9
43 },
44 0 });
45 $obj->request_handlers
46 ({
47 0 ▶ poll => sub { return 'ok' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
48 0 ▶ specs => sub { $host->_on_specs_request (@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 0 });
50  
51 0 return $obj;
52 }
53  
54  
55  
56 sub start
57 {
58 0 ▶ my ($this, @plugins) = @_;
Subroutine start: not called CC=1 SCAR=6.9
Pod documented
59  
60 0 my $host = $this;
61 0 weaken ($host);
62  
63 $this->nvim->run_loop
64 (
65 0 ▶ sub { $host->_on_request (@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 0 ▶ sub { $host->_on_notification (@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
67 0 ▶ sub { $host->_load (@plugins) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
68 0 );
69 }
70  
71  
72  
73 sub shutdown
74 {
75 0 ▶ my ($this) = @_;
Subroutine shutdown: not called CC=1 SCAR=6.9
Pod documented
76  
77 0 $this->_unload();
78 0 $this->nvim->stop_loop();
79 }
80  
81  
82  
83 sub _make_rpc_method
84 {
85 0 ▶ my ($path, $handler) = @_;
Subroutine _make_rpc_method: not called CC=2 SCAR=17.9
86  
87 0 my $method = join (':', $path, $handler->{type}, $handler->{name});
88 0 ▶ if ($handler->{options}{pattern})
Branch: 1 branch
BranchTrueFalseTotal
if ($handler->{"options"}{"pattern"}) 0 0 0
89 {
90 0 $method .= ":$handler->{options}{pattern}";
91 }
92  
93 0 return $method;
94 }
95  
96  
97  
98 sub _load
99 {
100 0 ▶ my ($this, @plugins) = @_;
Subroutine _load: not called CC=7 SCAR=40.3
101  
102 0 foreach my $path (@plugins)
103 {
104 0 ▶ next if (exists ($this->_loaded->{$path}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $this->_loaded->{$path} 0 0 0
105  
106 eval
107 0 {
108 0 require $path;
109 0 my $module = $lastPackage;
110 0 my $plugin = $module->new ($this->nvim, $this);
111  
112 0 $this->_specs->{$path} = $module->get_specs();
113  
114 0 my @handlers =
115 (
116 $module->get_commands(),
117 $module->get_functions(),
118 $module->get_autocmds(),
119 );
120  
121 # Module may not export any handlers
122 0 ▶ next if (scalar (@handlers) == 0);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @handlers == 0 0 0 0
123  
124 0 foreach my $handler (@handlers)
125 {
126 0 my $method = _make_rpc_method ($path, $handler);
127  
128 my $sub = sub
129 {
130 0 ▶ $handler->{symbol}->($plugin, @_);
Subroutine __ANON__: not called CC=1 SCAR=6.9
131 0 };
132  
133 0 ▶ if ($handler->{options}{sync})
Branch: 1 branch
BranchTrueFalseTotal
if ($handler->{'options'}{'sync'}) { } 0 0 0
134 {
135 0 $this->request_handlers->{$method} = $sub;
136 }
137 else
138 {
139 0 $this->notification_handlers->{$method} = $sub;
140 }
141 }
142  
143 0 $this->_loaded->{$path} =
144 {
145 handlers => \@handlers,
146 package => $module,
147 };
148 };
149  
150 0 ▶ if ($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
151 {
152 0 $this->_load_errors->{$path} = "Could not load plugin '$path': $@";
153 }
154 }
155  
156 $this->nvim->api->set_client_info
157 (
158 0 'perl-rplugin-host', {}, 'host',
159 {
160 poll => {},
161 specs => { nargs => 1 },
162 },
163 {
164 license => 'perl5',
165 website => 'https://github.com/jacquesg/p5-Neovim',
166 },
167 async_ => 1
168 );
169 }
170  
171  
172  
173 sub _unload
174 {
175 0 ▶ my ($this) = @_;
Subroutine _unload: not called CC=5 SCAR=34.0
176  
177 0 foreach my $path (keys %{$this->_loaded})
178 {
179 0 my $plugin = $this->_loaded->{$path};
180  
181 0 foreach my $hook ($plugin->{package}->get_shutdown_hooks())
182 {
183 0 $hook->();
184 }
185  
186 0 foreach my $handler (@{$plugin->{handlers}})
187 {
188 0 my $method = _make_rpc_method ($path, $handler);
189  
190 0 ▶ if ($handler->{options}{sync})
Branch: 1 branch
BranchTrueFalseTotal
if ($handler->{'options'}{'sync'}) { } 0 0 0
191 {
192 0 delete $this->request_handlers->{$method};
193 }
194 else
195 {
196 0 delete $this->notification_handlers->{$method};
197 }
198 }
199 }
200  
201 0 $this->_specs ({});
202 0 $this->_loaded ({});
203 }
204  
205  
206  
207 sub _on_error_event
208 {
209 0 ▶ my ($this, $kind, $msg) = @_;
Subroutine _on_error_event: not called CC=1 SCAR=6.9
210  
211 0 $this->nvim->err_write ("Async request cause and error: $msg\n",
212 async_ => 1);
213 }
214  
215  
216  
217 sub _on_async_err
218 {
219 0 ▶ my ($this, $msg) = @_;
Subroutine _on_async_err: not called CC=1 SCAR=6.9
220  
221 0 $this->nvim->err_write ($msg, async_ => 1);
222 }
223  
224  
225  
226 sub _on_specs_request
227 {
228 0 ▶ my ($this, $path) = @_;
Subroutine _on_specs_request: not called CC=1 SCAR=6.9
229 0 return $this->_specs->{$path};
230 }
231  
232  
233  
234 sub _on_request
235 {
236 0 ▶ my ($this, $name, $args) = @_;
Subroutine _on_request: not called CC=2 SCAR=17.9
237  
238 0 ▶ if (!exists ($this->request_handlers->{$name}))
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $this->request_handlers->{$name}) 0 0 0
239 {
240 0 my $msg = $this->_missing_handler_error ($name, 'request');
241 0 return;
242 }
243  
244 0 my $handler = $this->request_handlers->{$name};
245 0 return $handler->(@$args);
246 }
247  
248  
249  
250 sub _on_notification
251 {
252 0 ▶ my ($this, $name, $args) = @_;
Subroutine _on_notification: not called CC=2 SCAR=17.9
253  
254 0 ▶ if (!exists ($this->notification_handlers->{$name}))
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $this->notification_handlers->{$name}) 0 0 0
255 {
256 0 my $msg = $this->_missing_handler_error ($name, 'notification');
257 0 $this->_on_async_err ($msg."\n");
258 0 return;
259 }
260  
261 0 my $handler = $this->notification_handlers->{$name};
262 0 return $handler->(@$args);
263 }
264  
265  
266  
267 sub _missing_handler_error
268 {
269 0 ▶ my ($this, $name, $kind) = @_;
Subroutine _missing_handler_error: not called CC=1 SCAR=6.9
270 0 return "no $kind handler registered for $name\n";
271 }
272  
273  
274 =head1 NAME
275  
276 Neovim::Ext::Plugin::Host - Neovim Plugin::Host class
277  
278 =head1 VERSION
279  
280 version 0.01
281  
282 =head1 SYNOPSIS
283  
284 use Neovim::Ext;
285  
286 my $host = Neovim::Ext::Plugin::Host->new ($nvim);
287 $host->start ('/path/to/Plugin1.pm', '/path/to/Plugin2.pm');
288  
289 =head1 METHODS
290  
291 =head2 register( $package )
292  
293 Register C<$package> as a plugin. This should be called by a plugin on load.
294  
295 =head2 start( @plugins )
296  
297 Start listening for msgpack-rpc requests and notifications.
298  
299 =head2 shutdown( )
300  
301 Shutdown the host.
302  
303 =cut
304  
305 56 1;
↑ Summary Ext/LuaFuncs.pm »