From f286f9e4b6f3be592fba1211fb36eb31100dbbef Mon Sep 17 00:00:00 2001 From: James Cherti <60946298+jamescherti@users.noreply.github.com> Date: Fri, 23 Dec 2022 12:36:27 -0500 Subject: [PATCH] Key mapping R: Rename vars/functions/classes without deleting the word under the cursor This pull request also adds the support for 'set selection=exclusive' to the function 'rename_visual()' (bug fix). 'R' in Normal mode: --------------------------- Jedi-vim keeps the word under the cursor, moves the cursor to the end of the word, and puts Vim in insert mode, where the user is expected to enter the new variable name. 'R' in Visual mode: --------------------------- Use the selected text as the default answer to the prompt. --- README.rst | 1 + autoload/jedi.vim | 9 +++++++++ doc/jedi-vim.txt | 21 +++++++++++++++++---- ftplugin/python/jedi.vim | 4 ++++ pythonx/jedi_vim.py | 27 ++++++++++++++++++++++----- 5 files changed, 53 insertions(+), 9 deletions(-) diff --git a/README.rst b/README.rst index 852cf13..7d8c6d5 100644 --- a/README.rst +++ b/README.rst @@ -207,6 +207,7 @@ get more information. If you set them to ``""``, they are not assigned. let g:jedi#usages_command = "n" let g:jedi#completions_command = "" let g:jedi#rename_command = "r" + let g:jedi#rename_command_keep_name = "R" An example for setting up your project: diff --git a/autoload/jedi.vim b/autoload/jedi.vim index dcef9b6..0e6a9ba 100644 --- a/autoload/jedi.vim +++ b/autoload/jedi.vim @@ -24,6 +24,7 @@ let s:default_settings = { \ 'call_signatures_command': "'n'", \ 'usages_command': "'n'", \ 'rename_command': "'r'", + \ 'rename_command_keep_name': "'R'", \ 'completions_enabled': 1, \ 'popup_on_dot': 'g:jedi#completions_enabled', \ 'documentation_command': "'K'", @@ -370,6 +371,14 @@ function! jedi#rename_visual(...) abort python3 jedi_vim.rename_visual() endfunction +function! jedi#rename_keep_name(...) abort + python3 jedi_vim.rename(delete_word=False) +endfunction + +function! jedi#rename_visual_keep_name(...) abort + python3 jedi_vim.rename_visual(use_selected_text_as_prompt_answer=True) +endfunction + function! jedi#completions(findstart, base) abort python3 jedi_vim.completions() endfunction diff --git a/doc/jedi-vim.txt b/doc/jedi-vim.txt index fa3d0c5..f73fb38 100644 --- a/doc/jedi-vim.txt +++ b/doc/jedi-vim.txt @@ -27,8 +27,9 @@ Contents *jedi-vim-contents* 5.4 Go to stub |g:jedi#goto_stubs_command| 5.5. Show documentation |g:jedi#documentation_command| 5.6. Rename variables |g:jedi#rename_command| - 5.7. Show name usages |g:jedi#usages_command| - 5.8. Open module by name |:Pyimport| + 5.7. Rename variables (Reuse name) |g:jedi#rename_command_keep_name| + 5.8. Show name usages |g:jedi#usages_command| + 5.9. Open module by name |:Pyimport| 6. Configuration |jedi-vim-configuration| 6.1. auto_initialization |g:jedi#auto_initialization| 6.2. auto_vim_configuration |g:jedi#auto_vim_configuration| @@ -307,7 +308,19 @@ with the new one. The number of performed renames is displayed in the command line. ------------------------------------------------------------------------------ -5.7. `g:jedi#usages_command` *g:jedi#usages_command* +5.7. `g:jedi#rename_command_keep_name` *g:jedi#rename_command_keep_name* +Function: `jedi#rename()` +Default: R Rename variables + (This key mapping does not delete the word under the cursor) + +Jedi-vim keeps the word currently under the cursor, moves the cursor to the end +of the word, and puts Vim in insert mode, where the user is expected to enter +the new variable name. Upon leaving insert mode, Jedi-vim then renames all +occurrences of the old variable name with the new one. The number of performed +renames is displayed in the command line. + +------------------------------------------------------------------------------ +5.8. `g:jedi#usages_command` *g:jedi#usages_command* Function: `jedi#usages()` Default: n Show usages of a name. @@ -315,7 +328,7 @@ The quickfix window is populated with a list of all names which point to the definition of the name under the cursor. ------------------------------------------------------------------------------ -5.8. Open module by name *:Pyimport* +5.9. Open module by name *:Pyimport* Function: `jedi#py_import(args)` Default: :Pyimport e.g. `:Pyimport os` shows os.py in VIM. diff --git a/ftplugin/python/jedi.vim b/ftplugin/python/jedi.vim index abb9c5e..2cddeed 100644 --- a/ftplugin/python/jedi.vim +++ b/ftplugin/python/jedi.vim @@ -27,6 +27,10 @@ if g:jedi#auto_initialization execute 'nnoremap '.g:jedi#rename_command.' :call jedi#rename()' execute 'vnoremap '.g:jedi#rename_command.' :call jedi#rename_visual()' endif + if len(g:jedi#rename_command_keep_name) + execute 'nnoremap '.g:jedi#rename_command_keep_name.' :call jedi#rename_keep_name()' + execute 'vnoremap '.g:jedi#rename_command_keep_name.' :call jedi#rename_visual_keep_name()' + endif " documentation/pydoc if len(g:jedi#documentation_command) execute 'nnoremap '.g:jedi#documentation_command.' :call jedi#show_documentation()' diff --git a/pythonx/jedi_vim.py b/pythonx/jedi_vim.py index 20b371d..8d007a8 100644 --- a/pythonx/jedi_vim.py +++ b/pythonx/jedi_vim.py @@ -998,7 +998,7 @@ def cmdline_call_signatures(signatures): @_check_jedi_availability(show_error=True) @catch_and_print_exceptions -def rename(): +def rename(delete_word=True): if not int(vim.eval('a:0')): # Need to save the cursor position before insert mode cursor = vim.current.window.cursor @@ -1010,7 +1010,12 @@ def rename(): vim_command("let s:jedi_replace_orig = expand('')") line = vim_eval('getline(".")') - vim_command('normal! diw') + + if delete_word: + vim_command('normal! diw') + else: + vim_command('normal! yiwel') + if re.match(r'\w+$', line[cursor[1]:]): # In case the deleted word is at the end of the line we need to # move the cursor to the end. @@ -1042,9 +1047,17 @@ def rename(): return do_rename(replace) -def rename_visual(): - replace = vim.eval('input("Rename to: ")') - orig = vim.eval('getline(".")[(getpos("\'<")[2]-1):getpos("\'>")[2]]') +def rename_visual(use_selected_text_as_prompt_answer=False): + orig = vim.eval('getline(".")[(getpos("\'<")[2]-1):getpos("\'>")[2]' + '-((&selection ==# "exclusive") ? 2 : 1)]') + + input_text = "" + if use_selected_text_as_prompt_answer: + input_text = orig + + replace = vim.eval( + 'input("Rename to:", "{}")'.format(PythonToVimStr(input_text)) + ) do_rename(replace, orig) @@ -1056,6 +1069,10 @@ def do_rename(replace, orig=None): if orig is None: orig = vim_eval('s:jedi_replace_orig') + if orig == replace: + echo_highlight('Jedi did 0 renames.') + return + # Save original window / tab. saved_tab = int(vim_eval('tabpagenr()')) saved_win = int(vim_eval('winnr()'))