vscode "no refactorings available" for python
Asked Answered
O

6

29

Ctrl + alt + R for extension python.python 2020.1.58038 always gives me a "No refactorings available" message.

Probably a configuration issue but I've tried several times to uninstall/reinstall and restart vs code to no avail.

Thought maybe I was missing python-rope but python -m pip install --upgrade rope Requirement already up-to-date: rope in c:\users\cdoyle\appdata\roaming\python\python37\site-packages (0.16.0)

VS Code Version info : Version: 1.42.0 (user setup) Commit: ae08d5460b5a45169385ff3fd44208f431992451 Date: 2020-02-06T10:51:34.058Z Electron: 6.1.6 Chrome: 76.0.3809.146 Node.js: 12.4.0 V8: 7.6.303.31-electron.0 OS: Windows_NT x64 10.0.18362

Also I don't see any open issues https://github.com/Microsoft/vscode-python/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+refactoring

Not sure what to try next.

Thanks

Olnton answered 7/2, 2020 at 12:1 Comment(2)
AFAIK, Microsoft doesn't use Palantir's language server (which includes rope) any more, but their own written in C#.Renshaw
pip install rope solved it for me.Bridge
C
17

I'm new to programming Python in VS Code and ran into the same problem. I successfully used the "Rename Symbol" command to rename a function and update all its references. More detail at https://code.visualstudio.com/docs/editor/refactoring . Hope this helps!

Cymograph answered 5/7, 2020 at 0:59 Comment(2)
Thank you, this helped. Also of note: if you do this within a function, it only renames that variable inside the function. Which is actually kind of nice.Witherspoon
Is this intended to update references outside the file as well? Because for me, it only works inside the fileCohlier
J
2

I would like to add further:

  1. If you want to change a variable name / function name at one go, you can use "Rename Symbol" in VS Code editor by right clicking that particular name.

  2. If your code has something related to both variable name and function name and you want to apply the change at both places, then you can use "Change all Occurrences".

Example of above-

test = "Hello"
def test_func():
    print("World")

print(test)
test_func()

If i want to change variable "test" to "new" and function "test_func" to "new_func", i can use "Change all Occurrences". But if i want to change only say variable name, then i have too use "Rename symbol".

Jurado answered 16/2, 2021 at 5:2 Comment(1)
Using "Rename (F2)" did solve it for me. Thanks. So what is "Refactor... (Ctrl+Shift+R)" used for then?Leibowitz
P
2

Visual Studio Code and its Python extension is (at the time of writing this) very limited when it comes to refactoring. Out of the box it provides two refactoring actions: Extract Method and Extract Variable. The reason you're seeing "No refactorings available" is because the currently selected text doesn't match any of these actions. Try selecting a line of text and then press [Ctrl][Shift][R].

Rope support was removed in VSCode in favour of Language Server Protocol integration which is used by their Pylance language server extension. Pylance is now installed automatically with vscode-python extension, and it's actively developed - maybe they will implement more refactorings in the future. If you're looking for more robust refactoring, check out PyCharm refactoring, it is currently much more sophisticated.

Pontificals answered 11/2, 2022 at 22:20 Comment(0)
A
2

I noticed that you also get the "No refactorings available" response if there are any comments on the lines that you select, when trying to preform an "Extract method" operation. After removing the comments I was able to extract the method.

Alex answered 14/3 at 12:9 Comment(0)
C
1

For Python 3.10.2 use right-click, Change All Occurrences (CTRL-F2) on VSCode version 1.65.2

Ciceronian answered 14/3, 2022 at 9:24 Comment(1)
This also renames inside strings and not only variables so a pretty dangerous option.Wegner
S
0

I think @Wayne Barrrass-Brown's answer is the best answer available, but just wanted to add something I've found helpful in similar situations.

Double click the variable you want to change, then CTRL+D (which will select the next occurrence of that text) until you've selected all the occurrences you want to change, then type in the new text you want for the selected variable/text.

Seductress answered 19/6, 2022 at 4:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.