年底了,再記一篇vim的lsp的玩耍。
LSP並不是配置python開發環境的唯一方式,不過是一個有意思的方式。
她給VIM提供了更多類似與IDE的功能,而不是隻有補全。
展示(所見即所得)
-
Rename
-
auto completion
-
formatting
-
hover
-
code linting
-
find references
-
find symbols
LSP
什麼是LSP呢?
Language Server Protocol 簡單說就是一個給各種編輯器提供開發代碼功能的服務,因爲是protocol,她可以被各種不同的編輯器使用。
舉個例子:
開發python。用vim你需要裝python的vim插件。用vs code你需要裝vs code的插件,等等。LSP提供一個服務,這兩個編輯器都可以使用,並且行爲統一。
(那爲什麼不用IDE? 恩,你應該去問爲什麼那些人要用vim?爲什麼呢?爲什麼吶? lol )-
主要的功能:
- Code Linting (語法錯誤判定)
- Auto Completion (自動補全)
- Signature Help (function 變量提示)
- Go to definition (goto 聲明)
- Hover (提示)
- Find References (找到References)
- Document Symbols (顯示代碼的變量和function)
- Document Formatting (格式化代碼)
(翻譯的不好,自己體會吧 :P)
安裝
這裏要安裝的是Python Language Server 並把它和vim結合使用。
https://github.com/palantir/python-language-server
pip install jedi
pip install 'python-language-server[all]'
LanguageClient-neovim
LanguageClient-neovim 是neovim的Languae Client實現。我自己用neovim,這是我的首選。她號稱也支持vim,但是我沒使用過。
https://github.com/autozimu/LanguageClient-neovim
- 安裝
Usingvim-plug
:
Plug 'autozimu/LanguageClient-neovim', {
\ 'branch': 'next',
\ 'do': 'bash install.sh',
\ }
" (Optional) Multi-entry selection UI.
Plug 'junegunn/fzf'
Plug 'Shougo/deoplete.nvim', { 'do': ':UpdateRemotePlugins' }
- 配置.vimrc
help: https://github.com/autozimu/LanguageClient-neovim/blob/next/doc/LanguageClient.txt
下面是我的配置,做個參考。你可以只參考python的,其他語言需要安裝對應的LanguageServer
" LSP
let g:LanguageClient_rootMarkers = {
¦ \ 'go': ['.git', 'go.mod'],
¦ \ }
let g:LanguageClient_serverCommands = {
\ 'python': ['pyls'],
\'shell': ['shellcheck'],
\'java':['/opt/jdt/jdtls.sh'],
\ 'go': ['bingo', '--mode', 'stdio','--pprof', ':6060'],
\ }
" LanguageClient
nnoremap <space>li :call LanguageClient_textDocument_implementation()<CR>
nnoremap <space>ld :call LanguageClient#textDocument_definition()<CR>
nnoremap <space>lr :call LanguageClient#textDocument_rename()<CR>
nnoremap <space>lf :call LanguageClient#textDocument_formatting()<CR>
nnoremap <space>lt :call LanguageClient#textDocument_typeDefinition()<CR>
nnoremap <space>lx :call LanguageClient#textDocument_references()<CR>
nnoremap <space>la :call LanguageClient_workspace_applyEdit()<CR>
nnoremap <space>lh :call LanguageClient#textDocument_hover()<CR>
nnoremap <space>ls :call LanguageClient_textDocument_documentSymbol()<CR>
nnoremap <space>lc :call LanguageClient_textDocument_codeAction()<CR>
nnoremap <space>lu :call *LanguageClient#textDocument_documentHighlight()<CR>
nnoremap <space>lm :call LanguageClient_contextMenu()<CR>
nnoremap <space>pc :pc<CR>