Search Unity

Find all instances of a class in code (not editor)?

Discussion in 'Scripting' started by allworknoplay, Jun 2, 2019.

  1. allworknoplay

    allworknoplay

    Joined:
    Mar 5, 2015
    Posts:
    3
    I'm using Unity 2019 on a mac with Visual Studio.

    I want to do a bunch of refactoring to ensure conceptual/naming consistency across different bits of my game -- for example, I've got places where I've done things like name an enum list of abilities "Abilities" and named the property for it in some classes "ability", but then also used "abilities" for a list of abilities, and have decided it's going to get confusing fast if I don't get a little more clear. Since I've referred to these properties in different variables (e.g. `var userAbility` and `var targetAbility`) across bits of the app, I'm concerned about getting every last bit, even though the Visual Studio renaming tool is generally quite good. This problem is in more places than just abilities -- items, locations, enemies, effects, etc.

    So the question:
    Is there a way to locate all places where an instance of a class is assigned to a variable (so it would find `var userAbility` even though the class assignment is implicit)?

    It seems like the compiler should mostly have this information, but I don't know if this is a thing. My searches consistently turn up finding all instances of something in the editor, which isn't helpful here -- maybe it doesn't exist, or maybe I just don't have the right nomenclature.

    Thanks in advance!
     
  2. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    789
    Not sure exactly what would need changing. If you are renaming a class and a variable uses the var keyword, what would you expect var to change to? Give me an example of what it would have currently and what you would expect it to have after being changed.
     
    halley likes this.
  3. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    719
    TaleOf4Gamers is correct-- you should only need to replace the class name where it appears as the class name. The
    var
    keyword and any other anonymous casts would rebuild appropriately without any modification (assuming that all you're doing is renaming it).

    I suppose that it would look a little odd if you have other variations of the name in your variables, such as
    var schlitzBeer = new CoorsBeer()
    but I am not sure that even a close review by a team of human offshore outsource coders would find that, nevermind an automated tool.
     
    TaleOf4Gamers likes this.
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    6,504
    I think I read OP's question correctly... hopefully this will help.

    In Visual Studio highlight the class name, right click, select 'Find All References' (or press ctrl+k,r).
    FindRef.png
    That'll give a list of all areas in the code that reference that class/type.
    FindRefResults.png


    When you want to rename, highlight the same class name (where the class is declared), type your name over it and then put your cursor over that text. Visual Studio should show a little lightbulb that if you click it'll ask you to rename all places for it.
    RenameHelper.png
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    6,504
    Darn... I just read that they said Visual Studio... on mac.

    Not 100% sure if the interface will be the same as this.

    I do know that Visual Studio for mac is really just a rebadge of MonoDevelop or somethign like that. So I know these features exist, but like it might not be a 'light bulb' but some other thing instead.
     
  6. SparrowsNest

    SparrowsNest

    Joined:
    Apr 6, 2017
    Posts:
    1,726
    On PC MonoDevelop you have the exact same menu for selecting all references, to rename something simply highlight it, press F2, rename it, press enter - all references changed.
     
  7. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    1,735
    Or just use Jetbrains Rider. Right Click on variable -> Refactoring -> Do the thing.

    There's also a symbol search (triggered by shift + shift)
     
  8. allworknoplay

    allworknoplay

    Joined:
    Mar 5, 2015
    Posts:
    3
    First -- thanks so much for all the answers. I think I must have miscommunicated my goal :( A friend also just pointed me to jetbrains products, which I might look into, so thanks for that suggestion as well.

    @TaleOf4Gamers / @halley -- I'm interested in changing the variable names for clarity, not just the class names. I'm renaming the `Abilities` enum to `AbilityKind`, and want every instance of AbilityKind to be called `abilityKind` or `targetAbilityKind` or whatever. If there's a `var userAbility` that's assigned an instance of an `Abilities` enum and I rename the enum, it's now probably even more confusing than it was before -- hence wanting to find variables assigned instances of the class and not simply renaming the class.

    @lordofduct -- you're awesome with your helpful screenshots, but I think the result of using the Find all references tool is the same as simply using the global renaming tool?

    As per above, I've now renamed the Abilities enum to AbilityKind. Here's a made-up example where I'm assigning `var whateverAbility` an instance of `AbilityKind` via the `kind` property on an instance of an `Ability` class. I'd ideally like to ensure that `whateverAbility` is now called `whateverAbilityKind`. Finding all references doesn't find the variable -- only uses of the actual class name -- so if this was confusingly named in my code, I wouldn't easily be able to find and rename it.
    upload_2019-6-3_8-18-29.png

    Aaaaaanyway, this isn't the biggest deal; if I don't get what I want from Resharper/Rider, I'll probably just take the time to test and refactor all these classes manually -- just wanted to know if there were better tools. Thank you all again :)
     

    Attached Files:

  9. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    870
    Sounds like what you want is: find all variable declarations in your code where the variable has a particular type, including ones where that type is assigned implicitly via the "var" keyword.

    I agree that's information that Visual Studio must have, but I don't know of a way to get it.

    However, I feel I should point out that, stylistically speaking, using "var" is really only recommended in cases where the type is immediately obvious (e.g. var x = new Something) or where knowing the exact type isn't relevant to understanding the variable's purpose (e.g. complex nested generics whose details aren't relevant to the current function). In other cases, it tends to make your code harder to understand.

    It looks like the only instances that you are having trouble tracking down are ones that violate this guideline (the type is key to understanding the variable's purpose but doesn't appear anywhere on the same line of code). Perhaps you should reevaluate how you choose to use "var".
     
  10. allworknoplay

    allworknoplay

    Joined:
    Mar 5, 2015
    Posts:
    3
    @Antistone Yeah, honestly when I started I was explicitly typing everything, because I'm new to this strictly typed compiled language thing (aside from screwing around in all sorts of stuff, I mostly do ruby/python/js/bash) and wanted to commit to doing it "right". It started feeling too verbose, and the abovementioned friend told me I mostly shouldn't bother, so I mostly went in the other direction :p