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 9     9   54 use strict;
  9         18  
  9         227  
4 9     9   44 use warnings;
  9         26  
  9         183  
5              
6 9     9   36 use parent 'PLS::Server::Request';
  9         32  
  9         31  
7              
8 9     9   499 use List::Util;
  9         25  
  9         415  
9 9     9   60 use Path::Tiny;
  9         21  
  9         312  
10              
11 9     9   45 use PLS::JSON;
  9         18  
  9         424  
12 9     9   54 use PLS::Server::Request::Workspace::Configuration;
  9         18  
  9         296  
13 9     9   3186 use PLS::Server::Request::Client::RegisterCapability;
  9         28  
  9         239  
14 9     9   3200 use PLS::Server::Request::Progress;
  9         26  
  9         227  
15 9     9   3616 use PLS::Server::Request::Window::WorkDoneProgress::Create;
  9         27  
  9         243  
16 9     9   54 use PLS::Server::State;
  9         9  
  9         5135  
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 36 my ($self, $server) = @_;
34              
35             # now that we're initialized, put in a request for our configuration items.
36 5         235 $server->send_server_request(PLS::Server::Request::Workspace::Configuration->new());
37              
38             # request that we receive a notification any time configuration changes
39 5         61 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         31 my $index = PLS::Parser::Index->new();
44              
45 5 50       14 if (scalar @{$index->workspace_folders})
  5         32  
46             {
47 5         144 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         124 $server->send_server_request(PLS::Server::Request::Client::RegisterCapability->new(\@capabilities));
62              
63             # Now is a good time to start indexing files.
64 5         40 $self->index_files($index, $server);
65              
66 5         113 return;
67             } ## end sub service
68              
69             sub index_files
70             {
71 5     5 0 13 my ($self, $index, $server) = @_;
72              
73 5 50       120 if ($PLS::Server::State::CLIENT_CAPABILITIES->{window}{workDoneProgress})
74             {
75 5         82 $self->index_files_with_progress($index, $server);
76             }
77             else
78             {
79 0         0 $self->index_files_without_progress($index);
80             }
81              
82 5         127 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 15 my (undef, $index, $server) = @_;
97              
98 5         98 my $work_done_progress_create = PLS::Server::Request::Window::WorkDoneProgress::Create->new();
99 5         58 $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         43 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   541 my @futures = @_;
114              
115 4         27 my $done = 0;
116 4         29 my $total = scalar @futures;
117              
118 4         47 foreach my $future (@futures)
119             {
120             $future->then(
121             sub {
122 4         506 my ($file) = @_;
123              
124 4         51 my $workspace_folder = List::Util::first { path($_)->subsumes($file) } @{$index->workspace_folders};
  4         109  
  4         22  
125 4         1853 $file = path($file)->relative($workspace_folder);
126 4         1183 $done++;
127             $server->send_server_request(
128             PLS::Server::Request::Progress->new(
129             token => $work_done_progress_create->{params}{token},
130 4         25 kind => 'report',
131             message => "Indexed $file ($done/$total)",
132             percentage => int($done * 100 / $total)
133             )
134             );
135             }
136 4         138 )->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         827 kind => 'end',
145             message => 'Finished indexing all files'
146             )
147             );
148              
149             }
150 4         561 );
151             }
152 5         1220 )->retain();
153              
154 5         1773 return;
155             } ## end sub index_files_with_progress
156              
157             1;