如何在当前文档中的 Visual Studio 中生成代码? [英] How to generate code in Visual Studio in the current document?

查看:40
本文介绍了如何在当前文档中的 Visual Studio 中生成代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这是很容易搜索到的内容,我深表歉意,但我不确定正确的搜索词是什么.

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

我现在正在使用 Visual Studio,主要用于 Unity 开发.创建新脚本时,我经常声明一些私有成员变量,如下所示:

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;

为了初始化这些变量,我需要在名为Start"的函数中添加一个 GetComponent 调用,如下所示:

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>();
}

现在,我找到了一个 Visual Studio 片段来快速键入GetComponent"部分,这很好,但我正在寻找一种方法来自动生成 Start 函数中的代码行.在理想的世界中,我可以输入

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>();

到开始功能.

代码片段只能让我走到一半.我需要做什么才能为 Visual Studio 创建这种类型的扩展?这是一件很难完成的事情吗?

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?

推荐答案

你可以试试我的 Visual Commander 专为这种轻量级可扩展性而设计的扩展.它允许将文档编辑为文本或使用 Visual Studio 代码模型和 Roslyn.

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.

例如,当插入符号位于变量声明中的 _rigidbody 或 _myComponent 时,调用以下命令:

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;
}

这篇关于如何在当前文档中的 Visual Studio 中生成代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆