eVCWindows MobileVBScript.NET 1.0C++/CLIVisual Studio .NET 2003.NET 1.1移动应用C中级开发Visual StudioWindowsC++.NETVisual Basic
创建和更新评论印章宏






3.22/5 (5投票s)
这是一个 Visual Studio .NET 2003 宏,它可以自动将包含版权信息、创建日期、作者和更新等信息的注释块插入到您的源代码文件中,基本上是您希望在源代码文件头部包含的任何内容。
引言
这是一个 Visual Studio .NET 2003 宏,它可以自动将包含版权信息、创建日期、作者和更新等信息的注释块插入到您的源代码文件中,基本上是您希望在源代码文件头部包含的任何内容。
以下是代码:
Imports EnvDTE
Imports System.Diagnostics
Public Module Stamps
'DESCRIPTION: MQZ. Creates comment block
Dim g_strAuthor As String = "Modesty Zhang"
Dim g_strAuthorEmail As String = "ModestyZ@hotmail.com"
Dim g_strCompany As String = _
"COMPANY, Inc. Copyright © 2004-2006, All rights reserved."
Dim g_strTeam As String = "Modesty Team"
Dim g_stampMark As String = _
"/////////////////////////////////////////" & _
"////////////////////////////////////"
Dim g_createdBy As String = "// Created By: "
Dim g_updatedOn As String = "// Updated On: "
Sub StampHeaderCreation()
'DESCRIPTION: Creates a creation comment block
'for the active Source files
If (StampCheckDoc() = False) Then
Exit Sub
End If
Dim doc As Document = DTE.ActiveDocument
Dim ts As TextSelection = DTE.ActiveWindow.Selection
ts.SelectAll()
ts.StartOfDocument(True)
If Not ts.FindText(g_createdBy) Then
PrintText(g_stampMark, True)
PrintText("// " & g_strCompany, True)
PrintText("// ", True)
PrintText("// " & doc.Name & " - " & g_strTeam, True)
PrintText("//", True)
PrintText("// Description:", True)
PrintText("// [TODO: Write the purpose of "_
& doc.Name & ".]", True)
PrintText("//", True)
PrintText("// Created On: " & CDate(Now) & "", True)
PrintText(g_createdBy & g_strAuthor & _
" <mailto:" & g_strAuthorEmail & "> ", True)
PrintText(g_stampMark, True)
Else
StampHeaderUpdate()
End If
End Sub
Sub StampHeaderUpdate()
'DESCRIPTION: Creates a update comment block
'for the active Source files
If (StampCheckDoc() = False) Then
Exit Sub
End If
Dim doc As Document = DTE.ActiveDocument
Dim ts As TextSelection = DTE.ActiveWindow.Selection
Dim foundMark As Boolean = False
ts.SelectAll()
ts.StartOfDocument(True)
While ts.FindText(g_createdBy)
ts.FindText(g_stampMark)
foundMark = True
End While
While ts.FindText(g_updatedOn)
ts.FindText(g_stampMark)
foundMark = True
End While
If foundMark Then
'ts.LineDown(False, 1)
ts.EndOfLine()
ts.NewLine()
End If
PrintText("// Updated On: " & CDate(Now) & ". By: " & _
g_strAuthor & " <mailto:" & g_strAuthorEmail & _
">", True)
PrintText("// [TODO: Write the purpose of update on " _
& CDate(Now) & ".]", True)
PrintText(g_stampMark, True)
End Sub
Function StampCheckDoc() As Boolean
'DESCRIPTION: Creates a update comment block
'for the active Source files
Dim doc As Document = DTE.ActiveDocument
Dim badFile As Boolean = True
Dim name As String
If doc Is Nothing Then
MsgBox("Please run when a text editor window is active.")
Return False
End If
name = doc.Name.ToLower
If name.EndsWith(".h") Or name.EndsWith(".cpp") _
Or name.EndsWith(".js") Then
badFile = False
End If
If badFile Then
MsgBox("Please run with a c/c++ or JavaScript file.")
Return False
End If
Return True
End Function
Function PrintText(ByVal s As String, ByVal newline As Boolean)
Dim ts As TextSelection = DTE.ActiveWindow.Selection
ts.Text = s
If newline Then
ts.NewLine()
End If
End Function
End Module
在使用它之前,您可能需要自定义开头的字符串,例如 g_strAuthor
、g_strAuthorEmail
、g_strCompany
、g_strTeam
等。其余字符串是一些代码用来对齐插入内容的标记。
如果您创建一个文件并且它当前在 VS.NET 2003 IDE 中处于活动状态,运行 StampHeaderCreation()
将在文件的开头创建一个包含版权信息、创建日期和作者信息的页眉。如果您再次运行 StampHeaderCreation()
,它将通过调用 StampHeaderUpdate()
自动插入“更新”注释块。
当然,StampHeaderUpdate()
应该在您编辑由他人创建的文件时使用,它不会添加包含作者信息的创建块。每次调用时,都会插入一个新的“更新”注释块。
上述代码包含两个辅助函数:StampCheckDoc()
将确保 IDE 已经打开一个文档并且文档类型正确,您可以根据需要将其扩展为包含 .cs 或任何其他您认为合适的文件类型。另一个辅助函数是 PrintText()
,它无需解释…
如果您有任何问题,请发送电子邮件至 ModestyZ@hotmail.com。