1

In the codebase, there are two variables with the same name but with different types (ABC:str and ABC:bool). They both have used extensively in many different files. I want to rename one of them to prevent confusion, but I don't want to do it manually, as they have around 1000+ occurrences.

In vscode, when hovering over a variable, its type is shown. Assuming all the given types are exactly correct, is there a way to employ this functionality or something similar, to search variables with name and also with type? In other words, filtering out ABC variables which don't have the type str from search results.

2
  • 2
    I don't think vs code has a built-in way to do this and I think it's more work to automate this than to do it manually Commented Aug 21 at 7:45
  • you could start by searching for "if (ABC)" then /if \(ABC ==? [true|false]/gm and then double check that there is no version where if (ABC) is used to test the string is truthy. You might want to ignore whitespace in the longer version Commented Aug 21 at 8:21

1 Answer 1

1

There is no search&replace by variable type like that in VSCode, it currently only understands text.

Rename variables with F2

You can try if the rename symbol command works in your case.
Put your text cursor over the variable and press F2

You will get a small text box to edit the name. Change it and press enter and VSCode should correctly replace only this instance of your variable throughout the code, but not others named the same.

I hope for you this works with thousands of usages to replace. I used it in projects where it went through a couple files flawlessly, though. So there's hope.

Example from the VSCode documentation:

rename a variable

VSCode documentation:
https://code.visualstudio.com/docs/editing/refactoring#_rename-symbol

VSCode should understand the scope of your variable, but check if you have the extensions for javascript/typescript installed.


If this doesn't help you could rename the variable where it is first declared and do a build. Then you should get error messages of all instances where this variable has the old name. You could then write a script that renames the usages based on file and line of those error messages.
The other variable that keeps the old name will not show up in the build errors.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the suggestion about F2, I managed to correctly replace some of the variables using it, but it seems I'll have to manually replace the remaining ones, nevertheless.
Refactoring 1000s of uses easily is probably a bit much for VSCode. Probably works better the fewer uses it has to update. Hope you'll find a working solution, just wanted to post this as others may have also missed that F2 is a thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.