VS Code - Rename symbol too slow for Python
Asked Answered
F

2

20

I'm having this refactoring issue where I try to rename a function through the option "Rename symbol", but it will take a long time. There's a "progress bar" moving endlessly below the document tab. It is taking around 5 min to do the renaming (also with variable names).

Is this a normal behaviour? I have about 10 python files in the same folder of around ~100 lines. BUT, I have a data folder (belonging to the project) with some 100,000 txt files (which is ignored by git, btw). Are this documents also taken into account? Is there a way to only rename in current file?

VS Code version: 1.25.0 Python extension: 2018.6.0

Thanks, Rafa

Fidelafidelas answered 8/7, 2018 at 18:45 Comment(6)
what about search/replace using grepwin on *.py files? I had a poor experience with VS Code / python mode.Taxi
@Jean-François I don't see how search/replace with grep or any other purely textual tool could sensibly work. You simply need to parse code to avoid false positives when renaming.Bedcover
depends on what you want to replace. If it's search_minimum_by_least_square it will work all right. If it's min, then no; but python is so dynamic that a real smart refactoring is very difficult to achieve (unlike for java). A good tool for that is PyCharm that does an honest job given the difficulty of the task.Taxi
I do ctrl+D or ctrl+H/ctrl+shift+H when renaming, if it helps.Icosahedron
@Icosahedron those shortcuts work extremely well for my purposes! Thanks.Fidelafidelas
@Jean-FrançoisFabre I actually came from PyCharm where the rename function worked very well, but for some reason the whole IDE started lagging badly and not even a clean reinstall helped me with it. So far I'm happy with VS Code - Python. Maybe in a future update the extension will be a bit more optimized.Fidelafidelas
G
9

same problem here, it's apparently due to the Rope library (which is being used to do the refactoring) not fully supporting Python 3.

Here is an issue of people describing similar problems - https://github.com/Microsoft/vscode-python/issues/52

Update: Using Jedi instead of Microsoft Python Language Server for intellisense seems to have fixed this problem for me. Just add the following entry to your settings.json file :

"python.jediEnabled": true
Greatniece answered 12/1, 2019 at 12:3 Comment(2)
Does not help meGoingson
says "Unknown configuration setting" for python.jediEnabled. (by other searches) this is replaced with "python.languageServer": "Jedi",... but this also doesn't workKristoforo
V
2

Summary

I found rope wasn't ignoring the files in my virtual environment that were in my workspace directory. This created understandably slow refactoring performance. I added my virtual environment folder to the ignored_resources in the rope configuration file config.py. Immediately after making the change, refactoring performance greatly improved.


Example

Take the folder structure below.

-.venv
-.vscode
--.ropeproject
---config.py
---objectdb
--pythonpackage
---__init__.py
---[other files I want to refactor]
-main.py

Assuming .venv is the name of your virtual environment, for rope to ignore it, you need to include .venv in the ignored_resources list in the rope config.py file. Example shown below.

def set_prefs(prefs):
    """This function is called before opening the project"""

    # Specify which files and folders to ignore in the project.
    # Changes to ignored resources are not added to the history and
    # VCSs.  Also they are not returned in `Project.get_files()`.
    # Note that ``?`` and ``*`` match all characters but slashes.
    # '*.pyc': matches 'test.pyc' and 'pkg/test.pyc'
    # 'mod*.pyc': matches 'test/mod1.pyc' but not 'mod/1.pyc'
    # '.svn': matches 'pkg/.svn' and all of its children
    # 'build/*.o': matches 'build/lib.o' but not 'build/sub/lib.o'
    # 'build//*.o': matches 'build/lib.o' and 'build/sub/lib.o'
    prefs['ignored_resources'] = ['*.pyc', '*~', '.ropeproject',
                                  '.hg', '.svn', '_svn', '.git', '.tox', '.venv']

Variegate answered 27/8, 2021 at 4:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.