ÿþImports EnvDTE Imports System.Diagnostics Imports Microsoft.VisualBasic Imports Microsoft.VisualBasic.ControlChars Imports System.Text.RegularExpressions Public Module CommentGenerator '--- Konstanten Deklarationen Const C_AUTHOR_NAME = "Andreas Schibilla" Const C_COPYRIGHT_NAME = "Andreas Schibilla" Const C_TIME_FORMAT = "yyyy-MM-dd (HH:mm)" Const C_LOGO_LINE1 = " / \ _ __ __| |_ __ ___ __ _ ___ / ___| ___| |__ (_) |__ (_) | | __ _ " Const C_LOGO_LINE2 = " / _ \ | '_ \ / _` | '__/ _ \/ _` / __| \___ \ / __| '_ \| | '_ \| | | |/ _` |" Const C_LOGO_LINE3 = " / ___ \| | | | (_| | | | __/ (_| \__ \ ___) | (__| | | | | |_) | | | | (_| |" Const C_LOGO_LINE4 = "/_/ \_\_| |_|\__,_|_| \___|\__,_|___/ |____/ \___|_| |_|_|_.__/|_|_|_|\__,_|" '--- Variablen Deklarationen Dim strFunctionLine As String Dim strFunctionName As String Dim strClassName As String Dim strParamList As String Dim NamePos1, NamePos2 As Integer 'Anfangs- und Endepositionen des Funktionsnamen im String Dim ClassPos1, ClassPos2 As Integer 'Anfangs- und Endepositionen des Klassennamen im String Dim ParamsPos1, ParamsPos2 As Integer 'Anfangs- und Endepositionen der Parameterliste im String Dim i, j As Integer Dim NumParams As Integer Dim MaxParamLen As Integer Dim strParams() As String = Nothing Dim strDelimiter As String = ",;" Dim delimiter As Char() = strDelimiter.ToCharArray() Dim s As String Dim strDate As String ' Ersetzt Zeilenende-, Tabulator- und Backslashzeichen eines Strings zu Spaces Function RemoveNewlines(ByVal strIn As String) As String Return Regex.Replace(strIn, "[\r\n\t\\]", " ") End Function ' Entfernt folgend Zeichen aus einem String: '&', '*', '[', ']' Function CleanParams(ByVal strIn As String) As String Return Trim(Regex.Replace(strIn, "[&\*\[\]]", "")) End Function ' Variablennamen eines Parameters aus einem String ermitteln (Datentyp verwerfen) Function GetParam(ByVal strIn As String) As String Dim pos As Integer strIn = Trim(strIn) pos = InStrRev(strIn, " ") Return Right(strIn, Len(strIn) - pos) End Function ' Eine Zeile in das Editorfenster schreiben Function WriteLn(ByVal strIn) ActiveDocument.Selection.StartOfLine() ActiveDocument.Selection.text() = strIn ActiveDocument.Selection.NewLine() End Function '----------------------------------------- ' MACRO: Dateibeschreibung erzeugen '----------------------------------------- Sub GenerateFileComment() '--- Eine Quelldatei muss im Editor aktiv sein If (TypeOf (ActiveWindow) Is TextWindow) Then MsgBox("Dieses Makro kann nur ausgeführt werden, wenn ein Quelltexteditorfenster aktiv ist!") Exit Sub End If strDate = Format(Now(), C_TIME_FORMAT) '--- UndoContext setzen, damit Makro mit einem Undo-Aufruf rückgängig gemacht werden kann DTE.UndoContext.Open("Dateibeschreibung für " & DTE.ActiveDocument.Name) Try '--- Dateibeschreibung einfügen ActiveDocument.Selection.GotoLine(1, False) WriteLn("//------------------------------------------------------------------------------------------------") WriteLn("// ") WriteLn("// " & C_LOGO_LINE1) WriteLn("// " & C_LOGO_LINE2) WriteLn("// " & C_LOGO_LINE3) WriteLn("// " & C_LOGO_LINE4) WriteLn("// ") WriteLn("// Filename: " & DTE.ActiveDocument.Name) WriteLn("// Created: " & strDate & ", by " & C_AUTHOR_NAME) WriteLn("// Purpose: ") WriteLn("// ") WriteLn("// Coypright(c): " & C_COPYRIGHT_NAME) WriteLn("//------------------------------------------------------------------------------------------------") ' Cursor zur Bearbeitung an bessere Position bewegen ActiveDocument.Selection.FindText("// Purpose: ", vsFindOptions.vsFindOptionsBackwards) ActiveDocument.Selection.EndOfLine() Finally DTE.UndoContext.Close() End Try End Sub '----------------------------------------- ' MACRO: Funktionsbeschreibung erzeugen '----------------------------------------- Sub GenerateFunctionComment() '--- Eine Quelldatei muss im Editor aktiv sein If (TypeOf (ActiveWindow) Is TextWindow) Then MsgBox("Dieses Makro kann nur ausgeführt werden, wenn ein Quelltexteditorfenster aktiv ist!") Exit Sub End If '--- String für Funktionszeile(n) ermitteln und vorverarbeiten If ActiveDocument.Selection.isEmpty Then ActiveDocument.Selection.SelectLine() End If strFunctionLine = Trim(ActiveDocument.Selection.Text) i = ActiveDocument.Selection.TopPoint.Line ActiveDocument.Selection.MoveToLineAndOffset(i, 1) strFunctionLine = RemoveNewlines(strFunctionLine) If len(strFunctionLine) = 0 Then MsgBox("Keine gültige Funktionszeile ausgewählt!") Exit Sub End If '--- Funktionsnamen ermitteln strFunctionName = "" strClassName = "" ' Funktionsname ist rechts durch Klammer '(' begrenzt NamePos2 = InStr(1, strFunctionLine, "(", vbTextCompare) If NamePos2 < 2 Then MsgBox("Kein gültiger Funktionsname gefunden!") Exit Sub End If ' Funktionsname ist links durch Doppelpunkt ':' oder Space ' ' begrenzt i = InStrRev(strFunctionLine, ":", NamePos2) j = InStrRev(strFunctionLine, " ", NamePos2) If (i > j) Then NamePos1 = i + 1 Else NamePos1 = j + 1 End If strFunctionName = Trim(Mid(strFunctionLine, NamePos1, NamePos2 - NamePos1)) '--- Wenn Doppelpunkt zuvor gefunden -> Klassennamen ermitteln (links von Fkt-Namen) If (i > 1) Then ClassPos2 = NamePos1 - 3 i = InStrRev(strFunctionLine, ":", ClassPos2) j = InStrRev(strFunctionLine, " ", ClassPos2) If (i > j) Then ClassPos1 = i + 1 Else ClassPos1 = j + 1 End If strClassName = Trim(Mid(strFunctionLine, ClassPos1, ClassPos2 + 1 - ClassPos1)) End If '--- Parameterliste ermitteln ParamsPos1 = NamePos2 + 1 ParamsPos2 = InStr(ParamsPos1, strFunctionLine, ")", vbTextCompare) If (ParamsPos2 < 1) Then MsgBox("Keine gültige Parameterliste gefunden!") Exit Sub End If strParamList = Mid(strFunctionLine, ParamsPos1, ParamsPos2 - ParamsPos1) strParamList = CleanParams(strParamList) strParamList = strParamList.Replace("void", "") strParamList = strParamList.Replace("VOID", "") strParams = strParamList.Split(delimiter, 255) MaxParamLen = 0 NumParams = 0 For i = 0 To strParams.Length() - 1 Dim len As Integer strParams(i) = GetParam(strParams(i)) len = strParams(i).Length If len > MaxParamLen Then MaxParamLen = len End If If len > 0 Then NumParams = NumParams + 1 End If Next strDate = Format(Now(), C_TIME_FORMAT) '--- UndoContext setzen, damit Makro mit einem Undo-Aufruf rückgängig gemacht werden kann DTE.UndoContext.Open("Funktionsbeschreibung für " & strFunctionName & "()") Try '--- Durchsuche obere 3 Zeilen nach bereits vorhandenem Kommentarblock -> Modified einfügen For i = 0 To 3 ActiveDocument.Selection.LineUp() ActiveDocument.Selection.SelectLine() If ((InStr(1, ActiveDocument.Selection.Text, "Modified:", vbTextCompare) > 0) Or (InStr(1, ActiveDocument.Selection.Text, "Created:", vbTextCompare) > 0)) Then WriteLn("// Modified: " & strDate & ", by " & C_AUTHOR_NAME) Exit Sub End If Next i ' Wieder an alte Position gehen ActiveDocument.Selection.LineDown(False, 3) '--- Funktionsbeschreibung einfügen WriteLn("//------------------------------------------------------------------------------------------------") WriteLn("// Function: " & strFunctionName & "()") If (strClassName.Length > 0) Then WriteLn("// Class: " & strClassName) End If WriteLn("// Description: ") WriteLn("// Returns: -") If (NumParams > 0) Then WriteLn("// ") WriteLn("// Parameters: ") For i = 0 To NumParams - 1 strParams(i) = strParams(i).PadRight(MaxParamLen) WriteLn("// [->] " & strParams(i) & " - ") Next i End If WriteLn("// ") WriteLn("// Created: " & strDate & ", by " & C_AUTHOR_NAME) WriteLn("//------------------------------------------------------------------------------------------------") ' Cursor zur Bearbeitung an bessere Position bewegen ActiveDocument.Selection.FindText("// Description: ", vsFindOptions.vsFindOptionsBackwards) ActiveDocument.Selection.EndOfLine() Finally DTE.UndoContext.Close() End Try End Sub End Module