如何不使用VA创建头文件说明注释宏
安装Macros for Visual Studio插件
为何使用Macros for Visual Studio
因为公司代码规范要求每个头文件都需要一个头文件说明注释, 公司同事一般都是使用VA插件的snippet功能来实现这个功能, 然后出于一些原因个人并不是很喜欢用VA(虽然很好用), 考虑过使用VS自带的code snippet功能来实现这个功能, 折腾一会发现自带的code snippet并不能自动插入时间(脑壳疼).摸了一下度娘发现这篇文章http://www.cnblogs.com/ben121011/p/5970053.html, 这篇文章的作者通过结合code snippet和Macros for Visual Studio来"手动"添加头文件说明注释.这就是我使用Macros for Visual Studio的原因.
安装Macros for Visual Studio
在VS的菜单栏中,打开“工具>扩展与更新”,搜索并下载管理宏的插件Macros for Visual studio(没错, 这段我复制粘贴的).
设置
装完重启之后在下图中找到Macro Explorer
然后VS界面上便会出现
右键点击Macro Browser List>New Macro 创建新的宏文件, 复制以下的代码到该文件中去(仅供参考)
// Creates a standard copyright header
var doc = dte.ActiveDocument;
// Helper function to create a date and time
var MakeDateAndTime = function () {
var date = new Date();
var day = date.getDate();
var month = date.getMonth() + 1;
var year = date.getYear();
var hours = date.getHours();
var minutes = date.getMinutes();
// Add a zero if single digit
if (day <= 9) day = "0" + day;
if (month <= 9) month = "0" + month;
if (minutes <= 9) minutes = "0" + minutes;
if (hours <= 9) hours = "0" + hours;
return year + "年" + month + "月" + day + "日 " + hours + "时" + minutes + "分";
}
var filename = doc.Name;
if (dte.UndoContext.IsOpen)
dte.UndoContext.Close();
dte.UndoContext.Open("Insert Header");
// Go to start of document
doc.Selection.StartOfDocument(false);
// Start header
doc.Selection.Insert("/********************************************************************\n\
Copyright (c) 2018, IGG China R&D 3\n\
All rights reserved\n\
\n\
创建日期: " + MakeDateAndTime() + "\n" + "\
文件名称: " + filename + "\n" + "\
说 明: \n\
\n\
当前版本: 1.00\n\
作 者: Marskey\n\
概 述:\n\
\n\
********************************************************************/\n", 1);
//// Insert header content
//doc.Selection.Insert("/********************************************************************\n", 1);
//doc.Selection.Insert(" Copyright (c) 2018, IGG China R&D 3\n", 1);
//doc.Selection.Insert(" All rights reserved\n", 1);
//doc.Selection.Insert("\n", 1);
//doc.Selection.Insert(" 创建日期: " + MakeDateAndTime(), 1);
//doc.Selection.Insert(" 文件名称: " + filename, 1);
//doc.Selection.Insert("\n", 1);
//doc.Selection.Insert(" 说 明: \n", 1);
//doc.Selection.Insert("\n", 1);
//doc.Selection.Insert(" 当前版本: 1.00\n", 1);
//doc.Selection.Insert(" 作 者: Marskey\n", 1);
//doc.Selection.Insert(" 概 述:\n", 1);
//doc.Selection.Insert("\n", 1);
//// Close header
//doc.Selection.Insert("********************************************************************/\n", 1);
doc.Selection.GotoLine(7);
doc.Selection.EndOfLine();
dte.UndoContext.Close();
如果使用文中注释部分的代码, 也是可以的.唯一不足的就是当使用这个宏的时候, 注释文字是一行行出现的 ==||| 这就很尴尬, 所以使用了上面的一行insert进所有内容.
然后右键点击创建的宏文件选择Assign shortcut 来分配快捷键, 这里我选的是Ctrl + M, 1 这样就可以了, 在头文件中就能自动生成带有时间和文件名的说明注释了.