【问题标题】:Can I programmatically collapse/expand all preprocessor blocks of a certain name in Visual Studio 2012?我可以在 Visual Studio 2012 中以编程方式折叠/展开某个名称的所有预处理器块吗?
【发布时间】:2013-06-26 23:24:43
【问题描述】:

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

// Some code here

#ifdef PROJNAME_DEBUG
//unit tests, assumption testing, etc.
#endif

// code continues

这些块有时会变得相当大,它们的存在有时会抑制代码的可读性。在 Visual Studio 2012 中,我可以轻松地折叠这些,但如果我想查看其中的内容,自动折叠所有会很好,允许我展开它们。但是,由于我还有一堆标头保护,我不想折叠所有预处理器块,只有 #ifdef PROJNAME_DEBUG 块。

我可以这样做吗?

【问题讨论】:

  • @GManNickG 好的,我已经调整了标签以使其更合适。
  • @GManNickG:关于 IDE 的问题是这里的主题。 IDE 是一种工具,仅供编程人员使用。
  • @JohnSaunders:看起来你是对的,我的错。

标签: c++ visual-studio visual-studio-2012


【解决方案1】:

我认为这是您可以实现的最简单的方案。

您应该首先在 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 中检查。

你可以建造。

可以通过..\Documents\Visaul Studio 20[XY]\AddIns\ 中的[ProjectName].AddIn 文件部署加载项。 (由模板创建,如果您将加载项移动到其他地方,您应该复制) 您应该将您的插件程序集放置在您设置指向的上述文件的 Assembly 元素的位置。要更改版本,您应该修改 Version 元素中的文本。

部署并启动 Studio 后,您应该在 Toolsmenu 的管理器中激活插件。

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

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

MyAddinMenuBar.Connect.MyAddinMenuBar #ifdef PROJNAME_DEBUG

希望魔法会发生。

此解决方案与您编辑的代码语言无关,因此非常多功能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-28
    • 2012-07-01
    • 1970-01-01
    • 2015-08-19
    • 2017-05-07
    • 2011-07-06
    • 2014-12-18
    • 1970-01-01
    相关资源
    最近更新 更多