How to generate code in Visual Studio in the curre

2019-07-07 17:50发布

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

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 extension for Visual Studio? Is this a difficult thing to accomplish?

1条回答
何必那么认真
2楼-- · 2019-07-07 18:20

You can try my Visual Commander extension which is designed exactly for such kind of lightweight extensibility. It allows to edit a document as text or use Visual Studio code model and Roslyn.

For example, call the following command when the caret is on _rigidbody or _myComponent in a variable declaration:

public class C : VisualCommanderExt.ICommand
{
    public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package)
    {
        this.DTE = DTE;

        EnvDTE.CodeVariable v = FindCurrentVariable();
        if (v != null)
        {
            string initialization = v.Name + " = GetComponent<" + v.Type.CodeType.Name + ">();";
            AddLine(FindFunction("Start"), initialization);
        }
    }

    EnvDTE.CodeFunction FindFunction(string name)
    {
        EnvDTE.TextSelection ts = DTE.ActiveWindow.Selection as EnvDTE.TextSelection;
        if (ts == null)
            return null;
        EnvDTE.CodeClass codeClass = ts.ActivePoint.CodeElement[EnvDTE.vsCMElement.vsCMElementClass]
            as EnvDTE.CodeClass;
        if (codeClass == null)
            return null;
        foreach (EnvDTE.CodeElement elem in codeClass.Members)
        {
            if (elem.Kind == EnvDTE.vsCMElement.vsCMElementFunction && elem.Name == name)
                return elem as EnvDTE.CodeFunction;
        }
        return null;
    }

    EnvDTE.CodeVariable FindCurrentVariable()
    {
        EnvDTE.TextSelection ts = DTE.ActiveWindow.Selection as EnvDTE.TextSelection;
        if (ts == null)
            return null;
        return ts.ActivePoint.CodeElement[EnvDTE.vsCMElement.vsCMElementVariable]
            as EnvDTE.CodeVariable;
    }

    void AddLine(EnvDTE.CodeFunction f, string text)
    {
        EnvDTE.TextPoint tp = f.GetStartPoint(EnvDTE.vsCMPart.vsCMPartBody);
        EnvDTE.EditPoint p = tp.CreateEditPoint();
        p.Insert(text + System.Environment.NewLine);
        p.SmartFormat(tp);
    }

    EnvDTE80.DTE2 DTE;
}
查看更多
登录 后发表回答