我可以在Visual Studio 2012中以编程方式折叠/展开特定名称的所有预处理器块吗

Can I programmatically collapse/expand all preprocessor blocks of a certain name in Visual Studio 2012?

本文关键字:定名称 处理器 预处理 2012 Studio Visual 折叠 方式 编程 我可以      更新时间:2023-10-16

我当前的项目在整个代码中有很多调试预处理器块。这些宏的名称有意与系统_DEBUG和NDEBUG宏不同,所以我有很多这样的:

// Some code here
#ifdef PROJNAME_DEBUG
//unit tests, assumption testing, etc.
#endif
// code continues

这些块有时会变得相当大,它们的存在有时会抑制代码的可读性。在Visual Studio 2012中,我可以很容易地折叠这些,但如果能自动将所有折叠起来,那就太好了,如果我想查看其中的内容,我可以展开它们。然而,由于我还有一堆头保护,我不想折叠所有预处理器块,只想折叠#ifdef PROJNAME_DEBUG块。

我能做这个吗?

我认为这是最容易实现的场景。

您应该先在C#中创建一个外接程序。(在VS 2013中,它们被弃用:()

在OnConnection方法中,您应该添加命令:

public void OnConnection( object application, ext_ConnectMode connectMode, object addInInst, ref Array custom )
{
    _applicationObject = (DTE2)application;
    if (connectMode == ext_ConnectMode.ext_cm_AfterStartup || connectMode == ext_ConnectMode.ext_cm_Startup)
    {
        Commands2 commands = (Commands2)_applicationObject.Commands;
        try
        {
            //Add a command to the Commands collection:
            Command command = commands.AddNamedCommand2(_addInInstance, "MyAddinMenuBar", "MyAddinMenuBar", "Executes the command for MyAddinMenuBar", true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
        }
        catch (System.ArgumentException)
        {
            //If we are here, bla, bla... (Auto generated)
        }
    }
}

注意:您可以在AddNamedCommand2的引用中找到参数的作用方式模板创建的版本也可以,但自然地,正确命名命令是值得的。

之后,您需要将您的逻辑添加到Exec方法中:

public void Exec( string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled )
{
    handled = false;
    if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
    {
        if (commandName == "MyAddinMenuBar.Connect.MyAddinMenuBar")
        {
            List<string> args = (varIn as string).Split(' ').ToList();
            TextSelection ts;
            ts = (TextSelection)_applicationObject.ActiveDocument.Selection;
            EditPoint ep = (ts.ActivePoint).CreateEditPoint();
            ep.StartOfDocument();
            do
            {
                string actualLine = ep.GetLines(ep.Line, ep.Line + 1);
                if (args.TrueForAll(filter => actualLine.Contains(filter)))
                {
                    _applicationObject.ExecuteCommand("Edit.GoTo", ep.Line.ToString());
                    _applicationObject.ExecuteCommand("Edit.ToggleOutliningExpansion");
                }
                ep.LineDown();
            } while (!ep.AtEndOfDocument);
            handled = true;
            return;
        }
    }
}

注意:指定给命令的名称是在exec中检查的。

比你能建造的还要多。

外接程序的部署可以通过..DocumentsVisaul Studio 20[XY]AddIns中的[ProjectName].AddIn文件进行。(由模板创建,如果将加载项移到其他位置,则应进行复制)您应该将外接程序集放置在您设置的上述文件的assembly元素所指向的位置。要更改版本,您应该修改version元素中的文本。

部署并启动Studio后,应在Tools菜单中激活管理器中的外接程序。

您需要在代码文件中展开所有可折叠的部分(使用C#IDE设置的CTRL++L)。这是必需的,因为我只找到了一种方法来扭转湿陷状态。如果你找到更好的命令,你可以改变它。

接下来,您应该激活"命令窗口"以使用创建的命令。现在只需要键入命令名,如下所示:

MyAddinMenuBar.Connect.MyAddinMenuBar #ifdef PROJNAME_DEBUG

希望魔法会发生。

这个解决方案独立于您编辑的代码的语言,因此非常多功能。