ÿþ' Aurora Macros ' Handy macros for programmers. From: http://www.tilander.org/aurora ' ' (c) 2006 Jim Tilander ' ' From: http://blogs.msdn.com/craigskibo/archive/2004/06/29/169190.aspx ' To get rid of the bad balloon that runs in the lower corner, add this to your registry: ' '[HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\8.0] ' "DontShowMacrosBalloon"=dword:00000006 Imports System Imports System.IO Imports EnvDTE Imports EnvDTE80 Imports System.Diagnostics Imports System.Collections Public Module AuroraMacros Const __codeFromProjectOffset As String = "..\.." Const __tabString As String = " " Const __prefix As String = "aurora" Sub __WriteToOutputWindow(ByVal indentlevel As Integer, ByVal message As String) Dim win As OutputWindow = DTE.Windows.Item(Constants.vsWindowKindOutput).Object Dim pane As OutputWindowPane = win.OutputWindowPanes.Item("Build") While indentlevel > 0 pane.OutputString(" ") indentlevel = indentlevel - 1 End While pane.OutputString(message & vbCrLf) End Sub Sub __ExecuteScmCommand(ByVal command As String, ByVal target As String) Dim commandline As String = "p4 " + command + " " + target __WriteToOutputWindow(1, commandline) Shell(commandline, AppWinStyle.MinimizedNoFocus) End Sub Function __ParentDir(ByVal pathname As String) Dim dirname As String = Path.GetDirectoryName(pathname) Return Path.GetFileNameWithoutExtension(dirname) End Function Sub __CreateFile(ByVal filename As String) File.CreateText(filename).Close() __WriteToOutputWindow(1, "- Created " + filename) End Sub Sub __AddFileToProject(ByVal project As Project, ByVal path As String) project.ProjectItems.AddFromFile(path) __WriteToOutputWindow(1, "- Added " + path + " to project " + project.Name) End Sub Sub __ReplaceLine(ByVal startIt As EditPoint, ByVal line As String) Dim endIt As EditPoint = startIt.CreateEditPoint() startIt.StartOfLine() endIt.EndOfLine() startIt.ReplaceText(endIt, line, 0) End Sub Function __GetLine(ByVal it As EditPoint) Return it.GetLines(it.Line, it.Line + 1) End Function Function __TrimRight(ByVal s As String) While s.EndsWith(" ") s = s.Substring(0, s.Length - 1) End While Return s End Function Sub __AddEscapes(ByVal sel As TextSelection, ByVal startLine As Integer, ByVal endLine As Integer, ByVal maxColumn As Integer) Dim it As EditPoint = sel.TopPoint.CreateEditPoint() For i As Integer = startLine To endLine Dim line As String = __GetLine(it) Dim lineLen = line.Replace(vbTab, __tabString).Length Dim padding As String = maxColumn - lineLen For j As Integer = 1 To padding line = line + " " Next __ReplaceLine(it, line) it.LineDown() Next it = sel.TopPoint.CreateEditPoint() For i As Integer = startLine To endLine Dim line As String = __GetLine(it) __ReplaceLine(it, line + "\") it.LineDown() Next End Sub Sub __RemoveEscapes(ByVal sel As TextSelection, ByVal startLine As Integer, ByVal endLine As Integer) Dim it As EditPoint = sel.TopPoint.CreateEditPoint() For i As Integer = startLine To endLine Dim line As String = __GetLine(it) If -1 <> line.LastIndexOf("\") Then line = line.Remove(line.Length - 1) line = __TrimRight(line) End If __ReplaceLine(it, line) it.LineDown() Next End Sub Sub HideAllWindows() ' Iterates through all the windows and closes all of them except the main document window For Each Win As EnvDTE.Window In DTE.Windows If Not Win.Visible Then Continue For End If If Win.Kind = "Document" Then Continue For End If Win.Close() Next End Sub Sub EditModifiedDocumentsInPerforce() ' Opens up all the unsaved files for edit in perforce For Each doc As Document In DTE.Documents If Not doc.Saved Then __ExecuteScmCommand("edit", doc.FullName) End If Next End Sub Sub InsertHeaderTemplate() Dim filename As String = Path.GetFileNameWithoutExtension(DTE.ActiveDocument.Name) Dim guard As String = __prefix + "_" + LCase(__ParentDir(DTE.ActiveDocument.FullName)) + "_" + LCase(filename) + "_h" DTE.UndoContext.Open("Insert header template") Try Dim sel As TextSelection = DTE.ActiveDocument.Selection Dim it As EditPoint = sel.TopPoint.CreateEditPoint() it.StartOfDocument() it.Insert("#ifndef " + guard + vbCrLf) it.Insert("#define " + guard + vbCrLf) it.Insert(vbCrLf) it.Insert("namespace " + __prefix + " { " + vbCrLf) it.Insert(vbCrLf) it.Insert("class " + filename + vbCrLf) it.Insert("{" + vbCrLf) it.Insert("public:" + vbCrLf) it.Insert(vbTab + filename + "();" + vbCrLf) it.Insert(vbTab + "~" + filename + "();" + vbCrLf) it.Insert(vbCrLf) it.Insert("};" + vbCrLf) it.Insert(vbCrLf) it.Insert("}" + vbCrLf) it.Insert(vbCrLf) it.Insert("#endif" + vbCrLf) Finally DTE.UndoContext.Close() End Try End Sub Sub InsertBodyTemplate() Dim filename As String = Path.GetFileNameWithoutExtension(DTE.ActiveDocument.Name) DTE.UndoContext.Open("Insert body template") Try Dim sel As TextSelection = DTE.ActiveDocument.Selection Dim it As EditPoint = sel.TopPoint.CreateEditPoint() it.StartOfDocument() it.Insert("#include ""Precompiled.h""" + vbCrLf) it.Insert("#include """ + filename + ".h""" + vbCrLf) it.Insert(vbCrLf) it.Insert("namespace " + __prefix + " { " + vbCrLf) it.Insert(vbCrLf) it.Insert(filename + "::" + filename + "()" + vbCrLf) it.Insert("{" + vbCrLf) it.Insert("}" + vbCrLf) it.Insert(vbCrLf) it.Insert(filename + "::~" + filename + "()" + vbCrLf) it.Insert("{" + vbCrLf) it.Insert("}" + vbCrLf) it.Insert(vbCrLf) it.Insert("}" + vbCrLf) it.Insert(vbCrLf) Finally DTE.UndoContext.Close() End Try End Sub Sub AddClass() ' Add a class with a header/cpp file to the currently selected project Dim currentProject As Project = DTE.ActiveSolutionProjects(0) Dim destinationDir As String = InputBox("In directory: ", "Add class to: " + currentProject.Name, Path.GetFullPath(Path.Combine(Path.GetDirectoryName(currentProject.FullName), __codeFromProjectOffset))) If Len(destinationDir) = 0 Then __WriteToOutputWindow(1, "User cancelled add class dialog.") Return End If Dim className As String = InputBox("Adding class here: " + destinationDir, "Add class to: " + currentProject.Name, "NewClass") If Len(className) = 0 Then __WriteToOutputWindow(1, "User cancelled add class dialog.") Return End If Dim header As String = Path.Combine(destinationDir, className + ".h") Dim body As String = Path.Combine(destinationDir, className + ".cpp") __CreateFile(header) __CreateFile(body) __AddFileToProject(currentProject, header) __AddFileToProject(currentProject, body) Dim svndir As String = Path.Combine(destinationDir, ".svn") If IO.Directory.Exists(svndir) Then Shell("svn add " + header) Shell("svn add " + body) __WriteToOutputWindow(1, "- Added " + header + " to svn") __WriteToOutputWindow(1, "- Added " + body + " to svn") End If DTE.ItemOperations.OpenFile(header) InsertHeaderTemplate() DTE.ItemOperations.OpenFile(body) InsertBodyTemplate() End Sub Sub ToggleComments() ' Toggles CPP style comments in the selection. Dim sel As TextSelection = DTE.ActiveDocument.Selection Dim it As EditPoint = sel.TopPoint.CreateEditPoint() Dim startLine As Integer = sel.TopPoint.Line Dim endLine As Integer = sel.BottomPoint.Line If sel.BottomPoint.LineCharOffset = 1 Then endLine = endLine - 1 End If DTE.UndoContext.Open("ToggleComment") Try For i As Integer = startLine To endLine Const commentPrefix As String = "//" Dim line As String = __GetLine(it) If line.StartsWith(commentPrefix) Then __ReplaceLine(it, line.Substring(Len(commentPrefix))) Else __ReplaceLine(it, commentPrefix + line) End If it.LineDown() Next Finally DTE.UndoContext.Close() End Try End Sub Sub AlignEscapes() ' Aligns the escapes in macros Dim sel As TextSelection = DTE.ActiveDocument.Selection Dim it As EditPoint = sel.TopPoint.CreateEditPoint() DTE.UndoContext.Open("ToggleEscapes") Try Dim startLine As Integer = sel.TopPoint.Line Dim endLine As Integer = sel.BottomPoint.Line If sel.BottomPoint.LineCharOffset = 1 Then endLine = endLine - 1 End If Dim maxColumn As Integer = 1 Dim hasEscapes As Boolean = False For i As Integer = startLine To endLine Dim line As String = __GetLine(it) line = line.Replace(vbTab, __tabString) If line.Length > maxColumn Then maxColumn = line.Length End If it.LineDown() If line.EndsWith("\") Then hasEscapes = True End If Next If hasEscapes Then __RemoveEscapes(sel, startLine, endLine) Else __AddEscapes(sel, startLine, endLine, maxColumn) End If Finally DTE.UndoContext.Close() End Try End Sub End Module