vs2005 爲函數自動添加註釋

a) 自動添加文件註釋  效果如下:

  1. /* 
  2.  * Copyright(C) 2012,Company 保留所有權利。( All rights reserved. ) 
  3.  *  
  4.  * 文件名稱:TEDlg.cpp 
  5.  * 摘    要: 
  6.  * 當前版本:1.0 
  7.  * 作    者:YuJian 
  8.  * 創建日期:2012年3月18日 
  9.  */  


 

b) 自動添加函數註釋 效果如下:

  1. //-----------------------------------------------------------  
  2. //       
  3. //  函數名稱:CTEDlg::OnInitDialog  
  4. //       
  5. //  參    數: -無  
  6. //       
  7. //  返    回:BOOL  
  8. //       
  9. //  函數作用:  
  10. //       
  11. //  修改日期:2012年3月18日  By YuJian  
  12. //   
  13. //-----------------------------------------------------------  


 

所使用的途徑 是:編寫宏

而在,編寫宏之前,要給VS2005安裝補丁文件 ,否則 vs2005無法執行宏

英文補丁 431M

http://download.microsoft.com/download/6/3/c/63c69e5d-74c9-48ea-b905-30ac3831f288/VS80sp1-KB926601-X86-ENU.exe

中文補丁 
http://download.microsoft.com/download/8/0/7/8071514d-9370-45c3-8af1-4ff09a70e59d/VS80sp1-KB926604-X86-CHS.exe

 

安裝好了以後,就可以編寫自動註釋函數的宏了。。。

由於是VB,以前沒用過,因此借鑑網上的程序,自己根據需要稍微修改了下

 

        1 建立宏項目

    工具→宏→新建宏項目,輸入文件名稱,並選擇要保存的位置

    將下面的代碼複製到Module1模塊,保存。

 

  1. Imports System  
  2. Imports EnvDTE  
  3. Imports EnvDTE80  
  4. Imports System.Diagnostics  
  5.   
  6. Public Module Module1  
  7.     ' --------------------------------------------------  
  8.     ' 生成文件說明註釋  
  9.     ' --------------------------------------------------  
  10.     Sub FileDescription()  
  11.         Dim gAuthor As String = "YuJian"  
  12.         Dim gCompany As String = "Company"  
  13.   
  14.         Dim outTextDoc As TextDocument  
  15.         Dim outText As EditPoint  
  16.   
  17.         DTE.ActiveDocument.Selection.GotoLine(1)  
  18.         outTextDoc = DTE.ActiveDocument.Object("TextDocument")  
  19.         outText = outTextDoc.StartPoint.CreateEditPoint()  
  20.         outText.Insert("/*" + vbCrLf)  
  21.         outText.Insert(" * Copyright(C) " + Date.Today.Year.ToString() + "," + gCompany + " 保留所有權利。( All rights reserved. )" + vbCrLf)  
  22.         outText.Insert(" * " + vbCrLf)  
  23.         outText.Insert(" * 文件名稱:" + DTE.ActiveDocument.Name + vbCrLf)  
  24.         outText.Insert(" * 摘    要:" + vbCrLf)  
  25.         outText.Insert(" * 當前版本:1.0" + vbCrLf)  
  26.         outText.Insert(" * 作    者:" + gAuthor + vbCrLf)  
  27.         outText.Insert(" * 創建日期:" + Date.Today.ToLongDateString() + vbCrLf)  
  28.         outText.Insert(" */" + vbCrLf)  
  29.         DTE.ActiveDocument.Selection.GotoLine(10)  
  30.     End Sub  
  31.   
  32.     ' --------------------------------------------------  
  33.     ' 生成函數說明註釋  
  34.     ' --------------------------------------------------  
  35.     Sub FunctionRemark()  
  36.         Dim preSpaceCount As Integer = 0   ' 註釋前面的空格數, 縮進(單位:字符)  
  37.         Dim outTextDoc As TextDocument  
  38.         Dim outText As EditPoint  
  39.         Dim iCurrentLineNumber As Integer  
  40.   
  41.         iCurrentLineNumber = DTE.ActiveDocument.Selection.CurrentLine '.ToString()  
  42.         outTextDoc = DTE.ActiveDocument.Object("TextDocument")  
  43.         outText = outTextDoc.StartPoint.CreateEditPoint()  
  44.         ' 移動文本輸入點到指定行上  
  45.         outText.MoveToLineAndOffset(iCurrentLineNumber, 1)  
  46.         Dim strSpace As String = ""  
  47.         Dim iSpaceIndex As Integer  
  48.         For iSpaceIndex = 1 To preSpaceCount  
  49.             strSpace = strSpace + " "  
  50.         Next  
  51.         outText.Insert(strSpace + "//-----------------------------------------------------------" + vbCrLf)  
  52.         outText.Insert(strSpace + "// 函數名稱:" + vbCrLf)  
  53.         outText.Insert(strSpace + "//     " + vbCrLf)  
  54.         outText.Insert(strSpace + "// 參數:" + vbCrLf)  
  55.         outText.Insert(strSpace + "//    - " + vbCrLf)  
  56.         outText.Insert(strSpace + "// 返回:" + vbCrLf)  
  57.         outText.Insert(strSpace + "//     " + vbCrLf)  
  58.         outText.Insert(strSpace + "// 說明:" + vbCrLf)  
  59.         outText.Insert(strSpace + "//     " + vbCrLf)  
  60.         outText.Insert(strSpace + "//-----------------------------------------------------------" + vbCrLf)  
  61.     End Sub  
  62.   
  63.     ' --------------------------------------------------  
  64.     '  
  65.     ' 函數註釋解析部分  
  66.     '  
  67.     ' --------------------------------------------------  
  68.     Public Structure ITEMDATA  
  69.         Public itemType As Integer  
  70.         Public itemText As String  
  71.     End Structure  
  72.     Public lItemList As New System.Collections.Generic.List(Of ITEMDATA)(4)  
  73.     Private Function ParseFunctionDescription(ByVal funText As String) As Boolean  
  74.         Dim strItem As String  
  75.         Dim idata As ITEMDATA  
  76.         Dim strSplit As String() = funText.Split("(")  
  77.   
  78.         If strSplit.Length = 1 Then  
  79.             Return False  
  80.         End If  
  81.   
  82.         '解析函數名稱部分  
  83.         If strSplit.Length > 2 Then  
  84.             strItem = strSplit(strSplit.Length - 2).Trim()  
  85.         Else  
  86.             strItem = strSplit(0).Trim()  
  87.         End If  
  88.         Dim strHeadSplit As String() = strItem.Split(" ")  
  89.         strItem = strHeadSplit(strHeadSplit.Length - 1).Trim()  
  90.         idata.itemType = 1  
  91.         idata.itemText = strItem.Trim()  
  92.         lItemList.Add(idata)  
  93.   
  94.         '解析參數部分  
  95.         strItem = strSplit(strSplit.Length - 1).Trim()  
  96.         If strItem.Substring(0, 1) <> ")" Then  
  97.             Dim iend As Integer = strItem.IndexOf(")", 0)  
  98.             Dim strParams As String = strItem.Substring(0, iend).Trim()  
  99.             Dim strParamSplit As String() = strParams.Split(",")  
  100.             For Each strItem In strParamSplit  
  101.                 idata.itemType = 2  
  102.                 idata.itemText = strItem.Trim()  
  103.                 lItemList.Add(idata)  
  104.             Next strItem  
  105.         Else  
  106.             idata.itemType = 2  
  107.             idata.itemText = "無參數"  
  108.             lItemList.Add(idata)  
  109.         End If  
  110.         '解析返回值類型  
  111.         Dim iIndex As Integer  
  112.         For iIndex = 0 To strHeadSplit.Length - 2  
  113.             idata.itemType = 3  
  114.             idata.itemText = strHeadSplit(iIndex).Trim()  
  115.             lItemList.Add(idata)  
  116.         Next iIndex  
  117.         Return True  
  118.     End Function  
  119.   
  120.     ' --------------------------------------------------  
  121.     '  
  122.     ' 根據函數聲明生成註釋  
  123.     '  
  124.     ' --------------------------------------------------  
  125.     Sub MakeFunctionRemark()  
  126.         Dim preSpaceCount As Integer = 0   ' 註釋前面的空格數, 縮進(單位:字符)  
  127.         Dim outTextDoc As TextDocument  
  128.         Dim outText As EditPoint  
  129.         Dim iCurrentLineNumber As Integer  
  130.         Dim iLineLength As Integer  
  131.         Dim strFunText As String  
  132.         Dim iItemIndex As Integer  
  133.         Dim idata As ITEMDATA  
  134.   
  135.         lItemList.Clear()  
  136.   
  137.         iCurrentLineNumber = DTE.ActiveDocument.Selection.CurrentLine.ToString()  
  138.         outTextDoc = DTE.ActiveDocument.Object("TextDocument")  
  139.         outText = outTextDoc.StartPoint.CreateEditPoint()  
  140.         ' 移動文本輸入點到指定行上  
  141.         outText.MoveToLineAndOffset(iCurrentLineNumber, 1)  
  142.         iLineLength = outText.LineLength  
  143.         strFunText = outText.GetText(iLineLength)  
  144.         iLineLength = strFunText.Trim().Length  
  145.         '但前行沒有內容直接返回  
  146.         If iLineLength = 0 Then  
  147.             Return  
  148.         End If  
  149.   
  150.         ' 解析函數名稱  
  151.         Dim bResult As Boolean = ParseFunctionDescription(strFunText.Trim())  
  152.         If bResult = False Then  
  153.             lItemList.Clear()  
  154.             Return  
  155.         End If  
  156.   
  157.         Dim pcount As Integer = 0  
  158.         Dim rcount As Integer = 0  
  159.         Dim strSpace As String = ""  
  160.         Dim iSpaceIndex As Integer  
  161.         For iSpaceIndex = 1 To preSpaceCount  
  162.             strSpace = strSpace + " "  
  163.         Next  
  164.         outText.Insert(strSpace + "//-----------------------------------------------------------" + vbCrLf)  
  165.         For iItemIndex = 0 To lItemList.Count - 1  
  166.             idata = lItemList.Item(iItemIndex)  
  167.             Select Case idata.itemType  
  168.                 Case 1  
  169.                     outText.Insert(strSpace + "//     " + vbCrLf)  
  170.                     outText.Insert(strSpace + "//  函數名稱:" + idata.itemText + vbCrLf)  
  171.   
  172.                     outText.Insert(strSpace + "//     " + vbCrLf)  
  173.                 Case 2  
  174.   
  175.                     If idata.itemText = "無參數" Then  
  176.                         outText.Insert(strSpace + "//  參    數: -無" + vbCrLf)  
  177.                         outText.Insert(strSpace + "//     " + vbCrLf)  
  178.   
  179.                     Else  
  180.                         If pcount = 0 Then  
  181.                             outText.Insert(strSpace + "//  參    數:" + vbCrLf)  
  182.                         End If  
  183.                         pcount = pcount + 1  
  184.                         outText.Insert(strSpace + "//    - " + idata.itemText + vbCrLf)  
  185.                         outText.Insert(strSpace + "//     " + vbCrLf)  
  186.                     End If  
  187.   
  188.                 Case 3  
  189.                     If rcount = 0 Then  
  190.                         outText.Insert(strSpace + "//  返    回:" + idata.itemText + vbCrLf)  
  191.                         outText.Insert(strSpace + "//     " + vbCrLf)  
  192.                     End If  
  193.                     rcount = rcount + 1  
  194.                 Case 4  
  195.                 Case 5  
  196.             End Select  
  197.         Next  
  198.         outText.Insert(strSpace + "//  函數作用:" + vbCrLf)  
  199.         outText.Insert(strSpace + "//     " + vbCrLf)  
  200.   
  201.         outText.Insert(strSpace + "//  修改日期:" + Date.Today.ToLongDateString() + "  By YuJian" + vbCrLf)  
  202.         outText.Insert(strSpace + "// " + vbCrLf)  
  203.   
  204.         outText.Insert(strSpace + "//-----------------------------------------------------------" + vbCrLf)  
  205.   
  206.         lItemList.Clear()   '清楚所有元素  
  207.     End Sub  
  208.   
  209.   
  210.   
  211. End Module  


 

將宏項目加載後, 在宏資源器中,便會發現該項目有三個宏方法:

 

FileDescription :  是爲整個文件添加註釋

FunctionRemark: 爲函數添加註釋------(很粗糙的  信息需要自己輸入,這是中間函數  )

MakeFunctionRemark:  自動爲函數添加註釋

 

我們需要的是 FileDescription  和MarkFunctionRemark這兩個宏函數。

分別雙擊它們,便可建立文件註釋和函數註釋。  不過,建立函數註釋前, 光標必須位於函數定義處。

 

c) 對語句進行說明

  1. /******************************    add by yujian 2012年5月8日 10:19   *************************************/  
  2.   
  3.     if (FontType&RASTER_FONTTYPE)  
  4.     {  
  5.         return TRUE;  
  6.     }  
  7.   
  8. /******************************    add by yujian 2012年5月8日 10:19   *************************************/  


 

代碼:

 

  1. ' --------------------------------------------------  
  2. ' 生成函數語句說明註釋  
  3. ' --------------------------------------------------  
  4. Sub FunctionRemark()  
  5.   
  6.     Dim preSpaceCount As Integer = 0   ' 註釋前面的空格數, 縮進(單位:字符)  
  7.     Dim outTextDoc As TextDocument  
  8.     Dim outText As EditPoint  
  9.     Dim iCurrentLineNumber As Integer  
  10.   
  11.     ' iCurrentLineNumber = DTE.ActiveDocument.Selection.CurrentLine '.ToString()  
  12.   
  13.     iCurrentLineNumber = DTE.ActiveDocument.Selection.TopLine  
  14.   
  15.     outTextDoc = DTE.ActiveDocument.Object("TextDocument")  
  16.     outText = outTextDoc.StartPoint.CreateEditPoint()  
  17.     ' 移動文本輸入點到指定行上  
  18.     outText.MoveToLineAndOffset(iCurrentLineNumber - 1, 1)  
  19.     Dim strSpace As String = ""  
  20.     Dim iSpaceIndex As Integer  
  21.     For iSpaceIndex = 1 To preSpaceCount  
  22.         strSpace = strSpace + " "  
  23.     Next  
  24.     outText.Insert(strSpace + "/******************************   " + " add by yujian " + Date.Today.ToLongDateString() + " " + Date.Now.Hour.ToString + ":" + Date.Now.Minute.ToString + "   *************************************/" + vbCrLf)  
  25.     ' outText.Insert(strSpace + vbCrLf)  
  26.     '  outText.Insert(strSpace + "/******************************   " + " add by yujian " + Date.Today.ToLongDateString() + " " + Date.Now.Hour.ToString + ":" + Date.Now.Minute.ToString + "   *************************************/" + vbCrLf)  
  27.   
  28.   
  29.     iCurrentLineNumber = DTE.ActiveDocument.Selection.BottomLine + 1  
  30.   
  31.     ' 移動文本輸入點到指定行上  
  32.     outText.MoveToLineAndOffset(iCurrentLineNumber + 1, 1)  
  33.     outText.Insert(strSpace + "/******************************   " + " add by yujian " + Date.Today.ToLongDateString() + " " + Date.Now.Hour.ToString + ":" + Date.Now.Minute.ToString + "   *************************************/" + vbCrLf)  
  34.   
  35.   
  36.   
  37.       
  38. End Sub  


 

 

 

2 建立快捷鍵

 

在源項目工程中【不是在宏項目中】  依次選擇

工具→選項→環境→鍵盤

設置用來運行宏的快捷鍵,注意不要和已有的快捷鍵相沖突。

 

比如:  建立文件註釋  快捷鍵爲: ALT+A

              建立函數註釋 快捷鍵爲: ALT+R

 

 

 

 

參考資料:

http://z.chao.yue.blog.163.com/blog/static/13086262020114210373211/

http://www.cppblog.com/eday/archive/2007/07/15/28079.html

http://pppboy.blog.163.com/blog/static/3020379620091030111611858/

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