File Coverage

blib/lib/PLS/Server/Response/DocumentSymbol.pm
Criterion Covered Total %
statement 24 46 52.1
branch 0 2 0.0
condition 0 6 0.0
subroutine 8 14 57.1
pod 0 2 0.0
total 32 70 45.7


line stmt bran cond sub pod time code
1             package PLS::Server::Response::DocumentSymbol;
2              
3 9     9   46 use strict;
  9         18  
  9         281  
4 9     9   52 use warnings;
  9         18  
  9         1246  
5              
6 9     9   167 use parent 'PLS::Server::Response';
  9         24  
  9         94  
7              
8 9     9   639 use feature 'state';
  9         2019  
  9         1721  
9              
10 9     9   55 use IO::Async::Loop;
  9         24  
  9         280  
11 9     9   46 use IO::Async::Timer::Countdown;
  9         216  
  9         166  
12              
13 9     9   38 use PLS::Parser::Document;
  9         18  
  9         221  
14 9     9   3618 use PLS::Parser::DocumentSymbols;
  9         27  
  9         3432  
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             } ## end sub on_expire
83              
84             1;