Just a few days ago, the builtin Language Server Protocol support has been merged into Neovim master.

This post is a hands-on experience of setting up LSP support for Python with the python-language-server.

Install Neovim

To use the builtin LSP, you have to install Neovim master build release or build Neovim from source yourself. The easiest way is to install Neovim nightly release from here.

Install nvim-lsp

To ease configuration, the contributor to LSP has created the nvim-lsp plugin. Use vim-plug to install this plugin:

Plug 'neovim/nvim-lsp'

Configuration for pyls

First, you need to install python-language-server. You can install python-languages-server and all its dependencies with the following command:

pip install 'python-language-server[all]'

Open Neovim config init.vim and add the following settings:

Click here to show the settings.
let settings = {
          \   "pyls" : {
          \     "enable" : v:true,
          \     "trace" : { "server" : "verbose", },
          \     "commandPath" : "",
          \     "configurationSources" : [ "pycodestyle" ],
          \     "plugins" : {
          \       "jedi_completion" : { "enabled" : v:true, },
          \       "jedi_hover" : { "enabled" : v:true, },
          \       "jedi_references" : { "enabled" : v:true, },
          \       "jedi_signature_help" : { "enabled" : v:true, },
          \       "jedi_symbols" : {
          \         "enabled" : v:true,
          \         "all_scopes" : v:true,
          \       },
          \       "mccabe" : {
          \         "enabled" : v:true,
          \         "threshold" : 15,
          \       },
          \       "preload" : { "enabled" : v:true, },
          \       "pycodestyle" : { "enabled" : v:true, },
          \       "pydocstyle" : {
          \         "enabled" : v:false,
          \         "match" : "(?!test_).*\\.py",
          \         "matchDir" : "[^\\.].*",
          \       },
          \       "pyflakes" : { "enabled" : v:true, },
          \       "rope_completion" : { "enabled" : v:true, },
          \       "yapf" : { "enabled" : v:true, },
          \     }}}
call nvim_lsp#setup("pyls", settings)

" disable preview window
set completeopt-=preview

" use omni completion provided by lsp
set omnifunc=lsp#omnifunc

Then open a Python source file. Note that the builtin LSP does not support auto-completion like what is provided by deoplete. To invoke completion, you need to press <C-X><C-O>1. If all goes well, you should be able to see a completion menu with all the completion items.

The auto-completion experience

Neovim may hang or lag behind when you start completion since all the compeltion items will be shown in the completion menu and the number of items may be huge. There is also no auto-completion like what is provided by other more mature completion engines. It may take a while before you can really use this for your daily work. Right now, I suggest you stick to your current completion engines.

Useful mappings

There are other functions provided by LSP which you may find useful. You can add the following mappings for those functions:

nnoremap <silent> ;dc :call lsp#text_document_declaration()<CR>
nnoremap <silent> ;df :call lsp#text_document_definition()<CR>
nnoremap <silent> ;h  :call lsp#text_document_hover()<CR>
nnoremap <silent> ;i  :call lsp#text_document_implementation()<CR>
nnoremap <silent> ;s  :call lsp#text_document_signature_help()<CR>
nnoremap <silent> ;td :call lsp#text_document_type_definition()<CR>

I tried those mappings, but it seems that not all of the functions can be run with success.

References


  1. For more info, see :h 'omnifunc' and :h i_CTRL-X_CTRL-O. ↩︎