Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS Code: How to find all references of a variable in python?

I'd like a VS Code feature like Find Usages in Pycharm, which can find all reads and writes of a variable. Take a simple file for example:

class Foo:
    def __init__(self, bar):
        self.bar = bar

    def do_something(self):
        bar1 = "hhhhh"
        if bar1 == self.bar:
            print("do something")

In Pycharm using "Find Usages" of self.bar:

Find Usages screenshot

I've tried "Find all references" but it doesn't work:

Find all references screenshot

as it doesn't find if bar1 == self.bar:.

Update: searching for just occurrences is not what I am asking, as it just matches keywords which includes other variables with same name and also occurrences in comments.

Update: turns out to be a known issue in Microsoft python language server: github.com/microsoft/python-language-server/issues/1174

like image 331
monotasker Avatar asked Jul 19 '19 04:07

monotasker


People also ask

How can I see all references in VS Code?

You can use the Find All References command to find where particular code elements are referenced throughout your codebase. The Find All References command is available on the context (right-click) menu of the element you want to find references to. Or, if you are a keyboard user, press Shift + F12.


1 Answers

The problem occurs with my user setting of python extension jediEnabled option as false:

"python.jediEnabled": false,

so switch back to the default:

// Enables Jedi as IntelliSense engine instead of Microsoft Python Analysis Engine.
  "python.jediEnabled": true,

and "Find all references" works in the example!

Maybe it's a bug in Microsoft Python Analysis Engine.

like image 151
monotasker Avatar answered Oct 19 '22 15:10

monotasker