File Coverage

blib/lib/PLS/Server/Request/Initialized.pm
Criterion Covered Total %
statement 67 72 93.0
branch 2 4 50.0
condition n/a
subroutine 15 17 88.2
pod 0 4 0.0
total 84 97 86.6


line stmt bran cond sub pod time code
1             package PLS::Server::Request::Initialized;
2              
3 11     11   104 use strict;
  11         37  
  11         387  
4 11     11   49 use warnings;
  11         22  
  11         690  
5              
6 11     11   69 use parent 'PLS::Server::Request';
  11         23  
  11         68  
7              
8 11     11   790 use List::Util;
  11         30  
  11         1002  
9 11     11   75 use Path::Tiny;
  11         22  
  11         755  
10              
11 11     11   66 use PLS::JSON;
  11         27  
  11         571  
12 11     11   51 use PLS::Server::Request::Workspace::Configuration;
  11         38  
  11         252  
13 11     11   5398 use PLS::Server::Request::Client::RegisterCapability;
  11         43  
  11         533  
14 11     11   6816 use PLS::Server::Request::Progress;
  11         58  
  11         507  
15 11     11   7488 use PLS::Server::Request::Window::WorkDoneProgress::Create;
  11         41  
  11         476  
16 11     11   76 use PLS::Server::State;
  11         29  
  11         10208  
17              
18             =head1 NAME
19              
20             PLS::Server::Request::Initialized
21              
22             =head1 DESCRIPTION
23              
24             This is a request from the client to the server indicating that it received
25             the result of the initialize request from the server.
26              
27             The server sends back some initial requests that it needs to complete initialization.
28              
29             =cut
30              
31             sub service
32             {
33 5     5 0 78 my ($self, $server) = @_;
34              
35             # now that we're initialized, put in a request for our configuration items.
36 5         447 $server->send_server_request(PLS::Server::Request::Workspace::Configuration->new());
37              
38             # request that we receive a notification any time configuration changes
39 5         174 my @capabilities = ({id => 'did-change-configuration', method => 'workspace/didChangeConfiguration'});
40              
41             # request that we receive a notification every time a file changes,
42             # so that we can reindex it.
43 5         106 my $index = PLS::Parser::Index->new();
44              
45 5 50       59 if (scalar @{$index->workspace_folders})
  5         177  
46             {
47 5         298 push @capabilities,
48             {
49             id => 'did-change-watched-files',
50             method => 'workspace/didChangeWatchedFiles',
51             registerOptions => {
52             watchers => [
53             {
54             globPattern => '**/*'
55             }
56             ]
57             }
58             };
59             } ## end if (scalar @{$index->workspace_folders...})
60              
61 5         214 $server->send_server_request(PLS::Server::Request::Client::RegisterCapability->new(\@capabilities));
62              
63             # Now is a good time to start indexing files.
64 5         92 $self->index_files($index, $server);
65              
66 5         310 return;
67             } ## end sub service
68              
69             sub index_files
70             {
71 5     5 0 17 my ($self, $index, $server) = @_;
72              
73 5 50       590 if ($PLS::Server::State::CLIENT_CAPABILITIES->{window}{workDoneProgress})
74             {
75 5         216 $self->index_files_with_progress($index, $server);
76             }
77             else
78             {
79 0         0 $self->index_files_without_progress($index);
80             }
81              
82 5         263 return;
83             } ## end sub index_files
84              
85             sub index_files_without_progress
86             {
87 0     0 0 0 my (undef, $index) = @_;
88              
89 0     0   0 $index->index_files()->then(sub { Future->wait_all(@_) })->retain();
  0         0  
90              
91 0         0 return;
92             } ## end sub index_files_without_progress
93              
94             sub index_files_with_progress
95             {
96 5     5 0 30 my (undef, $index, $server) = @_;
97              
98 5         284 my $work_done_progress_create = PLS::Server::Request::Window::WorkDoneProgress::Create->new();
99 5         35 $server->send_server_request($work_done_progress_create);
100              
101             $server->send_server_request(
102             PLS::Server::Request::Progress->new(
103             token => $work_done_progress_create->{params}{token},
104 5         74 kind => 'begin',
105             title => 'Indexing',
106             cancellable => PLS::JSON::false,
107             percentage => 0
108             )
109             );
110              
111             $index->index_files()->then(
112             sub {
113 4     4   733 my @futures = @_;
114              
115 4         33 my $done = 0;
116 4         98 my $total = scalar @futures;
117              
118 4         59 foreach my $future (@futures)
119             {
120             $future->then(
121             sub {
122 4         946 my ($file) = @_;
123              
124 4         178 my $workspace_folder = List::Util::first { path($_)->subsumes($file) } @{$index->workspace_folders};
  4         254  
  4         120  
125 4         3598 $file = path($file)->relative($workspace_folder);
126 4         2091 $done++;
127             $server->send_server_request(
128             PLS::Server::Request::Progress->new(
129             token => $work_done_progress_create->{params}{token},
130 4         94 kind => 'report',
131             message => "Indexed $file ($done/$total)",
132             percentage => int($done * 100 / $total)
133             )
134             );
135             }
136 4         171 )->retain();
137             } ## end foreach my $future (@futures...)
138              
139             return Future->wait_all(@futures)->then(
140             sub {
141             $server->send_server_request(
142             PLS::Server::Request::Progress->new(
143             token => $work_done_progress_create->{params}{token},
144 4         1450 kind => 'end',
145             message => 'Finished indexing all files'
146             )
147             );
148              
149             }
150 4         724 );
151             }
152 5         314 )->retain();
153              
154 5         2615 return;
155             } ## end sub index_files_with_progress
156              
157             1;