Click here to Skip to main content
15,881,559 members
Articles / Mobile Apps

Creation and Update Comment Stamps Macro

Rate me:
Please Sign up or sign in to vote.
3.22/5 (5 votes)
15 Jul 2004CPOL1 min read 37.7K   20   1
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.

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:

VB
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 = "<a href="mailto:ModestyZ@hotmail.com">ModestyZ@hotmail.com</a>"
    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

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
United States United States
https://github.com/modesty

https://www.linkedin.com/in/modesty-zhang-9a43771

https://twitter.com/modestyqz

Comments and Discussions

 
GeneralIt is OK. Pin
nwlf16-Nov-06 20:59
nwlf16-Nov-06 20:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.