File Coverage

blib/lib/PLS/Server/Response/DocumentSymbol.pm
Criterion Covered Total %
statement 24 47 51.0
branch 0 2 0.0
condition 0 6 0.0
subroutine 8 14 57.1
pod 0 2 0.0
total 32 71 45.0


line stmt bran cond sub pod time code
1             package PLS::Server::Response::DocumentSymbol;
2              
3 11     11   94 use strict;
  11         59  
  11         1172  
4 11     11   115 use warnings;
  11         65  
  11         1094  
5              
6 11     11   118 use parent 'PLS::Server::Response';
  11         41  
  11         218  
7              
8 11     11   1641 use feature 'state';
  11         52  
  11         7625  
9              
10 11     11   356 use IO::Async::Loop;
  11         47  
  11         826  
11 11     11   132 use IO::Async::Timer::Countdown;
  11         47  
  11         641  
12              
13 11     11   85 use PLS::Parser::Document;
  11         30  
  11         688  
14 11     11   10183 use PLS::Parser::DocumentSymbols;
  11         75  
  11         8535  
15              
16             =head1 NAME
17              
18             PLS::Server::Response::DocumentSymbol
19              
20             =head1 DESCRIPTION
21              
22             This is a message from the server to the client with a list
23             of symbols in the current document.
24              
25             =cut
26              
27             sub new
28             {
29 0     0 0   my ($class, $request) = @_;
30              
31 0           my $self = bless {id => $request->{id}, result => undef}, $class;
32              
33 0           my $uri = $request->{params}{textDocument}{uri};
34              
35             # Delay document symbols by a couple of seconds to allow cancelling before processing starts.
36 0           my $future = Future->new();
37             my $timer = IO::Async::Timer::Countdown->new(
38             delay => 2,
39 0     0     on_expire => sub { $self->on_expire($uri, $future) },
40 0           remove_on_expire => 1
41             );
42              
43 0           IO::Async::Loop->new->add($timer->start());
44              
45             # When the future is canceled, make sure to stop the timer so that it never actually starts generating document symbols.
46             $future->on_cancel(
47             sub {
48 0     0     $timer->stop();
49 0           $timer->remove_from_parent();
50             }
51 0           );
52              
53 0           return $future;
54             } ## end sub new
55              
56             sub on_expire
57             {
58 0     0 0   my ($self, $uri, $future) = @_;
59              
60 0           my $version = PLS::Parser::Document::uri_version($uri);
61              
62             PLS::Parser::DocumentSymbols->get_all_document_symbols_async($uri)->on_done(
63             sub {
64 0     0     my ($symbols) = @_;
65              
66 0           my $current_version = PLS::Parser::Document::uri_version($uri);
67              
68 0 0 0       if (not length $current_version or length $version and $current_version > $version)
      0        
69             {
70 0           $future->done($self);
71 0           return;
72             }
73              
74 0           $self->{result} = $symbols;
75 0           $future->done($self);
76             }
77             )->on_fail(
78             sub {
79 0     0     $future->done($self);
80             }
81 0           )->retain();
82              
83 0           return;
84             } ## end sub on_expire
85              
86             1;