From 221d7dbee4291aaf6d06648f2e1ec2eefef7611e Mon Sep 17 00:00:00 2001 From: mozbugbox Date: Sun, 20 Mar 2016 23:05:15 +0800 Subject: [PATCH 1/2] Fix: rename() kept wrong cursor position #545 The cursor would change to something else once in the insert mode. If the new name was longer than the old one, the Parser couldn't get the stmt at the wrong cursor position, thus rename failed. Now save the cursor position before entering the insert mode. Then pass the saved position to the 2nd call to the rename(). --- jedi_vim.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/jedi_vim.py b/jedi_vim.py index 862ad3a..7eadcb0 100644 --- a/jedi_vim.py +++ b/jedi_vim.py @@ -507,8 +507,10 @@ def cmdline_call_signatures(signatures): @catch_and_print_exceptions def rename(): if not int(vim.eval('a:0')): + # Need to save the cursor position before insert mode + cursor = vim.current.window.cursor vim_command('augroup jedi_rename') - vim_command('autocmd InsertLeave call jedi#rename(1)') + vim_command('autocmd InsertLeave call jedi#rename({}, {})'.format(cursor[0], cursor[1])) vim_command('augroup END') vim_command("let s:jedi_replace_orig = expand('')") @@ -520,6 +522,8 @@ def rename(): # Remove autocommand. vim_command('autocmd! jedi_rename InsertLeave') + cursor = tuple(int(x) for x in vim.eval('a:000')) + # Get replacement, if there is something on the cursor. # This won't be the case when the user ends insert mode right away, # and `` would pick up the nearest word instead. @@ -528,8 +532,6 @@ def rename(): else: replace = None - cursor = vim.current.window.cursor - # Undo new word, but only if something was changed, which is not the # case when ending insert mode right away. if vim_eval('b:changedtick != s:jedi_changedtick') == '1': From 794af5b90ae889120baa3af21d2434acfe54a97f Mon Sep 17 00:00:00 2001 From: mozbugbox Date: Mon, 21 Mar 2016 09:53:57 +0800 Subject: [PATCH 2/2] Track undo properly in rename(). Fix #502 Use vim changenr() call to track change and undo. This is more robust than bindly call 'normal! u' --- jedi_vim.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/jedi_vim.py b/jedi_vim.py index 7eadcb0..f6aeb18 100644 --- a/jedi_vim.py +++ b/jedi_vim.py @@ -509,8 +509,10 @@ def rename(): if not int(vim.eval('a:0')): # Need to save the cursor position before insert mode cursor = vim.current.window.cursor + changenr = vim.eval('changenr()') # track undo tree vim_command('augroup jedi_rename') - vim_command('autocmd InsertLeave call jedi#rename({}, {})'.format(cursor[0], cursor[1])) + vim_command('autocmd InsertLeave call jedi#rename' + '({}, {}, {})'.format(cursor[0], cursor[1], changenr)) vim_command('augroup END') vim_command("let s:jedi_replace_orig = expand('')") @@ -522,7 +524,9 @@ def rename(): # Remove autocommand. vim_command('autocmd! jedi_rename InsertLeave') - cursor = tuple(int(x) for x in vim.eval('a:000')) + args = vim.eval('a:000') + cursor = tuple(int(x) for x in args[:2]) + changenr = args[2] # Get replacement, if there is something on the cursor. # This won't be the case when the user ends insert mode right away, @@ -532,11 +536,7 @@ def rename(): else: replace = None - # Undo new word, but only if something was changed, which is not the - # case when ending insert mode right away. - if vim_eval('b:changedtick != s:jedi_changedtick') == '1': - vim_command('normal! u') # Undo new word. - vim_command('normal! u') # Undo diw. + vim_command('undo {}'.format(changenr)) vim.current.window.cursor = cursor