0

This is what I write (a lot):

Rigidbody rb = G

Now I would expect ReSharper Code Completion to suggest me this:

GetComponent<Rigidbody>()

But it doesn't. It suggests GetComponent<>() and leaves me to fill out the type myself. What I actually want is something like this:

SomeComponent foo = GetComponent<SomeComponent>();

ReSharper should have all the information necessary to make this type inference at the time the variable is about to be assigned.

Is there a way to manually add this kind of suggestion to ReSharper, so it infers the correct type? I tried Live Templates, but there does not seem to be a way to access the type of the variable I want to assign.

Ken Wayne VanderLinde
  • 18,915
  • 3
  • 47
  • 72
jgollenz
  • 21
  • 5
  • You'll have to ask JetBrains, there's no reason we would know. – DavidG Apr 04 '21 at 19:50
  • That is like saying: "This question is about x, we wouldn't know about x". How can you assume that nobody here on StackOverflow knows the answer to the question? I know for a fact that ReSharper can do this – jgollenz Apr 05 '21 at 08:33

2 Answers2

1

I found a solution here

You can use macros for Live Template variables to infer the type that ReSharper guesses at the point of the variable.

  1. Open the Rider settings [Ctrl+Alt+S] and navigate to Editor > Live Templates > C# or Unity (whichever you prefer as location for your new template)
  2. Create a template with a shortcut of your preference (in my case gc) and the following contents: GetComponent<$TYPE$>();$END$

enter image description here

  1. Click on Availability and add the scopes for Unity C# file and C# 2.0+ expressions (this just defines where ReSharper will suggest the shortcut)
  2. Click on Edit Variables in the bottom right
  3. Change the macro of TYPE to Guess type expected at this point

enter image description here

  1. Click on Save and use the template like this:

enter image description here

Thanks to Andrew for coming up with this solution. If you'd like to see this feature by default in Rider, upvote it here

jgollenz
  • 21
  • 5
0

That is acually normal, the <> is like a () but diffrent, its like asking why ReSharper will not fill in the arguments, thats all there is to it.

Filip
  • 5
  • 3
  • That's not at all what this is like, since ReSharper has no clue what the arguments are while it does know what the type might be. Also, that is not my question. I didn't ask why ReSharper does not do that by default, but how I can MAKE ReSharper do that. – jgollenz Apr 05 '21 at 08:29