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

sta*_*ous 7 python neovim language-server-protocol

我花了很多时间弄清楚如何在 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]'
Run Code Online (Sandbox Code Playgroud)

在下一步中,我通过 vim-plug 为 neovim 安装了一个语言客户端。实际上我尝试了几个,但让我们坚持以 ale 为例(https://github.com/dense-analysis/ale):

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

" Initialize plugin system
call plug#end()
Run Code Online (Sandbox Code Playgroud)

并通过安装它 :PlugInstall

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

" initialize before ale is loaded
let g:ale_completion_enabled = 1
Run Code Online (Sandbox Code Playgroud)

要与 Omnicompletion 一起使用,还需要另外一项设置:

set omnifunc=ale#completion#OmniFunc
Run Code Online (Sandbox Code Playgroud)

经过更多的谷歌搜索,我读到我必须注册语言服务器(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
Run Code Online (Sandbox Code Playgroud)

这给了我最终的 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
Run Code Online (Sandbox Code Playgroud)

如果我现在打开一个如下所示的文件并在PySide2.我只在 nvim 中看到以下屏幕后按 Ctrl + N 完成:

#!/usr/bin/env python

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

Run Code Online (Sandbox Code Playgroud)

它只是一个已经出现在文件中的单词列表——就像普通的 Omnicompletion 但不是来自 PySide2 库的模块。

我只是在寻找一种简约的配置来通过 LSP 启用自动完成。

小智 5

coc.nvim 比 ale for lsp 更容易设置

vimrc:

Plug 'neoclide/coc.nvim', {'branch': 'release'}
Run Code Online (Sandbox Code Playgroud)

然后从 vim8/nvim 中:

:PlugInstall
:CocInstall coc-python
Run Code Online (Sandbox Code Playgroud)


sta*_*ous 2

自我回复1:

我还没有放弃这个。我发现以下有用的链接并想分享: https ://yufanlu.net/2018/09/03/neovim-python/

实际上,并非所有内容都与链接中显示的设置完美配合,但它已经向我展示了我犯的一些错误。

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

Plug 'w0rp/ale'
Run Code Online (Sandbox Code Playgroud)

到这一行:

Plug 'dense-analysis/ale'
Run Code Online (Sandbox Code Playgroud)

我假设。