pythonコーディング用にneovimを最低限整える(2019年春)
もうこの構成では使っていません shase428.hatenablog.jp
こちらを参照してください。
前提
- neovim用のvenvと、各種プロジェクトのvenv(ないしそれに類するもの)の2つの概念がある。
- pyenvでもなんでもいいのでpython3がちゃんと動く状態にある。
- pluginマネージャとして、deinをつかっている(tomlで)。
- ALEはまだ悩み中。
- 一旦最低限ですわ。
イメージ

流れ
- neovim用のvenv環境用意する
- vim pluginの導入と設定
1. neovim用のvenv環境用意する
mkdir ~/neovim-python3 cd neovim-python3/ python3 -m venv neovim source neovim/bin/activate pip3 install pynvim flake8 isort
2. vim pluginの導入と設定
今回導入するもの(python向け)
- 各種補完
- prabirshrestha/vim-lsp
- importのソート(isort)
- fisadev/vim-isort
- formatter(black)
- python/black
- linter
- nvie/vim-flake8
prabirshrestha/vim-lsp
plugin設定まだいろいろ悩み中。 この辺を参考にさせていただきました。m ( _ _ ) m
- Pythonのパス解決問題を解消し、pyls(Python Language Server)を導入した on NeoVim - Qiita
- Pythonの補完環境をjedi-vimからvim-lspに移行した話 — kashew_nuts-blog
- GitHub - prabirshrestha/asyncomplete.vim: async completion in pure vim script for vim8 and neovim
python-language-server をneovim側のvenvじゃなくて、プロジェクト側にpip installしないといけないのが気になっている。
いい方法ないかな。
[[plugins]]
repo = 'prabirshrestha/asyncomplete.vim'
[[plugins]]
repo = 'prabirshrestha/asyncomplete-lsp.vim'
[[plugins]]
repo = 'prabirshrestha/async.vim'
[[plugins]]
repo = 'prabirshrestha/vim-lsp'
depends = 'async.vim'
hook_add = '''
augroup ProjectLSP
autocmd!
let s:pyls_path = fnamemodify(g:project_python_path, ':h') . '/'. 'pyls'
if executable('pyls')
autocmd User lsp_setup call lsp#register_server({
\ 'name': 'pyls',
\ 'cmd': { server_info -> [expand(s:pyls_path)] },
\ 'whitelist': ['python']
\})
endif
augroup END
function! s:configure_lsp() abort
setlocal omnifunc=lsp#complete
nnoremap <buffer> <C-]> :<C-u>LspDefinition<CR>
nnoremap <buffer> gd :<C-u>LspDefinition<CR>
nnoremap <buffer> gD :<C-u>LspReferences<CR>
nnoremap <buffer> gs :<C-u>LspDocumentSymbol<CR>
nnoremap <buffer> gS :<C-u>LspWorkspaceSymbol<CR>
nnoremap <buffer> gQ :<C-u>LspDocumentFormat<CR>
vnoremap <buffer> gQ :LspDocumentRangeFormat<CR>
nnoremap <buffer> K :<C-u>LspHover<CR>
nnoremap <buffer> <F1> :<C-u>LspImplementation<CR>
nnoremap <buffer> <F2> :<C-u>LspRename<CR>
endfunction
augroup LaunchPyls
autocmd!
autocmd BufWinEnter *.py :call lsp#enable()
augroup END
augroup PylsCommands
autocmd!
autocmd BufWinEnter *.py :call lsp#enable()
" local key mapping
autocmd FileType python nnoremap <C-]> :<C-u>LspDefinition<CR>
autocmd FileType python nnoremap K :<C-u>LspHover<CR>
autocmd FileType python nnoremap <LocalLeader>R :<C-u>LspRename<CR>
autocmd FileType python nnoremap <LocalLeader>n :<C-u>LspReferences<CR>
augroup END
let g:lsp_diagnostics_enabled = 0
'''
例によって、 g:project_python_path を渡しています。
fisadev/vim-isort
plugin設定
[[plugins]] repo = 'fisadev/vim-isort' on_ft = ['python']
python/black
plugin設定
[[plugins]] repo = 'python/black' on_ft = ['python']
nvie/vim-flake8
plugin設定
[[plugins]]
repo = 'nvie/vim-flake8'
on_ft = ['python']
hook_source = '''
let g:flake8_show_in_gutter=1
let g:flake8_show_in_file=1
let s:flake8_path = fnamemodify(g:project_python_path, ':h') . '/'. 'flake8'
let g:flake8_cmd = expand(s:flake8_path) . " --config " . expand(g:project_flake8_config)
'''
g:project_python_path で、プロジェクトごとの setup.cfg を参照するようにしている。