File Coverage

blib/lib/PLS/Server/Response/Formatting.pm
Criterion Covered Total %
statement 26 31 83.8
branch 0 2 0.0
condition n/a
subroutine 8 9 88.8
pod 0 1 0.0
total 34 43 79.0


line stmt bran cond sub pod time code
1             package PLS::Server::Response::Formatting;
2              
3 11     11   229 use strict;
  11         160  
  11         632  
4 11     11   74 use warnings;
  11         157  
  11         709  
5              
6 11     11   82 use parent q(PLS::Server::Response);
  11         22  
  11         75  
7              
8 11     11   1095 use IO::Async::Function;
  11         213  
  11         513  
9 11     11   81 use IO::Async::Loop;
  11         43  
  11         601  
10              
11 11     11   96 use PLS::Parser::Document;
  11         33  
  11         791  
12 11     11   75 use PLS::Server::State;
  11         21  
  11         5931  
13              
14             =head1 NAME
15              
16             PLS::Server::Response::Formatting
17              
18             =head1 DESCRIPTION
19              
20             This is a message from the server to the client with the current document
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(
33             text => $text,
34             formatting_options => $request->{params}{options},
35             perltidyrc => $perltidyrc,
36             workspace_folders => $workspace_folders
37             );
38             return $ok, $formatted;
39             }
40             );
41             $loop->add($function);
42              
43             sub new
44             {
45 1     1 0 7 my ($class, $request) = @_;
46              
47 1         29 my $self = bless {id => $request->{id}}, $class;
48 1         15 my $text = PLS::Parser::Document->text_from_uri($request->{params}{textDocument}{uri});
49 1         10 my $workspace_folders = PLS::Parser::Index->new->workspace_folders;
50              
51             return $function->call(args => [$self, $request, $text, $PLS::Server::State::CONFIG->{perltidy}{perltidyrc}, $workspace_folders])->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 1         17 );
67             } ## end sub new
68              
69             1;