9

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
stack_anonymous
  • 129
  • 1
  • 1
  • 5
  • For one thing... To activate Omni completion, you need to use ``, not just `` which will actually only complete from symbols on the same file (which is what you're seeing there...) Not sure if that's the only issue, but I'd start there – filbranden Dec 21 '19 at 16:47
  • 1
    Thanks you so much for this hint! So far I've only been using the jedi-vim plugin and sometimes it completed names from Python modules and sometimes not. I never understood why it behaved kind of randomly. But after your hint and a bit more reading it totally makes sense now. `````` only completes from the local file and `````` also looks into the modules. Even the jedi-vim plugin without LSP has become so much more useful to me now. Big thanks! – stack_anonymous Dec 22 '19 at 18:04
  • Did it solve your issue? If so, feel free to post a self-answer with the details... – filbranden Dec 22 '19 at 18:05
  • 1
    I just tried it with LSP and the ALE plugin. Here I haven't got it working yet. But jedi-vim is now already pretty close to what I've been looking for. But I'll post a reply if I can make it work with ALE as well. – stack_anonymous Dec 22 '19 at 18:12
  • Consider trying [coc.nvim](https://github.com/neoclide/coc.nvim), it's a modern plug-in directly aimed at LSPs. Please note it's not NeoVim-only (as the name might suggest), it works on Vim 8 as well. – filbranden Dec 22 '19 at 18:22

4 Answers4

7

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
Ethan
  • 87
  • 1
  • `coc-python` is [not maintained since November 2020](https://github.com/neoclide/coc-python/commit/8c4bf3a6aa982df32b950170087a76f291046caa). – Michael Franzl Apr 28 '22 at 13:04
3

Self reply 1:

I haven't given up on this yet. I found the following useful link and want to share this: https://yufanlu.net/2018/09/03/neovim-python/

Actually not everything works perfectly with the setup shown in the link, but it already showed me some mistakes I made.

But the ALE plugin in the link is outdate, so you have to change the following line:

Plug 'w0rp/ale'

to this line:

Plug 'dense-analysis/ale'

I assume.

stack_anonymous
  • 129
  • 1
  • 1
  • 5
1

Note that the latest versions of neovim have language server (LSP) support built in. My relevant part of ~/.vimrc looks like this:

call plug#begin('~/.vim/plugged')
:Plug 'neovim/nvim-lsp'
call plug#end()

lua<<
    local status, nvim_lsp = pcall(require, "nvim_lsp")
    if(status) then
        nvim_lsp.pyls.setup{}
    end
.

While at it, I totally recommend adding nnoremap <silent> <F5> <cmd>lua vim.lsp.buf.formatting()<CR>. Having F5 bound to reformat capability bought me into LSP ecosystem immediately.

d33tah
  • 10,999
  • 13
  • 68
  • 158
0

Self reply 2:

Ok, no LSP yet for Python. But I got the feature working that is most important to me, autocompletion. Actually this requires three plugins: jedi-vim, deoplete.nvim and deoplete-jedi. Now nvim shows potential syntax as you're typing. The config file is still quite easy:

call plug#begin('~/.local/share/nvim/plugged')
" Plugins:

" autocompletion for Python
Plug 'davidhalter/jedi-vim'

" show autocompletion
if has('nvim')
  Plug 'Shougo/deoplete.nvim', { 'do': ':UpdateRemotePlugins' }
else
  Plug 'Shougo/deoplete.nvim'
  Plug 'roxma/nvim-yarp'
  Plug 'roxma/vim-hug-neovim-rpc'
endif
" further plugin for autocompletion
Plug 'deoplete-plugins/deoplete-jedi'

" Initialize plugin system
call plug#end()

" enable deoplete
let g:deoplete#enable_at_startup = 1

The first time autocompletion is used, there's a littel lag, as it need some time to load. But afterwards the suggestions are shown quickly. You can switch through the list via the arrow keys or <Ctrl>-Nand <Ctrl>-P, finally <Enter> pasts the selected expression.

And of course nvim needs Python to be available:

pip3 install --user neovim
stack_anonymous
  • 129
  • 1
  • 1
  • 5