=head1 Perl::LanguageServer

Language Server and Debug Protocol Adapter for Perl

=head2 Features

=over

=item * Language Server

=over

=item * Syntax checking

=item * Symbols in file

=item * Symbols in workspace/directory

=item * Goto Definition

=item * Find References

=item * Call Signatures

=item * Supports multiple workspace folders

=item * Document and selection formatting via perltidy

=item * Run on remote system via ssh

=item * Run inside docker container

=item * Run inside kubernetes

=back

=item * Debugger

=over

=item * Run, pause, step, next, return

=item * Support for coro threads

=item * Breakpoints

=item * Conditional breakpoints

=item * Breakpoints can be set while program runs and for modules not yet loaded

=item * Variable view, can switch to every stack frame or coro thread

=item * Set variable

=item * Watch variable

=item * Tooltips with variable values

=item * Evaluate perl code in debuggee, in context of every stack frame of coro thread

=item * Automatically reload changed Perl modules while debugging

=item * Debug multiple perl programs at once

=item * Run on remote system via ssh

=item * Run inside docker container

=item * Run inside kubernetes

=back

=back

=head2 Requirements

You need to install the perl module Perl::LanguageServer to make this extension work,
e.g. run C<cpan Perl::LanguageServer> on your target system.

Please make sure to always run the newest version of Perl::LanguageServer as well.

NOTE: Perl::LanguageServer depend on AnyEvent::AIO and Coro. There is a warning that
this might not work with newer Perls. It works fine for Perl::LanguageServer. So just
confirm the warning and install it.

Perl::LanguageServer depends on other Perl modules. It is a good idea to install most
of then with your linux package manager.

e.g. on Debian/Ubuntu run:


    
     sudo apt install libanyevent-perl libclass-refresh-perl libcompiler-lexer-perl \
     libdata-dump-perl libio-aio-perl libjson-perl libmoose-perl libpadwalker-perl \
     libscalar-list-utils-perl libcoro-perl
     
     sudo cpan Perl::LanguageServer
    

e.g. on Centos 7 run:


    
      sudo yum install perl-App-cpanminus perl-AnyEvent-AIO perl-Coro
      sudo cpanm Class::Refresh
      sudo cpanm Compiler::Lexer
      sudo cpanm Hash::SafeKeys
      sudo cpanm Perl::LanguageServer
    

In case any of the above packages are not available for your os version, just
leave them out. The cpan command will install missing dependencies. In case
the test fails, when running cpan C<install>, you should try to run C<force install>.

=head2 Extension Settings

This extension contributes the following settings:

=over

=item * C<perl.enable>: enable/disable this extension

=item * C<perl.sshAddr>: ip address of remote system

=item * C<perl.sshPort>: optional, port for ssh to remote system

=item * C<perl.sshUser>: user for ssh login

=item * C<perl.sshCmd>: defaults to ssh on unix and plink on windows

=item * C<perl.sshWorkspaceRoot>: path of the workspace root on remote system

=item * C<perl.perlCmd>: defaults to perl

=item * C<perl.perlArgs>: additional arguments passed to the perl interpreter that starts the LanguageServer

=item * C<perl.sshArgs>: optional arguments for ssh

=item * C<perl.pathMap>: mapping of local to remote paths

=item * C<perl.perlInc>: array with paths to add to perl library path. This setting is used by the syntax checker and for the debuggee and also for the LanguageServer itself.

=item * C<perl.fileFilter>: array for filtering perl file, defaults to [I<.pm,>.pl]

=item * C<perl.ignoreDirs>: directories to ignore, defaults to [.vscode, .git, .svn]

=item * C<perl.debugAdapterPort>: port to use for connection between vscode and debug adapter inside Perl::LanguageServer.

=item * C<perl.debugAdapterPortRange>: if debugAdapterPort is in use try ports from debugAdapterPort to debugAdapterPort + debugAdapterPortRange. Default 100.

=item * C<perl.showLocalVars>: if true, show also local variables in symbol view

=item * C<perl.logLevel>: Log level 0-2.

=item * C<perl.logFile>: If set, log output is written to the given logfile, instead of displaying it in the vscode output pane. Log output is always appended. Only use during debugging of LanguageServer itself.

=item * C<perl.disableCache>: If true, the LanguageServer will not cache the result of parsing source files on disk, so it can be used within readonly directories

=item * C<perl.containerCmd>: If set Perl::LanguageServer can run inside a container. Options are: 'docker', 'docker-compose', 'kubectl'

=item * C<perl.containerArgs>: arguments for containerCmd. Varies depending on containerCmd.

=item * C<perl.containerMode>: To start a new container, set to 'run', to execute inside an existing container set to 'exec'. Note: kubectl only supports 'exec'

=item * C<perl.containerName>: Image to start or container to exec inside or pod to use

=back

=head2 Debugger Settings for launch.json

=over

=item * C<type>: needs to be C<perl>

=item * C<request>: only C<launch> is supported (this is a restriction of perl itself)

=item * C<name>: name of this debug configuration

=item * C<program>: path to perl program to start

=item * C<stopOnEntry>: if true, program will stop on entry

=item * C<args>:   optional, array with arguments for perl program

=item * C<env>:    optional, object with environment settings

=item * C<cwd>:    optional, change working directory before launching the debuggee

=item * C<reloadModules>: if true, automatically reload changed Perl modules while debugging

=back

=head2 Remote syntax check & debugging

If you developing on a remote machine, you can instruct the Perl::LanguageServer to
run on that remote machine, so the correct modules etc. are available for syntax check and debugger is started on the remote machine.
To do so set sshAddr and sshUser, preferably in your workspace configuration.

Example:


    "sshAddr": "10.11.12.13",
    "sshUser": "root"

Also set sshWorkspaceRoot, so the local workspace path can be mapped to the remote one.

Example: if your local path is \10.11.12.13\share\path\to\ws and on the remote machine you have /path/to/ws


    "sshWorkspaceRoot": "/path/to/ws"

The other possibility is to provide a pathMap. This allows to having multiple mappings.

Examples:


    "sshpathMap": [
        ["remote uri", "local uri"],
        ["remote uri", "local uri"]
    ]
    
    "perl.pathMap": [
        [
        "file:///",
        "file:///home/systems/mountpoint/"
        ]
    ]

=head2 Syntax check & debugging inside a container

You can run the LanguageServer and/or debugger inside
a container by setting C<containerCmd> and C<conatinerName>.
There are more container options, see above.

.vscode/settings.json


    {
        "perl": {
            "enable": true,
            "containerCmd": "docker",
            "containerName": "perl_container",
        }
    }

=head2 FAQ

=head3 Working directory is not defined

It is not defined what the current working directory is at the start of a perl program.
So Perl::LanguageServer makes no assumptions about it. To solve the problem you can set
the directory via cwd configuration parameter in launch.json for debugging.

=head3 Module not found when debugging or during syntax check

If you reference a module with a relative path or if you assume that the current working directory
is part of the Perl search path, it will not work.
Instead set the perl include path to a fixed absolute path. In your settings.json do something like:


        "perl.perlInc": [
            "/path/a/lib",
            "/path/b/lib",
            "/path/c/lib",
        ],
Include path works for syntax check and inside of debugger.
C<perl.perlInc> should be an absolute path.

=head3 AnyEvent, Coro Warning during install

You need to install the AnyEvent::IO and Coro. Just ignore the warning that it might not work. For Perl::LanguageServer it works fine.

=head3 'richterger.perl' failed: options.port should be >= 0 and < 65536

Change port setting from string to integer

=head3 Error "Can't locate MODULE_NAME"

Please make sure the path to the module is in C<perl.perlInc> setting and use absolute path names in the perlInc settings
or make sure you are running in the expected directory by setting the C<cwd> setting in the lauch.json.

=head3 ERROR: Unknow perlmethod I<rpcnot>setTraceNotification

This is not an issue, that just means that not all features of the debugging protocol are implemented.
Also it says ERROR, it's just a warning and you can safely ignore it.

=head3 The debugger sometimes stops at random places

Upgrade to Version 2.4.0

=head3 Message about Perl::LanguageServer has crashed 5 times

This is a problem when more than one instance of Perl::LanguageServer is running.
Upgrade to Version 2.4.0 solves this problem.

=head3 Carton support

If you are using LL<https://metacpan.org/pod/Carton> to manage dependencies, add the full path to the Carton C<lib> dir to your workspace settings file at C<.vscode/settings.json>. For example:

=head4 Linux


    {
      "perl.perlInc": ["/home/myusername/projects/myprojectname/local/lib/perl5"]
    }

=head4 Mac


    {
      "perl.perlInc": ["/Users/myusername/projects/myprojectname/local/lib/perl5"]
    }

=head2 Known Issues

Does not yet work on windows, due to issues with reading from stdin.
I wasn't able to find a reliable way to do a non-blocking read from stdin on windows.
I would be happy, if anyone knows how to do this in Perl.

Anyway, Perl::LanguageServer runs without problems inside of Windows Subsystem for Linux (WSL).

=head2 Release Notes

see CHANGELOG.md

=head2 More Info

=over

=item * Presentation at German Perl Workshop 2020:

=back

https://github.com/richterger/Perl-LanguageServer/blob/master/docs/Perl-LanguageServer%20und%20Debugger%20f%C3%BCr%20Visual%20Studio%20Code%20u.a.%20Editoren%20-%20Perl%20Workshop%202020.pdf

=over

=item * Github: https://github.com/richterger/Perl-LanguageServer

=item * MetaCPAN: https://metacpan.org/release/Perl-LanguageServer

=back

For reporting bugs please use GitHub issues.

=head2 References

This is a Language Server and Debug Protocol Adapter for Perl

It implements the Language Server Protocol which provides
syntax-checking, symbol search, etc. Perl to various editors, for
example Visual Studio Code or Atom.

https://microsoft.github.io/language-server-protocol/specification

It also implements the Debug Adapter Protocol, which allows debugging
with various editors/includes

https://microsoft.github.io/debug-adapter-protocol/overview

To use both with Visual Studio Code, install the extension "perl"

https://marketplace.visualstudio.com/items?itemName=richterger.perl

Any comments and patches are welcome.

=head2 LICENSE AND COPYRIGHT

Copyright 2018-2022 Gerald Richter.

This program is free software; you can redistribute it and/or modify it
under the terms of the Artistic License (2.0). You may obtain a
copy of the full license at:

LL<http://www.perlfoundation.org/artistic_license_2_0>

Any use, modification, and distribution of the Standard or Modified
Versions is governed by this Artistic License. By using, modifying or
distributing the Package, you accept this license. Do not use, modify,
or distribute the Package, if you do not accept this license.

If your Modified Version has been derived from a Modified Version made
by someone other than you, you are nevertheless required to ensure that
your Modified Version complies with the requirements of this license.

This license does not grant you the right to use any trademark, service
mark, tradename, or logo of the Copyright Holder.

This license includes the non-exclusive, worldwide, free-of-charge
patent license to make, have made, use, offer to sell, sell, import and
otherwise transfer the Package with respect to any patent claims
licensable by the Copyright Holder that are necessarily infringed by the
Package. If you institute patent litigation (including a cross-claim or
counterclaim) against any party alleging that the Package constitutes
direct or contributory patent infringement, then this Artistic License
to you shall terminate on the date that such litigation is filed.

Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
