Tuesday, September 1, 2009

Visual Studio -> Debug - Start new instance shortcut

Here's an update sample of the macro that might help you while using Visual Studio 2005 or 2008.

Basically it replicates the actions of: Solution Explorer -> right click the project -> Debug -> Start new instance

Why do that? Because:
- it's a pain in the bucket to find the console/windows/web application project in solution explorer with your mouse if you are having a tons of projects and you currently work in a library document
- you have to wait for all sorts of timeouts until the right click works due to sourcecontrol/source safe/team foundation operations
- you have to find the Debug item and Start new instance.
- sometimes in speed you might even click something else or move some project to another folder, etc.

What it does is:
- runs the start new instance command on the project of the active document (the document you type in and it has focus)
- if the current document belongs to a class library(not webapp) it activates the previous document for which the command worked and starts the project. If no process was started it just tells you that a class library can't be started before invoking the actual build command (to be quicker).

How to use it?
Paste the code in Tools-MacroExplorer-MyMacros-Module1 inside the class of the VBA IDE. Save the file.
Return to 2005/2008 IDE Tools-Options-General-Keyboard - type a few chars from "StartDeb.." to spot this macro. Type in the shortcut box CTRL SHIFT D (from debug) and add the command.

Now, while on a document, press CTRL SHIFT D and the current project should start. (press Y to the confirmation dialog - you can comment that messagebox if you wish).

The VB code is:

Sub StartDebuggingOnSelectedProject()
Static _LastDocument As EnvDTE.Document
If (IsNothing(DTE.ActiveDocument)) Then
MsgBox("Open a document from project desired to start debug on first.", MsgBoxStyle.OkOnly, "No active document found.")
Else
Dim isClassOutputOrEmpty As Boolean
isClassOutputOrEmpty = False
'CHECKING IF CURRENT PROJECT IS NULL OR A CLASS LIBRARY
If (DTE.ActiveDocument.ProjectItem Is Nothing) Then
isClassOutputOrEmpty = True
Else
Dim prop As EnvDTE.Properties
prop = DTE.ActiveDocument.ProjectItem.ContainingProject.Properties
'Dim SB As System.Text.StringBuilder
'SB = New System.Text.StringBuilder()
For i As Int32 = 1 To prop.Count
'Try
'SB.Append(prop.Item(i).Name).Append("=").Append(prop.Item(i).Value.ToString()).Append(ControlChars.CrLf)
'Catch ex As Exception
'End Try
Try
If prop.Item(i).Name = "WebApplication.StartWebServerOnDebug" Then
isClassOutputOrEmpty = False
Exit For
End If
If prop.Item(i).Name = "OutputType" And prop.Item(i).Value.ToString() = "2" Then
isClassOutputOrEmpty = True
End If
Catch ex As Exception
End Try
Next
'MsgBox(SB.ToString())
End If
'IF LIBRARY START PREVIOUS PROJECT
If (isClassOutputOrEmpty) Then
If (_LastDocument Is Nothing) Then
MsgBox("Can't debug a class library.")
Return
Else
'MsgBox("Selecting document" + _LastDocument.Name)
Try
_LastDocument.Activate()
Catch ex As Exception
End Try
'Dim tree As EnvDTE.UIHierarchy = DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer).Object
'FindProject(tree.UIHierarchyItems, _LastProject)
End If
Else 'OTHERWISE SET THE PREVIOUS THE CURRENT
_LastDocument = DTE.ActiveDocument
End If
'MsgBox("Active project" + DTE.ActiveDocument.ProjectItem.ContainingProject.Name)
Try
If (MsgBox("Start debugging on: " + DTE.ActiveDocument.ProjectItem.ContainingProject.Name, MsgBoxStyle.YesNo, "Debug?") = MsgBoxResult.Yes) Then
DTE.ExecuteCommand("ClassViewContextMenus.ClassViewProject.Debug.Startnewinstance")
End If
Catch ex As Exception
End Try
End If
End Sub

3 comments:

Steve said...

Instead of using your Macro, you could simply add in the Options-Dialog under Environment->Keyboard "ClassViewContextMenus.ClassViewProject.Debug.Startnewinstance" as a shortcut. This starts debuging on the current project.
see Another nice Article

Cosmin S said...

Thanks for the tip. Although there's a problem when you try to start debugging on a class library. I made the script so that it remembers the previous ran project. It could be improved... if it wasn't in VB :)

Anonymous said...

Thanks Steve for the great tip! It was very useful for me!