VS2008中添加註釋的宏

 

一、功能介紹

環境: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>[email protected]</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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章