I've spend quite some time figuring out how to use the Language Server Protocol (LSP) for Python (3) in neovim. Mainly I'm looking for autocompletion with Python 3 and it's modules like PySide2.
Sadly I just can't get my config file (.config/vim/init.vim) to work. I know there are a lot of them on github. But they include so many additional functionality that I've not yet been able to adapt one of the to my needs. And some are also outdated.
So here is what I've tried so far:
www.langserver.org has a quite long list of language clients and servers.
I installed Palantirs Language Server Protocol for Python (https://github.com/palantir/python-language-server):
pip3 install 'python-language-server[all]'
In the next step I installed a language client for neovim via vim-plug. Actually I tried several but let's stick to ale for the example (https://github.com/dense-analysis/ale):
call plug#begin('~/.local/share/nvim/plugged')
" Plugins:
Plug 'dense-analysis/ale'
" Initialize plugin system
call plug#end()
And installed it via :PlugInstall
Then the for autocompletion to work a setting must be made before Ale is loaded:
" initialize before ale is loaded
let g:ale_completion_enabled = 1
For usage with Omnicompletion one more setting is needed:
set omnifunc=ale#completion#OmniFunc
And after some more googling I read that I've to register the language server (https://vi.stackexchange.com/questions/20958/use-the-pyls-python-lsp-with-ale-on-neovim):
if executable('pyls')
au User lsp_setup call lsp#register_server({
\ 'name': 'pyls',
\ 'cmd': {server_info->['pyls']},
\ 'whitelist': ['python'],
\ })
endif
This gives me the final init.vim:
" Specify a directory for plugins
" - For Neovim: ~/.local/share/nvim/plugged
" - Avoid using standard Vim directory names like 'plugin'
call plug#begin('~/.local/share/nvim/plugged')
" Plugins go here:
" Language Server Client
Plug 'dense-analysis/ale'
" initialize before ale is loaded
" is that the right spot?
let g:ale_completion_enabled = 1
" Initialize plugin system
call plug#end()
set omnifunc=ale#completion#OmniFunc
" register the language server
if executable('pyls')
au User lsp_setup call lsp#register_server({
\ 'name': 'pyls',
\ 'cmd': {server_info->['pyls']},
\ 'whitelist': ['python'],
\ })
endif
If I now open a file like the following and press Ctrl + N for completion after PySide2.
I only get the following screen in nvim:
#!/usr/bin/env python
>>from PySide2.usr
-- usr
~ bin
~ env
~ python
~ from
~ PySide2
~
It's just a list of the word that already appeared in the file - just like normal Omnicompletion but not the modules from the PySide2 lib.
I'm just looking for a minimalistic configuration to enable autocompletion via LSP.
Nvim supports the Language Server Protocol (LSP), which means it acts as a client to LSP servers and includes a Lua framework vim. lsp for building enhanced LSP tools.
Language Server Protocol (LSP) is an open, JSON-RPC-based protocol for communication between source code editors and language servers, which provide programming language-specific features such as: Go to definition. (auto)completion. Code Actions (automatic formatting, organize imports, ...)
pygls (pronounced like “pie glass”) is a generic implementation of the Language Server Protocol written in the Python programming language. It allows you to write your own language server in just a few lines of code.
coc.nvim is much easier to setup than ale for lsp
vimrc:
Plug 'neoclide/coc.nvim', {'branch': 'release'}
then from within vim8/nvim:
:PlugInstall
:CocInstall coc-python
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With