我可以在Visual Studio 2012中以编程方式折叠/展开特定名称的所有预处理器块吗
Can I programmatically collapse/expand all preprocessor blocks of a certain name in Visual Studio 2012?
我当前的项目在整个代码中有很多调试预处理器块。这些宏的名称有意与系统_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+
接下来,您应该激活"命令窗口"以使用创建的命令。现在只需要键入命令名,如下所示:
MyAddinMenuBar.Connect.MyAddinMenuBar #ifdef PROJNAME_DEBUG
希望魔法会发生。
这个解决方案独立于您编辑的代码的语言,因此非常多功能。
- #定义c-预处理器常量..我做错了什么
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- 预处理器:插入结构名称中的前一个行号
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 基类中的函数名称解析
- 如何在c++中实现处理器调度模拟器
- 模板元程序查找相似的连续类型名称
- Cygwin下的gcc 9.3.0预处理器:cmdline -Dname,但名称似乎未定义
- C++中名称查找和名称绑定之间的区别
- 不同函数名称的预处理器宏
- C / C++ 预处理器从名称创建标头
- 我需要重新定义父项目中的预处理器定义吗
- Cpp 预处理器和文件名的基本名称,行号为字符串
- 如何在 C 预处理器中合并符号名称和符号值
- 用相同名称的typedef替换预处理器宏
- C 预处理器中的功能绑定操作
- 预处理器:生成具有动态名称的函数.多个定义问题
- 我可以在Visual Studio 2012中以编程方式折叠/展开特定名称的所有预处理器块吗
- 在C++中具有名称绑定的EDSL