Wednesday, July 18, 2007

Indent code and surround with braces

This macro saves a bit of time by adding braces around a piece of code and indenting the block -- something I do a dozen times in a normal day.

' Takes the current selection, indents it, and surrounds with braces.
Sub CreateCodeBlock()
CreateCodeBlock_Helper("{", "}")
End Sub

' Takes the current selection, indents it, and surrounds it with startOfBlock and endOfBlock
Sub CreateCodeBlock_Helper(ByVal startOfBlock As String, ByVal endOfBlock As String)
Dim sel As TextSelection = DTE.ActiveDocument.Selection
Dim objAnchor As VirtualPoint = sel.AnchorPoint
Dim bottomPoint As VirtualPoint = sel.BottomPoint

Dim trimmedString As String = sel.Text.TrimStart()
Dim numChars As Integer = sel.Text.Length - trimmedString.Length
Dim spaceString As String = sel.Text.Substring(0, numChars)

objAnchor.CreateEditPoint().Insert(spaceString + startOfBlock + vbCrLf)
bottomPoint.CreateEditPoint().Insert(spaceString + endOfBlock + vbCrLf)
sel.Indent()
'sel.LineUp()


'sel.Text = "{" + vbCrLf + sel.Text + "}" + vbCrLf
End Sub


Once I started to write a whole library of code that was in a namespace, I found it useful to extend the macro so that it indents everything and surrounds it with a "namespace XXX {". The closing brace at the end includes a comment that says "} // end of namespace XXX"

' Takes the current selection and surrounds it with "namespace xxx {" and "}"
' (after prompting user for the namespace name xxx)
Sub SurroundWithNamespace()
Dim name As String
name = InputBox("Namespace name")
CreateCodeBlock_Helper("namespace " + name + vbCrLf + "{", vbCrLf + "} // end of namespace " + name)

End Sub

Close All Annoying Windows -- I just want a monitor full of code!

I find it annoying to have to constantly be closing those windows that appear at the bottom of the screen whenever you do a "Find in files", or the error list window. Mapping this macro to a hot-key is a handy shortcut to avoid having to close these all up by clicking on the "X".

Sub CloseBottomWindows()
DTE.Windows.Item(Constants.vsWindowKindOutput).Close()
DTE.Windows.Item(Constants.vsWindowKindFindResults1).Close()
DTE.Windows.Item(Constants.vsWindowKindTaskList).Close()
End Sub


Some of the windows may not have a "vsWindowKind" constant. In that case, you can close them by finding them by the window title.

' Hide windows that match certain titles
Sub HideOtherBottomWindows()
Dim window As Window
For Each window In DTE.Windows
'MsgBox(window.Caption)
If (window.Caption.Equals("File Finder")) Then
window.Close()
End If
If (window.Caption.Contains("Code Definition Window")) Then
window.Close()
End If
If (window.Caption.Contains("Error List")) Then
window.Close()
End If
If (window.Caption.Equals("VA Find References Results")) Then
window.Close()
End If
Next
End Sub

Adding a CodeWarrior breakpoint from inside Visual Studio


  • This macro uses CodeWarrior's COM interface to allow you to set a breakpoint from within Visual Studio

  • If you work exclusively on GameCube or PS2, you might want to map this macro to F9



Sub CWSetBreakpointHere()
Dim objSel As TextSelection = DTE.ActiveDocument.Selection
Dim objAnchor As VirtualPoint = objSel.AnchorPoint
Dim line As Long = objAnchor.Line
Dim fileName As String = DTE.ActiveDocument.FullName()

Dim CW = CreateObject("CodeWarrior.CodeWarriorApp")
CW.Debugger.SetBreakpointInSource(fileName, line, 0)

DTE.ExecuteCommand("Debug.ToggleBreakpoint")
MsgBox("Breakpoint set in CodeWarrior: " + vbCrLf + fileName.ToString() + ":" + line.ToString())
End Sub


Once I started working on a mix of GameCube and PC project files, I found it useful to map this next macro to F9. It looks at the name of the Visual Studio Project to determine whether to use the above CWSetBreakpointHere macro, or the normal Debug.ToggleBreakpoint command.

' Sets a breakpoint through CodeWarrior if the project name ends with wii or gamecube.
Sub MyToggleBreakpoint()
Dim ProjectName As String = ActiveSolutionProjects.GetValue(0).Name
If (ProjectName.ToLower().EndsWith("gamecube") Or ProjectName.ToLower().EndsWith("wii")) Then
CWSetBreakpointHere()
Else
DTE.ExecuteCommand("Debug.ToggleBreakpoint")
End If
End Sub