Visual Studio宏注释模板

转自:http://blog.csdn.net/jiejiaozhufu/article/details/16357721

  • 前言
         有时写代码需要写注释的时候

         甚是苦恼

         写吧 怕麻烦

         不写吧 似乎这代码估计自己都看不懂

         权衡之下

         似乎找一个自动写注释的方法最靠谱

         一直在VS下开发

         偶尔听人说过有一个宏工具可以帮助开发者快速注释

         但是寻匿了很久

         硬是木有找到

         后来才发现

         原来自VS2012以来,这个宏工具去掉了

         但是我使用的编译器恰恰是VS2012和VS2013

         所以...

         最近换了个电脑,

         直接装个VS2010


         今天又是周末

         于是就倒腾这个宏工具来着的


         虽然没学过VB

         但是有强大的网络在+搜素引擎


         于是乎

         便有了这个模板

  • 模板代码如下:

[vb]  view plain  copy
 print ?
  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 MyAutoCommemt '这里要与保存的Module文件名保持一致,不然无法调用宏  
  14.     Function AlignTitle(ByVal title As StringAs String  
  15.         title += Space(16 - title.Length)  
  16.         AlignTitle = " *" + title  
  17.     End Function  
  18.   
  19.     Function AlignValue(ByVal value As String)  
  20.         value += Space(16 - value.Length)  
  21.         AlignValue = value  
  22.     End Function  
  23.   
  24.     Sub ParamOnce()  
  25.         Dim objSel As TextSelection  
  26.         objSel = CType(DTE.ActiveDocument.Selection, TextSelection)  
  27.         DTE.UndoContext.Open("FileCreateEn")  
  28.         objSel.StartOfDocument(False)  
  29.         objSel.Insert("#pragma once" + vbNewLine)  
  30.         DTE.UndoContext.Close()  
  31.     End Sub  
  32.   
  33.     Public Sub FileCreateEn()  
  34.         'DESCRIPTION 文件签名  
  35.         Dim fil_info(64) As String  
  36.         Dim i As Integer  
  37.         i = 0  
  38.         fil_info(i) = "//Copyright (c) 2013 hustfisher All Rights Reserved"  
  39.         i += 1  
  40.   
  41.         fil_info(i) = "/*********************************************************************************************"  
  42.         i += 1  
  43.   
  44.         fil_info(i) = AlignTitle("file name")  
  45.         fil_info(i) += " : "  
  46.         fil_info(i) += DTE.ActiveDocument.Name  
  47.         i += 1  
  48.   
  49.         fil_info(i) = AlignTitle("description")  
  50.         fil_info(i) += " : "  
  51.         i += 1  
  52.   
  53.         fil_info(i) = AlignTitle("create time")  
  54.         fil_info(i) += " : "  
  55.         fil_info(i) += Date.Now.ToString()  
  56.         i += 1  
  57.   
  58.         fil_info(i) = AlignTitle("author name")  
  59.         fil_info(i) += " : "  
  60.         fil_info(i) += "hustfisher"  
  61.         i += 1  
  62.   
  63.         fil_info(i) = AlignTitle("author email")  
  64.         fil_info(i) += " : "  
  65.         fil_info(i) += "hustfisher@yeah.net"  
  66.         i += 1  
  67.   
  68.         fil_info(i) = AlignTitle("author blog")  
  69.         fil_info(i) += " : "  
  70.         fil_info(i) += "http://blog.csdn.net/jiejiaozhufu"  
  71.         i += 1  
  72.   
  73.         fil_info(i) = AlignTitle("version")  
  74.         fil_info(i) += " : "  
  75.         fil_info(i) += "1.0"  
  76.         i += 1  
  77.   
  78.         fil_info(i) = " **********************************************************************************************/"  
  79.         i += 1  
  80.   
  81.         Dim Description As New StringBuilder  
  82.         For v = 0 To i  
  83.             Description.AppendFormat("{0}{1}", fil_info(v), vbNewLine)  
  84.         Next  
  85.         '插入文件头部  
  86.         Dim objSel As TextSelection  
  87.         objSel = CType(DTE.ActiveDocument.Selection, TextSelection)  
  88.         DTE.UndoContext.Open("FileCreateEn")  
  89.         objSel.StartOfDocument(False)  
  90.         objSel.Insert(Description.ToString())  
  91.         DTE.UndoContext.Close()  
  92.     End Sub  
  93.   
  94.     Public Sub FileModifyEn()  
  95.         'modify file  
  96.         Dim fil_info(64) As String  
  97.         Dim i As Integer  
  98.         i = 0  
  99.         fil_info(i) = "//Copyright (c) 2013 hustfisher All Rights Reserved"  
  100.         i += 1  
  101.   
  102.         fil_info(i) = "/*********************************************************************************************"  
  103.         i += 1  
  104.   
  105.         fil_info(i) = AlignTitle("file name")  
  106.         fil_info(i) += " : "  
  107.         fil_info(i) += DTE.ActiveDocument.Name  
  108.         i += 1  
  109.   
  110.         fil_info(i) = AlignTitle("description")  
  111.         fil_info(i) += " : "  
  112.         i += 1  
  113.   
  114.         fil_info(i) = AlignTitle("modify time")  
  115.         fil_info(i) += " : "  
  116.         fil_info(i) += Date.Now.ToString()  
  117.         i += 1  
  118.   
  119.         fil_info(i) = AlignTitle("author name")  
  120.         fil_info(i) += " : "  
  121.         fil_info(i) += "hustfisher"  
  122.         i += 1  
  123.   
  124.         fil_info(i) = AlignTitle("author email")  
  125.         fil_info(i) += " : "  
  126.         fil_info(i) += "hustfisher@yeah.net"  
  127.         i += 1  
  128.   
  129.         fil_info(i) = AlignTitle("author blog")  
  130.         fil_info(i) += " : "  
  131.         fil_info(i) += "http://blog.csdn.net/jiejiaozhufu"  
  132.         i += 1  
  133.   
  134.         fil_info(i) = AlignTitle("version")  
  135.         fil_info(i) += " : "  
  136.         fil_info(i) += "1.1"  
  137.         i += 1  
  138.   
  139.         fil_info(i) = " **********************************************************************************************/"  
  140.         i += 1  
  141.   
  142.         Dim Description As New StringBuilder  
  143.         For v = 0 To i  
  144.             Description.AppendFormat("{0}{1}", fil_info(v), vbNewLine)  
  145.         Next  
  146.         Dim DocSel As EnvDTE.TextSelection  
  147.         DocSel = DTE.ActiveDocument.Selection  
  148.         DocSel.StartOfLine()  
  149.         DocSel.NewLine()  
  150.         DocSel.LineUp()  
  151.         DocSel.Insert(Description.ToString())  
  152.     End Sub  
  153.   
  154.     Sub FunctionSignEn()  
  155.         'function  
  156.         Dim data As New StringBuilder  
  157.         With data  
  158.             .Append(vbNewLine)  
  159.             .AppendFormat("/*********************************************************************************************{0}", vbNewLine)  
  160.             .AppendFormat(" *function name{0}: {1}", vbTab, vbNewLine)  
  161.             .AppendFormat(" *create   time{0}: {1} {2}", vbTab, Date.Now.ToString(), vbNewLine)  
  162.             .AppendFormat(" *author   name{0}: {1} {2}", vbTab, "hustfisher", vbNewLine)  
  163.             .AppendFormat(" *func  version{0}: 1.0 {1}", vbTab, vbNewLine)  
  164.             .AppendFormat(" *description  {0}: {1}", vbTab, vbNewLine)  
  165.             .AppendFormat(" *para title   {0}: IN/OUT{1}{2}TYPE{3}{4}{5}DESCRIPTION{6}", vbTab, vbTab, vbTab, vbTab, vbTab, vbTab, vbNewLine)  
  166.             .AppendFormat(" *parameter   1{0}: {1}", vbTab, vbNewLine)  
  167.             .AppendFormat(" *return type  {0}: {1}", vbTab, vbNewLine)  
  168.             .AppendFormat(" *********************************************************************************************/")  
  169.         End With  
  170.         Dim DocSel As EnvDTE.TextSelection  
  171.         DocSel = DTE.ActiveDocument.Selection  
  172.         DocSel.StartOfLine()  
  173.         DocSel.NewLine()  
  174.         DocSel.LineUp()  
  175.         DocSel.Insert(data.ToString())  
  176.     End Sub  
  177.   
  178.     Sub FunctionSignEnEx()  
  179.         'function  
  180.         Dim DocSel As EnvDTE.TextSelection  
  181.         DocSel = DTE.ActiveDocument.Selection  
  182.         Dim line As String  
  183.         DocSel.SelectLine()  
  184.         line = DocSel.Text().ToString()  
  185.         line = Trim(line)  
  186.         Dim name As String  
  187.         Dim para As String  
  188.         Dim return_type As String  
  189.         Dim pos As Integer  
  190.         'type  
  191.         pos = line.IndexOf(" ")  
  192.         If pos = -1 Then  
  193.             Return  
  194.         End If  
  195.         return_type = Mid(line, 1, pos)  
  196.         'name  
  197.         Dim pos1 As Integer  
  198.         pos1 = line.IndexOf("(")  
  199.         If pos1 = -1 Then  
  200.             Return  
  201.         End If  
  202.         name = Mid(line, pos + 1, pos1 - pos).Trim  
  203.         'para  
  204.         pos = pos1 + 1  
  205.         pos1 = line.LastIndexOf(")")  
  206.         If pos1 = -1 Then  
  207.             Return  
  208.         End If  
  209.         para = Mid(line, pos + 1, pos1 - pos).Trim()  
  210.         Dim words() As String  
  211.         words = Split(para, ",")  
  212.         Dim func_info(32) As String  
  213.         Dim i As Integer  
  214.         i = 0  
  215.         func_info(i) = "/*********************************************************************************************"  
  216.         i += 1  
  217.   
  218.         func_info(i) = AlignTitle("function name")  
  219.         func_info(i) += " : "  
  220.         func_info(i) += name  
  221.         i += 1  
  222.   
  223.         func_info(i) = AlignTitle("create time")  
  224.         func_info(i) += " : "  
  225.         func_info(i) += Date.Now.ToString()  
  226.         i += 1  
  227.   
  228.         func_info(i) = AlignTitle("author name")  
  229.         func_info(i) += " : "  
  230.         func_info(i) += "hustfisher"  
  231.         i += 1  
  232.   
  233.         func_info(i) = AlignTitle("version")  
  234.         func_info(i) += " : "  
  235.         func_info(i) += "1.0"  
  236.         i += 1  
  237.   
  238.         func_info(i) = AlignTitle("description")  
  239.         func_info(i) += " : "  
  240.         i += 1  
  241.   
  242.         func_info(i) = AlignTitle("return type")  
  243.         func_info(i) += " : "  
  244.         func_info(i) += return_type  
  245.         i += 1  
  246.   
  247.         func_info(i) = AlignTitle("parameter list")  
  248.         func_info(i) += " : "  
  249.         func_info(i) += AlignValue("IN/OUT")  
  250.         func_info(i) += AlignValue("TYPE")  
  251.         func_info(i) += AlignValue("NAME")  
  252.         func_info(i) += AlignValue("DESCRIPTION")  
  253.         i += 1  
  254.   
  255.         Dim j As Integer  
  256.         j = 1  
  257.         Dim paraBuf As String  
  258.         For Each v In words  
  259.             func_info(i) = "parameter "  
  260.             func_info(i) += j.ToString()  
  261.             func_info(i) = AlignTitle(func_info(i))  
  262.             func_info(i) += " : "  
  263.             func_info(i) += AlignValue("IN")  
  264.             paraBuf = v.Trim()  
  265.             pos = paraBuf.LastIndexOf(" ")  
  266.             If -1 = pos Then  
  267.                 func_info(i) += AlignValue("void")  
  268.             Else  
  269.                 func_info(i) += AlignValue(Mid(paraBuf, 1, pos).Trim())  
  270.                 func_info(i) += AlignValue(Mid(paraBuf, pos + 1, paraBuf.Length).Trim())  
  271.             End If  
  272.             i += 1  
  273.   
  274.             j += 1  
  275.         Next  
  276.         Dim Description As New StringBuilder  
  277.         For v = 0 To i - 1  
  278.             Description.AppendFormat("{0}{1}", func_info(v), vbNewLine)  
  279.         Next  
  280.         Description.AppendFormat(" *********************************************************************************************/{0}", vbNewLine)  
  281.         DocSel.StartOfLine()  
  282.         DocSel.LineUp()  
  283.         DocSel.Insert(Description.ToString)  
  284.     End Sub  
  285.   
  286.   
  287.     Sub AddPara()  
  288.         ActiveDocument.Selection.Text = "*parameter   2" + vbTab + ":"  
  289.     End Sub  
  290.   
  291.     Sub ClassSignEn()  
  292.         'function  
  293.         Dim data As New StringBuilder  
  294.         With data  
  295.             .Append(vbNewLine)  
  296.             .AppendFormat("/*********************************************************************************************{0}", vbNewLine)  
  297.             .AppendFormat(" *calss  name : {0}", vbNewLine)  
  298.             .AppendFormat(" *create time : {0} {1}"Date.Now.ToString(), vbNewLine)  
  299.             .AppendFormat(" *author name : {0} {1}""hustfisher", vbNewLine)  
  300.             .AppendFormat(" *calss  vers : 1.0 {0}", vbNewLine)  
  301.             .AppendFormat(" *description : {0}", vbNewLine)  
  302.             .AppendFormat(" *********************************************************************************************/")  
  303.         End With  
  304.         Dim DocSel As EnvDTE.TextSelection  
  305.         DocSel = DTE.ActiveDocument.Selection  
  306.         DocSel.StartOfLine()  
  307.         DocSel.NewLine()  
  308.         DocSel.LineUp()  
  309.         DocSel.Insert(data.ToString())  
  310.     End Sub  
  311.   
  312.     Sub ModifyTag()  
  313.         'DESCRIPTION 增添修改  
  314.         Dim DocSel As EnvDTE.TextSelection  
  315.         DocSel = DTE.ActiveDocument.Selection  
  316.         DocSel.EndOfLine()  
  317.         ActiveDocument.Selection.Text = "/* hustfisher modified at " + Date.Now.ToString() + " */"  
  318.     End Sub  
  319.   
  320.     Sub TodoTag()  
  321.         Dim DocSel As EnvDTE.TextSelection  
  322.         DocSel = DTE.ActiveDocument.Selection  
  323.         DocSel.EndOfLine()  
  324.         ActiveDocument.Selection.Text = " // TODO:" + Date.Now.ToString()  
  325.     End Sub  
  326.   
  327.     Sub AddCommentC()  
  328.         Dim data As New StringBuilder  
  329.         With data  
  330.             .Append(vbNewLine)  
  331.             .AppendFormat("/*********************************************************************************************{0}", vbNewLine)  
  332.             .AppendFormat(" *create time : {0} {1} author : {2} {3}"Date.Now.ToString(), vbTab, "hustfisher", vbNewLine)  
  333.             .AppendFormat(" *description : {0}", vbNewLine)  
  334.             .AppendFormat(" *********************************************************************************************/")  
  335.         End With  
  336.         Dim DocSel As EnvDTE.TextSelection  
  337.         DocSel = DTE.ActiveDocument.Selection  
  338.         DocSel.StartOfLine()  
  339.         DocSel.NewLine()  
  340.         DocSel.LineUp()  
  341.         DocSel.Insert(data.ToString())  
  342.     End Sub  
  343.   
  344.     Sub AddComment()  
  345.         Dim DocSel As EnvDTE.TextSelection  
  346.         DocSel = DTE.ActiveDocument.Selection  
  347.         DocSel.EndOfLine()  
  348.         ActiveDocument.Selection.Text = " // Add by hustfisher " + Date.Now.ToString  
  349.     End Sub  
  350.   
  351. End Module  

  • 使用注意事项
         模板使用的时候,在VS2010下按快捷键alt+F11或者在菜单栏下的tools 下打开宏管理器,此时在MyCacros下新建一个文件

         然后把模板文件考进去,不过需要注意的是,文件名和末班名需要相同

         否则...


  • 功能
  1. 文件创建注释模板
  2. 文件修改注释模板
  3. 类注释模板
  4. 函数注释模板
  5. 自动函数注释(我觉得这个很不错)
  6. TODO Tag模板
  7. C语言格式的一般注释模板
  8. C++语言格式的一般注释模板
  • 注释模板效果
    [cpp]  view plain  copy
     print ?
    1. //Copyright (c) 2013 hustfisher All Rights Reserved  
    2. /********************************************************************************************* 
    3.  *file name        : main.cpp 
    4.  *description      :  
    5.  *create time      : 2013/11/16 15:53:39 
    6.  *author name      : hustfisher 
    7.  *author email     : hustfisher@yeah.net 
    8.  *author blog      : http://blog.csdn.net/jiejiaozhufu 
    9.  *version          : 1.0 
    10.  **********************************************************************************************/  
    11.   
    12. #include "normalise.h"  
    13. #include <Windows.h>  
    14. #include <stdio.h>  
    15.   
    16. #define FRAME_IN_NUM    (128)  
    17. #define LOOP_TIMES      (60240)  
    18. #define ALIGN_SIZE      (64)  
    19.   
    20. /********************************************************************************************* 
    21.  *function name    : InitData 
    22.  *create time      : 2013/11/16 15:53:47 
    23.  *author name      : hustfisher 
    24.  *version          : 1.0 
    25.  *description      :  
    26.  *return type      : void 
    27.  *parameter list   : IN/OUT          TYPE            NAME            DESCRIPTION      
    28.  *parameter 1      : IN              float*          pData            
    29.  *parameter 2      : IN              size_t          nCount           
    30.  *********************************************************************************************/  
    31. void InitData(float* pData, size_t nCount)  
    32. {  
    33.     int my_sign[] = {1, -1};  
    34.     srand(GetTickCount());  
    35.     for (size_t i=0; i<nCount; i++)  
    36.     {  
    37.         pData[i] = rand()%10*my_sign[rand()%2]*1.0f;  
    38.     }  
    39. }  
    40. /********************************************************************************************* 
    41.  *function name    : main 
    42.  *create time      : 2013/11/16 15:53:53 
    43.  *author name      : hustfisher 
    44.  *version          : 1.0 
    45.  *description      :  
    46.  *return type      : int 
    47.  *parameter list   : IN/OUT          TYPE            NAME            DESCRIPTION      
    48.  *parameter 1      : IN              void             
    49.  *********************************************************************************************/  
    50. int main()  
    51. {  
    52.       
    53.     return 0;  
    54. }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值