引言
在公司,为了阅读和维护方便,代码的注释都有严格规定的格式。每次写好一个类或者一个函数,添加注释的时候都是从原来已经注释好的地方复制过来再修改,麻烦不说还容易出错或者忘记修改一些内容,尤其是函数注释,内容比较多,更容易出错。后来参考了几篇博客写了一个宏来专门添加函数的注释。
参考文章
http://blog.csdn.net/wlccomeon/article/details/9201675
http://blog.csdn.net/shuaishifu/article/details/20199957
解决方案
规定的函数格式如下:
1 // --------------------------------------------------------------- 2 // 名称: InsertShapeAt 3 // 功能: 从指定位置插入一个图元 4 // 变量: [in] index -- 指定位置索引 5 // [in] shape -- 要插入的图元指针 6 // 返回: 无 7 // 编写: 角落,20150407 8 // --------------------------------------------------------------- 9 void Layer::InsertShapeAt(int index,Shape *shape) 10 { 11 }
点击VS2010菜单栏“工具----宏----宏IDE...”,在左侧“项目资源管理器”中“MyMacros”上右键“添加----添加模块...”,新模块可以命名为ModuleAddText,双击新模块编辑,用下面代码完全替换
1 Imports System 2 Imports EnvDTE 3 Imports EnvDTE80 4 Imports EnvDTE90 5 Imports EnvDTE90a 6 Imports EnvDTE100 7 Imports System.Diagnostics 8 Imports System.Text 9 Imports System.Text.RegularExpressions 10 Imports System.IO 11 Imports System.Collections.Specialized 12 13 Public Module ModuleAddText \'这里要与保存的Module名保持一致,不然无法调用宏 14 Sub FunctionSign() 15 \'DESCRIPTION 文件签名 16 Dim obj 17 obj = Now() 18 Dim mo As Integer 19 Dim da As Integer 20 Dim funct As String \'保存选择的行 21 Dim ret As String 22 Dim func As String \'去除空格后的整个函数原型 23 Dim fName As String \'函数名 24 Dim fPara As String \'函数参数字符串 25 Dim paras() As String 26 Dim temps() As String 27 Dim flag1, flag2, flag3 As Integer 28 Dim DocSel As EnvDTE.TextSelection 29 DocSel = DTE.ActiveDocument.Selection 30 DocSel.SelectLine() 31 If DocSel.Text = "" Then 32 Exit Sub 33 End If 34 funct = DocSel.Text 35 func = Trim(DocSel.Text) 36 flag1 = InStr(func, " ") 37 ret = Left(func, flag1 - 1) 38 flag1 = InStr(func, "::") 39 If flag1 = 0 Then 40 flag1 = InStr(func, " ") 41 func = Right(func, Len(func) - flag1) 42 Else 43 func = Right(func, Len(func) - flag1 - 1) 44 End If 45 flag2 = InStr(func, "(") 46 fName = Left(func, flag2 - 1) 47 func = Right(func, Len(func) - flag2) 48 flag3 = InStr(func, ")") 49 fPara = Left(func, flag3 - 1) 50 paras = Split(fPara, ",") 51 For i As Integer = 0 To paras.Length - 1 52 paras(i) = Trim(paras(i)) 53 temps = Split(paras(i)) 54 paras(i) = temps(temps.Length - 1) 55 Next 56 DocSel.NewLine() 57 DocSel.Text = "// ---------------------------------------------------------------" 58 DocSel.NewLine() 59 DocSel.Text = "// 名称: " + fName 60 DocSel.NewLine() 61 DocSel.Text = "// 功能: " 62 DocSel.NewLine() 63 DocSel.Text = "// 变量: " 64 If paras.Length = 0 Then 65 DocSel.Text = "无" 66 ElseIf paras.Length = 1 And paras(0) = "" Then 67 DocSel.Text = "无" 68 Else 69 DocSel.Text = "[in] " + paras(0) + " -- " 70 For j As Integer = 1 To paras.Length - 1 71 DocSel.NewLine() 72 DocSel.Text = "// [in] " + paras(j) + " -- " 73 Next 74 End If 75 DocSel.NewLine() 76 DocSel.Text = "// 返回: " 77 If ret = "void" Then 78 DocSel.Text = "无" 79 End If 80 DocSel.NewLine() 81 DocSel.Text = "// 编写: 角落," + CStr(Year(obj)) 82 mo = Month(obj) 83 da = Day(obj) 84 If mo < 10 Then 85 DocSel.Text = "0" + CStr(mo) 86 Else 87 DocSel.Text = CStr(mo) 88 End If 89 If da < 10 Then 90 DocSel.Text = "0" + CStr(da) 91 Else 92 DocSel.Text = CStr(da) 93 End If 94 DocSel.NewLine() 95 DocSel.Text = "// ---------------------------------------------------------------" 96 DocSel.NewLine() 97 DocSel.Text = funct 98 End Sub 99 End Module
保存退出。在VS2010菜单栏点“工具----宏----Macro资源管理器”,在代码中把鼠标光标放到需要添加注释的函数行上,然后双击“Macro资源管理器”中的函数名“FunctionSign”就可以添加注释了。为方便使用可以给这个宏添加一个快捷键。在菜单栏点“工具----选项”在弹出的对话框中选择“环境----键盘”,在“显示命令包含”下面那里输入刚才的函数名,也就是“FunctionSign”,在下面选择自己刚刚创建的宏,然后把光标定位到下面“按快捷键”下面的文本框内,输入一个不冲突的快捷键比如“Alt+`”(Esc键下面的那个键),然后点“分配”“确定”。用的时候只要把光标放到需要添加注释的函数上,然后按快捷键就可以了。
其他问题
在实际使用时可能还会遇到宏功能不能使用的问题,这可能是因为系统打补丁的时候有补丁把宏功能禁用了,可以修改配置文件如下:
对于所有受影响的 Visual Studio 版本,配置文件的修改过程均相同。需要在 <runtime> 部分中添加 AllowDComReflection 开关,如下所示:
<AllowDComReflection enabled="1"/>