一、功能介绍
环境:VC 9(Visual Studio 2008),其他.net版本没有测试
功能:在VC 9编辑器中为代码添加符合Doxygen标准的注释,其中包括:
1. 模块注释
2. 分组注释
3. 新头文件注释
4. 文件头注释
5. 简要注释
6. 详细注释
7. 类注释
8. 函数注释
9. 成员注释
10. 项目符号标记注释
二、安装
1. 在开发环境中,点击“工具”à “宏”à“宏资源管理器”
2. 在“宏资源管理器”中,新建宏,然后复制下面的VBS代码
3. 将"* Author: ***"中的“***”改为自己的名字,这个名字会出现在文件注释中。同样,将ActiveDocument.Selection = "* <pre><b>email: </b>***@***</pre>"改为自己的邮箱地址。修改完成后保存。使用宏时,双机宏即可。
'/**
'* @file COMMENT.DSM
'* @brief 添加文件头注释、类注释、函数注释、模块注释等。
'* @author Hao Liming
'* @date 2009-03-04 8:42:21
'* @version 0.1
'* <pre><b>copyright: </b></pre>
'* <pre><b>email: </b>hao.limin@gmail.com</pre>
'* <pre><b>company: </b>http://blog.csdn.net/donhao</pre>
'* <pre><b>All rights reserved.</b></pre>
'* <pre><b>modification:</b></pre>
'*/
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics
Public Module COMMENT
Function StripTabs(ByVal MyStr)
Do While InStr(MyStr, vbTab) <> 0
MyStr = Right(MyStr, Len(MyStr) - InStr(MyStr, vbTab))
Loop
StripTabs = Trim(MyStr)
End Function
'生成Doxygen样式的函数注释
Public Sub FunctionDescription()
'判断所选择的行
Dim StartLine, endLine, Temp, tmpLine, Header, Reti, Loc, RetTp, Loc2, fcName, iPrm, iPrmA, prms, ParamArr, Last
StartLine = ActiveDocument.Selection.TopLine
endLine = ActiveDocument.Selection.BottomLine
If endLine < StartLine Then
Temp = StartLine
StartLine = endLine
endLine = Temp
End If
'如果行数大于1,则将各行的字符串合成一个字符串
tmpLine = StartLine
Do While tmpLine <= endLine
ActiveDocument.Selection.GoToLine(tmpLine)
ActiveDocument.Selection.SelectLine()
Header = Header & StripTabs(Trim(ActiveDocument.Selection.text))
tmpLine = tmpLine + 1
Loop
'把回车换成空格
Header = Replace(Header, vbCrLf, " ")
ActiveDocument.Selection.GoToLine(StartLine)
If Header <> "" Then
Reti = InStr(Header, " ")
Loc = InStr(Header, "(")
If Reti < Loc Then
RetTp = Left(Header, Reti)
Header = Right(Header, Len(Header) - Reti)
End If
Loc = InStr(Header, "(") - 1
Loc2 = InStr(Header, ")")
If Loc > 0 And Loc2 > 0 Then
fcName = Left(Header, Loc)
Header = Right(Header, Len(Header) - Len(fcName))
Trim(fcName)
'得到函数名称
Do While InStr(fcName, " ") <> 0
RetTp = RetTp + Left(fcName, InStr(fcName, " "))
fcName = Right(fcName, Len(fcName) - InStr(fcName, " "))
Loop
'如果函数名称第一个字符为"*"或"&",则做为返回值最后一个字符
If InStr(fcName, "*") = 1 Then
RetTp = RTrim(RetTp) + "*"
fcName = LTrim(Right(fcName, Len(fcName) - 1))
End If
If InStr(fcName, "&") = 1 Then
RetTp = RTrim(RetTp) + "&"
fcName = LTrim(Right(fcName, Len(fcName) - 1))
End If
'对返回值进行处理
'去掉virtual
If InStr(RetTp, "virtual") <> 0 Then
RetTp = LTrim(Right(RetTp, Len(RetTp) - Len("virtual")))
End If
'去掉inline
If InStr(RetTp, "inline") <> 0 Then
RetTp = LTrim(Right(RetTp, Len(RetTp) - Len("inline")))
End If
'去掉static
If InStr(RetTp, "static") <> 0 Then
RetTp = LTrim(Right(RetTp, Len(RetTp) - Len("static")))
End If
iPrm = 0
iPrmA = 0
prms = Header
Do While InStr(prms, ",") <> 0
iPrm = iPrm + 1
prms = Right(prms, Len(prms) - InStr(prms, ","))
Loop
If iPrm > 0 Then
iPrm = iPrm + 1
iPrmA = iPrm
ReDim ParamArr(iPrm)
Do While InStr(Header, ",") <> 0
ParamArr(iPrm) = Left(Header, InStr(Header, ",") - 1)
If InStr(ParamArr(iPrm), " (") <> 0 Then
ParamArr(iPrm) = Right(ParamArr(iPrm), _
Len(ParamArr(iPrm)) - InStr(ParamArr(iPrm), " ("))
Trim(ParamArr(iPrm))
End If
Header = Right(Header, Len(Header) - InStr(Header, ","))
iPrm = iPrm - 1
Loop
ParamArr(iPrm) = Header
If InStr(ParamArr(iPrm), ")") <> 0 Then
ParamArr(iPrm) = Left(ParamArr(iPrm), InStr(ParamArr(iPrm), ")") - 1)
Trim(ParamArr(iPrm))
End If
Else
ReDim ParamArr(1)
Header = Right(Header, Len(Header) - 1)
Trim(Header)
ParamArr(1) = StripTabs(Header)
If InStr(ParamArr(1), ")") <> 1 Then
ParamArr(1) = Left(ParamArr(1), InStr(ParamArr(1), ")") - 1)
Trim(ParamArr(1))
iPrmA = 1
If ParamArr(1) = "void" Then
iPrmA = 0
End If
End If
End If
If ActiveDocument.Selection.CurrentLine <> 1 Then
ActiveDocument.Selection.GoToLine(ActiveDocument.Selection.CurrentLine - 1)
ActiveDocument.Selection.MoveTo(ActiveDocument.Selection.CurrentLine, 0)
ActiveDocument.Selection.EndOfLine()
ActiveDocument.Selection.NewLine()
End If
ActiveDocument.Selection.text = "/** "
ActiveDocument.Selection.NewLine()
'判断是构造函数还是析构函数
If Len(Trim(RetTp)) > 0 Then
ActiveDocument.Selection.text = "* @brief " + fcName + " "
Else
'为构造函数
If InStr(fcName, "~") <> 0 Then
ActiveDocument.Selection.text = "* @brief " + "Destructor for " + Right(fcName, Len(fcName) - 1) + "."
'为析构函数
Else
ActiveDocument.Selection.text = "* @brief " + "Constructor for " + fcName + "."
End If
End If
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* Detailed description."
Last = iPrmA
Do While iPrmA <> 0
If InStr(ParamArr(iPrmA), vbLf) <> 0 Then
ParamArr(iPrmA) = Right(ParamArr(iPrmA), (Len(ParamArr(iPrmA)) - _
InStr(ParamArr(iPrmA), vbLf)))
Trim(ParamArr(iPrmA))
End If
ParamArr(iPrmA) = StripTabs(ParamArr(iPrmA))
If iPrmA = Last And Last <> 1 Then
ParamArr(iPrmA) = Right(ParamArr(iPrmA), Len(ParamArr(iPrmA)) - 1)
End If
ActiveDocument.Selection.NewLine()
'首先判断参数列表中有没有'='号,如果有,则等号左边为参数名,右边为默认值。
Dim defautValue
If InStr(ParamArr(iPrmA), "=") <> 0 Then
defautValue = LTrim(Right(ParamArr(iPrmA), Len(ParamArr(iPrmA)) - InStr(ParamArr(iPrmA), "=")))
ParamArr(iPrmA) = RTrim(Left(ParamArr(iPrmA), InStr(ParamArr(iPrmA), "=") - 1))
End If
Do While InStr(defautValue, " ") <> 0
defautValue = Right(defautValue, Len(defautValue) - InStr(defautValue, " "))
Loop
Do While InStr(ParamArr(iPrmA), " ") <> 0
ParamArr(iPrmA) = Right(ParamArr(iPrmA), Len(ParamArr(iPrmA)) - InStr(ParamArr(iPrmA), " "))
Loop
'如果形参形如std::string &name时,应该将引用符号放到前边
If InStr(ParamArr(iPrmA), "*") = 1 Or InStr(ParamArr(iPrmA), "&") = 1 Then
ParamArr(iPrmA) = LTrim(Right(ParamArr(iPrmA), Len(ParamArr(iPrmA)) - 1))
End If
If Len(Trim(defautValue)) > 0 Then
ActiveDocument.Selection.text = "* @param[in] " + LTrim(ParamArr(iPrmA)) + " Defaults to " + Trim(defautValue) + "."
Else
ActiveDocument.Selection.text = "* @param[in] " + LTrim(ParamArr(iPrmA)) + " "
End If
iPrmA = iPrmA - 1
Loop
ActiveDocument.Selection.NewLine()
If Len(Trim(RetTp)) > 0 And Trim(RetTp) <> "void" Then
ActiveDocument.Selection.text = "* @return " + RetTp + " "
ActiveDocument.Selection.NewLine()
End If
ActiveDocument.Selection.text = "*/"
Else
MsgBox("It is possible that the function you are trying to work with has a syntax error.")
End If
End If
End Sub
'生成doxygen样式的简要注释
Public Sub BriefDescription()
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "/** @brief */"
End Sub
'生成doxygen样式的公开变量的注释
Public Sub MemberDescription()
ActiveDocument.Selection.text = ActiveDocument.Selection.text + " /**< */"
End Sub
'生成doxygen样式的一般通用的注释
Public Sub DetailDescription()
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "/** "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @brief "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* Detailed description."
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "*/"
End Sub
'生成doxygen样式的一般通用的注释
Public Sub Define()
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "#define "
End Sub
'生成doxygen样式的一般通用的注释
Public Sub Include()
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "#include "
End Sub
'生成doxygen样式的一般通用的注释
Public Sub TypedefStruct()
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "typedef struct"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "{"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "}TSP_PACKED _Struct;"
ActiveDocument.Selection.NewLine()
End Sub
'生成doxygen样式的一般通用的注释
Public Sub TypedefEnum()
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "typedef Enum"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "{"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "}_Enum;"
ActiveDocument.Selection.NewLine()
End Sub
'生成doxygen样式的文件描述
Public Sub FileDescription()
If ActiveDocument.Selection.CurrentLine <> 1 Then
ActiveDocument.Selection.GoToLine(ActiveDocument.Selection.CurrentLine - 1)
ActiveDocument.Selection.MoveTo(ActiveDocument.Selection.CurrentLine, 0)
ActiveDocument.Selection.EndOfLine()
ActiveDocument.Selection.NewLine()
End If
ActiveDocument.Selection.text = "/**"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @file " + ActiveDocument.Name
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @brief "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @author ***"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @date "
ActiveDocument.Selection.text = DateTime.Today + " " + TimeOfDay
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @version "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* <pre><b>copyright: </b></pre>"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* <pre><b>email: </b>***@***</pre>"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* <pre><b>company: </b>http://</pre>"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* <pre><b>All rights reserved.</b></pre>"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* <pre><b>modification:</b></pre>"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* <pre>Write modifications here.</pre>"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "*/"
End Sub
'生成doxygen样式的新文件描述
Public Sub NewFileDescription()
If ActiveDocument.Selection.CurrentLine <> 1 Then
ActiveDocument.Selection.GoToLine(ActiveDocument.Selection.CurrentLine - 1)
ActiveDocument.Selection.MoveTo(ActiveDocument.Selection.CurrentLine, 0)
ActiveDocument.Selection.EndOfLine()
ActiveDocument.Selection.NewLine()
End If
ActiveDocument.Selection.text = "/**"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @file " + ActiveDocument.Name
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @brief "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @author ***"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @date "
ActiveDocument.Selection.text = DateTime.Today + " " + TimeOfDay
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @version "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* <pre><b>copyright: </b></pre>"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* <pre><b>email: </b>***@***</pre>"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* <pre><b>company: </b>http://</pre>"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* <pre><b>All rights reserved.</b></pre>"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* <pre><b>modification:</b></pre>"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* <pre>Write modifications here.</pre>"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "*/"
If InStr(ActiveDocument.Name, ".") > 0 Then
If Right(ActiveDocument.Name, Len(ActiveDocument.Name) - InStr(ActiveDocument.Name, ".")) = "h" _
Or Right(ActiveDocument.Name, Len(ActiveDocument.Name) - InStr(ActiveDocument.Name, ".")) = "hpp" _
Or Right(ActiveDocument.Name, Len(ActiveDocument.Name) - InStr(ActiveDocument.Name, ".")) = "hh" Then
Dim def
def = "_" + UCase(Left(ActiveDocument.Name, InStr(ActiveDocument.Name, ".") - 1) _
+ "_" + Right(ActiveDocument.Name, Len(ActiveDocument.Name) - InStr(ActiveDocument.Name, ".")))
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "#ifndef " + def
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "#define " + def
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "#endif "
ActiveDocument.Selection.NewLine()
End If
If Right(ActiveDocument.Name, Len(ActiveDocument.Name) - InStr(ActiveDocument.Name, ".")) = "c" _
Or Right(ActiveDocument.Name, Len(ActiveDocument.Name) - InStr(ActiveDocument.Name, ".")) = "cpp" Then
Dim def
def = "#include " + Chr(34) + Left(ActiveDocument.Name, InStr(ActiveDocument.Name, ".")) + "h" + Chr(34)
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = def
ActiveDocument.Selection.NewLine()
End If
End If
End Sub
'生成doxygen样式的项目编号描述
Public Sub ItemDescription()
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "/**"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* - "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* -# "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* -# "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* - "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* -# "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* -# "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "*/"
End Sub
'生成doxygen样式模块描述
Sub ModuleDescription()
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "/**"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @defgroup "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @brief "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* Detailed description."
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @{"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "*/"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "/** @} */ "
End Sub
'生成doxygen样式类描述
Sub ClassDescription()
Dim className
Dim StartLine
className = ActiveDocument.Selection.text
If Len(className) <= 0 Then
MsgBox("Please select the class name")
Else
StartLine = ActiveDocument.Selection.TopLine
ActiveDocument.Selection.GoToLine(StartLine)
If StartLine > 1 Then
ActiveDocument.Selection.MoveTo(StartLine - 1, 0)
ActiveDocument.Selection.EndOfLine()
ActiveDocument.Selection.NewLine()
End If
If InStr(className, "class") > 0 Then
className = LTrim(Right(className, Len(className) - Len("class")))
End If
If InStr(className, ":") > 0 Then
className = Trim(Left(className, InStr(className, ":") - 1))
Else
className = Trim(className)
End If
ActiveDocument.Selection.text = "/**"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @class " + className
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @brief "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* Detailed description."
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "*/"
End If
End Sub
'生成doxygen样式组描述
Sub GroupDescription()
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "/**"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @name "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @brief "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* "
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* Detailed description."
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "* @{"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "*/"
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.NewLine()
ActiveDocument.Selection.text = "/** @} */"
End Sub
End Module