Find usages of primary constructor of a Kotlin class
Asked Answered
F

4

26

Imagine you have a super important and widely-used class in your Kotlin-based project. It has the only constructor which is defined like that:

class MyAwesomeManager(argOne: String, argTwo: String)

For some reason, you need to quickly find all uses of its constructor. You're using Android Studio (or Intellj IDEA).

But... pressing Ctrl + LMB on its name gives a ton of rubbish results - uses in imports, companion object's fields calls etc. All the uses of a class, but not the constructor. The same for putting a cursor on its name and hitting Alt + F7.

So, how do I find all and only the usages of this primary constructor?

Faltboat answered 15/9, 2017 at 10:13 Comment(0)
F
21

I'm sorry for so quickly answering my own question, but I guess I've found a solution. In two simple step:

  1. Double-click the name of a class to select it
  2. Ctrl+B to find usages (edit: Alt+f7 would also work)

I'm not sure it works in all cases, but it worked for me.

Faltboat answered 15/9, 2017 at 10:21 Comment(5)
Upd: You can also use Alt+f7 as a second step.Faltboat
But in your question you said that Alt+F7 gives you rubbish as well. Which is it?Lauricelaurie
I meant to say if you first select the full name of a class and then use Alt+F7, it finds usages of a constructor only. I'll edit the answer to clarify this.Faltboat
Small note: if you don't want to do it with mouse use Ctrl+-> to navigate to the end of class name when the caret is on it, then use Ctrl+B.Salpa
Alt+F7 is showing the class usages, not the constructor ones, but Ctrl+B works for meFyke
B
31

You should put the caret next to the opening parenthesis of the primary constructor but not right before the first parameter. Possibly add a space or line break:

class MyAwesomeManager<caret>(argOne: String, argTwo: String)
//also works
class MyAwesomeManager(<caret> argOne: String, argTwo: String)

then invoke Find Usages. This should give you only the usages of the constructor invocation.

If there are no parentheses because the class has a default zero-argument constructor, you can temporarily add () after the class name, then proceed as above.

Bellyache answered 15/9, 2017 at 13:30 Comment(4)
Well, I guess it's also a correct answer, but this method requires a bit better aiming :)Faltboat
Small correction - the caret needs to be put before the opening brace. If you put it after the brace AS says it cannot find usages or searches for usages of the first argument.Faltboat
@Faltboat I edited the answer. There needs to be a space or line break before the first argument.Bellyache
Is there a youtrack ticket to ask for a better UX here?Gobioid
F
21

I'm sorry for so quickly answering my own question, but I guess I've found a solution. In two simple step:

  1. Double-click the name of a class to select it
  2. Ctrl+B to find usages (edit: Alt+f7 would also work)

I'm not sure it works in all cases, but it worked for me.

Faltboat answered 15/9, 2017 at 10:21 Comment(5)
Upd: You can also use Alt+f7 as a second step.Faltboat
But in your question you said that Alt+F7 gives you rubbish as well. Which is it?Lauricelaurie
I meant to say if you first select the full name of a class and then use Alt+F7, it finds usages of a constructor only. I'll edit the answer to clarify this.Faltboat
Small note: if you don't want to do it with mouse use Ctrl+-> to navigate to the end of class name when the caret is on it, then use Ctrl+B.Salpa
Alt+F7 is showing the class usages, not the constructor ones, but Ctrl+B works for meFyke
E
3

Since your "Find Usages" call (Alt+F7 on OSX) is from the Kotlin class name (which is also the constructor definition), you'll get more results than you would have if you'd done the same on a Java constructor - as you saw. If Find Usages didn't show usages of the class from this point, how else would you know where the class is used?

Doing this on one of my own Kotlin classes, I got the following:

enter image description here

Expanding "New instance creation" shows me constructor usage.

Extemporary answered 15/9, 2017 at 12:58 Comment(1)
I need to check this, but I guess it would find all the constructors' usages, not only the primary's?Faltboat
T
0

... as time goes by ... and IntelliJ evolves ...

if I put the cursor somewhere in the class name and invoke Find Usages (Alt+F7) it finds all usages of the class.

but ...

if I put the cursor directly in front of the ( and invoke Find Usages (Alt+F7) it only finds calls to the constructor.

Tantalizing answered 28/7, 2023 at 9:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.