mirror of
https://github.com/davidhalter/jedi-vim.git
synced 2025-12-07 03:04:30 +08:00
@@ -199,6 +199,10 @@ function! jedi#rename(...)
|
|||||||
PythonJedi jedi_vim.rename()
|
PythonJedi jedi_vim.rename()
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
function! jedi#rename_visual(...)
|
||||||
|
PythonJedi jedi_vim.rename_visual()
|
||||||
|
endfunction
|
||||||
|
|
||||||
function! jedi#completions(findstart, base)
|
function! jedi#completions(findstart, base)
|
||||||
PythonJedi jedi_vim.completions()
|
PythonJedi jedi_vim.completions()
|
||||||
endfunction
|
endfunction
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ if g:jedi#auto_initialization
|
|||||||
" rename
|
" rename
|
||||||
if g:jedi#rename_command != ''
|
if g:jedi#rename_command != ''
|
||||||
execute "nnoremap <buffer> ".g:jedi#rename_command." :call jedi#rename()<CR>"
|
execute "nnoremap <buffer> ".g:jedi#rename_command." :call jedi#rename()<CR>"
|
||||||
|
execute "vnoremap <buffer> ".g:jedi#rename_command." :call jedi#rename_visual()<CR>"
|
||||||
endif
|
endif
|
||||||
" documentation/pydoc
|
" documentation/pydoc
|
||||||
if g:jedi#documentation_command != ''
|
if g:jedi#documentation_command != ''
|
||||||
|
|||||||
53
jedi_vim.py
53
jedi_vim.py
@@ -431,10 +431,10 @@ def rename():
|
|||||||
vim_command('autocmd InsertLeave <buffer> call jedi#rename(1)')
|
vim_command('autocmd InsertLeave <buffer> call jedi#rename(1)')
|
||||||
vim_command('augroup END')
|
vim_command('augroup END')
|
||||||
|
|
||||||
|
vim_command("let s:jedi_replace_orig = expand('<cword>')")
|
||||||
vim_command('normal! diw')
|
vim_command('normal! diw')
|
||||||
vim_command(':startinsert')
|
vim_command(':startinsert')
|
||||||
else:
|
else:
|
||||||
window_path = vim.current.buffer.name
|
|
||||||
# reset autocommand
|
# reset autocommand
|
||||||
vim_command('autocmd! jedi_rename InsertLeave')
|
vim_command('autocmd! jedi_rename InsertLeave')
|
||||||
|
|
||||||
@@ -444,14 +444,33 @@ def rename():
|
|||||||
vim_command('normal! u') # undo the space at the end
|
vim_command('normal! u') # undo the space at the end
|
||||||
vim.current.window.cursor = cursor
|
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:
|
if replace is None:
|
||||||
echo_highlight('No rename possible, if no name is given.')
|
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)
|
temp_rename = goto(is_related_name=True, no_output=True)
|
||||||
# sort the whole thing reverse (positions at the end of the line
|
# sort the whole thing reverse (positions at the end of the line
|
||||||
# must be first, because they move the stuff before the position).
|
# must be first, because they move the stuff before the position).
|
||||||
temp_rename = sorted(temp_rename, reverse=True,
|
temp_rename = sorted(temp_rename, reverse=True,
|
||||||
key=lambda x: (x.module_path, x.start_pos))
|
key=lambda x: (x.module_path, x.start_pos))
|
||||||
|
buffers = set()
|
||||||
for r in temp_rename:
|
for r in temp_rename:
|
||||||
if r.in_builtin_module():
|
if r.in_builtin_module():
|
||||||
continue
|
continue
|
||||||
@@ -459,16 +478,30 @@ def rename():
|
|||||||
if vim.current.buffer.name != r.module_path:
|
if vim.current.buffer.name != r.module_path:
|
||||||
result = new_buffer(r.module_path)
|
result = new_buffer(r.module_path)
|
||||||
if not result:
|
if not result:
|
||||||
return
|
echo_highlight("Jedi-vim: failed to create buffer window for {}!".format(r.module_path))
|
||||||
|
continue
|
||||||
|
|
||||||
|
buffers.add(vim.current.buffer.name)
|
||||||
|
|
||||||
|
# Save view.
|
||||||
|
saved_view = vim_eval('winsaveview()')
|
||||||
|
|
||||||
|
# Replace original word.
|
||||||
vim.current.window.cursor = r.start_pos
|
vim.current.window.cursor = r.start_pos
|
||||||
vim_command('normal! cw%s' % replace)
|
vim_command('normal! c{:d}l{}'.format(len(orig), replace))
|
||||||
|
|
||||||
result = new_buffer(window_path)
|
# Restore view.
|
||||||
if not result:
|
vim_command('call winrestview(%s)' % PythonToVimStr(saved_view))
|
||||||
return
|
|
||||||
vim.current.window.cursor = cursor
|
# Restore previous tab and window.
|
||||||
echo_highlight('Jedi did %s renames!' % len(temp_rename))
|
vim_command('tabnext {:d}'.format(saved_tab))
|
||||||
|
vim_command('{:d}wincmd w'.format(saved_win))
|
||||||
|
|
||||||
|
if len(buffers) > 1:
|
||||||
|
echo_highlight('Jedi did {:d} renames in {:d} buffers!'.format(
|
||||||
|
len(temp_rename), len(buffers)))
|
||||||
|
else:
|
||||||
|
echo_highlight('Jedi did {:d} renames!'.format(len(temp_rename)))
|
||||||
|
|
||||||
|
|
||||||
@_check_jedi_availability(show_error=True)
|
@_check_jedi_availability(show_error=True)
|
||||||
@@ -567,6 +600,8 @@ def _tabnew(path, options=''):
|
|||||||
if buf_path == path:
|
if buf_path == path:
|
||||||
# tab exists, just switch to that tab
|
# tab exists, just switch to that tab
|
||||||
vim_command('tabfirst | tabnext %i' % (tab_nr + 1))
|
vim_command('tabfirst | tabnext %i' % (tab_nr + 1))
|
||||||
|
# Goto the buffer's window.
|
||||||
|
vim_command('exec bufwinnr(%i) . " wincmd w"' % (buf_nr + 1))
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
continue
|
continue
|
||||||
|
|||||||
Reference in New Issue
Block a user