File Coverage

blib/lib/PLS/Server/Response/RangeFormatting.pm
Criterion Covered Total %
statement 21 33 63.6
branch 0 4 0.0
condition n/a
subroutine 7 9 77.7
pod 0 1 0.0
total 28 47 59.5


line stmt bran cond sub pod time code
1             package PLS::Server::Response::RangeFormatting;
2              
3 9     9   46 use strict;
  9         26  
  9         292  
4 9     9   45 use warnings;
  9         82  
  9         252  
5              
6 9     9   111 use parent q(PLS::Server::Response);
  9         27  
  9         87  
7              
8 9     9   849 use IO::Async::Function;
  9         81  
  9         380  
9 9     9   94 use IO::Async::Loop;
  9         17  
  9         197  
10              
11 9     9   37 use PLS::Parser::Document;
  9         18  
  9         290  
12 9     9   355 use PLS::Server::State;
  9         22  
  9         3303  
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) = @_;
30              
31             my ($ok, $formatted) = PLS::Parser::Document->format_range(text => $text, range => $request->{params}{range}, formatting_options => $request->{params}{options}, perltidyrc => $perltidyrc);
32             return $ok, $formatted;
33             }
34             );
35             $loop->add($function);
36              
37             sub new
38             {
39 0     0 0   my ($class, $request) = @_;
40              
41 0 0         if (ref $request->{params}{options} eq 'HASH')
42             {
43             # these options aren't really valid for range formatting
44 0           delete $request->{params}{options}{trimFinalNewlines};
45 0           delete $request->{params}{options}{insertFinalNewline};
46             } ## end if (ref $request->{params...})
47              
48 0           my $self = bless {id => $request->{id}}, $class;
49 0           my $text = PLS::Parser::Document->text_from_uri($request->{params}{textDocument}{uri});
50              
51             return $function->call(args => [$self, $request, $text, $PLS::Server::State::CONFIG->{perltidy}{perltidyrc}])->then(
52             sub {
53 0     0     my ($ok, $formatted) = @_;
54              
55 0 0         if ($ok)
56             {
57 0           $self->{result} = $formatted;
58             }
59             else
60             {
61 0           $self->{error} = $formatted;
62             }
63              
64 0           return $self;
65             }
66 0           );
67             } ## end sub new
68              
69             1;