How to add code to a specific function as a refactor/live template/something else?

I apologize if this is something that is easily searched, but I'm not really sure what the correct search terms are for this.

The gist of what I am trying to do is this:  I'd like to generate a line of code that gets inserted into an EXISTING function, and that line of code is based off of the member variable declaration.  I don't see any functionality that lets me apply a template where I'm at, but also jump to an existing function and add a line of code there.  This is driving me nuts.  Here's a full explanation:

I am using Visual Studio right now, primarily for Unity development. When creating a new script, I often declare a some private member variables like this:

private Rigidbody _rigidbody;
private SomeOtherComponent _myComponent;

In order to initialize those variables, I need to add a GetComponent call in a function called "Start", like this:

void Start(){
    _rigidbody = GetComponent<Rigidbody>();
    _myComponent = GetComponent<SomeOtherComponent>();
}

Right now, I've found a Visual Studio snippet to quickly type the "GetComponent" part, which is nice, but I'm looking for a way to auto-generate the lines of code in the Start function. In an ideal world, I could type in

private Rigidbody _rigidbody;

then I could hit some key-combination, and it would automatically add

_rigidbody = GetComponent<Rigidbody>();

To the Start function.

Snippets only get me halfway there. What would I need to do to create this type of functionality in Resharper? 

1 comment
Comment actions Permalink
Official comment

This is a fantastic suggestion! Unfortunately, there isn't a quick easy way to set this up in the product - it would require writing some custom code in a plugin to be able to do this.

I've added this as an issue (#356) to the resharper-unity plugin for ReSharper and Rider, and hopefully we can get it added for the 2018.1 releases of Rider and ReSharper.

Please sign in to leave a comment.