A PHP language server plugin for LanguageClient-neovim. This uses Phan's static analysis capabilities to quickly emit diagnostic issue messages.
This uses Phan.
Also see LanguageServer-php-neovim, a plugin for a different language server (with different features) that this is based on.
- Adds improved error detection from Phan to neovim.
- Analyzes code while you're typing.
- Analyzes remaining statements of code with syntax errors.
- Supports "Go To Definition" (
:call LanguageClient#textDocument_definition()
) - Supports "Hover" (
:call LanguageClient#textDocument_hover()
) - Supports "Completion" (e.g. with C-x o) (requires
let g:phan_enable_completion = 1
to be added to your vim/neovim config)
See LanguageClient-neovim and Example Configuration for LanguageServer-phan-neovim for more documentation on how to use these features.
Note: This is just the neovim plugin that spawns Phan. Phan is implemented purely in PHP in its own repository, bugs in Phan analysis need to be fixed there and all issues should be reported there.
However, bugs in this neovim plugin (crashes, etc) or related to the language server protocol should be reported in this plugin's issue tracker
- PHP 7.2+ must be installed.
You can either add it to your PATH or set the
g:phan_php_binary
setting in~/.config/nvim/init.vim
. (e.g.let g:phan_php_binary = '/usr/local/php7.1/bin/php'
) - (Optional) For optimal performance and accuracy of analysis,
the
php-ast
PECL extension should be installed and enabled.
Using vim-plug.
See https://github.com/junegunn/vim-plug#usage if you are unfamiliar with vim-plug. After adding the plugins to your neovim config, :PlugInstall
must be called to install the plugins.
" Oddly, LanguageClient seems to need a key in LanguageClient_serverCommands when starting up in order for this plugin to work.
" Use literally anything as a placeholder. even 'php': ['/bin/false'],
let g:LanguageClient_serverCommands = {
\ 'php': [$HOME . '/.vim/plugged/LanguageServer-phan-neovim/vendor/phan/phan/phan', '--require-config-exists', '--language-server-on-stdin', '--quick', '--language-server-allow-missing-pcntl', '--use-fallback-parser', '--allow-polyfill-parser', '--memory-limit', '2G', '--language-server-enable-go-to-definition', '--language-server-enable-hover', '--language-server-enable-completion', '--language-server-hide-category', '--analyze-all-files', '--plugin', 'InvokePHPNativeSyntaxCheckPlugin'],
\ }
"" The below would be ~/.vim/plugged in vim
"" uncomment it if a section with plug#begin does not exist already
" call plug#begin('~/.local/share/nvim/plugged')
" Note: this may need to be 'composer.phar update', or contain the full path to composer.phar.
" To update the language server this vim plugin installs, run `:PlugUpdate! LanguageServer-phan-neovim`
Plug 'TysonAndre/LanguageServer-phan-neovim', {'do': 'composer update'}
" Currently, the Phan Language Server only works with a single directory,
" and that directory must be manually configured.
let g:phan_analyzed_directory = '/home/username/path/to/project-with-phan-config/'
"" As a workaround, you can try to guess other projects on startup based on the current working directory
" if getcwd() =~ 'my-other-project"
" let g:phan_analyzed_directory = '/home/username/path/to/my-other-project/'
" endif
" You can optionally use a different Phan version from the one that gets bundled with this
" (Or phan.phar)
" let g:phan_executable_path = '~/path/to/phan-installation/phan'
" Completion is enabled by default if you're running the latest version of this vim plugin.
" let g:phan_enable_completion = 1
" Enable this to run Phan analysis only on file save
" (Not while editing the file)
" This will reduce CPU used and make crashes less likely.
" (LanguageClient-neovim won't automatically restart the server)
" let g:phan_analyze_only_on_save = 1
" You can also add custom CLI flags manually
let g:phan_additional_cli_flags = [
\'--strict-type-checking',
\'--plugin', 'InvokePHPNativeSyntaxCheckPlugin'
\]
" Dependency:
Plug 'autozimu/LanguageClient-neovim', { 'branch': 'next', 'do': 'bash install.sh' }
" call plug#end() " uncomment if a section with plug#begin and plug#end does not exist already
Many more settings for the Phan Language Server exist. :help LanguageServer-phan-neovim
will bring up documentation for these settings
I recommend adding some of the following settings to your Vim 8/Neovim settings as well (this is optional):
function! CopenInBackground()
" get current window number
let winnum = winnr()
" Open the QuickFix window with a height of 5 rows
copen 5
" Switch back from the QuickFix window to the original window and redraw
execute winnum . "wincmd w"
:redraw!
endfunction!
" From :help LanguageClient
augroup LanguageClient_config
autocmd!
" The below setting is recommended for LanguageClient-neovim
" to stop the left column from flickering on and off
" Note: This affects all open panes of vim.
" Use setlocal instead of set if you want to only affect the active pane.
autocmd User LanguageClientStarted set signcolumn=yes
autocmd User LanguageClientStopped set signcolumn=auto
" Discard *all* errors in the QuickFix window if the language server
" stops or crashes
autocmd User LanguageClientStopped call setqflist([])
" This will automatically open the QuickFix window with 5 lines of space
" when the language server starts
" (and close it when it's stopped or crashes)
autocmd User LanguageClientStarted call CopenInBackground()
autocmd User LanguageClientStopped cclose
augroup end
If you install this plugin manually, then execute the following commands in this directory:
# Or possibly composer.phar install
composer install
This repo is updated much less frequently than the Phan repo itself.
To update the version of Phan that is bundled with this client, run :PlugUpdate! LanguageServer-phan-neovim
You must add this to your vimrc or neovim config:
autocmd FileType php LanguageClientStart
LanguageClient - Quick Start has example configuration.
Example aliases
" Keyboard shortcuts to go to the definition or type definition.
nnoremap <silent> g1 :call LanguageClient#textDocument_definition()<CR>
nnoremap <silent> g2 :call LanguageClient#textDocument_typeDefinition()<CR>
" By default, this hover command opens the hover description in a preview pane,
" which can be closed with C-w z or C-w C-z
nnoremap <silent> g3 :call LanguageClient#textDocument_hover()<CR>
" These shortcuts (Ctrl-W followed by the string 'g1', etc.)
" can be used to open the definition in a new pane.
nnoremap <silent> <C-W>g1 :call LanguageClient#textDocument_definition({'gotoCmd': 'split'})<CR>
nnoremap <silent> <C-W>g2 :call LanguageClient#textDocument_typeDefinition({'gotoCmd': 'split'})<CR>
nnoremap <silent> <C-W>g3 :call LanguageClient#textDocument_hover()<CR>
Note: By default, LanguageClient-neovim supports code completion requests with C-x o
:help LanguageServer-phan-neovim
can be used to see the documentation for this PHP language server.
:help LanguageClient
can be used to get documentation for the Vim/Neovim language client settings.