From d20c4badd21da7349bac28f7f3eaf8762f2d90de Mon Sep 17 00:00:00 2001 From: David Halter Date: Mon, 10 Sep 2012 23:07:46 +0200 Subject: [PATCH] renames are now possible = refactoring --- jedi.vim | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 52 insertions(+), 5 deletions(-) diff --git a/jedi.vim b/jedi.vim index a6b0bab..b6d4cfa 100644 --- a/jedi.vim +++ b/jedi.vim @@ -92,6 +92,44 @@ function! jedi#related_names() python _goto(is_related_name=True) endfunction +" ------------------------------------------------------------------------ +" rename +" ------------------------------------------------------------------------ +function! jedi#rename() +python << PYTHONEOF +if 1: + if temp_rename is None: + temp_rename = _goto(is_related_name=True, no_output=True) + _rename_cursor = vim.current.window.cursor + + vim.command('augroup jedi_rename') + vim.command('autocmd InsertLeave * call jedi#rename()') + vim.command('augroup END') + + vim.command('normal! diw') + vim.command(':startinsert') + else: + current_buf = vim.current.buffer.name + replace = vim.eval("expand('')") + vim.command('normal! u') # undo new word + vim.command('normal! u') # 2u didn't work... + + for r in temp_rename: + start_pos = r.start_pos + (0, 1) # vim cursor starts with 1 indent + # TODO switch modules + if vim.current.buffer.name == r.module_path: + vim.current.window.cursor = r.start_pos + vim.command('normal! cw%s' % replace) + + # reset autocommand + vim.command('autocmd! jedi_rename InsertLeave') + + echo_highlight('Jedi did %s renames!' % len(temp_rename)) + # reset rename variables + temp_rename = None +PYTHONEOF +endfunction + " ------------------------------------------------------------------------ " show_pydoc " ------------------------------------------------------------------------ @@ -172,7 +210,6 @@ if 1: # just do good old asking for forgiveness. don't know why this happens :-) pass else: - print buf_path, path if buf_path == path: # tab exists, just switch to that tab vim.command('tabfirst | tabnext %i' % (tab_nr + 1)) @@ -256,6 +293,9 @@ endif if !exists("g:jedi#related_names_command") let g:jedi#related_names_command = "n" endif +if !exists("g:jedi#rename_command") + let g:jedi#rename_command = "r" +endif if !exists("g:jedi#popup_on_dot") let g:jedi#popup_on_dot = 1 endif @@ -272,7 +312,8 @@ if g:jedi#auto_initialization autocmd FileType python execute "noremap ".g:jedi#goto_command." :call jedi#goto()" autocmd FileType python execute "noremap ".g:jedi#get_definition_command." :call jedi#get_definition()" autocmd FileType python execute "noremap ".g:jedi#related_names_command." :call jedi#related_names()" - + " rename + autocmd FileType python execute "noremap ".g:jedi#rename_command." :call jedi#rename()" " pydoc autocmd FileType python execute "nnoremap ".g:jedi#pydoc." :call jedi#show_pydoc()" end @@ -301,16 +342,19 @@ import re # copy that directly into the .vim directory. import functions +temp_rename = None # used for jedi#rename + class PythonToVimStr(str): """ Vim has a different string implementation of single quotes """ __slots__ = [] def __repr__(self): return '"%s"' % self.replace('"', r'\"') -def _goto(is_definition=False, is_related_name=False): - def echo_highlight(msg): - vim.command('echohl WarningMsg | echo "%s" | echohl None' % msg) +def echo_highlight(msg): + vim.command('echohl WarningMsg | echo "%s" | echohl None' % msg) +def _goto(is_definition=False, is_related_name=False, no_output=False): + definitions = [] row, column = vim.current.window.cursor buf_path = vim.current.buffer.name source = '\n'.join(vim.current.buffer) @@ -328,6 +372,8 @@ def _goto(is_definition=False, is_related_name=False): echo_highlight("Some different eror, this shouldn't happen.") print(traceback.format_exc()) else: + if no_output: + return definitions if not definitions: echo_highlight("Couldn't find any definitions for this.") elif len(definitions) == 1 and not is_related_name: @@ -356,6 +402,7 @@ def _goto(is_definition=False, is_related_name=False): lst.append(dict(filename=d.module_path, lnum=d.line_nr, col=d.column+1, text=d.description)) vim.command('call setqflist(%s)' % str(lst)) vim.command('call add_goto_window()') + return definitions PYTHONEOF " vim: set et ts=4: