Refactoring in Vim

前端 未结 15 2219
醉梦人生
醉梦人生 2021-01-29 17:44

Of course the fact that you can refactor on IDEs is priceless for many, I hardly ever do it when I am coding but I may try to do it when editing some one else\'s source. How do

15条回答
  •  感动是毒
    2021-01-29 18:23

    Language Server Protocol (LSP)

    The Language server protocol contains the feature for smart renaming of symbols across a project:

    https://microsoft.github.io//language-server-protocol/specifications/specification-3-14/#textDocument_rename

    For example following language server support this:

    • Clangd for C++
    • ccls for C/C++/Objective-C
    • Eclipse.jdt.ls for Java
    • pyls (with rope) for Python
    • tsserver for TypeScript
    • Solargraph for Ruby
    • gopls official lsp for Go (alpha stage in Nov 2019)
    • texlab for LaTeX

    You can find more language servers under https://langserver.org/.

    Vim

    A vim editor client is necessary to use them within vim. Following options exist:

    1. LanguageClient-neovim (requires rust) suggests the mapping:

       nnoremap   :call LanguageClient_textDocument_rename()
      
    2. coc.nvim (requires node.js) suggests the mapping:

       " Remap for rename current word
       nmap rn (coc-rename)
      
    3. Ale has

       nnoremap  (ale_rename) :ALERename
      

      Ale does not define any keybindings. This has to be done by the user.

    4. vim-lsp provides following command

       :LspRename
      

      Similar to Ale no mapping is suggested. However, of course you can define one as following

       nmap r (lsp-rename)
      

      (r is to be replaced by your choice; I do not know one which most plugins agree on)

    5. vim-lsc has a default mapping:

       'Rename': 'gR'
      

    See also YouCompleteMe which facilitates LSPs as well.

    Neovim

    Neovim has initial builtin support for lsp since 13.11.2019

    See for common configurations of LSPs https://github.com/neovim/nvim-lsp

    However, I could not figure out how smart renaming works. If someone knows this, please update this section.

    Other Refactorings

    I do not know if there are plans for the LSP protocol to support more complex refactorings, such as changing class structure, adding parameters to methods/functions or moving a method to a different class. For a list of refactorings see https://refactoring.com/catalog/.

提交回复
热议问题