Visual Studio环境使用宏自动添加文件注释

其实,我们每天都在搞编程,其中的很多的步骤其实都是重复的,不如,我们为文件添加注释,这些工作其实没什么技术含量,但是,这些也是我们作为一个程序员的基本素质,因为我们有责任让我们的代码保持清晰、可读,所以我们需要添加一些注释信息。特别是涉及到算法的代码,关键的地方,或者是比较有争议的地方更加应该添加注释,不仅是为了提醒自己,更是为了让其他人也能容易的阅读。

Visual Studio自动添加文件注释的方法,在这里我选择的是宏的功能,宏就是一段VBA的脚本,Visual Studio支持用户自定义快捷键和宏关联起来,所以我们完全写一个宏,完成自己的功能。作为一个学技术的开发人员,在这里就不说技术了,我们只谈论代码,至于如何去实施,就请参考微软的帮助文档吧!下面是我写的一个宏,主要完成的功能是添加文件注释、多行注释和单行注释。

Option Strict Off
Option Explicit Off
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics

Public Module ModuleComment
    Sub AddSingleComment()
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "/*  */"
        DTE.ActiveDocument.Selection.CharLeft(False, 3)
    End Sub

    Sub AddMutiComment()
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.StartOfLine()
        DTE.ActiveDocument.Selection.Text = "/* ------------------------------------------------------------------------- */"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.StartOfLine()
        DTE.ActiveDocument.Selection.Text = "/*                                                                           */"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.StartOfLine()
        DTE.ActiveDocument.Selection.Text = "/* ------------------------------------------------------------------------- */"
        DTE.ActiveDocument.Selection.LineUp()
        DTE.ActiveDocument.Selection.CharLeft(False, 76)
    End Sub

    Sub AddFileComment()
        Dim doc As Document
        Dim docName As String
        Dim companyName As String = "华中师范大学计算机科学系"
        Dim authorName As String = "张永昌"
        Dim copyrightText As String = String.Format("Copyright (c) {0} {1}. All rights reserved.", Date.Now.Year, companyName)

        ' Get the name of this object from the file name
        doc = DTE.ActiveDocument

        ' Get the name of the current document
        docName = doc.Name

        ' Set selection to top of document
        DTE.ActiveDocument.Selection.StartOfDocument()

        ' Write copyright tag
        DTE.ActiveDocument.Selection.Text = "/****************************************************************************"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "* ==========================================================================="
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "*"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "* 文件名: " + docName
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "* 描述: "
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "*"

        ' Write author name tag (optional)
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "* 版本: 1.0"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "* 创建时间: " + String.Format("{0:d}", Date.Now) + " " + String.Format("{0}:{1}:{2}", DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second)
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "* 编译环境: Visual Studio 2005"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "*"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "* 作者: " + authorName
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "* 单位: " + companyName
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "*"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "* ==========================================================================="
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "*****************************************************************************/"
        DTE.ActiveDocument.Selection.NewLine()
    End Sub

End Module


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值