1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-02 17:52:45 +01:00
psalm/docs/running_psalm/language_server.md

122 lines
4.1 KiB
Markdown
Raw Normal View History

2020-01-02 22:21:15 +01:00
# Using Psalms Language Server
2018-10-19 23:51:07 +02:00
2018-10-23 23:30:36 +02:00
Psalm now has built-in Language Server Compatibility support so you can run it in your favourite IDE.
2018-10-19 23:51:07 +02:00
2021-01-07 14:48:33 +01:00
It currently supports diagnostics (i.e. finding errors and warnings), go-to-definition and hover, with limited support for autocompletion (PRs are welcome!).
2018-10-19 23:51:07 +02:00
2018-10-23 23:30:36 +02:00
It works well in a variety of editors (listed alphabetically):
2018-10-19 23:51:07 +02:00
## Client configuration
### Emacs
2018-10-19 23:51:07 +02:00
I got it working with [eglot](https://github.com/joaotavora/eglot)
This is the config I used:
```
2018-10-23 16:20:31 +02:00
(when (file-exists-p "vendor/bin/psalm-language-server")
(progn
(require 'php-mode)
(require 'eglot)
(add-to-list 'eglot-server-programs '(php-mode . ("php" "vendor/bin/psalm-language-server")))
(add-hook 'php-mode-hook 'eglot-ensure)
(advice-add 'eglot-eldoc-function :around
(lambda (oldfun)
(let ((help (help-at-pt-kbd-string)))
(if help (message "%s" help) (funcall oldfun)))))
)
)
2018-10-19 23:51:07 +02:00
```
### PhpStorm
2018-10-19 23:51:07 +02:00
#### Native Support
2021-09-21 17:15:06 +02:00
As of PhpStorm 2020.3 support for psalm is supported and on by default, you can read more about that [here](https://www.jetbrains.com/help/phpstorm/using-psalm.html)
2018-10-19 23:51:07 +02:00
#### With LSP
2021-09-21 17:15:06 +02:00
Alternatively, psalm works with `gtache/intellij-lsp` plugin ([Jetbrains-approved version](https://plugins.jetbrains.com/plugin/10209-lsp-support), [latest version](https://github.com/gtache/intellij-lsp/releases/tag/v1.6.0)).
Setup is done via a GUI.
When you install the plugin, you should see a "Language Server Protocol" section under the "Languages & Frameworks" tab.
In the "Server definitions" tab you should add a definition for Psalm:
- Select `Executable`
- Extension: `php`
- Path: `<path-to-php-binary>` e.g. `/usr/local/bin/php` or `C:\php\php.exe`
- this should be an absolute path, not just `php`
2021-09-21 17:15:06 +02:00
- Args: `vendor/bin/psalm-language-server` (on Windows use `vendor/vimeo/psalm/psalm-language-server`, or for a 'global' install '%APPDATA%' + `\Composer\vendor\vimeo\psalm\psalm-language-server`, where the '%APPDATA%' environment variable is probably something like `C:\Users\<homedir>\AppData\Roaming\`)
2022-04-27 07:46:13 +02:00
In the "Timeouts" tab you can adjust the initialization timeout. This is important if you have a large project. You should set the "Init" value to the number of milliseconds you allow Psalm to scan your entire project and your project's dependencies. For opening a couple of projects that use large PHP frameworks, on a high-end business laptop, try `240000` milliseconds for Init.
2021-09-21 17:15:06 +02:00
### Sublime Text
2018-10-19 23:51:07 +02:00
I use the excellent Sublime [LSP plugin](https://github.com/tomv564/LSP) with the following config(Package Settings > LSP > Settings):
2018-10-19 23:51:07 +02:00
```json
"clients": {
"psalm": {
2018-10-19 23:51:07 +02:00
"command": ["php", "vendor/bin/psalm-language-server"],
"selector": "source.php | embedding.php",
"enabled": true
2018-10-19 23:51:07 +02:00
}
}
2018-10-19 23:51:07 +02:00
```
### Vim & Neovim
2018-10-19 23:51:07 +02:00
2018-10-23 23:46:05 +02:00
**ALE**
2018-10-25 19:31:04 +02:00
2019-01-03 13:25:14 +01:00
[ALE](https://github.com/w0rp/ale) has support for Psalm (since v2.3.0).
2018-10-19 23:51:07 +02:00
```vim
2018-11-13 21:28:39 +01:00
let g:ale_linters = { 'php': ['php', 'psalm'] }
```
2018-10-23 23:46:05 +02:00
**vim-lsp**
2018-10-19 23:51:07 +02:00
2019-05-22 05:06:50 +02:00
I also got it working with [vim-lsp](https://github.com/prabirshrestha/vim-lsp)
2018-10-19 23:51:07 +02:00
2018-10-23 23:37:48 +02:00
This is the config I used (for Vim):
2018-10-19 23:51:07 +02:00
```vim
2018-10-19 23:51:07 +02:00
au User lsp_setup call lsp#register_server({
\ 'name': 'psalm-language-server',
\ 'cmd': {server_info->[expand('vendor/bin/psalm-language-server')]},
\ 'allowlist': ['php'],
2018-10-19 23:51:07 +02:00
\ })
```
**coc.nvim**
It also works with [coc.nvim](https://github.com/neoclide/coc.nvim).
Add settings to `coc-settings.json`:
```jsonc
"languageserver": {
"psalmls": {
"command": "vendor/bin/psalm-language-server",
"filetypes": ["php"],
"rootPatterns": ["psalm.xml", "psalm.xml.dist"],
"requireRootPattern": true
}
}
```
### VS Code
2018-10-19 23:51:07 +02:00
2018-10-23 23:35:42 +02:00
[Get the Psalm plugin here](https://marketplace.visualstudio.com/items?itemName=getpsalm.psalm-vscode-plugin) (Requires VS Code 1.26+):
## Running the server in a docker container
Make sure you use `--map-folder` option. Using it without argument will map the server's CWD to the host's project root folder. You can also specify a custom mapping. For example:
```bash
docker-compose exec php /usr/share/php/psalm/psalm-language-server \
-r=/var/www/html \
--map-folder=/var/www/html:$PWD
```