Introduction
This is a Visual Studio .NET 2003 macro, it can automatically insert comment blocks to your source files about copyright, creation date, author and update, etc., basically anything you want for your source files’ header.
Here are the codes:
Imports EnvDTE
Imports System.Diagnostics
Public Module Stamps
Dim g_strAuthor As String = "Modesty Zhang"
Dim g_strAuthorEmail As String = "mailto:ModestyZ@hotmail.com">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()
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()
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.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
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
Before you use it, you may want to customize the strings in the beginning, like g_strAuthor
, g_strAuthorEmail
, g_strCompany
, g_strTeam
, etc. The rest of the strings are some markers that the code use to align inserted content.
If you create a file and it’s currently active in VS.NET 2003 IDE, running StampHeaderCreation()
will create a header about copyright, creation date, and author info block at the beginning of your file. If you run StampHeaderCreation()
again, it will automatically insert “update” comment block by calling StampHeaderUpdate()
.
Of course, StampHeaderUpdate()
should be used when you edit a file created by somebody else, it will not add creation block with author info. Each time it’s invoked, a new “update” comment block is inserted.
The above code has two helper functions: StampCheckDoc()
will make sure the IDE has a document open and the document type is correct, you can certainly expand it to include .cs or any other type of files you see fit. Another helper function is PrintText()
, it doesn’t need any explanation though…
If you have any questions, please send me an email to ModestyZ@hotmail.com.