Python开发环境:LanguageClient-neovim & Python Language Server

年底了,再记一篇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

Plug 'autozimu/LanguageClient-neovim', {
    \ 'branch': 'next',
    \ 'do': 'bash install.sh',
    \ }

" (Optional) Multi-entry selection UI.
Plug 'junegunn/fzf'

Plug 'Shougo/deoplete.nvim', { 'do': ':UpdateRemotePlugins' }
  " 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>    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章