renames are now possible = refactoring

This commit is contained in:
David Halter
2012-09-10 23:07:46 +02:00
parent 50906ea84a
commit d20c4badd2

View File

@@ -92,6 +92,44 @@ function! jedi#related_names()
python _goto(is_related_name=True) python _goto(is_related_name=True)
endfunction 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('<cword>')")
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 " show_pydoc
" ------------------------------------------------------------------------ " ------------------------------------------------------------------------
@@ -172,7 +210,6 @@ if 1:
# just do good old asking for forgiveness. don't know why this happens :-) # just do good old asking for forgiveness. don't know why this happens :-)
pass pass
else: else:
print buf_path, path
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))
@@ -256,6 +293,9 @@ endif
if !exists("g:jedi#related_names_command") if !exists("g:jedi#related_names_command")
let g:jedi#related_names_command = "<leader>n" let g:jedi#related_names_command = "<leader>n"
endif endif
if !exists("g:jedi#rename_command")
let g:jedi#rename_command = "<leader>r"
endif
if !exists("g:jedi#popup_on_dot") if !exists("g:jedi#popup_on_dot")
let g:jedi#popup_on_dot = 1 let g:jedi#popup_on_dot = 1
endif endif
@@ -272,7 +312,8 @@ if g:jedi#auto_initialization
autocmd FileType python execute "noremap <buffer>".g:jedi#goto_command." :call jedi#goto()<CR>" autocmd FileType python execute "noremap <buffer>".g:jedi#goto_command." :call jedi#goto()<CR>"
autocmd FileType python execute "noremap <buffer>".g:jedi#get_definition_command." :call jedi#get_definition()<CR>" autocmd FileType python execute "noremap <buffer>".g:jedi#get_definition_command." :call jedi#get_definition()<CR>"
autocmd FileType python execute "noremap <buffer>".g:jedi#related_names_command." :call jedi#related_names()<CR>" autocmd FileType python execute "noremap <buffer>".g:jedi#related_names_command." :call jedi#related_names()<CR>"
" rename
autocmd FileType python execute "noremap <buffer>".g:jedi#rename_command." :call jedi#rename()<CR>"
" pydoc " pydoc
autocmd FileType python execute "nnoremap <silent> <buffer>".g:jedi#pydoc." :call jedi#show_pydoc()<CR>" autocmd FileType python execute "nnoremap <silent> <buffer>".g:jedi#pydoc." :call jedi#show_pydoc()<CR>"
end end
@@ -301,16 +342,19 @@ import re
# copy that directly into the .vim directory. # copy that directly into the .vim directory.
import functions import functions
temp_rename = None # used for jedi#rename
class PythonToVimStr(str): class PythonToVimStr(str):
""" Vim has a different string implementation of single quotes """ """ Vim has a different string implementation of single quotes """
__slots__ = [] __slots__ = []
def __repr__(self): def __repr__(self):
return '"%s"' % self.replace('"', r'\"') return '"%s"' % self.replace('"', r'\"')
def _goto(is_definition=False, is_related_name=False): def echo_highlight(msg):
def echo_highlight(msg): vim.command('echohl WarningMsg | echo "%s" | echohl None' % 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 row, column = vim.current.window.cursor
buf_path = vim.current.buffer.name buf_path = vim.current.buffer.name
source = '\n'.join(vim.current.buffer) 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.") echo_highlight("Some different eror, this shouldn't happen.")
print(traceback.format_exc()) print(traceback.format_exc())
else: else:
if no_output:
return definitions
if not definitions: if not definitions:
echo_highlight("Couldn't find any definitions for this.") echo_highlight("Couldn't find any definitions for this.")
elif len(definitions) == 1 and not is_related_name: 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)) 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 setqflist(%s)' % str(lst))
vim.command('call <sid>add_goto_window()') vim.command('call <sid>add_goto_window()')
return definitions
PYTHONEOF PYTHONEOF
" vim: set et ts=4: " vim: set et ts=4: