您的位置:首页 > 其它

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

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

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐