如何在Neovim中使用Python语言服务器协议

问题描述 投票:1回答:2

我花了很多时间弄清楚如何在neovim中为Python(3)使用语言服务器协议(LSP)。主要是我在寻找使用Python 3的自动补全功能,它是像PySide2这样的模块。

可悲的是,我无法使我的配置文件(.config / vim / init.vim)正常工作。我知道github上有很多。但是它们包含了许多其他功能,以至于我还无法适应其中的一种。还有一些也已经过时。

所以这是我到目前为止尝试过的:

www.langserver.org具有相当长的语言客户端和服务器列表。

我安装了适用于Python的Palantirs语言服务器协议(https://github.com/palantir/python-language-server

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

下一步,我通过vim-plug为neovim安装了语言客户端。实际上,我尝试了几次,但我们还是以啤酒为例(https://github.com/dense-analysis/ale):

call plug#begin('~/.local/share/nvim/plugged')
" Plugins:
Plug 'dense-analysis/ale'

" Initialize plugin system
call plug#end()

并通过:PlugInstall安装

然后必须在加载Ale之前进行自动完成的设置:

" initialize before ale is loaded
let g:ale_completion_enabled = 1

要与Omnicompletion配合使用,还需要进行一项设置:

set omnifunc=ale#completion#OmniFunc

并且经过更多的谷歌搜索之后,我读到我必须注册语言服务器(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

这给了我最终的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

如果现在我打开如下文件,并在PySide2.之后按Ctrl + N进行完成,那么我只会在nvim中获得以下屏幕:

#!/usr/bin/env python

>>from PySide2.usr 
--             usr            
~              bin            
~              env            
~              python         
~              from           
~              PySide2        
~

这只是文件中已经出现的单词的列表,就像普通的Omnicompletion,但不是PySide2 lib中的模块。

我只是在寻找一种简化的配置,以通过LSP启用自动完成功能。

python neovim language-server-protocol
2个回答
0
投票

自我回复1:

我还没有放弃。我找到了以下有用的链接,并希望分享:https://yufanlu.net/2018/09/03/neovim-python/

实际上,并非所有内容都能完美显示链接中显示的设置,但是它已经显示出我所犯的一些错误。

但是链接中的ALE插件已过期,因此您必须更改以下行:

Plug 'w0rp/ale'

到此行:

Plug 'dense-analysis/ale'

我认为。


0
投票

自我回复2:

[确定,Python还没有LSP。但是,自动补全功能对我来说最重要。实际上,这需要三个插件:jedi-vim,deoplete.nvim和deoplete-jedi。现在,nvim会在您键入时显示潜在的语法。配置文件仍然很容易:

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

首次使用自动补全功能时,由于需要一些时间来加载,因此存在小延迟。但是之后,建议很快就会显示出来。您可以通过箭头键或<Ctrl>-N<Ctrl>-P在列表中进行切换,最后<Enter>将粘贴所选的表达式。

当然,nvim需要Python可用:

pip3 install --user neovim
© www.soinside.com 2019 - 2024. All rights reserved.