Refactor: "Extract Method" error and unexpected outcome

466 Views Asked by At

I'm experiencing weird refactoring behavior. With this line of code:

variable = 1 + 2 + 3

When I try to extract a variable, by highlighting "1 + 2", then right click -> choose "extract variable" option, i get this outcome:

newvariable74 = 1 + 2
a = newvariable74 2 + 3

I have other issues with refactoring. I get different results when testing the rope examples from the docs

For example, this code:

def f():
    a_var = 1
    # INFO: I'm printing `a_var`
    print 'a_var = %s' % a_var

...after renaming 'a_var' to 'new_var', the new code should look like this:

def f():
    new_var = 1
    # INFO: I'm printing `new_var`
    print 'new_var = %s' % new_var

but instead, I get this:

def f():
new_var = 1
# INFO: I'm printing `a_var`
print ('a_var = %s' % new_var)

notice how the content of the string doesn't change.

Some info:

  • I use Python 3.8.2 64-bit
  • Visual Studio Code: 1.47.1
  • Linux Mint 20
  • Linux Kernel 5.4.0-40-generic
1

There are 1 best solutions below

0
On BEST ANSWER

Currently, the refactoring function of vscode variables and methods is provided by Python extension, but its refactoring does not support renaming function.

For variable refactoring in vscode, you could try adding parentheses to it to make it recognized.

variable = (1 + 2) + 3

If you want to rename all 'a_var' to 'new_var' in vscode, you could try using Ctrl+F2.It will change all 'a_var' that appear in the current file.

When I use 'F2', it only changes the variables in the current file, 'new'_ Var =% s' will not be recognized.

You can refer to:Refactoring