-
Notifications
You must be signed in to change notification settings - Fork 165
Rope in Vim or Neovim
f-leeske edited this page Jul 3, 2024
·
25 revisions
Audience: Rope user
There are a number of Vim integrations for using Rope:
Feature matrix (note: currently incomplete!):
Features | ropevim | python-mode | python-lsp-server (pylsp-rope) |
---|---|---|---|
Renaming | ✔️ | ✔️ | ✔️1 |
Extract method | ✔️ | ✔️ | ✔️2 |
Extract variable | ✔️ | ✔️ | ✔️2 |
Move module/global/method | ✔️ | ✔️ | ❌ |
Inlining | ✔️ | ✔️ | ✔️ |
Use function | ✔️ | ✔️ | ✔️ |
Restructure | ✔️ | ❌ | ❌ |
Introduce Factory | ✔️ | ❌ | ❌ |
Introduce Parameter | ... | ❌ | ✔️ |
Change method signature | ✔️ | ✔️ | ❌ |
Transform module to package | ... | ... | ❌ |
Encapsulate field | ... | ... | ❌ |
Method to method object | ... | ... | ✔️ |
Convert local variable to field | ... | ... | ✔️ |
Organize Import | ... | ✔️ | ✔️ |
Generate variable/function/class | ... | ... | ✔️ |
Footnotes:
1. Currently, python-lsp-server defaults to using Jedi for Renaming; you have to enable Renaming using the built-in Rope integration.
2. extracted variable/methods can only be created using default name by pylsp-rope, you can use the Rename refactoring afterwards to set the function name
" To enable pylsp-rope logging:
" \ 'cmd': {server_info->['pylsp', '-v', '--log-file', '/tmp/vim-lsp-pylsp.log']},
autocmd User lsp_setup call lsp#register_server({
\ 'name': 'pylsp',
\ 'cmd': {server_info->['pylsp']},
\ 'whitelist': ['python'],
\ })
function! s:on_lsp_buffer_enabled() abort
" suggested key mappings
" simple code action for normal mode and visual mode
nmap <buffer> <Leader><Leader> <plug>(lsp-code-action)
vmap <buffer> <Leader><Leader> :LspCodeAction<CR>
" alternative that uses floating window
" nmap <buffer> <Leader>f <plug>(lsp-code-action-float)
" mapping for filtered code actions
nmap <buffer> <Leader>ri :LspCodeAction refactor.inline<CR>
nmap <buffer> <Leader>ro :LspCodeAction source.organizeImports<CR>
vmap <buffer> <Leader>rm :LspCodeAction refactor.extract<CR>
nmap <buffer> <Leader>rm :LspCodeAction refactor.extract<CR>
endfunction
augroup lsp_install
autocmd!
autocmd User lsp_buffer_enabled call s:on_lsp_buffer_enabled()
augroup END
" ALE LSP
let g:ale_linters = {
\ 'python': ['pylsp'],
\}
" To enable pylsp-rope logging:
" let g:ale_python_pylsp_options = '-v --log-file /tmp/ale-pylsp.log'
Note: currently very buggy, not recommended for now
lua << EOF
local nvim_lsp = require('lspconfig')
local on_attach = function(client, bufnr)
local function buf_set_keymap(...) vim.api.nvim_buf_set_keymap(bufnr, ...) end
local function buf_set_option(...) vim.api.nvim_buf_set_option(bufnr, ...) end
local opts = { noremap=true, silent=true }
buf_set_keymap('n', '<Leader>ca', '<cmd>lua vim.lsp.buf.code_action()<CR>', opts)
end
require'lspconfig'.pylsp.setup{
-- To enable pylsp-rope logging
-- cmd = { "pylsp", "-v", "--log-file", "/tmp/nvim-pylsp.log" },
cmd = { "pylsp" },
on_attach = on_attach,
}
EOF
In :CocConfig
:
{
"languageserver": {
"python": {
"command": "python3",
"args": [
"-mpylsp"
// To enable pylsp-rope logging
// ,"-vv", "--log-file", "/tmp/coc-lsp.log"
],
"trace.server": "verbose",
"filetypes": ["python"],
"settings": {
"pylsp": {
"enable": true,
"trace": { "server": "verbose" },
"commandPath": "/pylsp-venv/bin/python",
"configurationSources": ["flake8"],
"plugins": {
//"jedi_rename": { "enabled": false },
//"rope_rename": { "enabled": false },
//"pylsp_rope": { "enabled": true }
}
}
}
}
}
}
map <Leader>ca <Plug>(coc-codeaction-selected)