VS2005用VBA写的代码注释宏

原文地址:
http://blog.csdn.net/leatinfy/archive/2010/03/14/5379095.aspx

 

     代码注释有很多风格,纵多开源软件都使用doxygen中几种注释风格的一种。在多个人协作开发一款软件或系统或者项目时,需要统一编码规范。代码的注释 也是编码规范的一部分。我们在编写代码注释时为了统一风格,经常使用copy-paste方法,然后再修改不同的部分。为了更方便统一代码注释规范的应 用,加快开发中编码的速度,我们在VS2005 IDE中使用代码注释插件,设定相应的快捷键,在使用时只需要按快捷键即可,效果就跟使用VS2005中自带的功能一样,比如在VS2005中选中代码按 C-K C-C即可注释掉选中的代码。

     我们使用VS2005中的宏来编写代码注释插件,其宏代码如下:

  1. Imports  System  
  2. Imports  EnvDTE  
  3. Imports  EnvDTE80  
  4. Imports  System.Diagnostics  
  5.   
  6. 'Code Comment v1.0   
  7. 'leatinfy <leatinfy@gmail.com>   
  8. Public   Module  CodeComment  
  9.  'C-M C-H(head of file)   
  10.  Sub  AddFileHeaderComment()  
  11.     If  DTE.ActiveDocument.Type =  "Text"   Then   
  12.         Dim  selection  As  EnvDTE.TextSelection  
  13.         Dim  commText  As   String   
  14.         Dim  outTextDoc  As  TextDocument  
  15.         Dim  outText  As  EditPoint  
  16.   
  17.         selection = DTE.ActiveDocument.Selection  
  18.         commText = ""  + vbCrLf  
  19.   
  20.         outTextDoc = DTE.ActiveDocument.Object ( "TextDocument" )  
  21.         outText = outTextDoc.StartPoint.CreateEditPoint()  
  22.         outText.MoveToPoint(selection.TopPoint)  
  23.         selection.Text = ""   
  24.         outText.Insert(commText)  
  25.     End   If   
  26.  End   Sub   
  27.   
  28.  '   
  29.  'C-M C-C(code comment)   
  30.  Sub  AddCommentStyle1()  
  31.     If  DTE.ActiveDocument.Type =  "Text"   Then   
  32.         Dim  selection  As  EnvDTE.TextSelection  
  33.         Dim  commText  As   String   
  34.         Dim  topline  As   Integer   
  35.   
  36.         selection = DTE.ActiveDocument.Selection  
  37.         topline = selection.TopLine  
  38.   
  39.         commText = ""   
  40.   
  41.         selection.Text = commText  
  42.         selection.MoveToLineAndOffset(topline + 1, 1)  
  43.         selection.EndOfLine()  
  44.     End   If   
  45. End   Sub   
  46.   
  47.  '   
  48.  'C-M C-A(above the code line)   
  49.  Sub  AddCommentStyle2()  
  50.     If  DTE.ActiveDocument.Type =  "Text"   Then   
  51.         Dim  selection  As  EnvDTE.TextSelection  
  52.         Dim  commText  As   String   
  53.   
  54.         selection = DTE.ActiveDocument.Selection  
  55.         commText = ""   
  56.         selection.Text = commText  
  57.         selection.CharLeft(, 3)  
  58.     End   If   
  59.  End   Sub   
  60.    
  61.  '   
  62.  'C-M C-E (end of the code line)   
  63.  Sub  AddCommentStyle3()  
  64.     If  DTE.ActiveDocument.Type =  "Text"   Then   
  65.         Dim  selection  As  EnvDTE.TextSelection  
  66.         Dim  commText  As   String   
  67.   
  68.         selection = DTE.ActiveDocument.Selection  
  69.         commText = ""   
  70.         selection.Text = commText  
  71.         selection.CharLeft(, 3)  
  72.     End   If   
  73.  End   Sub   
  74.    
  75.  '   
  76.  'C-M C-N(normal c style)   
  77.  Sub  AddCommentStyle4()  
  78.     If  DTE.ActiveDocument.Type =  "Text"   Then   
  79.         Dim  selection  As  EnvDTE.TextSelection  
  80.         Dim  commText  As   String   
  81.   
  82.         selection = DTE.ActiveDocument.Selection  
  83.         commText = ""   
  84.         selection.Text = commText  
  85.         selection.CharLeft(, 3)  
  86.     End   If   
  87.  End   Sub   
  88.   
  89.  'C-M C-F(function comment)   
  90.  Sub  AddFunctionComment()  
  91.     Dim  selection  As  EnvDTE.TextSelection  
  92.     If  DTE.ActiveDocument.Type =  "Text"   Then   
  93.         selection = DTE.ActiveDocument.Selection  
  94.         Dim  functext  As   String   
  95.         Dim  funinfo  As  FuncInfo  
  96.         Dim  editpoint  As  EditPoint  
  97.         Dim  startLine  As   Integer   
  98.         Dim  endLine  As   Integer   
  99.         Dim  strTmp  As   String   
  100.         Dim  commentText  As   String   
  101.         Dim  col  As   Integer   
  102.         Dim  codefunc  As  CodeFunction  
  103.   
  104.   
  105.         codefunc = CType (selection.TopPoint.CodeElement(vsCMElement.vsCMElementFunction), CodeFunction)  
  106.         If  codefunc  Is   Nothing   Then   
  107.             Exit   Sub   
  108.         End   If   
  109.   
  110.         functext = String .Copy(selection.Text)  
  111.   
  112.         commentText = ""   
  113.   
  114.         col = selection.TopPoint.DisplayColumn  
  115.         selection.GotoLine(selection.TopLine)  
  116.         selection.NewLine()  
  117.         selection.LineUp(, 1)  
  118.         If  col > 1  Then   
  119.             selection.PadToColumn(col)  
  120.         Else   
  121.             selection.Text = GetFirstSpaceText(functext)  
  122.         End   If   
  123.         selection.Text = commentText  
  124.      End   If   
  125.  End   Sub   
  126.   
  127.  Public   Class  FuncInfo  
  128.     Public  FuncName  As   String   
  129.     Public  Args  As   New  Collections.ArrayList  
  130.     Public  hasReturn  As   Boolean   
  131.  End   Class   
  132.  Public   Structure  BracketCount  
  133.     Public  LCount  As   Integer   
  134.     Public  RCount  As   Integer   
  135.  End   Structure   
  136.   
  137.  Private   Function  GetFuncInfo( ByVal  funcstxt  As   String )  As  FuncInfo  
  138.     Dim  index  As   Integer   
  139.     Dim  funinfo  As   New  FuncInfo  
  140.     Dim  strTmp  As   String   
  141.     Dim  args  As   String ()  
  142.     index = funcstxt.IndexOf("(" )  
  143.     strTmp = funcstxt.Substring(0, index)  
  144.     funinfo.FuncName = GetVarPartFromEnd(GetLastWord(strTmp))  
  145.     funinfo.hasReturn = HasReturnValue(strTmp)  
  146.     If  index < 0  Then   
  147.         Return  funinfo  
  148.     End   If   
  149.     strTmp = funcstxt.Substring(index + 1)  
  150.     index = strTmp.IndexOf(")" )  
  151.     If  index < 0  Then   
  152.         Return  funinfo  
  153.     End   If   
  154.     strTmp = strTmp.Substring(0, index)  
  155.   
  156.     args = strTmp.Split("," .ToCharArray(), StringSplitOptions.RemoveEmptyEntries)  
  157.     Dim  i  As   Integer  = 0  
  158.     Dim  counter  As  BracketCount  
  159.     Dim  tmpcounter  As  BracketCount  
  160.     While  i < args.Length  
  161.         counter.LCount = 0  
  162.         counter.RCount = 0  
  163.         While  i < args.Length  
  164.             tmpcounter = GetBracketCount(args(i))  
  165.             counter.LCount = counter.LCount + tmpcounter.LCount  
  166.             counter.RCount = counter.RCount + tmpcounter.RCount  
  167.             If  counter.LCount = counter.RCount  Then   
  168.                 Exit   While   
  169.             Else   
  170.                 i = i + 1  
  171.             End   If   
  172.         End   While   
  173.         If  i < args.Length  And  counter.LCount = counter.RCount  Then   
  174.             funinfo.Args.Add(GetLastWord(args(i)))  
  175.         End   If   
  176.         i = i + 1  
  177.     End   While   
  178.   
  179.     Return  funinfo  
  180.  End   Function   
  181.   
  182.  Private   Function  GetVarPartFromEnd( ByVal  funcStr  As   String )  As   String   
  183.     Dim  strLen  As   Integer   
  184.     Dim  ch  As   Char   
  185.     strLen = funcStr.Length  
  186.     For  i  As   Integer  = strLen - 1  To  0  Step  -1  
  187.         ch = funcStr.Chars(i)  
  188.         If  (Asc(ch) >= 48  And  Asc(ch) <= 57)  Or  (Asc(ch) >= 65  And  Asc(ch) <= 90)  Or  (Asc(ch) >= 97  And  Asc(ch) <= 122)  Or  ch =  "_" c  Then   
  189.             Continue For   
  190.         Else   
  191.             Return  funcStr.Substring(i + 1)  
  192.         End   If   
  193.     Next   
  194.     Return  funcStr  
  195.  End   Function   
  196.   
  197.  Private   Function  GetLastWord( ByVal  str  As   String )  As   String   
  198.     Dim  strLen  As   Integer   
  199.     Dim  index  As   Integer   
  200.     Dim  strTmp  As   String   
  201.   
  202.     str = str.TrimEnd((" "  + vbTab + vbLf + vbCrLf).ToCharArray())  
  203.     strLen = str.Length  
  204.     For  i  As   Integer  = strLen - 1  To  0  Step  -1  
  205.         If  str.Chars(i) =  " "   Or  str.Chars(i) = vbTab  Or  str.Chars(i) = vbLf  Or  str.Chars(i) = vbCrLf  Then   
  206.             Return  str.Substring(i + 1)  
  207.         End   If   
  208.     Next   
  209.   
  210.     Return  str  
  211.  End   Function   
  212.   
  213.  Private   Function  GetBracketCount( ByVal  str  As   String )  As  BracketCount  
  214.     Dim  strLen  As   Integer   
  215.     Dim  counter  As  BracketCount  
  216.   
  217.     counter.LCount = 0  
  218.     counter.RCount = 0  
  219.     strLen = str.Length  
  220.   
  221.     For  i  As   Integer  = 0  To  strLen - 1  
  222.         If  str.Chars(i) =  "<" c  Then   
  223.             counter.LCount = counter.LCount + 1  
  224.         End   If   
  225.         If  str.Chars(i) =  ">" c  Then   
  226.             counter.RCount = counter.RCount + 1  
  227.         End   If   
  228.     Next   
  229.   
  230.     Return  counter  
  231.  End   Function   
  232.    
  233.  Private   Function  GetFirstTextPosition( ByVal  str  As   String ,  ByVal  tabSize  As   Integer )  As   Integer   
  234.     Dim  strLen  As   Integer   
  235.     Dim  pos  As   Integer  = 0  
  236.     strLen = str.Length  
  237.     Dim  i  As   Integer  = 0  
  238.   
  239.     For  i = 0  To  strLen - 1  
  240.         If  str.Chars(i) =  " " c  Then   
  241.             pos = pos + 1  
  242.         ElseIf  str.Chars(i) = vbTab  Then   
  243.             pos = pos + tabSize  
  244.         Else   
  245.             Return  pos  
  246.         End   If   
  247.     Next  i  
  248.   
  249.     Return  pos  
  250.  End   Function   
  251.    
  252.  Private   Function  GetFirstSpaceText( ByVal  str  As   String )  As   String   
  253.     Dim  strLen  As   Integer   
  254.     Dim  pos  As   Integer  = 0  
  255.     strLen = str.Length  
  256.     Dim  i  As   Integer  = 0  
  257.   
  258.     For  i = 0  To  strLen - 1  
  259.         If  str.Chars(i) <>  " "   And  str.Chars(i) <> vbTab  Then   
  260.             Exit   For   
  261.     End   If   
  262.     Next  i  
  263.   
  264.     Return  str.Substring(0, i)  
  265.  End   Function   
  266.   
  267.  'check function has return value   
  268.  Private   Function  HasReturnValue( ByVal  str  As   String )  As   Boolean   
  269.     Dim  strs  As   String ()  
  270.   
  271.     strs = str.Split((" "  + vbTab + vbLf + vbCrLf).ToCharArray(), StringSplitOptions.RemoveEmptyEntries)  
  272.     Return   Not  Array.Exists(strs,  AddressOf  IsVoidKeyWords)  
  273.  End   Function   
  274.    
  275.  Private   Function  IsVoidKeyWords( ByVal  str  As   String )  As   Boolean   
  276.     If  str.ToLower() =  "void"   Then   
  277.         Return   True   
  278.     Else   
  279.         Return   False   
  280.     End   If   
  281.  End   Function   
  282. End   Module  
这里我自己编写的宏代码,有四种代码注释风格,和一个文件注释、一个函数注释功能。其注释风格如:

     接下来我们简单说一下如何使用这个注释插件。在“工具->宏->宏IDE”先启动宏IDE,然后新建一个宏项目,就上面的宏代码copy进 去,接着就在VS2005中绑定快捷键,在“工具->选项->键盘”中选定相应的宏函数,然后分配快捷键。宏中的函数前面注释中的快捷键为推 荐的快捷键,自己完全可以跟根自身的喜好来设定。

    比如,我们选中函数getname,然后按C-M C-F(我绑定的是这两个键),然后生成函数上面部分的代码

  1.   
  2. int  getname( int  id)  
  3. {  
  4.     ...  
  5. }  

注:GetFuncInfo GetVarPartFromEnd GetLastWord GetBracketCount HasReturnValue IsVoidKeyWords这几个函数为我自已通过分析字符串来分析函数的参数代码,在查阅文档时发现可以通过.Net的 CodeElement(vsCMElement.vsCMElementFunction)过获取函数的相应信息,就对生成函数注释的宏代码进行了修 改,这几个函数仅供大家参考,在AddFunctionComment中并没有调用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值