1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 13:51:54 +01:00
psalm/docs/running_psalm/language_server.md

106 lines
3.5 KiB
Markdown
Raw Normal View History

2020-01-02 16:21:15 -05:00
# Using Psalms Language Server
2018-10-19 17:51:07 -04:00
2018-10-23 17:30:36 -04:00
Psalm now has built-in Language Server Compatibility support so you can run it in your favourite IDE.
2018-10-19 17:51:07 -04:00
2019-01-25 11:32:17 -05:00
It currently supports diagnostics (i.e. finding errors and warnings), go-to-definition and hover.
2018-10-19 17:51:07 -04:00
2018-10-23 17:30:36 -04:00
It works well in a variety of editors (listed alphabetically):
2018-10-19 17:51:07 -04:00
2018-10-23 17:46:05 -04:00
## Emacs
2018-10-19 17:51:07 -04:00
I got it working with [eglot](https://github.com/joaotavora/eglot)
This is the config I used:
```
2018-10-23 10:20:31 -04: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 17:51:07 -04:00
```
## PhpStorm
2020-02-12 21:09:33 +02:00
I've got it working 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)).
2018-10-23 11:19:27 -04:00
2018-10-23 11:20:30 -04:00
Setup is done via a GUI.
2018-10-23 11:19:27 -04:00
2018-10-27 12:54:13 -04:00
When you install the plugin, you should see a "Language Server Protocol" section under the "Languages & Frameworks" tab.
2018-10-23 11:19:27 -04:00
In the "Server definitions" tab you should add a definition for Psalm:
2018-10-23 18:31:49 -04:00
- Select `Executable`
2018-10-23 11:19:27 -04:00
- Extension: `php`
- Path: `<path-to-php-binary>` e.g. `/usr/local/bin/php` or `C:\php\php.exe`
2018-10-23 11:19:27 -04:00
- this should be an absolute path, not just `php`
- 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\`)
2018-10-23 11:19:27 -04: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.
2018-10-19 17:51:07 -04:00
2018-10-22 11:58:53 -04:00
## Sublime Text
2018-10-19 17:51:07 -04:00
I use the excellent Sublime [LSP plugin](https://github.com/tomv564/LSP) with the following config:
```json
"psalm":
{
"command": ["php", "vendor/bin/psalm-language-server"],
"scopes": ["source.php", "embedding.php"],
"syntaxes": ["Packages/PHP/PHP.sublime-syntax"],
"languageId": "php"
}
```
2018-10-23 17:37:48 -04:00
## Vim & Neovim
2018-10-19 17:51:07 -04:00
2018-10-23 17:46:05 -04:00
**ALE**
2018-10-25 13:31:04 -04:00
2019-01-03 07:25:14 -05:00
[ALE](https://github.com/w0rp/ale) has support for Psalm (since v2.3.0).
2018-10-19 17:51:07 -04:00
```vim
2018-11-13 15:28:39 -05:00
let g:ale_linters = { 'php': ['php', 'psalm'] }
```
2018-10-23 17:46:05 -04:00
**vim-lsp**
2018-10-19 17:51:07 -04:00
2019-05-21 23:06:50 -04:00
I also got it working with [vim-lsp](https://github.com/prabirshrestha/vim-lsp)
2018-10-19 17:51:07 -04:00
2018-10-23 17:37:48 -04:00
This is the config I used (for Vim):
2018-10-19 17:51:07 -04:00
```vim
2018-10-19 17:51:07 -04:00
au User lsp_setup call lsp#register_server({
\ 'name': 'psalm-language-server',
\ 'cmd': {server_info->[expand('vendor/bin/psalm-language-server')]},
\ 'whitelist': ['php'],
\ })
```
**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
}
}
```
2018-10-19 17:51:07 -04:00
## VS Code
2018-10-23 17:35:42 -04:00
[Get the Psalm plugin here](https://marketplace.visualstudio.com/items?itemName=getpsalm.psalm-vscode-plugin) (Requires VS Code 1.26+):