perl-language-server
perl-language-server copied to clipboard
Perl Language Server
PLS implements features of the Language Server Protocol for Perl 5.
It is still very much in its early stages and Pull Requests are more than welcome.
The features currently implemented are:
- Go to definition (for packages, subroutines, and variables)
- Listing all symbols in a document
- Hovering to show documentation
- Signature help (showing parameters for a function as you type)
- Formatting
- Range Formatting
- Auto-completion
- Syntax checking
- Linting (using perlcritic)
- Sorting imports
Installation
Install the PLS package from CPAN: https://metacpan.org/pod/PLS
Setup
VSCode
Install the fractalboy.pls extension in Visual Studio Code: https://marketplace.visualstudio.com/items?itemName=FractalBoy.pls
Neovim
This assumes Neovim 0.5.0 or greater.
Install nvim-lspconfig.
nvim-lspconfig comes with a default configuration for PLS and its name is perlpls (do not confuse this with perlls which is the default configuration for Perl::LanguageServer).
The simplest means of configuring PLS is to place the following somewhere in your Neovim config:
require'lspconfig'.perlpls.setup()
This will set you up with the defaults. It assumes that pls is in your $PATH. By default Perl Critic integration will be turned off.
A more complex configuration will look like this:
local config = {
cmd = { '/opt/bin/pls' }, -- complete path to where PLS is located
settings = {
pls = {
inc = { '/my/perl/5.34/lib', '/some/other/perl/lib' }, -- add list of dirs to @INC
cwd = { '/my/projects' }, -- working directory for PLS
perlcritic = { enabled = true, perlcriticrc = '/my/projects/.perlcriticrc' }, -- use perlcritic and pass a non-default location for its config
syntax = { enabled = true, perl = '/usr/bin/perl', args = { 'arg1', 'arg2' } }, -- enable syntax checking and use a non-default perl binary
perltidy = { perltidyrc = '/my/projects/.perltidyrc' } -- non-default location for perltidy's config
}
}
}
require'lspconfig'.perlpls.setup(config)
See perldoc PLS for more details about the configuration items.
The above assumes a Lua configuration. If you are using a Vimscript configuration remember to wrap everything in a Lua here-doc, e.g.:
lua <<EOF
...config...
EOF
BBEdit
BBEdit version 14.0 and higher adds support for Language Server Protocols, including PLS. Add the following JSON configuration file, adjusting paths accordingly, to the folder ~/Library/Application Support/BBEdit/Language Servers/Configuration/. Then enable the language server support for Perl following their recommendations, selecting the file you saved for the configuration.
{
"initializationOptions": {},
"workspaceConfigurations": {
"*": {
"pls": {
"inc": [],
"syntax": {
"enabled": true,
"perl": "/usr/bin/perl",
"args": []
},
"perltidy": {
"perltidyrc": "~/.perltidyrc"
},
"perlcritic": {
"enabled": true,
"perlcriticrc": "~/.perlcriticrc"
},
"cwd": "."
}
}
}
}
Configuration
- Make sure that
pls.cmdis set to the path to theplsscript on your system. If you rely on your$PATH, ensure that your editor is configured with the correct path, which may not be the same one that your terminal uses. - Add any additional arguments needed to execute
plsto thepls.argssetting. For example, if you runplsin a docker container,pls.cmdwould bedocker, andpls.argswould be["run", "--rm", "-i", "<image name>", "pls"]. - Optionally, change the current working directory to run PLS in by modifying the
pls.cwdsetting. If you use$ROOT_PATHhere, it will be replaced by the first or only workspace folder. - Add paths to
@INCby modifying thepls.incsetting. You can use the$ROOT_PATHmnemonic to stand in for your project's root directory, for example$ROOT_PATH/lib. If you are using multiple workspace folders and use$ROOT_PATH, the path will be multiplied by the number of workspace folders, and will be replaced that many times. - Configure the path to your
.perltidyrcfile using thepls.perltidy.perltidyrcsetting. The default is~/.perltidyrcif not configured. - Configure the path to your
.perlcriticrcfile using thepls.perlcritic.perlcriticrcsetting. The default is~/.perlcriticrcif not configured. - Disable
perlcriticchecking entirely by settingpls.perlcritic.enabledtofalse. - Disable
podcheckerchecking entirely by settingpls.podchecker.enabledtofalse. - Optionally, configure the path to an alternate
perlto use for syntax checking using thepls.syntax.perlsetting. By default, theperlused to run PLS will be used. - Disable syntax checking entirely by setting
pls.syntax.enabledtofalse. - Pass arguments to your code when syntax checking by setting
pls.syntax.args.- This is likely not useful for most developers, unless your code base changes behavior based on
@ARGVin aBEGINblock.
- This is likely not useful for most developers, unless your code base changes behavior based on
- Create a
.plsignorefile in your workspace root with Perl glob patterns that you do not wish to index. By default, PLS will index all files ending with.pl,.pm, or haveperlin the shebang line that are not.tfiles.- If you have a lot of files that are not Perl files in your workspace, it may slow down indexing if they are not ignored. This is the case for PLS itself, where the entire
clientdirectory is not Perl and contains many small files innode_modules.
- If you have a lot of files that are not Perl files in your workspace, it may slow down indexing if they are not ignored. This is the case for PLS itself, where the entire