File Coverage

blib/lib/PLS/Server/Response/RangeFormatting.pm
Criterion Covered Total %
statement 21 34 61.7
branch 0 4 0.0
condition n/a
subroutine 7 9 77.7
pod 0 1 0.0
total 28 48 58.3


line stmt bran cond sub pod time code
1             package PLS::Server::Response::RangeFormatting;
2              
3 11     11   205 use strict;
  11         65  
  11         619  
4 11     11   92 use warnings;
  11         84  
  11         1351  
5              
6 11     11   99 use parent q(PLS::Server::Response);
  11         54  
  11         108  
7              
8 11     11   1516 use IO::Async::Function;
  11         430  
  11         796  
9 11     11   90 use IO::Async::Loop;
  11         133  
  11         505  
10              
11 11     11   68 use PLS::Parser::Document;
  11         124  
  11         3987  
12 11     11   68 use PLS::Server::State;
  11         54  
  11         5628  
13              
14             =head1 NAME
15              
16             PLS::Server::Response::RangeFormatting
17              
18             =head1 DESCRIPTION
19              
20             This is a message from the server to the client with a document range
21             after having been formatted.
22              
23             =cut
24              
25             # Set up formatting as a function because it can be slow
26             my $loop = IO::Async::Loop->new();
27             my $function = IO::Async::Function->new(
28             code => sub {
29             my ($self, $request, $text, $perltidyrc, $workspace_folders) = @_;
30              
31             my ($ok, $formatted) =
32             PLS::Parser::Document->format_range(
33             text => $text,
34             range => $request->{params}{range},
35             formatting_options => $request->{params}{options},
36             perltidyrc => $perltidyrc,
37             workspace_folders => $workspace_folders
38             );
39             return $ok, $formatted;
40             }
41             );
42             $loop->add($function);
43              
44             sub new
45             {
46 0     0 0   my ($class, $request) = @_;
47              
48 0 0         if (ref $request->{params}{options} eq 'HASH')
49             {
50             # these options aren't really valid for range formatting
51 0           delete $request->{params}{options}{trimFinalNewlines};
52 0           delete $request->{params}{options}{insertFinalNewline};
53             } ## end if (ref $request->{params...})
54              
55 0           my $self = bless {id => $request->{id}}, $class;
56 0           my $text = PLS::Parser::Document->text_from_uri($request->{params}{textDocument}{uri});
57 0           my $workspace_folders = PLS::Parser::Index->new->workspace_folders;
58              
59             return $function->call(args => [$self, $request, $text, $PLS::Server::State::CONFIG->{perltidy}{perltidyrc}, $workspace_folders])->then(
60             sub {
61 0     0     my ($ok, $formatted) = @_;
62              
63 0 0         if ($ok)
64             {
65 0           $self->{result} = $formatted;
66             }
67             else
68             {
69 0           $self->{error} = $formatted;
70             }
71              
72 0           return $self;
73             }
74 0           );
75             } ## end sub new
76              
77             1;