rename: refactor into do_rename, add rename_visual

This adds a visual mode map for renaming, which asks for the new name
via input().

`rename` itself could be changed to use `input()`, too - but I've left
it for now.
This commit is contained in:
Daniel Hahler
2015-05-02 18:51:08 +02:00
parent 154e2dbae3
commit 4d67c1a6e3
3 changed files with 57 additions and 38 deletions

View File

@@ -179,6 +179,10 @@ function! jedi#rename(...)
PythonJedi jedi_vim.rename()
endfunction
function! jedi#rename_visual(...)
PythonJedi jedi_vim.rename_visual()
endfunction
function! jedi#completions(findstart, base)
PythonJedi jedi_vim.completions()
endfunction

View File

@@ -19,6 +19,7 @@ if g:jedi#auto_initialization
" rename
if g:jedi#rename_command != ''
execute "nnoremap <buffer> ".g:jedi#rename_command." :call jedi#rename()<CR>"
execute "vnoremap <buffer> ".g:jedi#rename_command." :call jedi#rename_visual()<CR>"
endif
" documentation/pydoc
if g:jedi#documentation_command != ''

View File

@@ -425,27 +425,40 @@ def rename():
vim_command('autocmd InsertLeave <buffer> call jedi#rename(1)')
vim_command('augroup END')
vim_eval("let s:jedi_replace_orig = expand('<cword>')")
vim_command("let s:jedi_replace_orig = expand('<cword>')")
vim_command('normal! diw')
vim_command(':startinsert')
else:
# reset autocommand
vim_command('autocmd! jedi_rename InsertLeave')
# Save original window / tab.
saved_tab = int(vim_eval('tabpagenr()'))
saved_win = int(vim_eval('winnr()'))
replace = vim_eval("expand('<cword>')")
vim_command('normal! u') # undo new word
cursor = vim.current.window.cursor
vim_command('normal! u') # undo the space at the end
vim.current.window.cursor = cursor
return do_rename(replace)
def rename_visual():
replace = vim.eval('input("Rename to: ")')
orig = vim.eval('getline(".")[(getpos("\'<")[2]-1):getpos("\'>")[2]]')
do_rename(replace, orig)
def do_rename(replace, orig = None):
if replace is None:
echo_highlight('No rename possible, if no name is given.')
else:
return
if orig is None:
orig = vim_eval('s:jedi_replace_orig')
# Save original window / tab.
saved_tab = int(vim_eval('tabpagenr()'))
saved_win = int(vim_eval('winnr()'))
temp_rename = goto(is_related_name=True, no_output=True)
# sort the whole thing reverse (positions at the end of the line
# must be first, because they move the stuff before the position).
@@ -467,6 +480,7 @@ def rename():
# Save view.
saved_view = vim_eval('winsaveview()')
# Replace original word.
vim.current.window.cursor = r.start_pos
vim_command('normal! c{:d}l{}'.format(len(orig), replace))