Is there any mechanism within VS Code (including plugins) to search for the place where a variable is given a value? I often search for myvariable = but that doesn't catch things like:
{ myvariable } = ...
[ myvariable ] = ...
function myfunc(myvariable) { }
myfunc(myvalue)
I'm working with JavaScript exclusively.
Not sure this is the right stack exchange for this question, but yes, there is a way to find these. The article on code navigation will help you further. Basically, VS Code has a command "Find references" (for me it's keybind F12 or ctrl+shift+F12 for a "full" version, but I don't know if this is the default) which will show you where a variable/type is declared and used.
As far as I know, there isn't an easy way to find only the places where a variable gets assigned. But these are included in the references search.
You can use Ctrl+Shift+O to open the Go to symbol in editor menu.
Here you can search for variable definitions, and once selected your cursor will automatically be moved to the definition.
Say my file looks like this:
I press Ctrl+Shift+O to bring up this:
Then I can select an item to move my cursor to the definition like this:
Here is another idea just for fun. It uses an extension I wrote, Find and Transform, that handles your request pretty easily (as long as you can make the appropriate regex ;>}).
With this keybinding:
{
"key": "alt+y", // whatever keybinding you want
"command": "findInCurrentFile",
"args": {
"find": "${selectedText}(?=\\s*[}\\]]?\\s*=)",
"isRegex": true,
}
}
It'll get your selected text (which may just be the word under the cursor - see demo) and uses a positive lookahead to find the examples you mentioned in your question. The match will be scroll-revealed if necessary. Ctrl+U to return the cursor to your previous position.