davidhalter / jedi-vim

Using the jedi autocompletion library for VIM.
MIT License
5.29k stars 372 forks source link

Problem when renaming variable with a name containing parentheses/brackets #1108

Closed axvutt closed 1 year ago

axvutt commented 1 year ago

Issue

Renaming a variable with <leader>r and typing a new name containing parentheses or brackets yields unexpected results.

Steps to reproduce

Using Vundle as plugin manager.[^1] Using enclosed minimal configuration file my.vimrc [^1]: by the way, I could not update jedi-vim because of a git error, see following log file vundle.log

set nocompatible

let script_dir = fnamemodify(expand('<sfile>'), ':h')
let &runtimepath .= ','.script_dir.','.script_dir.'/after'

" Put your config changes here.
" let g:jedi#show_call_signatures=1

" BEGIN: axvutt settings
" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'

" ********** PLUGINS BELOW *************

" Plugin 'tpope/vim-surround'
" Plugin 'tpope/vim-commentary'
" Plugin 'tpope/vim-repeat'
" Plugin 'tpope/vim-fugitive'
" Plugin 'scrooloose/nerdcommenter'
" Plugin 'scrooloose/nerdtree'
Plugin 'davidhalter/jedi-vim'
" Plugin 'lervag/vimtex'
" Plugin 'bkad/camelcasemotion'
" Plugin 'kshenoy/vim-signature'
" Plugin 'konfekt/fastfold'
" Plugin 'tmhedberg/simpylfold'

" ********** PLUGINS ABOVE *************
call vundle#end()            " required
" END: axvutt settings

syntax on
filetype plugin indent on

Sample file test.py:

import numpy as np

xlist = [None, None, None]                                                                               
x = np.linspace(0,1,10)
y = np.sqrt(x)
  1. Open file with vim test.py -u my.vimrc
  2. Place cursor on x, line 4
  3. Type, for instance, <leader>r xlist[1] All occurrences of x are replaced by np in my case instead of the intended name. Similar problems occur if typing any variation of blah(, foo), bar[... also if renaming with cursor placed on x on line 5.

Output of “:verbose JediDebugInfo”

Jedi-vim debug information

jedi-vim version
Global Python

Using Python version 3 to access Jedi.

Jedi
Jedi environment: <SameEnvironment: 3.8.10 in /usr>
Known environments
Settings
  omnifunc=jedi#completions
        Last set from ~/.vim/bundle/jedi-vim/autoload/jedi.vim line 723
  completeopt=menuone,longest,popup
        Last set from ~/.vim/bundle/jedi-vim/plugin/jedi.vim line 38

:version

VIM - Vi IMproved 8.1 (2018 May 18, compiled Apr 18 2023 10:20:44)
Included patches: 1-2269, 3612, 3625, 3669, 3741
Modified by team+vim@tracker.debian.org
Compiled by team+vim@tracker.debian.org
Huge version without GUI.  Features included (+) or not (-):
+acl               -clientserver      +diff              +folding           +libcall           +mouse_gpm         +packages          -ruby              -tag_any_white     +vartabs           -xfontset
+arabic            -clipboard         +digraphs          -footer            +linebreak         -mouse_jsbterm     +path_extra        +scrollbind        -tcl               +vertsplit         -xim
+autocmd           +cmdline_compl     -dnd               +fork()            +lispindent        +mouse_netterm     -perl              +signs             +termguicolors     +virtualedit       -xpm
+autochdir         +cmdline_hist      -ebcdic            +gettext           +listcmds          +mouse_sgr         +persistent_undo   +smartindent       +terminal          +visual            -xsmp
-autoservername    +cmdline_info      +emacs_tags        -hangul_input      +localmap          -mouse_sysmouse    +postscript        +sound             +terminfo          +visualextra       -xterm_clipboard
-balloon_eval      +comments          +eval              +iconv             -lua               +mouse_urxvt       +printer           +spell             +termresponse      +viminfo           -xterm_save
+balloon_eval_term +conceal           +ex_extra          +insert_expand     +menu              +mouse_xterm       +profile           +startuptime       +textobjects       +vreplace
-browse            +cryptv            +extra_search      +job               +mksession         +multi_byte        -python            +statusline        +textprop          +wildignore
++builtin_terms    +cscope            -farsi             +jumplist          +modify_fname      +multi_lang        +python3           -sun_workshop      +timers            +wildmenu
+byte_offset       +cursorbind        +file_in_path      +keymap            +mouse             -mzscheme          +quickfix          +syntax            +title             +windows
+channel           +cursorshape       +find_in_path      +lambda            -mouseshape        +netbeans_intg     +reltime           +tag_binary        -toolbar           +writebackup
+cindent           +dialog_con        +float             +langmap           +mouse_dec         +num64             +rightleft         -tag_old_static    +user_commands     -X11
   system vimrc file: "$VIM/vimrc"
     user vimrc file: "$HOME/.vimrc"
 2nd user vimrc file: "~/.vim/vimrc"
      user exrc file: "$HOME/.exrc"
       defaults file: "$VIMRUNTIME/defaults.vim"
  fall-back for $VIM: "/usr/share/vim"
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H   -Wdate-time  -g -O2 -fdebug-prefix-map=/build/vim-JJcc3W/vim-8.1.2269=. -fstack-protector-strong -Wformat -Werror=format-security -D_REENTRANT -U_FORTIFY_SOURCE
-D_FORTIFY_SOURCE=1
Linking: gcc   -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -o vim        -lm -ltinfo -lnsl  -lselinux  -lcanberra -lacl -lattr -lgpm -ldl     -L/usr/lib/python3.8/config-3.8-x86_64-linux-gnu
 -lpython3.8 -lcrypt -lpthread -ldl -lutil -lm -lm

:messages

Messages maintainer: Bram Moolenaar <Bram@vim.org>
"jedi-test.py" 6L, 87C
VIM - Vi IMproved 8.1 (2018 May 18, compiled Apr 18 2023 10:20:44)
:scriptnames ``` 1: ~/tests/python/jedi-vim-bug/my.vimrc 2: /usr/share/vim/vim81/syntax/syntax.vim 3: /usr/share/vim/vim81/syntax/synload.vim 4: /usr/share/vim/vim81/syntax/syncolor.vim 5: /usr/share/vim/vim81/filetype.vim 6: /usr/share/vim/vim81/ftoff.vim 7: ~/.vim/bundle/Vundle.vim/autoload/vundle.vim 8: ~/.vim/bundle/Vundle.vim/autoload/vundle/config.vim 9: ~/.vim/bundle/vim-fugitive/ftdetect/fugitive.vim 10: ~/.vim/bundle/vimtex/ftdetect/cls.vim 11: ~/.vim/bundle/vimtex/ftdetect/tex.vim 12: ~/.vim/bundle/vimtex/ftdetect/tikz.vim 13: /usr/share/vim/vim81/ftplugin.vim 14: /usr/share/vim/vim81/indent.vim 15: ~/.vim/bundle/vim-surround/plugin/surround.vim 16: ~/.vim/bundle/vim-commentary/plugin/commentary.vim 17: ~/.vim/bundle/vim-fugitive/plugin/fugitive.vim 18: ~/.vim/bundle/nerdcommenter/plugin/nerdcommenter.vim 19: ~/.vim/bundle/nerdtree/plugin/NERD_tree.vim 20: ~/.vim/bundle/nerdtree/autoload/nerdtree.vim 21: ~/.vim/bundle/nerdtree/lib/nerdtree/path.vim 22: ~/.vim/bundle/nerdtree/lib/nerdtree/menu_controller.vim 23: ~/.vim/bundle/nerdtree/lib/nerdtree/menu_item.vim 24: ~/.vim/bundle/nerdtree/lib/nerdtree/key_map.vim 25: ~/.vim/bundle/nerdtree/lib/nerdtree/bookmark.vim 26: ~/.vim/bundle/nerdtree/lib/nerdtree/tree_file_node.vim 27: ~/.vim/bundle/nerdtree/lib/nerdtree/tree_dir_node.vim 28: ~/.vim/bundle/nerdtree/lib/nerdtree/opener.vim 29: ~/.vim/bundle/nerdtree/lib/nerdtree/creator.vim 30: ~/.vim/bundle/nerdtree/lib/nerdtree/flag_set.vim 31: ~/.vim/bundle/nerdtree/lib/nerdtree/nerdtree.vim 32: ~/.vim/bundle/nerdtree/lib/nerdtree/ui.vim 33: ~/.vim/bundle/nerdtree/lib/nerdtree/event.vim 34: ~/.vim/bundle/nerdtree/lib/nerdtree/notifier.vim 35: ~/.vim/bundle/nerdtree/autoload/nerdtree/ui_glue.vim 36: ~/.vim/bundle/nerdtree/nerdtree_plugin/exec_menuitem.vim 37: ~/.vim/bundle/nerdtree/nerdtree_plugin/fs_menu.vim 38: ~/.vim/bundle/nerdtree/nerdtree_plugin/vcs.vim 39: ~/.vim/bundle/jedi-vim/plugin/jedi.vim 40: ~/.vim/bundle/vimtex/plugin/vimtex.vim 41: ~/.vim/bundle/camelcasemotion/plugin/camelcasemotion.vim 42: ~/.vim/bundle/vim-signature/plugin/signature.vim 43: ~/.vim/bundle/vim-signature/autoload/signature/utils.vim 44: ~/.vim/bundle/fastfold/plugin/fastfold.vim 45: ~/.vim/bundle/simpylfold/plugin/SimpylFold.vim 46: /usr/share/vim/vim81/plugin/getscriptPlugin.vim 47: /usr/share/vim/vim81/plugin/gzip.vim 48: /usr/share/vim/vim81/plugin/logiPat.vim 49: /usr/share/vim/vim81/plugin/manpager.vim 50: /usr/share/vim/vim81/plugin/matchparen.vim 51: /usr/share/vim/vim81/plugin/netrwPlugin.vim 52: /usr/share/vim/vim81/plugin/rrhelper.vim 53: /usr/share/vim/vim81/plugin/spellfile.vim 54: /usr/share/vim/vim81/plugin/tarPlugin.vim 55: /usr/share/vim/vim81/plugin/tohtml.vim 56: /usr/share/vim/vim81/plugin/vimballPlugin.vim 57: /usr/share/vim/vim81/plugin/zipPlugin.vim 58: ~/.vim/bundle/vim-signature/after/plugin/signature.vim 59: /usr/share/vim/vim81/syntax/python.vim 60: ~/.vim/bundle/jedi-vim/after/syntax/python.vim 61: ~/.vim/bundle/jedi-vim/autoload/jedi.vim 62: ~/.vim/bundle/jedi-vim/ftplugin/python/jedi.vim 63: ~/.vim/bundle/simpylfold/ftplugin/python/SimpylFold.vim 64: ~/.vim/bundle/simpylfold/autoload/SimpylFold.vim 65: /usr/share/vim/vim81/ftplugin/python.vim 66: ~/.vim/bundle/jedi-vim/after/ftplugin/python/jedi.vim 67: /usr/share/vim/vim81/indent/python.vim 68: ~/.vim/bundle/vim-signature/autoload/signature/sign.vim 69: ~/.vim/bundle/vim-signature/autoload/signature/mark.vim ```
davidhalter commented 1 year ago

Sorry but this is not going to change. The way how it currently works is that it essentially does a ciw and then copies the word under the cursor. This is just not a very stable thing to do in VIM, but it works well enough if you don't do things like adding parentheses. So this is essentially not easily fixable, because it's hard to tell with VIM what exactly changed.

What were you even trying to do?

axvutt commented 1 year ago

I see, no worries. Thanks for the answer.

What I wanted to do was to replace a variable name by a container name (list, dict, ...) whose items are accessed by indexing. In the minimal example, we could start with:

x = np.linspace(0,1,10)
y = np.sqrt(x)

Then, for whatever reason, do something like this:

xlist = [None, None, None]
for n in range(3):
    xlist[n] = np.linspace(0,1,10*n)
    y = np.sqrt(xlist[n])

For something else I am currently working on, I tried a replacement like a --> some_long_name[n], thinking of possibly change the index n manually where relevant.

Probably a vim command like :%s/a/some_long_name\[n\]/gc is in the end the best way to go.

bew commented 1 year ago

I don't know about vim, but couldn't you use extmarks on neovim, to track the start/end of the word and know what was added?