• 前言

有时写代码需要写注释的时候

甚是苦恼

写吧 怕麻烦

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

权衡之下

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

一直在VS下开发

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

但是寻匿了很久

硬是木有找到

后来才发现

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

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

所以...

最近换了个电脑,

直接装个VS2010

今天又是周末

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

虽然没学过VB

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

于是乎

便有了这个模板

  • 模板代码如下:
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics
Imports System.Text
Imports System.Text.RegularExpressions
Imports System.IO
Imports System.Collections.Specialized Public Module MyAutoCommemt '这里要与保存的Module文件名保持一致,不然无法调用宏
Function AlignTitle(ByVal title As String) As String
title += Space(16 - title.Length)
AlignTitle = " *" + title
End Function Function AlignValue(ByVal value As String)
value += Space(16 - value.Length)
AlignValue = value
End Function Sub ParamOnce()
Dim objSel As TextSelection
objSel = CType(DTE.ActiveDocument.Selection, TextSelection)
DTE.UndoContext.Open("FileCreateEn")
objSel.StartOfDocument(False)
objSel.Insert("#pragma once" + vbNewLine)
DTE.UndoContext.Close()
End Sub Public Sub FileCreateEn()
'DESCRIPTION 文件签名
Dim fil_info(64) As String
Dim i As Integer
i = 0
fil_info(i) = "//Copyright (c) 2013 hustfisher All Rights Reserved"
i += 1 fil_info(i) = "/*********************************************************************************************"
i += 1 fil_info(i) = AlignTitle("file name")
fil_info(i) += " : "
fil_info(i) += DTE.ActiveDocument.Name
i += 1 fil_info(i) = AlignTitle("description")
fil_info(i) += " : "
i += 1 fil_info(i) = AlignTitle("create time")
fil_info(i) += " : "
fil_info(i) += Date.Now.ToString()
i += 1 fil_info(i) = AlignTitle("author name")
fil_info(i) += " : "
fil_info(i) += "hustfisher"
i += 1 fil_info(i) = AlignTitle("author email")
fil_info(i) += " : "
fil_info(i) += "hustfisher@yeah.net"
i += 1 fil_info(i) = AlignTitle("author blog")
fil_info(i) += " : "
fil_info(i) += "http://blog.csdn.net/jiejiaozhufu"
i += 1 fil_info(i) = AlignTitle("version")
fil_info(i) += " : "
fil_info(i) += "1.0"
i += 1 fil_info(i) = " **********************************************************************************************/"
i += 1 Dim Description As New StringBuilder
For v = 0 To i
Description.AppendFormat("{0}{1}", fil_info(v), vbNewLine)
Next
'插入文件头部
Dim objSel As TextSelection
objSel = CType(DTE.ActiveDocument.Selection, TextSelection)
DTE.UndoContext.Open("FileCreateEn")
objSel.StartOfDocument(False)
objSel.Insert(Description.ToString())
DTE.UndoContext.Close()
End Sub Public Sub FileModifyEn()
'modify file
Dim fil_info(64) As String
Dim i As Integer
i = 0
fil_info(i) = "//Copyright (c) 2013 hustfisher All Rights Reserved"
i += 1 fil_info(i) = "/*********************************************************************************************"
i += 1 fil_info(i) = AlignTitle("file name")
fil_info(i) += " : "
fil_info(i) += DTE.ActiveDocument.Name
i += 1 fil_info(i) = AlignTitle("description")
fil_info(i) += " : "
i += 1 fil_info(i) = AlignTitle("modify time")
fil_info(i) += " : "
fil_info(i) += Date.Now.ToString()
i += 1 fil_info(i) = AlignTitle("author name")
fil_info(i) += " : "
fil_info(i) += "hustfisher"
i += 1 fil_info(i) = AlignTitle("author email")
fil_info(i) += " : "
fil_info(i) += "hustfisher@yeah.net"
i += 1 fil_info(i) = AlignTitle("author blog")
fil_info(i) += " : "
fil_info(i) += "http://blog.csdn.net/jiejiaozhufu"
i += 1 fil_info(i) = AlignTitle("version")
fil_info(i) += " : "
fil_info(i) += "1.1"
i += 1 fil_info(i) = " **********************************************************************************************/"
i += 1 Dim Description As New StringBuilder
For v = 0 To i
Description.AppendFormat("{0}{1}", fil_info(v), vbNewLine)
Next
Dim DocSel As EnvDTE.TextSelection
DocSel = DTE.ActiveDocument.Selection
DocSel.StartOfLine()
DocSel.NewLine()
DocSel.LineUp()
DocSel.Insert(Description.ToString())
End Sub Sub FunctionSignEn()
'function
Dim data As New StringBuilder
With data
.Append(vbNewLine)
.AppendFormat("/*********************************************************************************************{0}", vbNewLine)
.AppendFormat(" *function name{0}: {1}", vbTab, vbNewLine)
.AppendFormat(" *create time{0}: {1} {2}", vbTab, Date.Now.ToString(), vbNewLine)
.AppendFormat(" *author name{0}: {1} {2}", vbTab, "hustfisher", vbNewLine)
.AppendFormat(" *func version{0}: 1.0 {1}", vbTab, vbNewLine)
.AppendFormat(" *description {0}: {1}", vbTab, vbNewLine)
.AppendFormat(" *para title {0}: IN/OUT{1}{2}TYPE{3}{4}{5}DESCRIPTION{6}", vbTab, vbTab, vbTab, vbTab, vbTab, vbTab, vbNewLine)
.AppendFormat(" *parameter 1{0}: {1}", vbTab, vbNewLine)
.AppendFormat(" *return type {0}: {1}", vbTab, vbNewLine)
.AppendFormat(" *********************************************************************************************/")
End With
Dim DocSel As EnvDTE.TextSelection
DocSel = DTE.ActiveDocument.Selection
DocSel.StartOfLine()
DocSel.NewLine()
DocSel.LineUp()
DocSel.Insert(data.ToString())
End Sub Sub FunctionSignEnEx()
'function
Dim DocSel As EnvDTE.TextSelection
DocSel = DTE.ActiveDocument.Selection
Dim line As String
DocSel.SelectLine()
line = DocSel.Text().ToString()
line = Trim(line)
Dim name As String
Dim para As String
Dim return_type As String
Dim pos As Integer
'type
pos = line.IndexOf(" ")
If pos = -1 Then
Return
End If
return_type = Mid(line, 1, pos)
'name
Dim pos1 As Integer
pos1 = line.IndexOf("(")
If pos1 = -1 Then
Return
End If
name = Mid(line, pos + 1, pos1 - pos).Trim
'para
pos = pos1 + 1
pos1 = line.LastIndexOf(")")
If pos1 = -1 Then
Return
End If
para = Mid(line, pos + 1, pos1 - pos).Trim()
Dim words() As String
words = Split(para, ",")
Dim func_info(32) As String
Dim i As Integer
i = 0
func_info(i) = "/*********************************************************************************************"
i += 1 func_info(i) = AlignTitle("function name")
func_info(i) += " : "
func_info(i) += name
i += 1 func_info(i) = AlignTitle("create time")
func_info(i) += " : "
func_info(i) += Date.Now.ToString()
i += 1 func_info(i) = AlignTitle("author name")
func_info(i) += " : "
func_info(i) += "hustfisher"
i += 1 func_info(i) = AlignTitle("version")
func_info(i) += " : "
func_info(i) += "1.0"
i += 1 func_info(i) = AlignTitle("description")
func_info(i) += " : "
i += 1 func_info(i) = AlignTitle("return type")
func_info(i) += " : "
func_info(i) += return_type
i += 1 func_info(i) = AlignTitle("parameter list")
func_info(i) += " : "
func_info(i) += AlignValue("IN/OUT")
func_info(i) += AlignValue("TYPE")
func_info(i) += AlignValue("NAME")
func_info(i) += AlignValue("DESCRIPTION")
i += 1 Dim j As Integer
j = 1
Dim paraBuf As String
For Each v In words
func_info(i) = "parameter "
func_info(i) += j.ToString()
func_info(i) = AlignTitle(func_info(i))
func_info(i) += " : "
func_info(i) += AlignValue("IN")
paraBuf = v.Trim()
pos = paraBuf.LastIndexOf(" ")
If -1 = pos Then
func_info(i) += AlignValue("void")
Else
func_info(i) += AlignValue(Mid(paraBuf, 1, pos).Trim())
func_info(i) += AlignValue(Mid(paraBuf, pos + 1, paraBuf.Length).Trim())
End If
i += 1 j += 1
Next
Dim Description As New StringBuilder
For v = 0 To i - 1
Description.AppendFormat("{0}{1}", func_info(v), vbNewLine)
Next
Description.AppendFormat(" *********************************************************************************************/{0}", vbNewLine)
DocSel.StartOfLine()
DocSel.LineUp()
DocSel.Insert(Description.ToString)
End Sub Sub AddPara()
ActiveDocument.Selection.Text = "*parameter 2" + vbTab + ":"
End Sub Sub ClassSignEn()
'function
Dim data As New StringBuilder
With data
.Append(vbNewLine)
.AppendFormat("/*********************************************************************************************{0}", vbNewLine)
.AppendFormat(" *calss name : {0}", vbNewLine)
.AppendFormat(" *create time : {0} {1}", Date.Now.ToString(), vbNewLine)
.AppendFormat(" *author name : {0} {1}", "hustfisher", vbNewLine)
.AppendFormat(" *calss vers : 1.0 {0}", vbNewLine)
.AppendFormat(" *description : {0}", vbNewLine)
.AppendFormat(" *********************************************************************************************/")
End With
Dim DocSel As EnvDTE.TextSelection
DocSel = DTE.ActiveDocument.Selection
DocSel.StartOfLine()
DocSel.NewLine()
DocSel.LineUp()
DocSel.Insert(data.ToString())
End Sub Sub ModifyTag()
'DESCRIPTION 增添修改
Dim DocSel As EnvDTE.TextSelection
DocSel = DTE.ActiveDocument.Selection
DocSel.EndOfLine()
ActiveDocument.Selection.Text = "/* hustfisher modified at " + Date.Now.ToString() + " */"
End Sub Sub TodoTag()
Dim DocSel As EnvDTE.TextSelection
DocSel = DTE.ActiveDocument.Selection
DocSel.EndOfLine()
ActiveDocument.Selection.Text = " // TODO:" + Date.Now.ToString()
End Sub Sub AddCommentC()
Dim data As New StringBuilder
With data
.Append(vbNewLine)
.AppendFormat("/*********************************************************************************************{0}", vbNewLine)
.AppendFormat(" *create time : {0} {1} author : {2} {3}", Date.Now.ToString(), vbTab, "hustfisher", vbNewLine)
.AppendFormat(" *description : {0}", vbNewLine)
.AppendFormat(" *********************************************************************************************/")
End With
Dim DocSel As EnvDTE.TextSelection
DocSel = DTE.ActiveDocument.Selection
DocSel.StartOfLine()
DocSel.NewLine()
DocSel.LineUp()
DocSel.Insert(data.ToString())
End Sub Sub AddComment()
Dim DocSel As EnvDTE.TextSelection
DocSel = DTE.ActiveDocument.Selection
DocSel.EndOfLine()
ActiveDocument.Selection.Text = " // Add by hustfisher " + Date.Now.ToString
End Sub End Module
  • 使用注意事项

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

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

否则...

  • 功能
  1. 文件创建注释模板
  2. 文件修改注释模板
  3. 类注释模板
  4. 函数注释模板
  5. 自动函数注释(我觉得这个很不错)
  6. TODO Tag模板
  7. C语言格式的一般注释模板
  8. C++语言格式的一般注释模板
  • 注释模板效果
    //Copyright (c) 2013 hustfisher All Rights Reserved
    /*********************************************************************************************
    *file name : main.cpp
    *description :
    *create time : 2013/11/16 15:53:39
    *author name : hustfisher
    *author email : hustfisher@yeah.net
    *author blog : http://blog.csdn.net/jiejiaozhufu
    *version : 1.0
    **********************************************************************************************/ #include "normalise.h"
    #include <Windows.h>
    #include <stdio.h> #define FRAME_IN_NUM (128)
    #define LOOP_TIMES (60240)
    #define ALIGN_SIZE (64) /*********************************************************************************************
    *function name : InitData
    *create time : 2013/11/16 15:53:47
    *author name : hustfisher
    *version : 1.0
    *description :
    *return type : void
    *parameter list : IN/OUT TYPE NAME DESCRIPTION
    *parameter 1 : IN float* pData
    *parameter 2 : IN size_t nCount
    *********************************************************************************************/
    void InitData(float* pData, size_t nCount)
    {
    int my_sign[] = {1, -1};
    srand(GetTickCount());
    for (size_t i=0; i<nCount; i++)
    {
    pData[i] = rand()%10*my_sign[rand()%2]*1.0f;
    }
    }
    /*********************************************************************************************
    *function name : main
    *create time : 2013/11/16 15:53:53
    *author name : hustfisher
    *version : 1.0
    *description :
    *return type : int
    *parameter list : IN/OUT TYPE NAME DESCRIPTION
    *parameter 1 : IN void
    *********************************************************************************************/
    int main()
    { return 0;
    }

Visual Studio宏注释模板的更多相关文章

  1. Visual Studio 宏的高级用法

    因为自 Visual Studio 2012 开始,微软已经取消了对宏的支持,所以本篇文章所述内容只适用于 Visual Studio 2010 或更早期版本的 VS. 在上一篇中,我已经介绍了如何编 ...

  2. DotNetBar for Windows Forms 12.9.0.0_冰河之刃重打包版及制作Visual Studio C#项目模板文件详解

    关于 DotNetBar for Windows Forms 12.9.0.0_冰河之刃重打包版 --------------------11.8.0.8_冰河之刃重打包版-------------- ...

  3. Android Studio(六):Android Studio添加注释模板

    Android Studio相关博客: Android Studio(一):介绍.安装.配置 Android Studio(二):快捷键设置.插件安装 Android Studio(三):设置Andr ...

  4. 修改Visual Studio的默认模板

    如果我在Visual Studio创建的项目中每次新建一个文件,自动生成注释或者是结构的话,那么就需要改下默认的模板了.下面以vs2013为例 我们添加的文件有很多种,这里就举例3种,CSharp类文 ...

  5. Visual Studio图片注释image-comments扩展

            有一个开源的Visual Studio小工具image-comments,它用于在源代码注释中插入图片,您可以到这儿下载.目前支持Visual Studio 2010/2012 Sta ...

  6. Visual Studio 2010 类模板的修改

    第一步:找到类文件模板路径 C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\C ...

  7. Visual Studio安装卸载模板

    Visual Studio中有两种类型的模板:项目模板和项模板 一.已安装模板: 默认情况下,与产品一起安装的模板位于以下位置: ①\<Visual Studio 安装目录>\Common ...

  8. Android Studio自定义注释模板及生成JavaDoc

    刚开始学习Android,使用了Android Studio IDE.为了将来生产JavaDoc,学习一下如何自定义注释模板. . 自定义注释模板 1. 通过 File –>Settings 或 ...

  9. Visual Studio 自定义项目模板

    经常我们需要新建一个项目,然后新建我们的View文件夹,ViewModel文件夹,Model文件夹,还有把我们的ViewModelBase放入我们的VIewModel,如果还用框架,还需要加上好多. ...

随机推荐

  1. WP8.1 页面导航 缓存问题

    最近开始学习wp8.1开发,在页面的导航学习时发现了一点问题,即当使用Frame.Navigate()方法进行页面的跳转时,每次都会重新实例化一个页面.而在新的页面采用Frame.GoBack()或者 ...

  2. (原) c++ 杂

    Declaration of variables   C++ is a strongly-typed language, and requires every variable to be decla ...

  3. css派生选择器

    后代选择器:即包含选择器,选择某元素的后代元素. 子元素选择器:只能选择某元素的子元素. 相邻兄弟选择器:可选择紧接在另一个元素后的元素,且两者有相同的夫元素.

  4. 【6】使用nginx

    sudo vim /etc/nginx/nginx.conf user root; worker_processes 2; error_log /var/log/nginx/error.log; pi ...

  5. Google谷歌搜索引擎登录网站 - Blog透视镜

    建置好了网站之后,为了能提升流量或是增加曝光度,Mix通常会到Google谷歌,用手动登录的方式,登录网站,不久之后,搜索引擎就会派遣蜘蛛机器人,来检索你的网站,等一段时间之后,就会出现在搜索引擎内, ...

  6. C语言必背18个经典程序

    C语言必背18个经典程序 1./*输出9*9口诀.共9行9列,i控制行,j控制列.*/ #include "stdio.h" main() {int i,j,result; for ...

  7. 大量客户反映wordpress的网站打开巨慢,经分析发现,这些网站大都使用了google的字体服务,由于最近google的服务已经被大陆屏蔽,所以wordpress的网站打开时,会卡在字体加载上。

     一会你安装完wp,发现打开巨卡的话,看看这个帖子:http://bbs.myhostcn.com/thread-1026-1-1.html最近一段时间,大量客户反映wordpress的网站打开巨慢, ...

  8. 【CF 675D Tree Construction】BST

    题目链接:http://codeforces.com/problemset/problem/675/D 题意:给一个由n个互异整数组成的序列a[],模拟BST的插入过程,依次输出每插入一个元素a[i] ...

  9. 使用jquery获取网页中图片的高度——解惑

    jQuery获取网页中图片的高度 使用jquery获取网页中图片的高度其实很简单,有两种常用的方法都可以打到我们的目的 $("img").whith();(返回纯数字) $(&qu ...

  10. 小KING教你做android项目(一)

    写在项目开始之前: 最近关于android的视频教程,入门的书籍也已经有很多了,例如我的入门就是看了mars的视频教程.但是这么一圈学习下来,觉得真正快速提高的,不是在看视频,而是在实际工作中动手做项 ...