File Coverage

blib/lib/PLS/Server/Request/TextDocument/DidChange.pm
Criterion Covered Total %
statement 27 44 61.3
branch 0 4 0.0
condition n/a
subroutine 9 11 81.8
pod 0 1 0.0
total 36 60 60.0


line stmt bran cond sub pod time code
1             package PLS::Server::Request::TextDocument::DidChange;
2              
3 9     9   54 use strict;
  9         18  
  9         225  
4 9     9   37 use warnings;
  9         18  
  9         189  
5              
6 9     9   37 use parent 'PLS::Server::Request';
  9         19  
  9         44  
7              
8 9     9   519 use feature 'state';
  9         18  
  9         569  
9              
10 9     9   55 use IO::Async::Loop;
  9         66  
  9         230  
11 9     9   45 use IO::Async::Timer::Countdown;
  9         10  
  9         338  
12              
13 9     9   44 use PLS::Parser::Document;
  9         18  
  9         172  
14 9     9   37 use PLS::Parser::Index;
  9         17  
  9         226  
15 9     9   3673 use PLS::Server::Request::TextDocument::PublishDiagnostics;
  9         105  
  9         5652  
16              
17             =head1 NAME
18              
19             PLS::Server::Request::TextDocument::DidChange
20              
21             =head1 DESCRIPTION
22              
23             This is a notification from the client to the server that
24             a text document was changed.
25              
26             =cut
27              
28             sub service
29             {
30 0     0 0   my ($self, $server) = @_;
31              
32 0 0         return unless (ref $self->{params}{contentChanges} eq 'ARRAY');
33             PLS::Parser::Document->update_file(
34             uri => $self->{params}{textDocument}{uri},
35             changes => $self->{params}{contentChanges},
36             version => $self->{params}{textDocument}{version}
37 0           );
38              
39 0           state %timers;
40              
41 0           my $uri = $self->{params}{textDocument}{uri};
42              
43 0 0         if (ref $timers{$uri} eq 'IO::Async::Timer::Countdown')
44             {
45             # If we get another change before the timer goes off, reset the timer.
46             # This will allow us to limit the diagnostics to a time one second after the user stopped typing.
47 0           $timers{$uri}->reset();
48             } ## end if (ref $timers{$uri} ...)
49             else
50             {
51             $timers{$uri} = IO::Async::Timer::Countdown->new(
52             delay => 2,
53             on_expire => sub {
54 0     0     my $index = PLS::Parser::Index->new();
55 0           $index->index_files($uri)->then(sub { Future->wait_all(@_) })->retain();
  0            
56              
57 0           $server->send_server_request(PLS::Server::Request::TextDocument::PublishDiagnostics->new(uri => $uri));
58 0           delete $timers{$uri};
59             },
60 0           remove_on_expire => 1
61             );
62              
63 0           my $loop = IO::Async::Loop->new();
64 0           $loop->add($timers{$uri});
65 0           $timers{$uri}->start();
66             } ## end else [ if (ref $timers{$uri} ...)]
67              
68 0           return;
69             } ## end sub service
70              
71             1;