File Coverage

blib/lib/PLS/Server/Request/Workspace/ExecuteCommand.pm
Criterion Covered Total %
statement 15 23 65.2
branch 0 4 0.0
condition n/a
subroutine 5 6 83.3
pod 0 1 0.0
total 20 34 58.8


line stmt bran cond sub pod time code
1              
2             use strict;
3 9     9   54 use warnings;
  9         18  
  9         224  
4 9     9   36  
  9         82  
  9         244  
5             use parent 'PLS::Server::Request';
6 9     9   44  
  9         18  
  9         36  
7             use PLS::Parser::Document;
8 9     9   405 use PLS::Server::Request::Workspace::ApplyEdit;
  9         17  
  9         253  
9 9     9   4060  
  9         18  
  9         1888  
10             =head1 NAME
11              
12             PLS::Server::Request::Workspace::ExecuteCommand
13              
14             =head1 DESCRIPTION
15              
16             This is a message from the client to the server requesting that a
17             command be executed.
18              
19             The commands that are currently implemented are:
20              
21             =over
22              
23             =item pls.sortImports
24              
25             This sorts the imports of the current Perl file. The sorting follows this order:
26              
27             =over
28              
29             =item C<use strict> and C<use warnings>
30              
31             =item C<use parent> and C<use base>
32              
33             =item Other pragmas (excluding C<use constant>)
34              
35             =item Core and external imports
36              
37             =item Internal imports (from the current project)
38              
39             =item Constants (C<use constant>)
40              
41             =back
42              
43             This command is not perfect and is a work in progress. It does not handle
44             comments or non-contiguous imports well.
45              
46             =back
47              
48             =cut
49              
50             {
51             my ($self, $server) = @_;
52              
53 0     0 0   if ($self->{params}{command} eq 'pls.sortImports')
54             {
55 0 0         my $file = $self->{params}{arguments}[0]{path};
56             my $doc = PLS::Parser::Document->new(path => $file);
57 0           return
58 0           PLS::Server::Response->new(
59             {
60             id => $self->{id},
61             error => {
62             code => -32602,
63 0 0         message => 'Failed to sort imports.',
64             data => $file
65             }
66             }
67             )
68             if (ref $doc ne 'PLS::Parser::Document');
69             my ($new_text, $lines) = $doc->sort_imports();
70              
71 0           $server->send_server_request(PLS::Server::Request::Workspace::ApplyEdit->new(text => $new_text, path => $file, lines => $lines));
72             } ## end if ($self->{params}{command...})
73 0            
74             return PLS::Server::Response->new({id => $self->{id}, result => undef});
75             } ## end sub service
76 0            
77             1;